blob: 396edf61f9ed5d1124174be624bc3477421cf560 [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
David Pursehouse8d0b5202013-08-01 14:13:17 +090063[[self_getPluginName]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080064=== self.getPluginName()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070065Returns the name this plugin was installed as by the server
66administrator. The plugin name is required to access REST API
67views installed by the plugin, or to access resources.
68
David Pursehouse8d0b5202013-08-01 14:13:17 +090069[[self_post]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080070=== self.post()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070071Issues a POST REST API request to the Gerrit server.
72
73.Signature
David Pursehouse68153d72013-09-04 10:09:17 +090074[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070075----
76self.post(url, input, callback)
77----
78
79* url: URL relative to the plugin's URL space. The JavaScript
80 library prefixes the supplied URL with `/plugins/{getPluginName}/`.
81
82* input: JavaScript object to serialize as the request payload.
83
84* callback: JavaScript function to be invoked with the parsed JSON
85 result of the API call. If the API returns a string the result is
86 a string, otherwise the result is a JavaScript object or array,
87 as described in the relevant REST API documentation.
88
David Pursehouse68153d72013-09-04 10:09:17 +090089[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -070090----
91self.post(
92 '/my-servlet',
93 {start_build: true, platform_type: 'Linux'},
94 function (r) {});
95----
96
David Pursehouse8d0b5202013-08-01 14:13:17 +090097[[self_put]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080098=== self.put()
Shawn Pearce92a1fa82013-07-16 15:01:40 -070099Issues a PUT REST API request to the Gerrit server.
100
101.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900102[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700103----
104self.put(url, input, callback)
105----
106
107* url: URL relative to the plugin's URL space. The JavaScript
108 library prefixes the supplied URL with `/plugins/{getPluginName}/`.
109
110* input: JavaScript object to serialize as the request payload.
111
112* callback: JavaScript function to be invoked with the parsed JSON
113 result of the API call. If the API returns a string the result is
114 a string, otherwise the result is a JavaScript object or array,
115 as described in the relevant REST API documentation.
116
David Pursehouse68153d72013-09-04 10:09:17 +0900117[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700118----
119self.put(
120 '/builds',
121 {start_build: true, platform_type: 'Linux'},
122 function (r) {});
123----
124
Shawn Pearcedb284cd2013-11-29 20:48:21 -0800125[[self_on]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800126=== self.on()
Shawn Pearcedb284cd2013-11-29 20:48:21 -0800127Register a JavaScript callback to be invoked when events occur within
128the web interface.
129
130.Signature
131[source,javascript]
132----
133Gerrit.on(event, callback);
134----
135
136* event: A supported event type. See below for description.
137
138* callback: JavaScript function to be invoked when event happens.
139 Arguments may be passed to this function, depending on the event.
140
141Supported events:
142
Shawn Pearced5c844f2013-12-26 15:32:26 -0800143* `history`: Invoked when the view is changed to a new screen within
144 the Gerrit web application. The token after "#" is passed as the
Shawn Pearcea48826e2013-11-29 20:38:55 -0800145 argument to the callback function, for example "/c/42/" while
146 showing change 42.
147
Shawn Pearcedb284cd2013-11-29 20:48:21 -0800148* `showchange`: Invoked when a change is made visible. A
149 link:rest-api-changes.html#change-info[ChangeInfo] and
150 link:rest-api-changes.html#revision-info[RevisionInfo]
151 are passed as arguments.
152
Shawn Pearce7c418992013-11-29 20:34:28 -0800153* `submitchange`: Invoked when the submit button is clicked
154 on a change. A link:rest-api-changes.html#change-info[ChangeInfo]
155 and link:rest-api-changes.html#revision-info[RevisionInfo] are
156 passed as arguments. Similar to a form submit validation, the
157 function must return true to allow the operation to continue, or
158 false to prevent it.
159
David Pursehouse8d0b5202013-08-01 14:13:17 +0900160[[self_onAction]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800161=== self.onAction()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700162Register a JavaScript callback to be invoked when the user clicks
163on a button associated with a server side `UiAction`.
164
165.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900166[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700167----
Shawn Pearced5c844f2013-12-26 15:32:26 -0800168self.onAction(type, view_name, callback);
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700169----
170
David Ostrovsky9e8b2fb2013-08-30 08:09:53 +0200171* type: `'change'`, `'revision'` or `'project'`, indicating which type
172 of resource the `UiAction` was bound to in the server.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700173
174* view_name: string appearing in URLs to name the view. This is the
175 second argument of the `get()`, `post()`, `put()`, and `delete()`
176 binding methods in a `RestApiModule`.
177
178* callback: JavaScript function to invoke when the user clicks. The
179 function will be passed a link:#ActionContext[action context].
180
Shawn Pearced5c844f2013-12-26 15:32:26 -0800181[[self_screen]]
182=== self.screen()
183Register a JavaScript callback to be invoked when the user navigates
184to an extension screen provided by the plugin. Extension screens are
185usually linked from the link:dev-plugins.html#top-menu-extensions[top menu].
186The callback can populate the DOM with the screen's contents.
187
188.Signature
189[source,javascript]
190----
191self.screen(pattern, callback);
192----
193
194* pattern: URL token pattern to identify the screen. Argument can be
195 either a string (`'index'`) or a RegExp object (`/list\/(.*)/`).
196 If a RegExp is used the matching groups will be available inside of
197 the context as `token_match`.
198
199* callback: JavaScript function to invoke when the user navigates to
200 the screen. The function will be passed a link:#ScreenContext[screen context].
201
David Pursehouse8d0b5202013-08-01 14:13:17 +0900202[[self_url]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800203=== self.url()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700204Returns a URL within the plugin's URL space. If invoked with no
David Pursehouse8d0b5202013-08-01 14:13:17 +0900205parameter the URL of the plugin is returned. If passed a string
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700206the argument is appended to the plugin URL.
207
David Pursehouse68153d72013-09-04 10:09:17 +0900208[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700209----
210self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/"
211self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png"
212----
213
214
215[[ActionContext]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800216== Action Context
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700217A new action context is passed to the `onAction` callback function
218each time the associated action button is clicked by the user. A
219context is initialized with sufficient state to issue the associated
220REST API RPC.
221
David Pursehouse8d0b5202013-08-01 14:13:17 +0900222[[context_action]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800223=== context.action
David Pursehouse68153d72013-09-04 10:09:17 +0900224An link:rest-api-changes.html#action-info[ActionInfo] object instance
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700225supplied by the server describing the UI button the user used to
226invoke the action.
227
David Pursehouse8d0b5202013-08-01 14:13:17 +0900228[[context_call]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800229=== context.call()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700230Issues the REST API call associated with the action. The HTTP method
231used comes from `context.action.method`, hiding the JavaScript from
232needing to care.
233
234.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900235[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700236----
237context.call(input, callback)
238----
239
240* input: JavaScript object to serialize as the request payload. This
241 parameter is ignored for GET and DELETE methods.
242
243* callback: JavaScript function to be invoked with the parsed JSON
244 result of the API call. If the API returns a string the result is
245 a string, otherwise the result is a JavaScript object or array,
246 as described in the relevant REST API documentation.
247
David Pursehouse68153d72013-09-04 10:09:17 +0900248[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700249----
250context.call(
251 {message: "..."},
252 function (result) {
253 // ... use result here ...
254 });
255----
256
David Pursehouse8d0b5202013-08-01 14:13:17 +0900257[[context_change]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800258=== context.change
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700259When the action is invoked on a change a
260link:rest-api-changes.html#change-info[ChangeInfo] object instance
261describing the change. Available fields of the ChangeInfo may vary
262based on the options used by the UI when it loaded the change.
263
David Pursehouse8d0b5202013-08-01 14:13:17 +0900264[[context_delete]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800265=== context.delete()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700266Issues a DELETE REST API call to the URL associated with the action.
267
268.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900269[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700270----
271context.delete(callback)
272----
273
274* callback: JavaScript function to be invoked with the parsed
275 JSON result of the API call. DELETE methods often return
276 `204 No Content`, which is passed as null.
277
David Pursehouse68153d72013-09-04 10:09:17 +0900278[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700279----
280context.delete(function () {});
281----
282
David Pursehouse8d0b5202013-08-01 14:13:17 +0900283[[context_get]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800284=== context.get()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700285Issues a GET REST API call to the URL associated with the action.
286
287.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900288[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700289----
290context.get(callback)
291----
292
293* callback: JavaScript function to be invoked with the parsed JSON
294 result of the API call. If the API returns a string the result is
295 a string, otherwise the result is a JavaScript object or array,
296 as described in the relevant REST API documentation.
297
David Pursehouse68153d72013-09-04 10:09:17 +0900298[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700299----
300context.get(function (result) {
301 // ... use result here ...
302});
303----
304
David Pursehouse8d0b5202013-08-01 14:13:17 +0900305[[context_go]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800306=== context.go()
Shawn Pearced5c844f2013-12-26 15:32:26 -0800307Go to a screen. Shorthand for link:#Gerrit_go[`Gerrit.go()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700308
David Pursehouse8d0b5202013-08-01 14:13:17 +0900309[[context_hide]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800310=== context.hide()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700311Hide the currently visible popup displayed by
David Pursehouse8d0b5202013-08-01 14:13:17 +0900312link:#context_popup[`context.popup()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700313
David Pursehouse8d0b5202013-08-01 14:13:17 +0900314[[context_post]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800315=== context.post()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700316Issues a POST REST API call to the URL associated with the action.
317
318.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900319[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700320----
321context.post(input, callback)
322----
323
324* input: JavaScript object to serialize as the request payload.
325
326* callback: JavaScript function to be invoked with the parsed JSON
327 result of the API call. If the API returns a string the result is
328 a string, otherwise the result is a JavaScript object or array,
329 as described in the relevant REST API documentation.
330
David Pursehouse68153d72013-09-04 10:09:17 +0900331[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700332----
333context.post(
334 {message: "..."},
335 function (result) {
336 // ... use result here ...
337 });
338----
339
David Pursehouse8d0b5202013-08-01 14:13:17 +0900340[[context_popup]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800341=== context.popup()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700342
343Displays a small popup near the activation button to gather
344additional input from the user before executing the REST API RPC.
345
346The caller is always responsible for closing the popup with
David Pursehouse8d0b5202013-08-01 14:13:17 +0900347link#context_hide[`context.hide()`]. Gerrit will handle closing a
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700348popup if the user presses `Escape` while keyboard focus is within
349the popup.
350
351.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900352[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700353----
354context.popup(element)
355----
356
357* element: an HTML DOM element to display as the body of the
358 popup. This is typically a `div` element but can be any valid HTML
359 element. CSS can be used to style the element beyond the defaults.
360
361A common usage is to gather more input:
David Pursehouse68153d72013-09-04 10:09:17 +0900362
363[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700364----
365self.onAction('revision', 'start-build', function (c) {
366 var l = c.checkbox();
367 var m = c.checkbox();
368 c.popup(c.div(
369 c.div(c.label(l, 'Linux')),
370 c.div(c.label(m, 'Mac OS X')),
371 c.button('Build', {onclick: function() {
372 c.call(
373 {
374 commit: c.revision.name,
375 linux: l.checked,
376 mac: m.checked,
377 },
378 function() { c.hide() });
379 });
380});
381----
382
David Pursehouse8d0b5202013-08-01 14:13:17 +0900383[[context_put]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800384=== context.put()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700385Issues a PUT REST API call to the URL associated with the action.
386
387.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900388[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700389----
390context.put(input, callback)
391----
392
393* input: JavaScript object to serialize as the request payload.
394
395* callback: JavaScript function to be invoked with the parsed JSON
396 result of the API call. If the API returns a string the result is
397 a string, otherwise the result is a JavaScript object or array,
398 as described in the relevant REST API documentation.
399
David Pursehouse68153d72013-09-04 10:09:17 +0900400[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700401----
402context.put(
403 {message: "..."},
404 function (result) {
405 // ... use result here ...
406 });
407----
408
David Pursehouse8d0b5202013-08-01 14:13:17 +0900409[[context_refresh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800410=== context.refresh()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700411Refresh the current display. Shorthand for
David Pursehouse8d0b5202013-08-01 14:13:17 +0900412link:#Gerrit_refresh[`Gerrit.refresh()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700413
David Pursehouse8d0b5202013-08-01 14:13:17 +0900414[[context_revision]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800415=== context.revision
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700416When the action is invoked on a specific revision of a change,
417a link:rest-api-changes.html#revision-info[RevisionInfo]
418object instance describing the revision. Available fields of the
419RevisionInfo may vary based on the options used by the UI when it
420loaded the change.
421
David Ostrovsky9e8b2fb2013-08-30 08:09:53 +0200422[[context_project]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800423=== context.project
David Ostrovsky9e8b2fb2013-08-30 08:09:53 +0200424When the action is invoked on a specific project,
425the name of the project.
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700426
Shawn Pearced5c844f2013-12-26 15:32:26 -0800427=== HTML Helpers
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700428The link:#ActionContext[action context] includes some HTML helper
429functions to make working with DOM based widgets less painful.
430
431* `br()`: new `<br>` element.
432
433* `button(label, options)`: new `<button>` with the string `label`
434 wrapped inside of a `div`. The optional `options` object may
435 define `onclick` as a function to be invoked upon clicking. This
436 calling pattern avoids circular references between the element
437 and the onclick handler.
438
439* `checkbox()`: new `<input type='checkbox'>` element.
440* `div(...)`: a new `<div>` wrapping the (optional) arguments.
441* `hr()`: new `<hr>` element.
442
443* `label(c, label)`: a new `<label>` element wrapping element `c`
444 and the string `label`. Used to wrap a checkbox with its label,
445 `label(checkbox(), 'Click Me')`.
446
David Ostrovskya1067a32013-08-19 09:09:17 +0200447* `prependLabel(label, c)`: a new `<label>` element wrapping element `c`
448 and the string `label`. Used to wrap an input field with its label,
449 `prependLabel('Greeting message', textfield())`.
450
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700451* `textarea(options)`: new `<textarea>` element. The options
452 object may optionally include `rows` and `cols`. The textarea
453 comes with an onkeypress handler installed to play nicely with
454 Gerrit's keyboard binding system.
455
456* `textfield()`: new `<input type='text'>` element. The text field
457 comes with an onkeypress handler installed to play nicely with
458 Gerrit's keyboard binding system.
459
Edwin Kempin15ce80c2013-11-14 17:16:24 +0100460* `select(a,i)`: a new `<select>` element containing one `<option>`
461 element for each entry in the provided array `a`. The option with
462 the index `i` will be pre-selected in the drop-down-list.
463
464* `selected(s)`: returns the text of the `<option>` element that is
465 currently selected in the provided `<select>` element `s`.
466
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700467* `span(...)`: a new `<span>` wrapping the (optional) arguments.
468
David Ostrovsky7dfc8022013-08-25 23:05:50 +0200469* `msg(label)`: a new label.
470
Shawn Pearced5c844f2013-12-26 15:32:26 -0800471
472[[ScreenContext]]
473== Screen Context
474A new screen context is passed to the `screen` callback function
475each time the user navigates to a matching URL.
476
477[[screen_body]]
478=== screen.body
479Empty HTML `<div>` node the plugin should add its content to. The
480node is already attached to the document, but is invisible. Plugins
481must call `screen.show()` to display the DOM node. Deferred display
482allows an implementor to partially populate the DOM, make remote HTTP
483requests, finish populating when the callbacks arrive, and only then
484make the view visible to the user.
485
486[[screen_token]]
487=== screen.token
488URL token fragment that activated this screen. The value is identical
489to `screen.token_match[0]`. If the URL is `/#/x/hello/list` the token
490will be `"list"`.
491
492[[screen_token_match]]
493=== screen.token_match
494Array of matching subgroups from the pattern specified to `screen()`.
495This is identical to the result of RegExp.exec. Index 0 contains the
496entire matching expression; index 1 the first matching group, etc.
497
498[[screen_onUnload]]
499=== screen.onUnload()
500Configures an optional callback to be invoked just before the screen
501is deleted from the browser DOM. Plugins can use this callback to
502remove event listeners from DOM nodes, preventing memory leaks.
503
504.Signature
505[source,javascript]
506----
507screen.onUnload(callback)
508----
509
510* callback: JavaScript function to be invoked just before the
511 `screen.body` DOM element is removed from the browser DOM.
512 This event happens when the user navigates to another screen.
513
514[[screen.setTitle]]
515=== screen.setTitle()
516Sets the heading text to be displayed when the screen is visible.
517This is presented in a large bold font below the menus, but above the
518content in `screen.body`. Setting the title also sets the window
519title to the same string, if it has not already been set.
520
521.Signature
522[source,javascript]
523----
524screen.setPageTitle(titleText)
525----
526
527[[screen.setWindowTitle]]
528=== screen.setWindowTitle()
529Sets the text to be displayed in the browser's title bar when the
530screen is visible. Plugins should always prefer this method over
531trying to set `window.title` directly. The window title defaults to
532the title given to `setTitle`.
533
534.Signature
535[source,javascript]
536----
537screen.setWindowTitle(titleText)
538----
539
540[[screen_show]]
541=== screen.show()
542Destroy the currently visible screen and display the plugin's screen.
543This method must be called after adding content to `screen.body`.
544
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700545[[Gerrit]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800546== Gerrit
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700547
548The `Gerrit` object is the only symbol provided into the global
549namespace by Gerrit Code Review. All top-level functions can be
550accessed through this name.
551
Shawn Pearce210b5392013-12-07 22:41:36 -0800552[[Gerrit_css]]
553Gerrit.css()
554~~~~~~~~~~~~
555Creates a new unique CSS class and injects it into the document.
556The name of the class is returned and can be used by the plugin.
557See link:#Gerrit_html[`Gerrit.html()`] for an easy way to use
558generated class names.
559
560Classes created with this function should be created once at install
561time and reused throughout the plugin. Repeatedly creating the same
562class will explode the global stylesheet.
563
564.Signature
565[source,javascript]
566----
567Gerrit.install(function(self)) {
568 var style = {
569 name: Gerrit.css('background: #fff; color: #000;'),
570 };
571});
572----
573
David Pursehouse8d0b5202013-08-01 14:13:17 +0900574[[Gerrit_delete]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800575=== Gerrit.delete()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700576Issues a DELETE REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900577private REST API URLs see link:#self_delete[self.delete()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700578
579.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900580[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700581----
582Gerrit.delete(url, callback)
583----
584
585* url: URL relative to the Gerrit server. For example to access the
586 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
587
588* callback: JavaScript function to be invoked with the parsed
589 JSON result of the API call. DELETE methods often return
590 `204 No Content`, which is passed as null.
591
David Pursehouse68153d72013-09-04 10:09:17 +0900592[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700593----
594Gerrit.delete(
595 '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic',
596 function () {});
597----
598
David Pursehouse8d0b5202013-08-01 14:13:17 +0900599[[Gerrit_get]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800600=== Gerrit.get()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700601Issues a GET REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900602private REST API URLs see link:#self_get[self.get()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700603
604.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900605[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700606----
607Gerrit.get(url, callback)
608----
609
610* url: URL relative to the Gerrit server. For example to access the
611 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
612
613* callback: JavaScript function to be invoked with the parsed JSON
614 result of the API call. If the API returns a string the result is
615 a string, otherwise the result is a JavaScript object or array,
616 as described in the relevant REST API documentation.
617
David Pursehouse68153d72013-09-04 10:09:17 +0900618[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700619----
620Gerrit.get('/changes/?q=status:open', function (open) {
621 for (var i = 0; i < open.length; i++) {
622 console.log(open.get(i).change_id);
623 }
624});
625----
626
David Pursehouse8d0b5202013-08-01 14:13:17 +0900627[[Gerrit_getPluginName]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800628=== Gerrit.getPluginName()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700629Returns the name this plugin was installed as by the server
630administrator. The plugin name is required to access REST API
631views installed by the plugin, or to access resources.
632
David Pursehouse8d0b5202013-08-01 14:13:17 +0900633Unlike link:#self_getPluginName[`self.getPluginName()`] this method
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700634must guess the name from the JavaScript call stack. Plugins are
635encouraged to use `self.getPluginName()` whenever possible.
636
David Pursehouse8d0b5202013-08-01 14:13:17 +0900637[[Gerrit_go]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800638=== Gerrit.go()
Shawn Pearced5c844f2013-12-26 15:32:26 -0800639Updates the web UI to display the screen identified by the supplied
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700640URL token. The URL token is the text after `#` in the browser URL.
641
David Pursehouse68153d72013-09-04 10:09:17 +0900642[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700643----
644Gerrit.go('/admin/projects/');
645----
646
647If the URL passed matches `http://...`, `https://...`, or `//...`
648the current browser window will navigate to the non-Gerrit URL.
649The user can return to Gerrit with the back button.
650
Shawn Pearce210b5392013-12-07 22:41:36 -0800651[[Gerrit_html]]
652Gerrit.html()
653~~~~~~~~~~~~~
654Parses an HTML fragment after performing template replacements. If
655the HTML has a single root element or node that node is returned,
656otherwise it is wrapped inside a `<div>` and the div is returned.
657
658.Signature
659[source,javascript]
660----
661Gerrit.html(htmlText, options, wantElements);
662----
663
664* htmlText: string of HTML to be parsed. A new unattached `<div>` is
665 created in the browser's document and the innerHTML property is
666 assigned to the passed string, after performing replacements. If
667 the div has exactly one child, that child will be returned instead
668 of the div.
669
670* options: optional object reference supplying replacements for any
671 `{name}` references in htmlText. Navigation through objects is
672 supported permitting `{style.bar}` to be replaced with `"foo"` if
673 options was `{style: {bar: "foo"}}`. Value replacements are HTML
674 escaped before being inserted into the document fragment.
675
676* wantElements: if options is given and wantElements is also true
677 an object consisting of `{root: parsedElement, elements: {...}}` is
678 returned instead of the parsed element. The elements object contains
679 a property for each element using `id={name}` in htmlText.
680
681.Example
682[source,javascript]
683----
684var style = {bar: Gerrit.css('background: yellow')};
685Gerrit.html(
686 '<span class="{style.bar}">Hello {name}!</span>',
687 {style: style, name: "World"});
688----
689
690Event handlers can be automatically attached to elements referenced
691through an attribute id. Object navigation is not supported for ids,
692and the parser strips the id attribute before returning the result.
693Handler functions must begin with `on` and be a function to be
694installed on the element. This approach is useful for onclick and
695other handlers that do not want to create circular references that
696will eventually leak browser memory.
697
698.Example
699[source,javascript]
700----
701var options = {
702 link: {
703 onclick: function(e) { window.close() },
704 },
705};
706Gerrit.html('<a href="javascript:;" id="{link}">Close</a>', options);
707----
708
709When using options to install handlers care must be taken to not
710accidentally include the returned element into the event handler's
711closure. This is why options is built before calling `Gerrit.html()`
712and not inline as a shown above with "Hello World".
713
714DOM nodes can optionally be returned, allowing handlers to access the
715elements identified by `id={name}` at a later point in time.
716
717.Example
718[source,javascript]
719----
720var w = Gerrit.html(
721 '<div>Name: <input type="text" id="{name}"></div>'
722 + '<div>Age: <input type="text" id="{age}"></div>'
723 + '<button id="{submit}"><div>Save</div></button>',
724 {
725 submit: {
726 onclick: function(s) {
727 var e = w.elements;
728 window.alert(e.name.value + " is " + e.age.value);
729 },
730 },
731 }, true);
732----
733
734To prevent memory leaks `w.root` and `w.elements` should be set to
735null when the elements are no longer necessary. Screens can use
736link:#screen_onUnload[screen.onUnload()] to define a callback function
737to perform this cleanup:
738
739[source,javascript]
740----
741var w = Gerrit.html(...);
742screen.body.appendElement(w.root);
743screen.onUnload(function() { w.clear() });
744----
745
746[[Gerrit_injectCss]]
747Gerrit.injectCss()
748~~~~~~~~~~~~~~~~~~
749Injects CSS rules into the document by appending onto the end of the
750existing rule list. CSS rules are global to the entire application
751and must be manually scoped by each plugin. For an automatic scoping
752alternative see link:#Gerrit_css[`css()`].
753
754[source,javascript]
755----
756Gerrit.injectCss('.myplugin_bg {background: #000}');
757----
758
David Pursehouse8d0b5202013-08-01 14:13:17 +0900759[[Gerrit_install]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800760=== Gerrit.install()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700761Registers a new plugin by invoking the supplied initialization
762function. The function is passed the link:#self[plugin instance].
763
David Pursehouse68153d72013-09-04 10:09:17 +0900764[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700765----
766Gerrit.install(function (self) {
767 // ... plugin JavaScript code here ...
768});
769----
770
David Pursehouse8d0b5202013-08-01 14:13:17 +0900771[[Gerrit_post]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800772=== Gerrit.post()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700773Issues a POST REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900774private REST API URLs see link:#self_post[self.post()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700775
776.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900777[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700778----
779Gerrit.post(url, input, callback)
780----
781
782* url: URL relative to the Gerrit server. For example to access the
783 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
784
785* input: JavaScript object to serialize as the request payload.
786
787* callback: JavaScript function to be invoked with the parsed JSON
788 result of the API call. If the API returns a string the result is
789 a string, otherwise the result is a JavaScript object or array,
790 as described in the relevant REST API documentation.
791
David Pursehouse68153d72013-09-04 10:09:17 +0900792[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700793----
794Gerrit.post(
795 '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic',
796 {topic: 'tests', message: 'Classify work as for testing.'},
797 function (r) {});
798----
799
David Pursehouse8d0b5202013-08-01 14:13:17 +0900800[[Gerrit_put]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800801=== Gerrit.put()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700802Issues a PUT REST API request to the Gerrit server. For plugin
David Pursehouse8d0b5202013-08-01 14:13:17 +0900803private REST API URLs see link:#self_put[self.put()].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700804
805.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900806[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700807----
808Gerrit.put(url, input, callback)
809----
810
811* url: URL relative to the Gerrit server. For example to access the
812 link:rest-api-changes.html[changes REST API] use `'/changes/'`.
813
814* input: JavaScript object to serialize as the request payload.
815
816* callback: JavaScript function to be invoked with the parsed JSON
817 result of the API call. If the API returns a string the result is
818 a string, otherwise the result is a JavaScript object or array,
819 as described in the relevant REST API documentation.
820
David Pursehouse68153d72013-09-04 10:09:17 +0900821[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700822----
823Gerrit.put(
824 '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic',
825 {topic: 'tests', message: 'Classify work as for testing.'},
826 function (r) {});
827----
828
David Pursehouse8d0b5202013-08-01 14:13:17 +0900829[[Gerrit_onAction]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800830=== Gerrit.onAction()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700831Register a JavaScript callback to be invoked when the user clicks
832on a button associated with a server side `UiAction`.
833
834.Signature
David Pursehouse68153d72013-09-04 10:09:17 +0900835[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700836----
837Gerrit.onAction(type, view_name, callback);
838----
839
840* type: `'change'` or `'revision'`, indicating what sort of resource
841 the `UiAction` was bound to in the server.
842
843* view_name: string appearing in URLs to name the view. This is the
844 second argument of the `get()`, `post()`, `put()`, and `delete()`
845 binding methods in a `RestApiModule`.
846
847* callback: JavaScript function to invoke when the user clicks. The
848 function will be passed a link:#ActionContext[ActionContext].
849
Shawn Pearced5c844f2013-12-26 15:32:26 -0800850[[Gerrit_screen]]
851=== Gerrit.screen()
852Register a JavaScript callback to be invoked when the user navigates
853to an extension screen provided by the plugin. Extension screens are
854usually linked from the link:dev-plugins.html#top-menu-extensions[top menu].
855The callback can populate the DOM with the screen's contents.
856
857.Signature
858[source,javascript]
859----
860Gerrit.screen(pattern, callback);
861----
862
863* pattern: URL token pattern to identify the screen. Argument can be
864 either a string (`'index'`) or a RegExp object (`/list\/(.*)/`).
865 If a RegExp is used the matching groups will be available inside of
866 the context as `token_match`.
867
868* callback: JavaScript function to invoke when the user navigates to
869 the screen. The function will be passed link:#ScreenContext[screen context].
870
David Pursehouse8d0b5202013-08-01 14:13:17 +0900871[[Gerrit_refresh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800872=== Gerrit.refresh()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700873Redisplays the current web UI view, refreshing all information.
874
Edwin Kempin049c3552014-04-15 11:26:23 +0200875[[Gerrit_refreshMenuBar]]
876=== Gerrit.refreshMenuBar()
877Refreshes Gerrit's menu bar.
878
David Pursehouse8d0b5202013-08-01 14:13:17 +0900879[[Gerrit_url]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800880=== Gerrit.url()
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700881Returns the URL of the Gerrit Code Review server. If invoked with
882no parameter the URL of the site is returned. If passed a string
883the argument is appended to the site URL.
884
David Pursehouse68153d72013-09-04 10:09:17 +0900885[source,javascript]
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700886----
887Gerrit.url(); // "https://gerrit-review.googlesource.com/"
888Gerrit.url('/123'); // "https://gerrit-review.googlesource.com/123"
889----
890
David Pursehouse8d0b5202013-08-01 14:13:17 +0900891For a plugin specific version see link:#self_url()[`self.url()`].
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700892
Edwin Kempin792c8d82014-01-31 16:16:52 +0100893[[Gerrit_showError]]
894=== Gerrit.showError(message)
895Displays the given message in the Gerrit ErrorDialog.
896
Shawn Pearce92a1fa82013-07-16 15:01:40 -0700897GERRIT
898------
899Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -0700900
901SEARCHBOX
902---------