blob: 8c9950ecf42e3393aa08fec7928a245f2dc5ce74 [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
David Pursehouse8d0b5202013-08-01 14:13:17 +090027[[self_delete]]
Edwin Kempinfc998c42014-02-03 17:51:20 +010028=== self.delete() / self.del()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070029Issues a DELETE REST API request to the Gerrit server.
30
31.Signature
David Pursehouse68153d72013-09-04 10:09:17 +090032[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070033----
34Gerrit.delete(url, callback)
Edwin Kempinfc998c42014-02-03 17:51:20 +010035Gerrit.del(url, callback)
Shawn Pearce92a1fa82013-07-16 15:01:40 -070036----
37
38* url: URL relative to the plugin's URL space. The JavaScript
39 library prefixes the supplied URL with `/plugins/{getPluginName}/`.
40
41* callback: JavaScript function to be invoked with the parsed
42 JSON result of the API call. DELETE methods often return
43 `204 No Content`, which is passed as null.
44
David Pursehouse8d0b5202013-08-01 14:13:17 +090045[[self_get]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080046=== self.get()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070047Issues a GET REST API request to the Gerrit server.
48
49.Signature
David Pursehouse68153d72013-09-04 10:09:17 +090050[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070051----
52self.get(url, callback)
53----
54
55* url: URL relative to the plugin's URL space. The JavaScript
56 library prefixes the supplied URL with `/plugins/{getPluginName}/`.
57
58* callback: JavaScript function to be invoked with the parsed JSON
59 result of the API call. If the API returns a string the result is
60 a string, otherwise the result is a JavaScript object or array,
61 as described in the relevant REST API documentation.
62
Edwin Kempin2fc17f42015-07-27 11:51:01 +020063[[self_getServerInfo]]
64=== self.getServerInfo()
65Returns the server's link:rest-api-config.html#server-info[ServerInfo]
66data.
67
David Ostrovsky0a4c0ff2014-09-21 23:06:52 +020068[[self_getCurrentUser]]
69=== self.getCurrentUser()
70Returns the currently signed in user's AccountInfo data; empty account
71data if no user is currently signed in.
72
Edwin Kempin936d1ed2015-07-22 15:01:59 +020073[[Gerrit_getUserPreferences]]
74=== Gerrit.getUserPreferences()
75Returns the preferences of the currently signed in user; the default
76preferences if no user is currently signed in.
77
78[[Gerrit_refreshUserPreferences]]
79=== Gerrit.refreshUserPreferences()
80Refreshes the preferences of the current user.
81
David Pursehouse8d0b5202013-08-01 14:13:17 +090082[[self_getPluginName]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080083=== self.getPluginName()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070084Returns the name this plugin was installed as by the server
85administrator. The plugin name is required to access REST API
86views installed by the plugin, or to access resources.
87
David Pursehouse8d0b5202013-08-01 14:13:17 +090088[[self_post]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080089=== self.post()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070090Issues a POST REST API request to the Gerrit server.
91
92.Signature
David Pursehouse68153d72013-09-04 10:09:17 +090093[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070094----
95self.post(url, input, callback)
96----
97
98* url: URL relative to the plugin's URL space. The JavaScript
99 library prefixes the supplied URL with `/plugins/{getPluginName}/`.
100
101* input: JavaScript object to serialize as the request payload.
102
103* callback: JavaScript function to be invoked with the parsed JSON
104 result of the API call. If the API returns a string the result is
105 a string, otherwise the result is a JavaScript object or array,
106 as described in the relevant REST API documentation.
107
David Pursehouse68153d72013-09-04 10:09:17 +0900108[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700109----
110self.post(
111 '/my-servlet',
112 {start_build: true, platform_type: 'Linux'},
113 function (r) {});
114----
115
David Pursehouse8d0b5202013-08-01 14:13:17 +0900116[[self_put]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800117=== self.put()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700118Issues a PUT REST API request to the Gerrit server.
119
120.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900121[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700122----
123self.put(url, input, callback)
124----
125
126* url: URL relative to the plugin's URL space. The JavaScript
127 library prefixes the supplied URL with `/plugins/{getPluginName}/`.
128
129* input: JavaScript object to serialize as the request payload.
130
131* callback: JavaScript function to be invoked with the parsed JSON
132 result of the API call. If the API returns a string the result is
133 a string, otherwise the result is a JavaScript object or array,
134 as described in the relevant REST API documentation.
135
David Pursehouse68153d72013-09-04 10:09:17 +0900136[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700137----
138self.put(
139 '/builds',
140 {start_build: true, platform_type: 'Linux'},
141 function (r) {});
142----
143
Shawn Pearcedb284cd2013-11-29 20:48:21 -0800144[[self_on]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800145=== self.on()
Shawn Pearcedb284cd2013-11-29 20:48:21 -0800146Register a JavaScript callback to be invoked when events occur within
147the web interface.
148
149.Signature
150[source,javascript]
151----
152Gerrit.on(event, callback);
153----
154
155* event: A supported event type. See below for description.
156
157* callback: JavaScript function to be invoked when event happens.
158 Arguments may be passed to this function, depending on the event.
159
160Supported events:
161
Shawn Pearced5c844f2013-12-26 15:32:26 -0800162* `history`: Invoked when the view is changed to a new screen within
163 the Gerrit web application. The token after "#" is passed as the
Shawn Pearcea48826e2013-11-29 20:38:55 -0800164 argument to the callback function, for example "/c/42/" while
165 showing change 42.
166
Shawn Pearcedb284cd2013-11-29 20:48:21 -0800167* `showchange`: Invoked when a change is made visible. A
168 link:rest-api-changes.html#change-info[ChangeInfo] and
169 link:rest-api-changes.html#revision-info[RevisionInfo]
170 are passed as arguments.
171
Shawn Pearce7c418992013-11-29 20:34:28 -0800172* `submitchange`: Invoked when the submit button is clicked
173 on a change. A link:rest-api-changes.html#change-info[ChangeInfo]
174 and link:rest-api-changes.html#revision-info[RevisionInfo] are
175 passed as arguments. Similar to a form submit validation, the
176 function must return true to allow the operation to continue, or
177 false to prevent it.
178
Edwin Kempindaefed02015-12-22 16:22:44 +0100179* `comment`: Invoked when a DOM element that represents a comment is
180 created. This DOM element is passed as argument. This DOM element
181 contains nested elements that Gerrit uses to format the comment. The
182 DOM structure may differ between comment types such as inline
183 comments, file-level comments and summary comments, and it may change
184 with new Gerrit versions.
185
David Pursehouse8d0b5202013-08-01 14:13:17 +0900186[[self_onAction]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800187=== self.onAction()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700188Register a JavaScript callback to be invoked when the user clicks
189on a button associated with a server side `UiAction`.
190
191.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900192[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700193----
Shawn Pearced5c844f2013-12-26 15:32:26 -0800194self.onAction(type, view_name, callback);
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700195----
196
David Ostrovskycdda3312014-08-03 14:03:13 +0200197* type: `'change'`, `'edit'`, `'revision'`, `'project'`, or `'branch'`
Shawn Pearced1cf9622014-03-24 16:21:51 -0700198 indicating which type of resource the `UiAction` was bound to
199 in the server.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700200
201* view_name: string appearing in URLs to name the view. This is the
202 second argument of the `get()`, `post()`, `put()`, and `delete()`
203 binding methods in a `RestApiModule`.
204
205* callback: JavaScript function to invoke when the user clicks. The
206 function will be passed a link:#ActionContext[action context].
207
Shawn Pearced5c844f2013-12-26 15:32:26 -0800208[[self_screen]]
209=== self.screen()
210Register a JavaScript callback to be invoked when the user navigates
211to an extension screen provided by the plugin. Extension screens are
212usually linked from the link:dev-plugins.html#top-menu-extensions[top menu].
213The callback can populate the DOM with the screen's contents.
214
215.Signature
216[source,javascript]
217----
218self.screen(pattern, callback);
219----
220
221* pattern: URL token pattern to identify the screen. Argument can be
222 either a string (`'index'`) or a RegExp object (`/list\/(.*)/`).
223 If a RegExp is used the matching groups will be available inside of
224 the context as `token_match`.
225
226* callback: JavaScript function to invoke when the user navigates to
227 the screen. The function will be passed a link:#ScreenContext[screen context].
228
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200229[[self_settingsScreen]]
230=== self.settingsScreen()
231Register a JavaScript callback to be invoked when the user navigates
232to an extension settings screen provided by the plugin. Extension settings
233screens are automatically linked from the settings menu under the given
234menu entry.
235The callback can populate the DOM with the screen's contents.
236
237.Signature
238[source,javascript]
239----
240self.settingsScreen(path, menu, callback);
241----
242
243* path: URL path to identify the settings screen.
244
245* menu: The name of the menu entry in the settings menu that should
246 link to the settings screen.
247
248* callback: JavaScript function to invoke when the user navigates to
249 the settings screen. The function will be passed a
250 link:#SettingsScreenContext[settings screen context].
251
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200252[[self_panel]]
253=== self.panel()
254Register a JavaScript callback to be invoked when a screen with the
255given extension point is loaded.
256The callback can populate the DOM with the panel's contents.
257
258.Signature
259[source,javascript]
260----
261self.panel(extensionpoint, callback);
262----
263
264* extensionpoint: The name of the extension point that marks the
265 position where the panel is added to an existing screen. The
266 available extension points are described in the
267 link:dev-plugins.html#panels[plugin development documentation].
268
269* callback: JavaScript function to invoke when a screen with the
270 extension point is loaded. The function will be passed a
271 link:#PanelContext[panel context].
272
David Pursehouse8d0b5202013-08-01 14:13:17 +0900273[[self_url]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800274=== self.url()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700275Returns a URL within the plugin's URL space. If invoked with no
David Pursehouse8d0b5202013-08-01 14:13:17 +0900276parameter the URL of the plugin is returned. If passed a string
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700277the argument is appended to the plugin URL.
278
David Pursehouse68153d72013-09-04 10:09:17 +0900279[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700280----
281self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/"
282self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png"
283----
284
285
286[[ActionContext]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800287== Action Context
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700288A new action context is passed to the `onAction` callback function
289each time the associated action button is clicked by the user. A
290context is initialized with sufficient state to issue the associated
291REST API RPC.
292
David Pursehouse8d0b5202013-08-01 14:13:17 +0900293[[context_action]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800294=== context.action
David Pursehouse68153d72013-09-04 10:09:17 +0900295An link:rest-api-changes.html#action-info[ActionInfo] object instance
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700296supplied by the server describing the UI button the user used to
297invoke the action.
298
David Pursehouse8d0b5202013-08-01 14:13:17 +0900299[[context_call]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800300=== context.call()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700301Issues the REST API call associated with the action. The HTTP method
302used comes from `context.action.method`, hiding the JavaScript from
303needing to care.
304
305.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900306[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700307----
308context.call(input, callback)
309----
310
311* input: JavaScript object to serialize as the request payload. This
312 parameter is ignored for GET and DELETE methods.
313
314* callback: JavaScript function to be invoked with the parsed JSON
315 result of the API call. If the API returns a string the result is
316 a string, otherwise the result is a JavaScript object or array,
317 as described in the relevant REST API documentation.
318
David Pursehouse68153d72013-09-04 10:09:17 +0900319[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700320----
321context.call(
322 {message: "..."},
323 function (result) {
324 // ... use result here ...
325 });
326----
327
David Pursehouse8d0b5202013-08-01 14:13:17 +0900328[[context_change]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800329=== context.change
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700330When the action is invoked on a change a
331link:rest-api-changes.html#change-info[ChangeInfo] object instance
332describing the change. Available fields of the ChangeInfo may vary
333based on the options used by the UI when it loaded the change.
334
David Pursehouse8d0b5202013-08-01 14:13:17 +0900335[[context_delete]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800336=== context.delete()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700337Issues a DELETE REST API call to the URL associated with the action.
338
339.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900340[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700341----
342context.delete(callback)
343----
344
345* callback: JavaScript function to be invoked with the parsed
346 JSON result of the API call. DELETE methods often return
347 `204 No Content`, which is passed as null.
348
David Pursehouse68153d72013-09-04 10:09:17 +0900349[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700350----
351context.delete(function () {});
352----
353
David Pursehouse8d0b5202013-08-01 14:13:17 +0900354[[context_get]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800355=== context.get()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700356Issues a GET REST API call to the URL associated with the action.
357
358.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900359[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700360----
361context.get(callback)
362----
363
364* callback: JavaScript function to be invoked with the parsed JSON
365 result of the API call. If the API returns a string the result is
366 a string, otherwise the result is a JavaScript object or array,
367 as described in the relevant REST API documentation.
368
David Pursehouse68153d72013-09-04 10:09:17 +0900369[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700370----
371context.get(function (result) {
372 // ... use result here ...
373});
374----
375
David Pursehouse8d0b5202013-08-01 14:13:17 +0900376[[context_go]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800377=== context.go()
Shawn Pearced5c844f2013-12-26 15:32:26 -0800378Go to a screen. Shorthand for link:#Gerrit_go[`Gerrit.go()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700379
David Pursehouse8d0b5202013-08-01 14:13:17 +0900380[[context_hide]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800381=== context.hide()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700382Hide the currently visible popup displayed by
David Pursehouse8d0b5202013-08-01 14:13:17 +0900383link:#context_popup[`context.popup()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700384
David Pursehouse8d0b5202013-08-01 14:13:17 +0900385[[context_post]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800386=== context.post()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700387Issues a POST REST API call to the URL associated with the action.
388
389.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900390[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700391----
392context.post(input, callback)
393----
394
395* input: JavaScript object to serialize as the request payload.
396
397* callback: JavaScript function to be invoked with the parsed JSON
398 result of the API call. If the API returns a string the result is
399 a string, otherwise the result is a JavaScript object or array,
400 as described in the relevant REST API documentation.
401
David Pursehouse68153d72013-09-04 10:09:17 +0900402[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700403----
404context.post(
405 {message: "..."},
406 function (result) {
407 // ... use result here ...
408 });
409----
410
David Pursehouse8d0b5202013-08-01 14:13:17 +0900411[[context_popup]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800412=== context.popup()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700413
414Displays a small popup near the activation button to gather
415additional input from the user before executing the REST API RPC.
416
417The caller is always responsible for closing the popup with
David Pursehouse8d0b5202013-08-01 14:13:17 +0900418link#context_hide[`context.hide()`]. Gerrit will handle closing a
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700419popup if the user presses `Escape` while keyboard focus is within
420the popup.
421
422.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900423[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700424----
425context.popup(element)
426----
427
428* element: an HTML DOM element to display as the body of the
429 popup. This is typically a `div` element but can be any valid HTML
430 element. CSS can be used to style the element beyond the defaults.
431
432A common usage is to gather more input:
David Pursehouse68153d72013-09-04 10:09:17 +0900433
434[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700435----
436self.onAction('revision', 'start-build', function (c) {
437 var l = c.checkbox();
438 var m = c.checkbox();
439 c.popup(c.div(
440 c.div(c.label(l, 'Linux')),
441 c.div(c.label(m, 'Mac OS X')),
442 c.button('Build', {onclick: function() {
443 c.call(
444 {
445 commit: c.revision.name,
446 linux: l.checked,
447 mac: m.checked,
448 },
449 function() { c.hide() });
450 });
451});
452----
453
David Pursehouse8d0b5202013-08-01 14:13:17 +0900454[[context_put]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800455=== context.put()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700456Issues a PUT REST API call to the URL associated with the action.
457
458.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900459[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700460----
461context.put(input, callback)
462----
463
464* input: JavaScript object to serialize as the request payload.
465
466* callback: JavaScript function to be invoked with the parsed JSON
467 result of the API call. If the API returns a string the result is
468 a string, otherwise the result is a JavaScript object or array,
469 as described in the relevant REST API documentation.
470
David Pursehouse68153d72013-09-04 10:09:17 +0900471[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700472----
473context.put(
474 {message: "..."},
475 function (result) {
476 // ... use result here ...
477 });
478----
479
David Pursehouse8d0b5202013-08-01 14:13:17 +0900480[[context_refresh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800481=== context.refresh()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700482Refresh the current display. Shorthand for
David Pursehouse8d0b5202013-08-01 14:13:17 +0900483link:#Gerrit_refresh[`Gerrit.refresh()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700484
David Pursehouse8d0b5202013-08-01 14:13:17 +0900485[[context_revision]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800486=== context.revision
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700487When the action is invoked on a specific revision of a change,
488a link:rest-api-changes.html#revision-info[RevisionInfo]
489object instance describing the revision. Available fields of the
490RevisionInfo may vary based on the options used by the UI when it
491loaded the change.
492
David Ostrovsky9e8b2fb2013-08-30 08:09:53 +0200493[[context_project]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800494=== context.project
David Ostrovsky9e8b2fb2013-08-30 08:09:53 +0200495When the action is invoked on a specific project,
496the name of the project.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700497
Shawn Pearced5c844f2013-12-26 15:32:26 -0800498=== HTML Helpers
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700499The link:#ActionContext[action context] includes some HTML helper
500functions to make working with DOM based widgets less painful.
501
502* `br()`: new `<br>` element.
503
504* `button(label, options)`: new `<button>` with the string `label`
505 wrapped inside of a `div`. The optional `options` object may
506 define `onclick` as a function to be invoked upon clicking. This
507 calling pattern avoids circular references between the element
508 and the onclick handler.
509
510* `checkbox()`: new `<input type='checkbox'>` element.
511* `div(...)`: a new `<div>` wrapping the (optional) arguments.
512* `hr()`: new `<hr>` element.
513
514* `label(c, label)`: a new `<label>` element wrapping element `c`
515 and the string `label`. Used to wrap a checkbox with its label,
516 `label(checkbox(), 'Click Me')`.
517
David Ostrovskya1067a32013-08-19 09:09:17 +0200518* `prependLabel(label, c)`: a new `<label>` element wrapping element `c`
519 and the string `label`. Used to wrap an input field with its label,
520 `prependLabel('Greeting message', textfield())`.
521
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700522* `textarea(options)`: new `<textarea>` element. The options
523 object may optionally include `rows` and `cols`. The textarea
524 comes with an onkeypress handler installed to play nicely with
525 Gerrit's keyboard binding system.
526
527* `textfield()`: new `<input type='text'>` element. The text field
528 comes with an onkeypress handler installed to play nicely with
529 Gerrit's keyboard binding system.
530
Edwin Kempin15ce80c2013-11-14 17:16:24 +0100531* `select(a,i)`: a new `<select>` element containing one `<option>`
532 element for each entry in the provided array `a`. The option with
533 the index `i` will be pre-selected in the drop-down-list.
534
535* `selected(s)`: returns the text of the `<option>` element that is
536 currently selected in the provided `<select>` element `s`.
537
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700538* `span(...)`: a new `<span>` wrapping the (optional) arguments.
539
David Ostrovsky7dfc8022013-08-25 23:05:50 +0200540* `msg(label)`: a new label.
541
Shawn Pearced5c844f2013-12-26 15:32:26 -0800542
543[[ScreenContext]]
544== Screen Context
545A new screen context is passed to the `screen` callback function
546each time the user navigates to a matching URL.
547
548[[screen_body]]
549=== screen.body
550Empty HTML `<div>` node the plugin should add its content to. The
551node is already attached to the document, but is invisible. Plugins
552must call `screen.show()` to display the DOM node. Deferred display
553allows an implementor to partially populate the DOM, make remote HTTP
554requests, finish populating when the callbacks arrive, and only then
555make the view visible to the user.
556
557[[screen_token]]
558=== screen.token
559URL token fragment that activated this screen. The value is identical
560to `screen.token_match[0]`. If the URL is `/#/x/hello/list` the token
561will be `"list"`.
562
563[[screen_token_match]]
564=== screen.token_match
565Array of matching subgroups from the pattern specified to `screen()`.
566This is identical to the result of RegExp.exec. Index 0 contains the
567entire matching expression; index 1 the first matching group, etc.
568
569[[screen_onUnload]]
570=== screen.onUnload()
571Configures an optional callback to be invoked just before the screen
572is deleted from the browser DOM. Plugins can use this callback to
573remove event listeners from DOM nodes, preventing memory leaks.
574
575.Signature
576[source,javascript]
577----
578screen.onUnload(callback)
579----
580
581* callback: JavaScript function to be invoked just before the
582 `screen.body` DOM element is removed from the browser DOM.
583 This event happens when the user navigates to another screen.
584
585[[screen.setTitle]]
586=== screen.setTitle()
587Sets the heading text to be displayed when the screen is visible.
588This is presented in a large bold font below the menus, but above the
589content in `screen.body`. Setting the title also sets the window
590title to the same string, if it has not already been set.
591
592.Signature
593[source,javascript]
594----
595screen.setPageTitle(titleText)
596----
597
598[[screen.setWindowTitle]]
599=== screen.setWindowTitle()
600Sets the text to be displayed in the browser's title bar when the
601screen is visible. Plugins should always prefer this method over
602trying to set `window.title` directly. The window title defaults to
603the title given to `setTitle`.
604
605.Signature
606[source,javascript]
607----
608screen.setWindowTitle(titleText)
609----
610
611[[screen_show]]
612=== screen.show()
613Destroy the currently visible screen and display the plugin's screen.
614This method must be called after adding content to `screen.body`.
615
Edwin Kempindcaec6c2016-06-03 09:43:10 +0200616[[SettingsScreenContext]]
617== Settings Screen Context
618A new settings screen context is passed to the `settingsScreen` callback
619function each time the user navigates to a matching URL.
620
621[[settingsScreen_body]]
622=== settingsScreen.body
623Empty HTML `<div>` node the plugin should add its content to. The
624node is already attached to the document, but is invisible. Plugins
625must call `settingsScreen.show()` to display the DOM node. Deferred
626display allows an implementor to partially populate the DOM, make
627remote HTTP requests, finish populating when the callbacks arrive, and
628only then make the view visible to the user.
629
630[[settingsScreen_onUnload]]
631=== settingsScreen.onUnload()
632Configures an optional callback to be invoked just before the screen
633is deleted from the browser DOM. Plugins can use this callback to
634remove event listeners from DOM nodes, preventing memory leaks.
635
636.Signature
637[source,javascript]
638----
639settingsScreen.onUnload(callback)
640----
641
642* callback: JavaScript function to be invoked just before the
643 `settingsScreen.body` DOM element is removed from the browser DOM.
644 This event happens when the user navigates to another screen.
645
646[[settingsScreen.setTitle]]
647=== settingsScreen.setTitle()
648Sets the heading text to be displayed when the screen is visible.
649This is presented in a large bold font below the menus, but above the
650content in `settingsScreen.body`. Setting the title also sets the
651window title to the same string, if it has not already been set.
652
653.Signature
654[source,javascript]
655----
656settingsScreen.setPageTitle(titleText)
657----
658
659[[settingsScreen.setWindowTitle]]
660=== settingsScreen.setWindowTitle()
661Sets the text to be displayed in the browser's title bar when the
662screen is visible. Plugins should always prefer this method over
663trying to set `window.title` directly. The window title defaults to
664the title given to `setTitle`.
665
666.Signature
667[source,javascript]
668----
669settingsScreen.setWindowTitle(titleText)
670----
671
672[[settingsScreen_show]]
673=== settingsScreen.show()
674Destroy the currently visible screen and display the plugin's screen.
675This method must be called after adding content to
676`settingsScreen.body`.
677
Edwin Kempin9ef951b2016-06-03 13:27:50 +0200678[[PanelContext]]
679== Panel Context
680A new panel context is passed to the `panel` callback function each
681time a screen with the given extension point is loaded.
682
683[[panel_body]]
684=== panel.body
685Empty HTML `<div>` node the plugin should add the panel content to.
686The node is already attached to the document.
687
688[[PanelProperties]]
689=== Properties
690
691The extension panel parameters that are described in the
692link:dev-plugins.html#panels[plugin development documentation] are
693contained in the context as properties. Which properties are available
694depends on the extension point.
695
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700696[[Gerrit]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800697== Gerrit
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700698
699The `Gerrit` object is the only symbol provided into the global
700namespace by Gerrit Code Review. All top-level functions can be
701accessed through this name.
702
Shawn Pearce210b5392013-12-07 22:41:36 -0800703[[Gerrit_css]]
704Gerrit.css()
705~~~~~~~~~~~~
706Creates a new unique CSS class and injects it into the document.
707The name of the class is returned and can be used by the plugin.
708See link:#Gerrit_html[`Gerrit.html()`] for an easy way to use
709generated class names.
710
711Classes created with this function should be created once at install
712time and reused throughout the plugin. Repeatedly creating the same
713class will explode the global stylesheet.
714
715.Signature
716[source,javascript]
717----
718Gerrit.install(function(self)) {
719 var style = {
720 name: Gerrit.css('background: #fff; color: #000;'),
721 };
722});
723----
724
David Pursehouse8d0b5202013-08-01 14:13:17 +0900725[[Gerrit_delete]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800726=== Gerrit.delete()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700727Issues a DELETE REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900728private REST API URLs see link:#self_delete[self.delete()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700729
730.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900731[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700732----
733Gerrit.delete(url, callback)
734----
735
736* url: URL relative to the Gerrit server. For example to access the
737 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
738
739* callback: JavaScript function to be invoked with the parsed
740 JSON result of the API call. DELETE methods often return
741 `204 No Content`, which is passed as null.
742
David Pursehouse68153d72013-09-04 10:09:17 +0900743[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700744----
745Gerrit.delete(
746 '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic',
747 function () {});
748----
749
David Pursehouse8d0b5202013-08-01 14:13:17 +0900750[[Gerrit_get]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800751=== Gerrit.get()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700752Issues a GET REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900753private REST API URLs see link:#self_get[self.get()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700754
755.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900756[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700757----
758Gerrit.get(url, callback)
759----
760
761* url: URL relative to the Gerrit server. For example to access the
762 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
763
764* callback: JavaScript function to be invoked with the parsed JSON
765 result of the API call. If the API returns a string the result is
766 a string, otherwise the result is a JavaScript object or array,
767 as described in the relevant REST API documentation.
768
David Pursehouse68153d72013-09-04 10:09:17 +0900769[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700770----
771Gerrit.get('/changes/?q=status:open', function (open) {
772 for (var i = 0; i < open.length; i++) {
773 console.log(open.get(i).change_id);
774 }
775});
776----
777
David Ostrovsky0a4c0ff2014-09-21 23:06:52 +0200778[[Gerrit_getCurrentUser]]
779=== Gerrit.getCurrentUser()
780Returns the currently signed in user's AccountInfo data; empty account
781data if no user is currently signed in.
782
David Pursehouse8d0b5202013-08-01 14:13:17 +0900783[[Gerrit_getPluginName]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800784=== Gerrit.getPluginName()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700785Returns the name this plugin was installed as by the server
786administrator. The plugin name is required to access REST API
787views installed by the plugin, or to access resources.
788
David Pursehouse8d0b5202013-08-01 14:13:17 +0900789Unlike link:#self_getPluginName[`self.getPluginName()`] this method
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700790must guess the name from the JavaScript call stack. Plugins are
791encouraged to use `self.getPluginName()` whenever possible.
792
David Pursehouse8d0b5202013-08-01 14:13:17 +0900793[[Gerrit_go]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800794=== Gerrit.go()
Shawn Pearced5c844f2013-12-26 15:32:26 -0800795Updates the web UI to display the screen identified by the supplied
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700796URL token. The URL token is the text after `#` in the browser URL.
797
David Pursehouse68153d72013-09-04 10:09:17 +0900798[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700799----
800Gerrit.go('/admin/projects/');
801----
802
803If the URL passed matches `http://...`, `https://...`, or `//...`
804the current browser window will navigate to the non-Gerrit URL.
805The user can return to Gerrit with the back button.
806
Shawn Pearce210b5392013-12-07 22:41:36 -0800807[[Gerrit_html]]
808Gerrit.html()
809~~~~~~~~~~~~~
810Parses an HTML fragment after performing template replacements. If
811the HTML has a single root element or node that node is returned,
812otherwise it is wrapped inside a `<div>` and the div is returned.
813
814.Signature
815[source,javascript]
816----
817Gerrit.html(htmlText, options, wantElements);
818----
819
820* htmlText: string of HTML to be parsed. A new unattached `<div>` is
821 created in the browser's document and the innerHTML property is
822 assigned to the passed string, after performing replacements. If
823 the div has exactly one child, that child will be returned instead
824 of the div.
825
826* options: optional object reference supplying replacements for any
827 `{name}` references in htmlText. Navigation through objects is
828 supported permitting `{style.bar}` to be replaced with `"foo"` if
829 options was `{style: {bar: "foo"}}`. Value replacements are HTML
830 escaped before being inserted into the document fragment.
831
832* wantElements: if options is given and wantElements is also true
833 an object consisting of `{root: parsedElement, elements: {...}}` is
834 returned instead of the parsed element. The elements object contains
835 a property for each element using `id={name}` in htmlText.
836
837.Example
838[source,javascript]
839----
840var style = {bar: Gerrit.css('background: yellow')};
841Gerrit.html(
842 '<span class="{style.bar}">Hello {name}!</span>',
843 {style: style, name: "World"});
844----
845
846Event handlers can be automatically attached to elements referenced
847through an attribute id. Object navigation is not supported for ids,
848and the parser strips the id attribute before returning the result.
849Handler functions must begin with `on` and be a function to be
850installed on the element. This approach is useful for onclick and
851other handlers that do not want to create circular references that
852will eventually leak browser memory.
853
854.Example
855[source,javascript]
856----
857var options = {
858 link: {
859 onclick: function(e) { window.close() },
860 },
861};
862Gerrit.html('<a href="javascript:;" id="{link}">Close</a>', options);
863----
864
865When using options to install handlers care must be taken to not
866accidentally include the returned element into the event handler's
867closure. This is why options is built before calling `Gerrit.html()`
868and not inline as a shown above with "Hello World".
869
870DOM nodes can optionally be returned, allowing handlers to access the
871elements identified by `id={name}` at a later point in time.
872
873.Example
874[source,javascript]
875----
876var w = Gerrit.html(
877 '<div>Name: <input type="text" id="{name}"></div>'
878 + '<div>Age: <input type="text" id="{age}"></div>'
879 + '<button id="{submit}"><div>Save</div></button>',
880 {
881 submit: {
882 onclick: function(s) {
883 var e = w.elements;
884 window.alert(e.name.value + " is " + e.age.value);
885 },
886 },
887 }, true);
888----
889
890To prevent memory leaks `w.root` and `w.elements` should be set to
891null when the elements are no longer necessary. Screens can use
892link:#screen_onUnload[screen.onUnload()] to define a callback function
893to perform this cleanup:
894
895[source,javascript]
896----
897var w = Gerrit.html(...);
898screen.body.appendElement(w.root);
899screen.onUnload(function() { w.clear() });
900----
901
902[[Gerrit_injectCss]]
903Gerrit.injectCss()
904~~~~~~~~~~~~~~~~~~
905Injects CSS rules into the document by appending onto the end of the
906existing rule list. CSS rules are global to the entire application
907and must be manually scoped by each plugin. For an automatic scoping
908alternative see link:#Gerrit_css[`css()`].
909
910[source,javascript]
911----
912Gerrit.injectCss('.myplugin_bg {background: #000}');
913----
914
David Pursehouse8d0b5202013-08-01 14:13:17 +0900915[[Gerrit_install]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800916=== Gerrit.install()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700917Registers a new plugin by invoking the supplied initialization
918function. The function is passed the link:#self[plugin instance].
919
David Pursehouse68153d72013-09-04 10:09:17 +0900920[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700921----
922Gerrit.install(function (self) {
923 // ... plugin JavaScript code here ...
924});
925----
926
David Pursehouse8d0b5202013-08-01 14:13:17 +0900927[[Gerrit_post]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800928=== Gerrit.post()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700929Issues a POST REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900930private REST API URLs see link:#self_post[self.post()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700931
932.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900933[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700934----
935Gerrit.post(url, input, callback)
936----
937
938* url: URL relative to the Gerrit server. For example to access the
939 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
940
941* input: JavaScript object to serialize as the request payload.
942
943* callback: JavaScript function to be invoked with the parsed JSON
944 result of the API call. If the API returns a string the result is
945 a string, otherwise the result is a JavaScript object or array,
946 as described in the relevant REST API documentation.
947
David Pursehouse68153d72013-09-04 10:09:17 +0900948[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700949----
950Gerrit.post(
951 '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic',
952 {topic: 'tests', message: 'Classify work as for testing.'},
953 function (r) {});
954----
955
David Pursehouse8d0b5202013-08-01 14:13:17 +0900956[[Gerrit_put]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800957=== Gerrit.put()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700958Issues a PUT REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900959private REST API URLs see link:#self_put[self.put()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700960
961.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900962[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700963----
964Gerrit.put(url, input, callback)
965----
966
967* url: URL relative to the Gerrit server. For example to access the
968 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
969
970* input: JavaScript object to serialize as the request payload.
971
972* callback: JavaScript function to be invoked with the parsed JSON
973 result of the API call. If the API returns a string the result is
974 a string, otherwise the result is a JavaScript object or array,
975 as described in the relevant REST API documentation.
976
David Pursehouse68153d72013-09-04 10:09:17 +0900977[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700978----
979Gerrit.put(
980 '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic',
981 {topic: 'tests', message: 'Classify work as for testing.'},
982 function (r) {});
983----
984
David Pursehouse8d0b5202013-08-01 14:13:17 +0900985[[Gerrit_onAction]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800986=== Gerrit.onAction()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700987Register a JavaScript callback to be invoked when the user clicks
988on a button associated with a server side `UiAction`.
989
990.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900991[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700992----
993Gerrit.onAction(type, view_name, callback);
994----
995
David Ostrovskycdda3312014-08-03 14:03:13 +0200996* type: `'change'`, `'edit'`, `'revision'`, `'project'` or `'branch'`
997 indicating what sort of resource the `UiAction` was bound to in the server.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700998
999* view_name: string appearing in URLs to name the view. This is the
1000 second argument of the `get()`, `post()`, `put()`, and `delete()`
1001 binding methods in a `RestApiModule`.
1002
1003* callback: JavaScript function to invoke when the user clicks. The
1004 function will be passed a link:#ActionContext[ActionContext].
1005
Shawn Pearced5c844f2013-12-26 15:32:26 -08001006[[Gerrit_screen]]
1007=== Gerrit.screen()
1008Register a JavaScript callback to be invoked when the user navigates
1009to an extension screen provided by the plugin. Extension screens are
1010usually linked from the link:dev-plugins.html#top-menu-extensions[top menu].
1011The callback can populate the DOM with the screen's contents.
1012
1013.Signature
1014[source,javascript]
1015----
1016Gerrit.screen(pattern, callback);
1017----
1018
1019* pattern: URL token pattern to identify the screen. Argument can be
1020 either a string (`'index'`) or a RegExp object (`/list\/(.*)/`).
1021 If a RegExp is used the matching groups will be available inside of
1022 the context as `token_match`.
1023
1024* callback: JavaScript function to invoke when the user navigates to
1025 the screen. The function will be passed link:#ScreenContext[screen context].
1026
David Pursehouse8d0b5202013-08-01 14:13:17 +09001027[[Gerrit_refresh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001028=== Gerrit.refresh()
Shawn Pearce92a1fa82013-07-16 15:01:40 -07001029Redisplays the current web UI view, refreshing all information.
1030
Edwin Kempin049c3552014-04-15 11:26:23 +02001031[[Gerrit_refreshMenuBar]]
1032=== Gerrit.refreshMenuBar()
1033Refreshes Gerrit's menu bar.
1034
Edwin Kempina30c8352015-01-20 23:16:26 +01001035[[Gerrit_isSignedIn]]
1036=== Gerrit.isSignedIn()
1037Checks if user is signed in.
1038
David Pursehouse8d0b5202013-08-01 14:13:17 +09001039[[Gerrit_url]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001040=== Gerrit.url()
Shawn Pearce92a1fa82013-07-16 15:01:40 -07001041Returns the URL of the Gerrit Code Review server. If invoked with
1042no parameter the URL of the site is returned. If passed a string
1043the argument is appended to the site URL.
1044
David Pursehouse68153d72013-09-04 10:09:17 +09001045[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -07001046----
1047Gerrit.url(); // "https://gerrit-review.googlesource.com/"
1048Gerrit.url('/123'); // "https://gerrit-review.googlesource.com/123"
1049----
1050
David Pursehouse8d0b5202013-08-01 14:13:17 +09001051For a plugin specific version see link:#self_url()[`self.url()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -07001052
Edwin Kempin792c8d82014-01-31 16:16:52 +01001053[[Gerrit_showError]]
1054=== Gerrit.showError(message)
1055Displays the given message in the Gerrit ErrorDialog.
1056
Shawn Pearce92a1fa82013-07-16 15:01:40 -07001057GERRIT
1058------
1059Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07001060
1061SEARCHBOX
1062---------