Marian Harbach | ebeb154 | 2019-12-13 10:42:46 +0100 | [diff] [blame] | 1 | :linkattrs: |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 2 | = Gerrit Code Review - JavaScript Plugin Development and API |
Viktar Donich | 2b2fdb7 | 2017-03-13 16:21:44 -0700 | [diff] [blame] | 3 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 4 | Gerrit Code Review supports an API for JavaScript plugins to interact |
| 5 | with the web UI and the server process. |
Viktar Donich | 15ef24c | 2017-12-07 16:04:38 -0800 | [diff] [blame] | 6 | |
Viktar Donich | 9bd0ce6 | 2017-02-07 08:56:33 -0800 | [diff] [blame] | 7 | [[loading]] |
Viktar Donich | 2b2fdb7 | 2017-03-13 16:21:44 -0700 | [diff] [blame] | 8 | == Plugin loading and initialization |
| 9 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 10 | JavaScript is loaded using a standard `<script src='...'>` HTML tag. |
| 11 | Plugins should protect the global namespace by defining their code |
| 12 | within an anonymous function passed to `Gerrit.install()`. The plugin |
| 13 | will be passed an object describing its registration with Gerrit. |
Viktar Donich | 2b2fdb7 | 2017-03-13 16:21:44 -0700 | [diff] [blame] | 14 | |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 15 | * The plugin provides pluginname.js, and can be a standalone file or a static |
Viktar Donich | 5055e8d | 2017-11-09 13:02:42 -0800 | [diff] [blame] | 16 | asset in a jar as a link:dev-plugins.html#deployment[Web UI plugin]. |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 17 | * pluginname.js contains a call to `Gerrit.install()`. There should |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 18 | only be a single `Gerrit.install()` call per file. |
| 19 | * The Gerrit web app imports pluginname.js. |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 20 | * For standalone plugins, the entry point file is a `pluginname.js` file |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 21 | located in `gerrit-site/plugins` folder, where `pluginname` is an alphanumeric |
| 22 | plugin name. |
Viktar Donich | 2b2fdb7 | 2017-03-13 16:21:44 -0700 | [diff] [blame] | 23 | |
Ben Rohlfs | 233d86e | 2021-04-29 17:08:23 +0200 | [diff] [blame] | 24 | === Examples |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 25 | Here's a recommended starter `myplugin.js`: |
Viktar Donich | 2b2fdb7 | 2017-03-13 16:21:44 -0700 | [diff] [blame] | 26 | |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 27 | ``` js |
| 28 | Gerrit.install(plugin => { |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 29 | // Your code here. |
| 30 | }); |
Viktar Donich | 2b2fdb7 | 2017-03-13 16:21:44 -0700 | [diff] [blame] | 31 | ``` |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 32 | |
Ben Rohlfs | 233d86e | 2021-04-29 17:08:23 +0200 | [diff] [blame] | 33 | You can find more elaborate examples in the |
| 34 | link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/app/samples/[polygerrit-ui/app/samples/] |
| 35 | directory of the source tree. |
| 36 | |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 37 | [[low-level-api-concepts]] |
| 38 | == Low-level DOM API concepts |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 39 | |
| 40 | Basically, the DOM is the API surface. Low-level API provides methods for |
| 41 | decorating, replacing, and styling DOM elements exposed through a set of |
Viktar Donich | 6d10eca | 2017-11-13 17:57:43 -0800 | [diff] [blame] | 42 | link:pg-plugin-endpoints.html[endpoints]. |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 43 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 44 | Gerrit provides a simple way for accessing the DOM via DOM hooks API. A DOM |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 45 | hook is a custom element that is instantiated for the plugin endpoint. In the |
| 46 | decoration case, a hook is set with a `content` attribute that points to the DOM |
| 47 | element. |
| 48 | |
Viktar Donich | ab491f77 | 2017-08-15 08:02:58 -0700 | [diff] [blame] | 49 | 1. Get the DOM hook API instance via `plugin.hook(endpointName)` |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 50 | 2. Set up an `onAttached` callback |
| 51 | 3. Callback is called when the hook element is created and inserted into DOM |
| 52 | 4. Use element.content to get UI element |
| 53 | |
| 54 | ``` js |
Viktar Donich | 5055e8d | 2017-11-09 13:02:42 -0800 | [diff] [blame] | 55 | Gerrit.install(plugin => { |
Viktar Donich | ab491f77 | 2017-08-15 08:02:58 -0700 | [diff] [blame] | 56 | const domHook = plugin.hook('reply-text'); |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 57 | domHook.onAttached(element => { |
| 58 | if (!element.content) { return; } |
| 59 | // element.content is a reply dialog text area. |
| 60 | }); |
| 61 | }); |
| 62 | ``` |
| 63 | |
| 64 | [[low-level-decorating]] |
| 65 | === Decorating DOM Elements |
| 66 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 67 | For each endpoint, Gerrit provides a list of DOM properties (such as |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 68 | attributes and events) that are supported in the long-term. |
| 69 | |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 70 | ``` js |
Viktar Donich | 5055e8d | 2017-11-09 13:02:42 -0800 | [diff] [blame] | 71 | Gerrit.install(plugin => { |
Viktar Donich | ab491f77 | 2017-08-15 08:02:58 -0700 | [diff] [blame] | 72 | const domHook = plugin.hook('reply-text'); |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 73 | domHook.onAttached(element => { |
| 74 | if (!element.content) { return; } |
| 75 | element.content.style.border = '1px red dashed'; |
| 76 | }); |
| 77 | }); |
| 78 | ``` |
| 79 | |
| 80 | [[low-level-replacing]] |
| 81 | === Replacing DOM Elements |
| 82 | |
| 83 | An endpoint's contents can be replaced by passing the replace attribute as an |
| 84 | option. |
| 85 | |
| 86 | ``` js |
Viktar Donich | 5055e8d | 2017-11-09 13:02:42 -0800 | [diff] [blame] | 87 | Gerrit.install(plugin => { |
Viktar Donich | ab491f77 | 2017-08-15 08:02:58 -0700 | [diff] [blame] | 88 | const domHook = plugin.hook('header-title', {replace: true}); |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 89 | domHook.onAttached(element => { |
| 90 | element.appendChild(document.createElement('my-site-header')); |
| 91 | }); |
| 92 | }); |
| 93 | ``` |
| 94 | |
| 95 | [[low-level-style]] |
| 96 | === Styling DOM Elements |
| 97 | |
| 98 | A plugin may provide Polymer's |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 99 | https://polymer-library.polymer-project.org/3.0/docs/devguide/style-shadow-dom[style |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 100 | modules,role=external,window=_blank] to style individual endpoints using |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 101 | `plugin.registerStyleModule(endpointName, moduleName)`. A style must be defined |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 102 | as a standalone `<dom-module>` defined in the same .js file. |
| 103 | |
Ben Rohlfs | 233d86e | 2021-04-29 17:08:23 +0200 | [diff] [blame] | 104 | See |
| 105 | link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/app/samples/theme-plugin.js[samples/theme-plugin.js] |
| 106 | for an example. |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 107 | |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 108 | ``` js |
| 109 | const styleElement = document.createElement('dom-module'); |
| 110 | styleElement.innerHTML = |
| 111 | `<template> |
| 112 | <style> |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 113 | html { |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 114 | --primary-text-color: red; |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 115 | } |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 116 | </style> |
| 117 | </template>`; |
| 118 | |
| 119 | styleElement.register('some-style-module'); |
| 120 | |
| 121 | Gerrit.install(plugin => { |
| 122 | plugin.registerStyleModule('change-metadata', 'some-style-module'); |
| 123 | }); |
Viktar Donich | cbc2567 | 2017-07-24 13:23:18 -0700 | [diff] [blame] | 124 | ``` |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 125 | |
| 126 | [[high-level-api-concepts]] |
| 127 | == High-level DOM API concepts |
| 128 | |
Quinten Yearsley | 888e638 | 2017-12-05 11:11:09 -0800 | [diff] [blame] | 129 | High level API is based on low-level DOM API and is essentially a standardized |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 130 | way for doing common tasks. It's less flexible, but will be a bit more stable. |
| 131 | |
Quinten Yearsley | 888e638 | 2017-12-05 11:11:09 -0800 | [diff] [blame] | 132 | The common way to access high-level API is through `plugin` instance passed |
| 133 | into setup callback parameter of `Gerrit.install()`, also sometimes referred to |
| 134 | as `self`. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 135 | |
| 136 | [[low-level-api]] |
| 137 | == Low-level DOM API |
| 138 | |
Quinten Yearsley | 888e638 | 2017-12-05 11:11:09 -0800 | [diff] [blame] | 139 | The low-level DOM API methods are the base of all UI customization. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 140 | |
| 141 | === attributeHelper |
| 142 | `plugin.attributeHelper(element)` |
| 143 | |
Viktar Donich | 6e1e69b | 2018-01-22 14:08:21 -0800 | [diff] [blame] | 144 | Alternative for |
Tao Zhou | d2a8ded | 2020-06-05 16:08:54 +0200 | [diff] [blame] | 145 | link:https://polymer-library.polymer-project.org/3.0/docs/devguide/data-binding[Polymer data |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 146 | binding,role=external,window=_blank] for plugins that don't use Polymer. Can be used to bind element |
Viktar Donich | 6e1e69b | 2018-01-22 14:08:21 -0800 | [diff] [blame] | 147 | attribute changes to callbacks. |
| 148 | |
Ben Rohlfs | 233d86e | 2021-04-29 17:08:23 +0200 | [diff] [blame] | 149 | See |
| 150 | link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/app/samples/bind-parameters.js[samples/bind-parameters.js] |
| 151 | for an example. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 152 | |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 153 | === hook |
| 154 | `plugin.hook(endpointName, opt_options)` |
| 155 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 156 | See link:pg-plugin-endpoints.html[endpoints]. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 157 | |
| 158 | === registerCustomComponent |
| 159 | `plugin.registerCustomComponent(endpointName, opt_moduleName, opt_options)` |
| 160 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 161 | See link:pg-plugin-endpoints.html[endpoints]. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 162 | |
Thomas Shafer | 0327586 | 2019-02-26 15:39:16 -0800 | [diff] [blame] | 163 | === registerDynamicCustomComponent |
| 164 | `plugin.registerDynamicCustomComponent(dynamicEndpointName, opt_moduleName, |
| 165 | opt_options)` |
| 166 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 167 | See link:pg-plugin-endpoints.html[endpoints]. |
Thomas Shafer | 0327586 | 2019-02-26 15:39:16 -0800 | [diff] [blame] | 168 | |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 169 | === registerStyleModule |
| 170 | `plugin.registerStyleModule(endpointName, moduleName)` |
| 171 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 172 | See link:#low-level-style[above]. |
| 173 | |
| 174 | === on |
| 175 | Register a JavaScript callback to be invoked when events occur within |
| 176 | the web interface. Signature |
| 177 | |
| 178 | ``` js |
| 179 | self.on(event, callback); |
| 180 | ``` |
| 181 | |
| 182 | Parameters |
| 183 | |
| 184 | * event: A supported event type. See below for description. |
| 185 | |
| 186 | * callback: JavaScript function to be invoked when event happens. |
| 187 | Arguments may be passed to this function, depending on the event. |
| 188 | |
| 189 | Supported events: |
| 190 | |
| 191 | * `history`: Invoked when the view is changed to a new screen within |
| 192 | the Gerrit web application. The token after "#" is passed as the |
| 193 | argument to the callback function, for example "/c/42/" while |
| 194 | showing change 42. |
| 195 | |
| 196 | * `showchange`: Invoked when a change is made visible. A |
| 197 | link:rest-api-changes.html#change-info[ChangeInfo] and |
| 198 | link:rest-api-changes.html#revision-info[RevisionInfo] |
| 199 | are passed as arguments. Gerrit provides a third parameter which |
| 200 | is an object with a `mergeable` boolean. |
| 201 | |
| 202 | * `submitchange`: Invoked when the submit button is clicked |
| 203 | on a change. A link:rest-api-changes.html#change-info[ChangeInfo] |
| 204 | and link:rest-api-changes.html#revision-info[RevisionInfo] are |
| 205 | passed as arguments. Similar to a form submit validation, the |
| 206 | function must return true to allow the operation to continue, or |
| 207 | false to prevent it. The function may be called multiple times, for |
| 208 | example, if submitting a change shows a confirmation dialog, this |
| 209 | event may be called to validate that the check whether dialog can be |
| 210 | shown, and called again when the submit is confirmed to check whether |
| 211 | the actual submission action can proceed. |
| 212 | |
| 213 | * `comment`: Invoked when a DOM element that represents a comment is |
| 214 | created. This DOM element is passed as argument. This DOM element |
| 215 | contains nested elements that Gerrit uses to format the comment. The |
| 216 | DOM structure may differ between comment types such as inline |
| 217 | comments, file-level comments and summary comments, and it may change |
| 218 | with new Gerrit versions. |
| 219 | |
| 220 | * `highlightjs-loaded`: Invoked when the highlight.js library has |
| 221 | finished loading. The global `hljs` object (also now accessible via |
| 222 | `window.hljs`) is passed as an argument to the callback function. |
| 223 | This event can be used to register a new language highlighter with |
| 224 | the highlight.js library before syntax highlighting begins. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 225 | |
| 226 | [[high-level-api]] |
| 227 | == High-level API |
| 228 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 229 | Plugin instance provides access to a number of more specific APIs and methods |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 230 | to be used by plugin authors. |
| 231 | |
Wyatt Allen | c148593 | 2018-03-30 10:53:36 -0700 | [diff] [blame] | 232 | === admin |
| 233 | `plugin.admin()` |
| 234 | |
| 235 | .Params: |
| 236 | - none |
| 237 | |
| 238 | .Returns: |
| 239 | - Instance of link:pg-plugin-admin-api.html[GrAdminApi]. |
| 240 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 241 | === changeActions |
| 242 | `self.changeActions()` |
| 243 | |
| 244 | Returns an instance of the |
| 245 | link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/app/api/change-actions.ts[ChangeActionsPluginApi]. |
| 246 | |
| 247 | ==== changeActions.add() |
| 248 | Adds a new action to the change actions section. Returns the key of the newly |
| 249 | added action. |
| 250 | |
| 251 | ``` js |
| 252 | changeActions.add(type, label) |
| 253 | ``` |
| 254 | |
| 255 | * type: The type of the action, either `change` or `revision`. |
| 256 | |
| 257 | * label: The label to be used in UI for this action. |
| 258 | |
| 259 | |
| 260 | ==== changeActions.remove() |
| 261 | Removes an action from the change actions section. |
| 262 | |
| 263 | ``` js |
| 264 | changeActions.remove(key) |
| 265 | ``` |
| 266 | |
| 267 | * key: The key of the action. |
| 268 | |
| 269 | |
| 270 | ==== changeActions.addTapListener() |
| 271 | Adds a tap listener to an action that will be invoked when the action |
| 272 | is tapped. |
| 273 | |
| 274 | ``` js |
| 275 | changeActions.addTapListener(key, callback) |
| 276 | ``` |
| 277 | |
| 278 | * key: The key of the action. |
| 279 | |
| 280 | * callback: JavaScript function to be invoked when action tapped. |
| 281 | |
| 282 | |
| 283 | ==== changeActions.removeTapListener() |
| 284 | Removes an existing tap listener on an action. |
| 285 | |
| 286 | ``` js |
| 287 | changeActions.removeTapListener(key, callback) |
| 288 | ``` |
| 289 | |
| 290 | * key: The key of the action. |
| 291 | |
| 292 | * callback: JavaScript function to be removed. |
| 293 | |
| 294 | |
| 295 | ==== changeActions.setLabel() |
| 296 | Sets the label for an action. |
| 297 | |
| 298 | ``` js |
| 299 | changeActions.setLabel(key, label) |
| 300 | ``` |
| 301 | |
| 302 | * key: The key of the action. |
| 303 | |
| 304 | * label: The label of the action. |
| 305 | |
| 306 | |
| 307 | ==== changeActions.setTitle() |
| 308 | Sets the title for an action. |
| 309 | |
| 310 | ``` js |
| 311 | changeActions.setTitle(key, title) |
| 312 | ``` |
| 313 | |
| 314 | * key: The key of the action. |
| 315 | |
| 316 | * title: The title of the action. |
| 317 | |
| 318 | |
| 319 | ==== changeActions.setIcon() |
| 320 | Sets an icon for an action. |
| 321 | |
| 322 | ``` js |
| 323 | changeActions.setIcon(key, icon) |
| 324 | ``` |
| 325 | |
| 326 | * key: The key of the action. |
| 327 | |
| 328 | * icon: The name of the icon. |
| 329 | |
| 330 | |
| 331 | ==== changeActions.setEnabled() |
| 332 | Sets an action to enabled or disabled. |
| 333 | |
| 334 | ``` js |
| 335 | changeActions.setEnabled(key, enabled) |
| 336 | ``` |
| 337 | |
| 338 | * key: The key of the action. |
| 339 | |
| 340 | * enabled: The status of the action, true to enable. |
| 341 | |
| 342 | |
| 343 | ==== changeActions.setActionHidden() |
| 344 | Sets an action to be hidden. |
| 345 | |
| 346 | ``` js |
| 347 | changeActions.setActionHidden(type, key, hidden) |
| 348 | ``` |
| 349 | |
| 350 | * type: The type of the action. |
| 351 | |
| 352 | * key: The key of the action. |
| 353 | |
| 354 | * hidden: True to hide the action, false to show the action. |
| 355 | |
| 356 | |
| 357 | ==== changeActions.setActionOverflow() |
| 358 | Sets an action to show in overflow menu. |
| 359 | |
| 360 | ``` js |
| 361 | changeActions.setActionOverflow(type, key, overflow) |
| 362 | ``` |
| 363 | |
| 364 | * type: The type of the action. |
| 365 | |
| 366 | * key: The key of the action. |
| 367 | |
| 368 | * overflow: True to move the action to overflow menu, false to move |
| 369 | the action out of the overflow menu. |
| 370 | |
| 371 | |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 372 | === changeReply |
| 373 | `plugin.changeReply()` |
| 374 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 375 | Returns an instance of the |
| 376 | link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/app/api/change-reply.ts[ChangeReplyPluginApi]. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 377 | |
Ben Rohlfs | ab7fbbf | 2021-04-29 10:27:03 +0200 | [diff] [blame] | 378 | [[checks]] |
| 379 | === checks |
| 380 | `plugin.checks()` |
| 381 | |
| 382 | Returns an instance of the link:pg-plugin-checks-api.html[ChecksApi]. |
| 383 | |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 384 | === getPluginName |
| 385 | `plugin.getPluginName()` |
| 386 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 387 | Returns the name this plugin was installed as by the server |
| 388 | administrator. The plugin name is required to access REST API |
| 389 | views installed by the plugin, or to access resources. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 390 | |
| 391 | === getServerInfo |
| 392 | `plugin.getServerInfo()` |
| 393 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 394 | Returns the host config as a link:rest-api-config.html#server-info[ServerInfo] |
| 395 | object. |
Viktar Donich | 9c7164a | 2017-12-19 15:03:58 -0800 | [diff] [blame] | 396 | |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 397 | === popup |
| 398 | `plugin.popup(moduleName)` |
| 399 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 400 | Creates a popup that contains the given web components. Can be controlled with |
| 401 | calling `open()` and `close()` on the return value. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 402 | |
Viktar Donich | bc8088e | 2018-04-23 15:29:57 -0700 | [diff] [blame] | 403 | [[plugin-rest-api]] |
| 404 | === restApi |
| 405 | `plugin.restApi(opt_prefix)` |
| 406 | |
| 407 | .Params: |
| 408 | - (optional) URL prefix, for easy switching into plugin URL space, |
| 409 | e.g. `changes/1/revisions/1/cookbook~say-hello` |
| 410 | |
| 411 | .Returns: |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 412 | - Instance of link:pg-plugin-rest-api.html[RestPluginApi]. |
Viktar Donich | bc8088e | 2018-04-23 15:29:57 -0700 | [diff] [blame] | 413 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 414 | [[plugin-screen]] |
Viktar Donich | 6ea8c8a | 2017-12-12 14:41:47 -0800 | [diff] [blame] | 415 | === screen |
| 416 | `plugin.screen(screenName, opt_moduleName)` |
| 417 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 418 | Registers a web component as a dedicated top-level page that the router |
| 419 | understands and that has a URL (/x/pluginname/screenname) that can be navigated |
| 420 | to. Extension screens are usually linked from the |
| 421 | link:dev-plugins.html#top-menu-extensions[top menu]. |
| 422 | |
Viktar Donich | 6ea8c8a | 2017-12-12 14:41:47 -0800 | [diff] [blame] | 423 | .Params: |
| 424 | - `*string* screenName` URL path fragment of the screen, e.g. |
| 425 | `/x/pluginname/*screenname*` |
| 426 | - `*string* opt_moduleName` (Optional) Web component to be instantiated for this |
| 427 | screen. |
| 428 | |
| 429 | .Returns: |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 430 | - Instance of HookApi. |
Viktar Donich | 7610f78 | 2017-10-02 11:51:41 +0100 | [diff] [blame] | 431 | |
| 432 | === url |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 433 | `plugin.url(opt_path)` |
| 434 | |
| 435 | Returns a URL within the plugin's URL space. If invoked with no |
| 436 | parameter the URL of the plugin is returned. If passed a string |
| 437 | the argument is appended to the plugin URL. |
| 438 | |
| 439 | A plugin's URL is where this plugin is loaded, it doesn't |
| 440 | necessary to be the same as the Gerrit host. Use `window.location` |
| 441 | if you need to access the Gerrit host info. |
| 442 | |
| 443 | ``` js |
| 444 | self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/" |
| 445 | self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png" |
| 446 | ``` |