blob: 030541d43b66b36004dfc9682115b0621d9c800a [file] [log] [blame]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001= Gerrit Code Review - JavaScript API
Shawn Pearce92a1fa82013-07-16 15:01:40 -07002
3Gerrit Code Review supports an API for JavaScript plugins to interact
4with the web UI and the server process.
5
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08006== Entry Point
Shawn Pearce92a1fa82013-07-16 15:01:40 -07007
8JavaScript is loaded using a standard `<script src='...'>` HTML tag.
9Plugins should protect the global namespace by defining their code
10within an anonymous function passed to `Gerrit.install()`. The plugin
11will be passed an object describing its registration with Gerrit:
12
David Pursehouse68153d72013-09-04 10:09:17 +090013[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070014----
15Gerrit.install(function (self) {
16 // ... plugin JavaScript code here ...
17});
18----
19
20
21[[self]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080022== Plugin Instance
Shawn Pearce92a1fa82013-07-16 15:01:40 -070023
24The plugin instance is passed to the plugin's initialization function
25and provides a number of utility services to plugin authors.
26
Edwin Kempin2fc17f42015-07-27 11:51:01 +020027[[self_getServerInfo]]
28=== self.getServerInfo()
29Returns the server's link:rest-api-config.html#server-info[ServerInfo]
30data.
31
David Pursehouse8d0b5202013-08-01 14:13:17 +090032[[self_getPluginName]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080033=== self.getPluginName()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070034Returns the name this plugin was installed as by the server
35administrator. The plugin name is required to access REST API
36views installed by the plugin, or to access resources.
37
Shawn Pearcedb284cd2013-11-29 20:48:21 -080038[[self_on]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080039=== self.on()
Shawn Pearcedb284cd2013-11-29 20:48:21 -080040Register a JavaScript callback to be invoked when events occur within
41the web interface.
42
43.Signature
44[source,javascript]
45----
Tao Zhou803a3812019-09-26 13:42:05 +020046self.on(event, callback);
Shawn Pearcedb284cd2013-11-29 20:48:21 -080047----
48
49* event: A supported event type. See below for description.
50
51* callback: JavaScript function to be invoked when event happens.
52 Arguments may be passed to this function, depending on the event.
53
54Supported events:
55
Shawn Pearced5c844f2013-12-26 15:32:26 -080056* `history`: Invoked when the view is changed to a new screen within
57 the Gerrit web application. The token after "#" is passed as the
Shawn Pearcea48826e2013-11-29 20:38:55 -080058 argument to the callback function, for example "/c/42/" while
59 showing change 42.
60
Shawn Pearcedb284cd2013-11-29 20:48:21 -080061* `showchange`: Invoked when a change is made visible. A
62 link:rest-api-changes.html#change-info[ChangeInfo] and
63 link:rest-api-changes.html#revision-info[RevisionInfo]
Wyatt Allen95a4d122018-01-25 14:04:26 -080064 are passed as arguments. PolyGerrit provides a third parameter which
65 is an object with a `mergeable` boolean.
Shawn Pearcedb284cd2013-11-29 20:48:21 -080066
Shawn Pearce7c418992013-11-29 20:34:28 -080067* `submitchange`: Invoked when the submit button is clicked
68 on a change. A link:rest-api-changes.html#change-info[ChangeInfo]
69 and link:rest-api-changes.html#revision-info[RevisionInfo] are
70 passed as arguments. Similar to a form submit validation, the
71 function must return true to allow the operation to continue, or
Wyatt Allen7af56a22018-04-20 15:13:37 +020072 false to prevent it. The function may be called multiple times, for
73 example, if submitting a change shows a confirmation dialog, this
74 event may be called to validate that the check whether dialog can be
75 shown, and called again when the submit is confirmed to check whether
76 the actual submission action can proceed.
Shawn Pearce7c418992013-11-29 20:34:28 -080077
Edwin Kempindaefed02015-12-22 16:22:44 +010078* `comment`: Invoked when a DOM element that represents a comment is
79 created. This DOM element is passed as argument. This DOM element
80 contains nested elements that Gerrit uses to format the comment. The
81 DOM structure may differ between comment types such as inline
82 comments, file-level comments and summary comments, and it may change
83 with new Gerrit versions.
84
Peter Marshalld1f36422019-03-11 15:02:56 +010085* `highlightjs-loaded`: Invoked when the highlight.js library has
86 finished loading. The global `hljs` object (also now accessible via
87 `window.hljs`) is passed as an argument to the callback function.
88 This event can be used to register a new language highlighter with
89 the highlight.js library before syntax highlighting begins.
90
Tao Zhou803a3812019-09-26 13:42:05 +020091[[self_changeActions]]
92=== self.changeActions()
93Returns an instance of ChangeActions API.
Shawn Pearce92a1fa82013-07-16 15:01:40 -070094
95.Signature
David Pursehouse68153d72013-09-04 10:09:17 +090096[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070097----
Tao Zhou803a3812019-09-26 13:42:05 +020098self.changeActions();
Shawn Pearce92a1fa82013-07-16 15:01:40 -070099----
100
Shawn Pearced5c844f2013-12-26 15:32:26 -0800101[[self_screen]]
102=== self.screen()
Tao Zhou803a3812019-09-26 13:42:05 +0200103Register a module to be attached when the user navigates
Shawn Pearced5c844f2013-12-26 15:32:26 -0800104to an extension screen provided by the plugin. Extension screens are
105usually linked from the link:dev-plugins.html#top-menu-extensions[top menu].
Shawn Pearced5c844f2013-12-26 15:32:26 -0800106
107.Signature
108[source,javascript]
109----
Tao Zhou803a3812019-09-26 13:42:05 +0200110self.screen(pattern, opt_moduleName);
Shawn Pearced5c844f2013-12-26 15:32:26 -0800111----
112
113* pattern: URL token pattern to identify the screen. Argument can be
114 either a string (`'index'`) or a RegExp object (`/list\/(.*)/`).
115 If a RegExp is used the matching groups will be available inside of
116 the context as `token_match`.
117
Tao Zhou803a3812019-09-26 13:42:05 +0200118* opt_moduleName: The module to load when the user navigates to
Shawn Pearced5c844f2013-12-26 15:32:26 -0800119 the screen. The function will be passed a link:#ScreenContext[screen context].
120
Tao Zhou803a3812019-09-26 13:42:05 +0200121[[self_settings]]
122=== self.settings()
123Returns the Settings API.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200124
125.Signature
126[source,javascript]
127----
Tao Zhou803a3812019-09-26 13:42:05 +0200128self.settings();
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200129----
130
Tao Zhou803a3812019-09-26 13:42:05 +0200131[[self_registerCustomComponent]]
132=== self.registerCustomComponent()
133Register a custom component to a specific endpoint.
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200134
135.Signature
136[source,javascript]
137----
Tao Zhou803a3812019-09-26 13:42:05 +0200138self.registerCustomComponent(endpointName, opt_moduleName, opt_options);
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200139----
140
Tao Zhou803a3812019-09-26 13:42:05 +0200141* endpointName: The endpoint this plugin should be reigistered to.
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200142
Tao Zhou803a3812019-09-26 13:42:05 +0200143* opt_moduleName: The module name the custom component will use.
144
145* opt_options: Options to register this custom component.
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200146
David Pursehouse8d0b5202013-08-01 14:13:17 +0900147[[self_url]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800148=== self.url()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700149Returns a URL within the plugin's URL space. If invoked with no
David Pursehouse8d0b5202013-08-01 14:13:17 +0900150parameter the URL of the plugin is returned. If passed a string
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700151the argument is appended to the plugin URL.
152
David Pursehouse68153d72013-09-04 10:09:17 +0900153[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700154----
155self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/"
156self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png"
157----
158
Tao Zhou803a3812019-09-26 13:42:05 +0200159[[self_restApi]]
160=== self.restApi()
161Returns an instance of the Plugin REST API.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700162
163.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900164[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700165----
Tao Zhou803a3812019-09-26 13:42:05 +0200166self.restApi(prefix_url)
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700167----
168
Tao Zhou803a3812019-09-26 13:42:05 +0200169* prefix_url: Base url for subsequent .get(), .post() etc requests.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700170
Tao Zhou803a3812019-09-26 13:42:05 +0200171[[PluginRestAPI]]
172== Plugin Rest API
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700173
Tao Zhou803a3812019-09-26 13:42:05 +0200174[[plugin_rest_delete]]
175=== restApi.delete()
176Issues a DELETE REST API request to the Gerrit server.
177Returns a promise with the response of the request.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700178
179.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900180[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700181----
Tao Zhou803a3812019-09-26 13:42:05 +0200182restApi.delete(url)
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700183----
184
Tao Zhou803a3812019-09-26 13:42:05 +0200185* url: URL relative to the base url.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700186
Tao Zhou803a3812019-09-26 13:42:05 +0200187[[plugin_rest_get]]
188=== restApi.get()
189Issues a GET REST API request to the Gerrit server.
190Returns a promise with the response of the request.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700191
192.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900193[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700194----
Tao Zhou803a3812019-09-26 13:42:05 +0200195restApi.get(url)
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700196----
197
Tao Zhou803a3812019-09-26 13:42:05 +0200198* url: URL relative to the base url.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700199
Tao Zhou803a3812019-09-26 13:42:05 +0200200[[plugin_rest_post]]
201=== restApi.post()
202Issues a POST REST API request to the Gerrit server.
203Returns a promise with the response of the request.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700204
205.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900206[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700207----
Tao Zhou803a3812019-09-26 13:42:05 +0200208restApi.post(url, opt_payload, opt_errFn, opt_contentType)
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700209----
210
Tao Zhou803a3812019-09-26 13:42:05 +0200211* url: URL relative to the base url.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700212
Tao Zhou803a3812019-09-26 13:42:05 +0200213* opt_payload: JavaScript object to serialize as the request payload.
214
215* opt_errFn: JavaScript function to be invoked when error occured.
216
217* opt_contentType: Content-Type to be sent along with the request.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700218
David Pursehouse68153d72013-09-04 10:09:17 +0900219[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700220----
Tao Zhou803a3812019-09-26 13:42:05 +0200221restApi.post(
222 '/my-servlet',
223 {start_build: true, platform_type: 'Linux'});
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700224----
225
Tao Zhou803a3812019-09-26 13:42:05 +0200226[[plugin_rest_put]]
227=== restApi.put()
228Issues a PUT REST API request to the Gerrit server.
229Returns a promise with the response of the request.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700230
231.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900232[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700233----
Tao Zhou803a3812019-09-26 13:42:05 +0200234restApi.put(url, opt_payload, opt_errFn, opt_contentType)
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700235----
236
Tao Zhou803a3812019-09-26 13:42:05 +0200237* url: URL relative to the base url.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700238
Tao Zhou803a3812019-09-26 13:42:05 +0200239* opt_payload: JavaScript object to serialize as the request payload.
240
241* opt_errFn: JavaScript function to be invoked when error occured.
242
243* opt_contentType: Content-Type to be sent along with the request.
David Pursehouse68153d72013-09-04 10:09:17 +0900244
245[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700246----
Tao Zhou803a3812019-09-26 13:42:05 +0200247restApi.put(
248 '/builds',
249 {start_build: true, platform_type: 'Linux'});
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700250----
251
Tao Zhou803a3812019-09-26 13:42:05 +0200252[[ChangeActions]]
253== Change Actions API
254A new Change Actions API instance will be created when `changeActions()`
255is invoked.
256
257[[change_actions_add]]
258=== changeActions.add()
259Adds a new action to the change actions section.
260Returns the key of the newly added action.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700261
262.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900263[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700264----
Tao Zhou803a3812019-09-26 13:42:05 +0200265changeActions.add(type, label)
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700266----
267
Tao Zhou803a3812019-09-26 13:42:05 +0200268* type: The type of the action, either `change` or `revision`.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700269
Tao Zhou803a3812019-09-26 13:42:05 +0200270* label: The label to be used in UI for this action.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700271
David Pursehouse68153d72013-09-04 10:09:17 +0900272[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700273----
Tao Zhou803a3812019-09-26 13:42:05 +0200274changeActions.add("change", "test")
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700275----
276
Tao Zhou803a3812019-09-26 13:42:05 +0200277[[change_actions_remove]]
278=== changeActions.remove()
279Removes an action from the change actions section.
Shawn Pearced5c844f2013-12-26 15:32:26 -0800280
281.Signature
282[source,javascript]
283----
Tao Zhou803a3812019-09-26 13:42:05 +0200284changeActions.remove(key)
Shawn Pearced5c844f2013-12-26 15:32:26 -0800285----
286
Tao Zhou803a3812019-09-26 13:42:05 +0200287* key: The key of the action.
Shawn Pearced5c844f2013-12-26 15:32:26 -0800288
Tao Zhou803a3812019-09-26 13:42:05 +0200289[[change_actions_addTapListener]]
290=== changeActions.addTapListener()
291Adds a tap listener to an action that will be invoked when the action
292is tapped.
Shawn Pearced5c844f2013-12-26 15:32:26 -0800293
294.Signature
295[source,javascript]
296----
Tao Zhou803a3812019-09-26 13:42:05 +0200297changeActions.addTapListener(key, callback)
Shawn Pearced5c844f2013-12-26 15:32:26 -0800298----
299
Tao Zhou803a3812019-09-26 13:42:05 +0200300* key: The key of the action.
301
302* callback: JavaScript function to be invoked when action tapped.
303
304[source,javascript]
305----
306changeActions.addTapListener("__key_for_my_action__", () => {
307 // do something when my action gets clicked
308})
309----
310
311[[change_actions_removeTapListener]]
312=== changeActions.removeTapListener()
313Removes an existing tap listener on an action.
Shawn Pearced5c844f2013-12-26 15:32:26 -0800314
315.Signature
316[source,javascript]
317----
Tao Zhou803a3812019-09-26 13:42:05 +0200318changeActions.removeTapListener(key, callback)
Shawn Pearced5c844f2013-12-26 15:32:26 -0800319----
320
Tao Zhou803a3812019-09-26 13:42:05 +0200321* key: The key of the action.
Shawn Pearced5c844f2013-12-26 15:32:26 -0800322
Tao Zhou803a3812019-09-26 13:42:05 +0200323* callback: JavaScript function to be removed.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200324
Tao Zhou803a3812019-09-26 13:42:05 +0200325[[change_actions_setLabel]]
326=== changeActions.setLabel()
327Sets the label for an action.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200328
329.Signature
330[source,javascript]
331----
Tao Zhou803a3812019-09-26 13:42:05 +0200332changeActions.setLabel(key, label)
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200333----
334
Tao Zhou803a3812019-09-26 13:42:05 +0200335* key: The key of the action.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200336
Tao Zhou803a3812019-09-26 13:42:05 +0200337* label: The label of the action.
338
339[[change_actions_setTitle]]
340=== changeActions.setTitle()
341Sets the title for an action.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200342
343.Signature
344[source,javascript]
345----
Tao Zhou803a3812019-09-26 13:42:05 +0200346changeActions.setTitle(key, title)
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200347----
348
Tao Zhou803a3812019-09-26 13:42:05 +0200349* key: The key of the action.
350
351* title: The title of the action.
352
353[[change_actions_setIcon]]
354=== changeActions.setIcon()
355Sets an icon for an action.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200356
357.Signature
358[source,javascript]
359----
Tao Zhou803a3812019-09-26 13:42:05 +0200360changeActions.setIcon(key, icon)
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200361----
362
Tao Zhou803a3812019-09-26 13:42:05 +0200363* key: The key of the action.
364
365* icon: The name of the icon.
366
367[[change_actions_setEnabled]]
368=== changeActions.setEnabled()
369Sets an action to enabled or disabled.
370
371.Signature
372[source,javascript]
373----
374changeActions.setEnabled(key, enabled)
375----
376
377* key: The key of the action.
378
379* enabled: The status of the action, true to enable.
380
381[[change_actions_setActionHidden]]
382=== changeActions.setActionHidden()
383Sets an action to be hidden.
384
385.Signature
386[source,javascript]
387----
388changeActions.setActionHidden(type, key, hidden)
389----
390
391* type: The type of the action.
392
393* key: The key of the action.
394
395* hidden: True to hide the action, false to show the action.
396
397[[change_actions_setActionOverflow]]
398=== changeActions.setActionOverflow()
399Sets an action to show in overflow menu.
400
401.Signature
402[source,javascript]
403----
404changeActions.setActionOverflow(type, key, overflow)
405----
406
407* type: The type of the action.
408
409* key: The key of the action.
410
411* overflow: True to move the action to overflow menu, false to move
412 the action out of the overflow menu.
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200413
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200414[[PanelContext]]
415== Panel Context
416A new panel context is passed to the `panel` callback function each
417time a screen with the given extension point is loaded.
418
419[[panel_body]]
420=== panel.body
421Empty HTML `<div>` node the plugin should add the panel content to.
422The node is already attached to the document.
423
424[[PanelProperties]]
425=== Properties
426
427The extension panel parameters that are described in the
428link:dev-plugins.html#panels[plugin development documentation] are
429contained in the context as properties. Which properties are available
430depends on the extension point.
431
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700432[[Gerrit]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800433== Gerrit
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700434
435The `Gerrit` object is the only symbol provided into the global
436namespace by Gerrit Code Review. All top-level functions can be
437accessed through this name.
438
Shawn Pearce210b5392013-12-07 22:41:36 -0800439[[Gerrit_css]]
David Pursehouseb10c2662016-12-06 08:41:33 +0900440=== Gerrit.css()
Dmitrii Filippov941ee632019-08-22 15:52:37 +0000441[WARNING]
442This method is deprecated. It doesn't work with Shadow DOM and
443will be removed in the future. Please, use link:pg-plugin-dev.html#plugin-styles[plugin.styles] instead.
444
Shawn Pearce210b5392013-12-07 22:41:36 -0800445Creates a new unique CSS class and injects it into the document.
446The name of the class is returned and can be used by the plugin.
Shawn Pearce210b5392013-12-07 22:41:36 -0800447
448Classes created with this function should be created once at install
449time and reused throughout the plugin. Repeatedly creating the same
450class will explode the global stylesheet.
451
452.Signature
453[source,javascript]
454----
455Gerrit.install(function(self)) {
456 var style = {
457 name: Gerrit.css('background: #fff; color: #000;'),
458 };
459});
460----
461
David Pursehouse8d0b5202013-08-01 14:13:17 +0900462[[Gerrit_install]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800463=== Gerrit.install()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700464Registers a new plugin by invoking the supplied initialization
465function. The function is passed the link:#self[plugin instance].
466
David Pursehouse68153d72013-09-04 10:09:17 +0900467[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700468----
469Gerrit.install(function (self) {
470 // ... plugin JavaScript code here ...
471});
472----
473
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700474GERRIT
475------
476Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -0700477
478SEARCHBOX
479---------