Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1 | = Gerrit Code Review - JavaScript API |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 2 | |
| 3 | Gerrit Code Review supports an API for JavaScript plugins to interact |
| 4 | with the web UI and the server process. |
| 5 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 6 | == Entry Point |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 7 | |
| 8 | JavaScript is loaded using a standard `<script src='...'>` HTML tag. |
| 9 | Plugins should protect the global namespace by defining their code |
| 10 | within an anonymous function passed to `Gerrit.install()`. The plugin |
| 11 | will be passed an object describing its registration with Gerrit: |
| 12 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 13 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 14 | ---- |
| 15 | Gerrit.install(function (self) { |
| 16 | // ... plugin JavaScript code here ... |
| 17 | }); |
| 18 | ---- |
| 19 | |
| 20 | |
| 21 | [[self]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 22 | == Plugin Instance |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 23 | |
| 24 | The plugin instance is passed to the plugin's initialization function |
| 25 | and provides a number of utility services to plugin authors. |
| 26 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 27 | [[self_delete]] |
Edwin Kempin | fc998c4 | 2014-02-03 17:51:20 +0100 | [diff] [blame] | 28 | === self.delete() / self.del() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 29 | Issues a DELETE REST API request to the Gerrit server. |
| 30 | |
| 31 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 32 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 33 | ---- |
| 34 | Gerrit.delete(url, callback) |
Edwin Kempin | fc998c4 | 2014-02-03 17:51:20 +0100 | [diff] [blame] | 35 | Gerrit.del(url, callback) |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 36 | ---- |
| 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 Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 45 | [[self_get]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 46 | === self.get() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 47 | Issues a GET REST API request to the Gerrit server. |
| 48 | |
| 49 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 50 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 51 | ---- |
| 52 | self.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 Kempin | 2fc17f4 | 2015-07-27 11:51:01 +0200 | [diff] [blame] | 63 | [[self_getServerInfo]] |
| 64 | === self.getServerInfo() |
| 65 | Returns the server's link:rest-api-config.html#server-info[ServerInfo] |
| 66 | data. |
| 67 | |
David Ostrovsky | 0a4c0ff | 2014-09-21 23:06:52 +0200 | [diff] [blame] | 68 | [[self_getCurrentUser]] |
| 69 | === self.getCurrentUser() |
| 70 | Returns the currently signed in user's AccountInfo data; empty account |
| 71 | data if no user is currently signed in. |
| 72 | |
Edwin Kempin | 936d1ed | 2015-07-22 15:01:59 +0200 | [diff] [blame] | 73 | [[Gerrit_getUserPreferences]] |
| 74 | === Gerrit.getUserPreferences() |
| 75 | Returns the preferences of the currently signed in user; the default |
| 76 | preferences if no user is currently signed in. |
| 77 | |
| 78 | [[Gerrit_refreshUserPreferences]] |
| 79 | === Gerrit.refreshUserPreferences() |
| 80 | Refreshes the preferences of the current user. |
| 81 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 82 | [[self_getPluginName]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 83 | === self.getPluginName() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 84 | Returns the name this plugin was installed as by the server |
| 85 | administrator. The plugin name is required to access REST API |
| 86 | views installed by the plugin, or to access resources. |
| 87 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 88 | [[self_post]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 89 | === self.post() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 90 | Issues a POST REST API request to the Gerrit server. |
| 91 | |
| 92 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 93 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 94 | ---- |
| 95 | self.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 108 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 109 | ---- |
| 110 | self.post( |
| 111 | '/my-servlet', |
| 112 | {start_build: true, platform_type: 'Linux'}, |
| 113 | function (r) {}); |
| 114 | ---- |
| 115 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 116 | [[self_put]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 117 | === self.put() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 118 | Issues a PUT REST API request to the Gerrit server. |
| 119 | |
| 120 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 121 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 122 | ---- |
| 123 | self.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 136 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 137 | ---- |
| 138 | self.put( |
| 139 | '/builds', |
| 140 | {start_build: true, platform_type: 'Linux'}, |
| 141 | function (r) {}); |
| 142 | ---- |
| 143 | |
Shawn Pearce | db284cd | 2013-11-29 20:48:21 -0800 | [diff] [blame] | 144 | [[self_on]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 145 | === self.on() |
Shawn Pearce | db284cd | 2013-11-29 20:48:21 -0800 | [diff] [blame] | 146 | Register a JavaScript callback to be invoked when events occur within |
| 147 | the web interface. |
| 148 | |
| 149 | .Signature |
| 150 | [source,javascript] |
| 151 | ---- |
| 152 | Gerrit.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 | |
| 160 | Supported events: |
| 161 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 162 | * `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 Pearce | a48826e | 2013-11-29 20:38:55 -0800 | [diff] [blame] | 164 | argument to the callback function, for example "/c/42/" while |
| 165 | showing change 42. |
| 166 | |
Shawn Pearce | db284cd | 2013-11-29 20:48:21 -0800 | [diff] [blame] | 167 | * `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 Pearce | 7c41899 | 2013-11-29 20:34:28 -0800 | [diff] [blame] | 172 | * `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 Kempin | daefed0 | 2015-12-22 16:22:44 +0100 | [diff] [blame] | 179 | * `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 Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 186 | [[self_onAction]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 187 | === self.onAction() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 188 | Register a JavaScript callback to be invoked when the user clicks |
| 189 | on a button associated with a server side `UiAction`. |
| 190 | |
| 191 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 192 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 193 | ---- |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 194 | self.onAction(type, view_name, callback); |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 195 | ---- |
| 196 | |
David Ostrovsky | cdda331 | 2014-08-03 14:03:13 +0200 | [diff] [blame] | 197 | * type: `'change'`, `'edit'`, `'revision'`, `'project'`, or `'branch'` |
Shawn Pearce | d1cf962 | 2014-03-24 16:21:51 -0700 | [diff] [blame] | 198 | indicating which type of resource the `UiAction` was bound to |
| 199 | in the server. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 200 | |
| 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 Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 208 | [[self_screen]] |
| 209 | === self.screen() |
| 210 | Register a JavaScript callback to be invoked when the user navigates |
| 211 | to an extension screen provided by the plugin. Extension screens are |
| 212 | usually linked from the link:dev-plugins.html#top-menu-extensions[top menu]. |
| 213 | The callback can populate the DOM with the screen's contents. |
| 214 | |
| 215 | .Signature |
| 216 | [source,javascript] |
| 217 | ---- |
| 218 | self.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 Kempin | dcaec6c | 2016-06-03 09:43:10 +0200 | [diff] [blame] | 229 | [[self_settingsScreen]] |
| 230 | === self.settingsScreen() |
| 231 | Register a JavaScript callback to be invoked when the user navigates |
| 232 | to an extension settings screen provided by the plugin. Extension settings |
| 233 | screens are automatically linked from the settings menu under the given |
| 234 | menu entry. |
| 235 | The callback can populate the DOM with the screen's contents. |
| 236 | |
| 237 | .Signature |
| 238 | [source,javascript] |
| 239 | ---- |
| 240 | self.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 Kempin | 9ef951b | 2016-06-03 13:27:50 +0200 | [diff] [blame] | 252 | [[self_panel]] |
| 253 | === self.panel() |
| 254 | Register a JavaScript callback to be invoked when a screen with the |
| 255 | given extension point is loaded. |
| 256 | The callback can populate the DOM with the panel's contents. |
| 257 | |
| 258 | .Signature |
| 259 | [source,javascript] |
| 260 | ---- |
| 261 | self.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 Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 273 | [[self_url]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 274 | === self.url() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 275 | Returns a URL within the plugin's URL space. If invoked with no |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 276 | parameter the URL of the plugin is returned. If passed a string |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 277 | the argument is appended to the plugin URL. |
| 278 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 279 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 280 | ---- |
| 281 | self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/" |
| 282 | self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png" |
| 283 | ---- |
| 284 | |
| 285 | |
| 286 | [[ActionContext]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 287 | == Action Context |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 288 | A new action context is passed to the `onAction` callback function |
| 289 | each time the associated action button is clicked by the user. A |
| 290 | context is initialized with sufficient state to issue the associated |
| 291 | REST API RPC. |
| 292 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 293 | [[context_action]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 294 | === context.action |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 295 | An link:rest-api-changes.html#action-info[ActionInfo] object instance |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 296 | supplied by the server describing the UI button the user used to |
| 297 | invoke the action. |
| 298 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 299 | [[context_call]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 300 | === context.call() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 301 | Issues the REST API call associated with the action. The HTTP method |
| 302 | used comes from `context.action.method`, hiding the JavaScript from |
| 303 | needing to care. |
| 304 | |
| 305 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 306 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 307 | ---- |
| 308 | context.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 319 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 320 | ---- |
| 321 | context.call( |
| 322 | {message: "..."}, |
| 323 | function (result) { |
| 324 | // ... use result here ... |
| 325 | }); |
| 326 | ---- |
| 327 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 328 | [[context_change]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 329 | === context.change |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 330 | When the action is invoked on a change a |
| 331 | link:rest-api-changes.html#change-info[ChangeInfo] object instance |
| 332 | describing the change. Available fields of the ChangeInfo may vary |
| 333 | based on the options used by the UI when it loaded the change. |
| 334 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 335 | [[context_delete]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 336 | === context.delete() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 337 | Issues a DELETE REST API call to the URL associated with the action. |
| 338 | |
| 339 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 340 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 341 | ---- |
| 342 | context.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 349 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 350 | ---- |
| 351 | context.delete(function () {}); |
| 352 | ---- |
| 353 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 354 | [[context_get]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 355 | === context.get() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 356 | Issues a GET REST API call to the URL associated with the action. |
| 357 | |
| 358 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 359 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 360 | ---- |
| 361 | context.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 369 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 370 | ---- |
| 371 | context.get(function (result) { |
| 372 | // ... use result here ... |
| 373 | }); |
| 374 | ---- |
| 375 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 376 | [[context_go]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 377 | === context.go() |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 378 | Go to a screen. Shorthand for link:#Gerrit_go[`Gerrit.go()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 379 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 380 | [[context_hide]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 381 | === context.hide() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 382 | Hide the currently visible popup displayed by |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 383 | link:#context_popup[`context.popup()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 384 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 385 | [[context_post]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 386 | === context.post() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 387 | Issues a POST REST API call to the URL associated with the action. |
| 388 | |
| 389 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 390 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 391 | ---- |
| 392 | context.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 402 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 403 | ---- |
| 404 | context.post( |
| 405 | {message: "..."}, |
| 406 | function (result) { |
| 407 | // ... use result here ... |
| 408 | }); |
| 409 | ---- |
| 410 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 411 | [[context_popup]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 412 | === context.popup() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 413 | |
| 414 | Displays a small popup near the activation button to gather |
| 415 | additional input from the user before executing the REST API RPC. |
| 416 | |
| 417 | The caller is always responsible for closing the popup with |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 418 | link#context_hide[`context.hide()`]. Gerrit will handle closing a |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 419 | popup if the user presses `Escape` while keyboard focus is within |
| 420 | the popup. |
| 421 | |
| 422 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 423 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 424 | ---- |
| 425 | context.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 | |
| 432 | A common usage is to gather more input: |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 433 | |
| 434 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 435 | ---- |
| 436 | self.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 Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 454 | [[context_put]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 455 | === context.put() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 456 | Issues a PUT REST API call to the URL associated with the action. |
| 457 | |
| 458 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 459 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 460 | ---- |
| 461 | context.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 471 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 472 | ---- |
| 473 | context.put( |
| 474 | {message: "..."}, |
| 475 | function (result) { |
| 476 | // ... use result here ... |
| 477 | }); |
| 478 | ---- |
| 479 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 480 | [[context_refresh]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 481 | === context.refresh() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 482 | Refresh the current display. Shorthand for |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 483 | link:#Gerrit_refresh[`Gerrit.refresh()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 484 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 485 | [[context_revision]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 486 | === context.revision |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 487 | When the action is invoked on a specific revision of a change, |
| 488 | a link:rest-api-changes.html#revision-info[RevisionInfo] |
| 489 | object instance describing the revision. Available fields of the |
| 490 | RevisionInfo may vary based on the options used by the UI when it |
| 491 | loaded the change. |
| 492 | |
David Ostrovsky | 9e8b2fb | 2013-08-30 08:09:53 +0200 | [diff] [blame] | 493 | [[context_project]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 494 | === context.project |
David Ostrovsky | 9e8b2fb | 2013-08-30 08:09:53 +0200 | [diff] [blame] | 495 | When the action is invoked on a specific project, |
| 496 | the name of the project. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 497 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 498 | === HTML Helpers |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 499 | The link:#ActionContext[action context] includes some HTML helper |
| 500 | functions 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 Ostrovsky | a1067a3 | 2013-08-19 09:09:17 +0200 | [diff] [blame] | 518 | * `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 Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 522 | * `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 Kempin | 15ce80c | 2013-11-14 17:16:24 +0100 | [diff] [blame] | 531 | * `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 Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 538 | * `span(...)`: a new `<span>` wrapping the (optional) arguments. |
| 539 | |
David Ostrovsky | 7dfc802 | 2013-08-25 23:05:50 +0200 | [diff] [blame] | 540 | * `msg(label)`: a new label. |
| 541 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 542 | |
| 543 | [[ScreenContext]] |
| 544 | == Screen Context |
| 545 | A new screen context is passed to the `screen` callback function |
| 546 | each time the user navigates to a matching URL. |
| 547 | |
| 548 | [[screen_body]] |
| 549 | === screen.body |
| 550 | Empty HTML `<div>` node the plugin should add its content to. The |
| 551 | node is already attached to the document, but is invisible. Plugins |
| 552 | must call `screen.show()` to display the DOM node. Deferred display |
| 553 | allows an implementor to partially populate the DOM, make remote HTTP |
| 554 | requests, finish populating when the callbacks arrive, and only then |
| 555 | make the view visible to the user. |
| 556 | |
| 557 | [[screen_token]] |
| 558 | === screen.token |
| 559 | URL token fragment that activated this screen. The value is identical |
| 560 | to `screen.token_match[0]`. If the URL is `/#/x/hello/list` the token |
| 561 | will be `"list"`. |
| 562 | |
| 563 | [[screen_token_match]] |
| 564 | === screen.token_match |
| 565 | Array of matching subgroups from the pattern specified to `screen()`. |
| 566 | This is identical to the result of RegExp.exec. Index 0 contains the |
| 567 | entire matching expression; index 1 the first matching group, etc. |
| 568 | |
| 569 | [[screen_onUnload]] |
| 570 | === screen.onUnload() |
| 571 | Configures an optional callback to be invoked just before the screen |
| 572 | is deleted from the browser DOM. Plugins can use this callback to |
| 573 | remove event listeners from DOM nodes, preventing memory leaks. |
| 574 | |
| 575 | .Signature |
| 576 | [source,javascript] |
| 577 | ---- |
| 578 | screen.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() |
| 587 | Sets the heading text to be displayed when the screen is visible. |
| 588 | This is presented in a large bold font below the menus, but above the |
| 589 | content in `screen.body`. Setting the title also sets the window |
| 590 | title to the same string, if it has not already been set. |
| 591 | |
| 592 | .Signature |
| 593 | [source,javascript] |
| 594 | ---- |
| 595 | screen.setPageTitle(titleText) |
| 596 | ---- |
| 597 | |
| 598 | [[screen.setWindowTitle]] |
| 599 | === screen.setWindowTitle() |
| 600 | Sets the text to be displayed in the browser's title bar when the |
| 601 | screen is visible. Plugins should always prefer this method over |
| 602 | trying to set `window.title` directly. The window title defaults to |
| 603 | the title given to `setTitle`. |
| 604 | |
| 605 | .Signature |
| 606 | [source,javascript] |
| 607 | ---- |
| 608 | screen.setWindowTitle(titleText) |
| 609 | ---- |
| 610 | |
| 611 | [[screen_show]] |
| 612 | === screen.show() |
| 613 | Destroy the currently visible screen and display the plugin's screen. |
| 614 | This method must be called after adding content to `screen.body`. |
| 615 | |
Edwin Kempin | dcaec6c | 2016-06-03 09:43:10 +0200 | [diff] [blame] | 616 | [[SettingsScreenContext]] |
| 617 | == Settings Screen Context |
| 618 | A new settings screen context is passed to the `settingsScreen` callback |
| 619 | function each time the user navigates to a matching URL. |
| 620 | |
| 621 | [[settingsScreen_body]] |
| 622 | === settingsScreen.body |
| 623 | Empty HTML `<div>` node the plugin should add its content to. The |
| 624 | node is already attached to the document, but is invisible. Plugins |
| 625 | must call `settingsScreen.show()` to display the DOM node. Deferred |
| 626 | display allows an implementor to partially populate the DOM, make |
| 627 | remote HTTP requests, finish populating when the callbacks arrive, and |
| 628 | only then make the view visible to the user. |
| 629 | |
| 630 | [[settingsScreen_onUnload]] |
| 631 | === settingsScreen.onUnload() |
| 632 | Configures an optional callback to be invoked just before the screen |
| 633 | is deleted from the browser DOM. Plugins can use this callback to |
| 634 | remove event listeners from DOM nodes, preventing memory leaks. |
| 635 | |
| 636 | .Signature |
| 637 | [source,javascript] |
| 638 | ---- |
| 639 | settingsScreen.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() |
| 648 | Sets the heading text to be displayed when the screen is visible. |
| 649 | This is presented in a large bold font below the menus, but above the |
| 650 | content in `settingsScreen.body`. Setting the title also sets the |
| 651 | window title to the same string, if it has not already been set. |
| 652 | |
| 653 | .Signature |
| 654 | [source,javascript] |
| 655 | ---- |
| 656 | settingsScreen.setPageTitle(titleText) |
| 657 | ---- |
| 658 | |
| 659 | [[settingsScreen.setWindowTitle]] |
| 660 | === settingsScreen.setWindowTitle() |
| 661 | Sets the text to be displayed in the browser's title bar when the |
| 662 | screen is visible. Plugins should always prefer this method over |
| 663 | trying to set `window.title` directly. The window title defaults to |
| 664 | the title given to `setTitle`. |
| 665 | |
| 666 | .Signature |
| 667 | [source,javascript] |
| 668 | ---- |
| 669 | settingsScreen.setWindowTitle(titleText) |
| 670 | ---- |
| 671 | |
| 672 | [[settingsScreen_show]] |
| 673 | === settingsScreen.show() |
| 674 | Destroy the currently visible screen and display the plugin's screen. |
| 675 | This method must be called after adding content to |
| 676 | `settingsScreen.body`. |
| 677 | |
Edwin Kempin | 9ef951b | 2016-06-03 13:27:50 +0200 | [diff] [blame] | 678 | [[PanelContext]] |
| 679 | == Panel Context |
| 680 | A new panel context is passed to the `panel` callback function each |
| 681 | time a screen with the given extension point is loaded. |
| 682 | |
| 683 | [[panel_body]] |
| 684 | === panel.body |
| 685 | Empty HTML `<div>` node the plugin should add the panel content to. |
| 686 | The node is already attached to the document. |
| 687 | |
| 688 | [[PanelProperties]] |
| 689 | === Properties |
| 690 | |
| 691 | The extension panel parameters that are described in the |
| 692 | link:dev-plugins.html#panels[plugin development documentation] are |
| 693 | contained in the context as properties. Which properties are available |
| 694 | depends on the extension point. |
| 695 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 696 | [[Gerrit]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 697 | == Gerrit |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 698 | |
| 699 | The `Gerrit` object is the only symbol provided into the global |
| 700 | namespace by Gerrit Code Review. All top-level functions can be |
| 701 | accessed through this name. |
| 702 | |
Shawn Pearce | 210b539 | 2013-12-07 22:41:36 -0800 | [diff] [blame] | 703 | [[Gerrit_css]] |
| 704 | Gerrit.css() |
| 705 | ~~~~~~~~~~~~ |
| 706 | Creates a new unique CSS class and injects it into the document. |
| 707 | The name of the class is returned and can be used by the plugin. |
| 708 | See link:#Gerrit_html[`Gerrit.html()`] for an easy way to use |
| 709 | generated class names. |
| 710 | |
| 711 | Classes created with this function should be created once at install |
| 712 | time and reused throughout the plugin. Repeatedly creating the same |
| 713 | class will explode the global stylesheet. |
| 714 | |
| 715 | .Signature |
| 716 | [source,javascript] |
| 717 | ---- |
| 718 | Gerrit.install(function(self)) { |
| 719 | var style = { |
| 720 | name: Gerrit.css('background: #fff; color: #000;'), |
| 721 | }; |
| 722 | }); |
| 723 | ---- |
| 724 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 725 | [[Gerrit_delete]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 726 | === Gerrit.delete() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 727 | Issues a DELETE REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 728 | private REST API URLs see link:#self_delete[self.delete()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 729 | |
| 730 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 731 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 732 | ---- |
| 733 | Gerrit.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 743 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 744 | ---- |
| 745 | Gerrit.delete( |
| 746 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 747 | function () {}); |
| 748 | ---- |
| 749 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 750 | [[Gerrit_get]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 751 | === Gerrit.get() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 752 | Issues a GET REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 753 | private REST API URLs see link:#self_get[self.get()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 754 | |
| 755 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 756 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 757 | ---- |
| 758 | Gerrit.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 769 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 770 | ---- |
| 771 | Gerrit.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 Ostrovsky | 0a4c0ff | 2014-09-21 23:06:52 +0200 | [diff] [blame] | 778 | [[Gerrit_getCurrentUser]] |
| 779 | === Gerrit.getCurrentUser() |
| 780 | Returns the currently signed in user's AccountInfo data; empty account |
| 781 | data if no user is currently signed in. |
| 782 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 783 | [[Gerrit_getPluginName]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 784 | === Gerrit.getPluginName() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 785 | Returns the name this plugin was installed as by the server |
| 786 | administrator. The plugin name is required to access REST API |
| 787 | views installed by the plugin, or to access resources. |
| 788 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 789 | Unlike link:#self_getPluginName[`self.getPluginName()`] this method |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 790 | must guess the name from the JavaScript call stack. Plugins are |
| 791 | encouraged to use `self.getPluginName()` whenever possible. |
| 792 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 793 | [[Gerrit_go]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 794 | === Gerrit.go() |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 795 | Updates the web UI to display the screen identified by the supplied |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 796 | URL token. The URL token is the text after `#` in the browser URL. |
| 797 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 798 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 799 | ---- |
| 800 | Gerrit.go('/admin/projects/'); |
| 801 | ---- |
| 802 | |
| 803 | If the URL passed matches `http://...`, `https://...`, or `//...` |
| 804 | the current browser window will navigate to the non-Gerrit URL. |
| 805 | The user can return to Gerrit with the back button. |
| 806 | |
Shawn Pearce | 210b539 | 2013-12-07 22:41:36 -0800 | [diff] [blame] | 807 | [[Gerrit_html]] |
| 808 | Gerrit.html() |
| 809 | ~~~~~~~~~~~~~ |
| 810 | Parses an HTML fragment after performing template replacements. If |
| 811 | the HTML has a single root element or node that node is returned, |
| 812 | otherwise it is wrapped inside a `<div>` and the div is returned. |
| 813 | |
| 814 | .Signature |
| 815 | [source,javascript] |
| 816 | ---- |
| 817 | Gerrit.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 | ---- |
| 840 | var style = {bar: Gerrit.css('background: yellow')}; |
| 841 | Gerrit.html( |
| 842 | '<span class="{style.bar}">Hello {name}!</span>', |
| 843 | {style: style, name: "World"}); |
| 844 | ---- |
| 845 | |
| 846 | Event handlers can be automatically attached to elements referenced |
| 847 | through an attribute id. Object navigation is not supported for ids, |
| 848 | and the parser strips the id attribute before returning the result. |
| 849 | Handler functions must begin with `on` and be a function to be |
| 850 | installed on the element. This approach is useful for onclick and |
| 851 | other handlers that do not want to create circular references that |
| 852 | will eventually leak browser memory. |
| 853 | |
| 854 | .Example |
| 855 | [source,javascript] |
| 856 | ---- |
| 857 | var options = { |
| 858 | link: { |
| 859 | onclick: function(e) { window.close() }, |
| 860 | }, |
| 861 | }; |
| 862 | Gerrit.html('<a href="javascript:;" id="{link}">Close</a>', options); |
| 863 | ---- |
| 864 | |
| 865 | When using options to install handlers care must be taken to not |
| 866 | accidentally include the returned element into the event handler's |
| 867 | closure. This is why options is built before calling `Gerrit.html()` |
| 868 | and not inline as a shown above with "Hello World". |
| 869 | |
| 870 | DOM nodes can optionally be returned, allowing handlers to access the |
| 871 | elements identified by `id={name}` at a later point in time. |
| 872 | |
| 873 | .Example |
| 874 | [source,javascript] |
| 875 | ---- |
| 876 | var 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 | |
| 890 | To prevent memory leaks `w.root` and `w.elements` should be set to |
| 891 | null when the elements are no longer necessary. Screens can use |
| 892 | link:#screen_onUnload[screen.onUnload()] to define a callback function |
| 893 | to perform this cleanup: |
| 894 | |
| 895 | [source,javascript] |
| 896 | ---- |
| 897 | var w = Gerrit.html(...); |
| 898 | screen.body.appendElement(w.root); |
| 899 | screen.onUnload(function() { w.clear() }); |
| 900 | ---- |
| 901 | |
| 902 | [[Gerrit_injectCss]] |
| 903 | Gerrit.injectCss() |
| 904 | ~~~~~~~~~~~~~~~~~~ |
| 905 | Injects CSS rules into the document by appending onto the end of the |
| 906 | existing rule list. CSS rules are global to the entire application |
| 907 | and must be manually scoped by each plugin. For an automatic scoping |
| 908 | alternative see link:#Gerrit_css[`css()`]. |
| 909 | |
| 910 | [source,javascript] |
| 911 | ---- |
| 912 | Gerrit.injectCss('.myplugin_bg {background: #000}'); |
| 913 | ---- |
| 914 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 915 | [[Gerrit_install]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 916 | === Gerrit.install() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 917 | Registers a new plugin by invoking the supplied initialization |
| 918 | function. The function is passed the link:#self[plugin instance]. |
| 919 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 920 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 921 | ---- |
| 922 | Gerrit.install(function (self) { |
| 923 | // ... plugin JavaScript code here ... |
| 924 | }); |
| 925 | ---- |
| 926 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 927 | [[Gerrit_post]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 928 | === Gerrit.post() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 929 | Issues a POST REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 930 | private REST API URLs see link:#self_post[self.post()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 931 | |
| 932 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 933 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 934 | ---- |
| 935 | Gerrit.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 948 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 949 | ---- |
| 950 | Gerrit.post( |
| 951 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 952 | {topic: 'tests', message: 'Classify work as for testing.'}, |
| 953 | function (r) {}); |
| 954 | ---- |
| 955 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 956 | [[Gerrit_put]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 957 | === Gerrit.put() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 958 | Issues a PUT REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 959 | private REST API URLs see link:#self_put[self.put()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 960 | |
| 961 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 962 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 963 | ---- |
| 964 | Gerrit.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 Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 977 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 978 | ---- |
| 979 | Gerrit.put( |
| 980 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 981 | {topic: 'tests', message: 'Classify work as for testing.'}, |
| 982 | function (r) {}); |
| 983 | ---- |
| 984 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 985 | [[Gerrit_onAction]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 986 | === Gerrit.onAction() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 987 | Register a JavaScript callback to be invoked when the user clicks |
| 988 | on a button associated with a server side `UiAction`. |
| 989 | |
| 990 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 991 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 992 | ---- |
| 993 | Gerrit.onAction(type, view_name, callback); |
| 994 | ---- |
| 995 | |
David Ostrovsky | cdda331 | 2014-08-03 14:03:13 +0200 | [diff] [blame] | 996 | * type: `'change'`, `'edit'`, `'revision'`, `'project'` or `'branch'` |
| 997 | indicating what sort of resource the `UiAction` was bound to in the server. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 998 | |
| 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 Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 1006 | [[Gerrit_screen]] |
| 1007 | === Gerrit.screen() |
| 1008 | Register a JavaScript callback to be invoked when the user navigates |
| 1009 | to an extension screen provided by the plugin. Extension screens are |
| 1010 | usually linked from the link:dev-plugins.html#top-menu-extensions[top menu]. |
| 1011 | The callback can populate the DOM with the screen's contents. |
| 1012 | |
| 1013 | .Signature |
| 1014 | [source,javascript] |
| 1015 | ---- |
| 1016 | Gerrit.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 Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 1027 | [[Gerrit_refresh]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1028 | === Gerrit.refresh() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 1029 | Redisplays the current web UI view, refreshing all information. |
| 1030 | |
Edwin Kempin | 049c355 | 2014-04-15 11:26:23 +0200 | [diff] [blame] | 1031 | [[Gerrit_refreshMenuBar]] |
| 1032 | === Gerrit.refreshMenuBar() |
| 1033 | Refreshes Gerrit's menu bar. |
| 1034 | |
Edwin Kempin | a30c835 | 2015-01-20 23:16:26 +0100 | [diff] [blame] | 1035 | [[Gerrit_isSignedIn]] |
| 1036 | === Gerrit.isSignedIn() |
| 1037 | Checks if user is signed in. |
| 1038 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 1039 | [[Gerrit_url]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1040 | === Gerrit.url() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 1041 | Returns the URL of the Gerrit Code Review server. If invoked with |
| 1042 | no parameter the URL of the site is returned. If passed a string |
| 1043 | the argument is appended to the site URL. |
| 1044 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1045 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 1046 | ---- |
| 1047 | Gerrit.url(); // "https://gerrit-review.googlesource.com/" |
| 1048 | Gerrit.url('/123'); // "https://gerrit-review.googlesource.com/123" |
| 1049 | ---- |
| 1050 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 1051 | For a plugin specific version see link:#self_url()[`self.url()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 1052 | |
Edwin Kempin | 792c8d8 | 2014-01-31 16:16:52 +0100 | [diff] [blame] | 1053 | [[Gerrit_showError]] |
| 1054 | === Gerrit.showError(message) |
| 1055 | Displays the given message in the Gerrit ErrorDialog. |
| 1056 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 1057 | GERRIT |
| 1058 | ------ |
| 1059 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 1060 | |
| 1061 | SEARCHBOX |
| 1062 | --------- |