Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 1 | Gerrit Code Review - JavaScript API |
| 2 | =================================== |
| 3 | |
| 4 | Gerrit Code Review supports an API for JavaScript plugins to interact |
| 5 | with the web UI and the server process. |
| 6 | |
| 7 | Entry Point |
| 8 | ----------- |
| 9 | |
| 10 | JavaScript is loaded using a standard `<script src='...'>` HTML tag. |
| 11 | Plugins should protect the global namespace by defining their code |
| 12 | within an anonymous function passed to `Gerrit.install()`. The plugin |
| 13 | will be passed an object describing its registration with Gerrit: |
| 14 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 15 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 16 | ---- |
| 17 | Gerrit.install(function (self) { |
| 18 | // ... plugin JavaScript code here ... |
| 19 | }); |
| 20 | ---- |
| 21 | |
| 22 | |
| 23 | [[self]] |
| 24 | Plugin Instance |
| 25 | --------------- |
| 26 | |
| 27 | The plugin instance is passed to the plugin's initialization function |
| 28 | and provides a number of utility services to plugin authors. |
| 29 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 30 | [[self_delete]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 31 | self.delete() |
| 32 | ~~~~~~~~~~~~~ |
| 33 | Issues a DELETE REST API request to the Gerrit server. |
| 34 | |
| 35 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 36 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 37 | ---- |
| 38 | Gerrit.delete(url, callback) |
| 39 | ---- |
| 40 | |
| 41 | * url: URL relative to the plugin's URL space. The JavaScript |
| 42 | library prefixes the supplied URL with `/plugins/{getPluginName}/`. |
| 43 | |
| 44 | * callback: JavaScript function to be invoked with the parsed |
| 45 | JSON result of the API call. DELETE methods often return |
| 46 | `204 No Content`, which is passed as null. |
| 47 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 48 | [[self_get]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 49 | self.get() |
| 50 | ~~~~~~~~~~ |
| 51 | Issues a GET REST API request to the Gerrit server. |
| 52 | |
| 53 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 54 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 55 | ---- |
| 56 | self.get(url, callback) |
| 57 | ---- |
| 58 | |
| 59 | * url: URL relative to the plugin's URL space. The JavaScript |
| 60 | library prefixes the supplied URL with `/plugins/{getPluginName}/`. |
| 61 | |
| 62 | * callback: JavaScript function to be invoked with the parsed JSON |
| 63 | result of the API call. If the API returns a string the result is |
| 64 | a string, otherwise the result is a JavaScript object or array, |
| 65 | as described in the relevant REST API documentation. |
| 66 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 67 | [[self_getPluginName]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 68 | self.getPluginName() |
| 69 | ~~~~~~~~~~~~~~~~~~~~ |
| 70 | Returns the name this plugin was installed as by the server |
| 71 | administrator. The plugin name is required to access REST API |
| 72 | views installed by the plugin, or to access resources. |
| 73 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 74 | [[self_post]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 75 | self.post() |
| 76 | ~~~~~~~~~~~ |
| 77 | Issues a POST REST API request to the Gerrit server. |
| 78 | |
| 79 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 80 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 81 | ---- |
| 82 | self.post(url, input, callback) |
| 83 | ---- |
| 84 | |
| 85 | * url: URL relative to the plugin's URL space. The JavaScript |
| 86 | library prefixes the supplied URL with `/plugins/{getPluginName}/`. |
| 87 | |
| 88 | * input: JavaScript object to serialize as the request payload. |
| 89 | |
| 90 | * callback: JavaScript function to be invoked with the parsed JSON |
| 91 | result of the API call. If the API returns a string the result is |
| 92 | a string, otherwise the result is a JavaScript object or array, |
| 93 | as described in the relevant REST API documentation. |
| 94 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 95 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 96 | ---- |
| 97 | self.post( |
| 98 | '/my-servlet', |
| 99 | {start_build: true, platform_type: 'Linux'}, |
| 100 | function (r) {}); |
| 101 | ---- |
| 102 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 103 | [[self_put]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 104 | self.put() |
| 105 | ~~~~~~~~~~ |
| 106 | Issues a PUT REST API request to the Gerrit server. |
| 107 | |
| 108 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 109 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 110 | ---- |
| 111 | self.put(url, input, callback) |
| 112 | ---- |
| 113 | |
| 114 | * url: URL relative to the plugin's URL space. The JavaScript |
| 115 | library prefixes the supplied URL with `/plugins/{getPluginName}/`. |
| 116 | |
| 117 | * input: JavaScript object to serialize as the request payload. |
| 118 | |
| 119 | * callback: JavaScript function to be invoked with the parsed JSON |
| 120 | result of the API call. If the API returns a string the result is |
| 121 | a string, otherwise the result is a JavaScript object or array, |
| 122 | as described in the relevant REST API documentation. |
| 123 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 124 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 125 | ---- |
| 126 | self.put( |
| 127 | '/builds', |
| 128 | {start_build: true, platform_type: 'Linux'}, |
| 129 | function (r) {}); |
| 130 | ---- |
| 131 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 132 | [[self_onAction]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 133 | self.onAction() |
| 134 | ~~~~~~~~~~~~~~~ |
| 135 | Register a JavaScript callback to be invoked when the user clicks |
| 136 | on a button associated with a server side `UiAction`. |
| 137 | |
| 138 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 139 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 140 | ---- |
| 141 | Gerrit.onAction(type, view_name, callback); |
| 142 | ---- |
| 143 | |
David Ostrovsky | 9e8b2fb | 2013-08-30 08:09:53 +0200 | [diff] [blame] | 144 | * type: `'change'`, `'revision'` or `'project'`, indicating which type |
| 145 | of resource the `UiAction` was bound to in the server. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 146 | |
| 147 | * view_name: string appearing in URLs to name the view. This is the |
| 148 | second argument of the `get()`, `post()`, `put()`, and `delete()` |
| 149 | binding methods in a `RestApiModule`. |
| 150 | |
| 151 | * callback: JavaScript function to invoke when the user clicks. The |
| 152 | function will be passed a link:#ActionContext[action context]. |
| 153 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 154 | [[self_url]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 155 | self.url() |
| 156 | ~~~~~~~~~~ |
| 157 | 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] | 158 | parameter the URL of the plugin is returned. If passed a string |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 159 | the argument is appended to the plugin URL. |
| 160 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 161 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 162 | ---- |
| 163 | self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/" |
| 164 | self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png" |
| 165 | ---- |
| 166 | |
| 167 | |
| 168 | [[ActionContext]] |
| 169 | Action Context |
| 170 | -------------- |
| 171 | A new action context is passed to the `onAction` callback function |
| 172 | each time the associated action button is clicked by the user. A |
| 173 | context is initialized with sufficient state to issue the associated |
| 174 | REST API RPC. |
| 175 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 176 | [[context_action]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 177 | context.action |
| 178 | ~~~~~~~~~~~~~~ |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 179 | An link:rest-api-changes.html#action-info[ActionInfo] object instance |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 180 | supplied by the server describing the UI button the user used to |
| 181 | invoke the action. |
| 182 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 183 | [[context_call]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 184 | context.call() |
| 185 | ~~~~~~~~~~~~~~ |
| 186 | Issues the REST API call associated with the action. The HTTP method |
| 187 | used comes from `context.action.method`, hiding the JavaScript from |
| 188 | needing to care. |
| 189 | |
| 190 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 191 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 192 | ---- |
| 193 | context.call(input, callback) |
| 194 | ---- |
| 195 | |
| 196 | * input: JavaScript object to serialize as the request payload. This |
| 197 | parameter is ignored for GET and DELETE methods. |
| 198 | |
| 199 | * callback: JavaScript function to be invoked with the parsed JSON |
| 200 | result of the API call. If the API returns a string the result is |
| 201 | a string, otherwise the result is a JavaScript object or array, |
| 202 | as described in the relevant REST API documentation. |
| 203 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 204 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 205 | ---- |
| 206 | context.call( |
| 207 | {message: "..."}, |
| 208 | function (result) { |
| 209 | // ... use result here ... |
| 210 | }); |
| 211 | ---- |
| 212 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 213 | [[context_change]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 214 | context.change |
| 215 | ~~~~~~~~~~~~~~ |
| 216 | When the action is invoked on a change a |
| 217 | link:rest-api-changes.html#change-info[ChangeInfo] object instance |
| 218 | describing the change. Available fields of the ChangeInfo may vary |
| 219 | based on the options used by the UI when it loaded the change. |
| 220 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 221 | [[context_delete]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 222 | context.delete() |
| 223 | ~~~~~~~~~~~~~~~~ |
| 224 | Issues a DELETE REST API call to the URL associated with the action. |
| 225 | |
| 226 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 227 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 228 | ---- |
| 229 | context.delete(callback) |
| 230 | ---- |
| 231 | |
| 232 | * callback: JavaScript function to be invoked with the parsed |
| 233 | JSON result of the API call. DELETE methods often return |
| 234 | `204 No Content`, which is passed as null. |
| 235 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 236 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 237 | ---- |
| 238 | context.delete(function () {}); |
| 239 | ---- |
| 240 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 241 | [[context_get]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 242 | context.get() |
| 243 | ~~~~~~~~~~~~~ |
| 244 | Issues a GET REST API call to the URL associated with the action. |
| 245 | |
| 246 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 247 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 248 | ---- |
| 249 | context.get(callback) |
| 250 | ---- |
| 251 | |
| 252 | * callback: JavaScript function to be invoked with the parsed JSON |
| 253 | result of the API call. If the API returns a string the result is |
| 254 | a string, otherwise the result is a JavaScript object or array, |
| 255 | as described in the relevant REST API documentation. |
| 256 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 257 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 258 | ---- |
| 259 | context.get(function (result) { |
| 260 | // ... use result here ... |
| 261 | }); |
| 262 | ---- |
| 263 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 264 | [[context_go]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 265 | context.go() |
| 266 | ~~~~~~~~~~~~ |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 267 | Go to a page. Shorthand for link:#Gerrit_go[`Gerrit.go()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 268 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 269 | [[context_hide]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 270 | context.hide() |
| 271 | ~~~~~~~~~~~~~~ |
| 272 | Hide the currently visible popup displayed by |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 273 | link:#context_popup[`context.popup()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 274 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 275 | [[context_post]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 276 | context.post() |
| 277 | ~~~~~~~~~~~~~~ |
| 278 | Issues a POST REST API call to the URL associated with the action. |
| 279 | |
| 280 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 281 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 282 | ---- |
| 283 | context.post(input, callback) |
| 284 | ---- |
| 285 | |
| 286 | * input: JavaScript object to serialize as the request payload. |
| 287 | |
| 288 | * callback: JavaScript function to be invoked with the parsed JSON |
| 289 | result of the API call. If the API returns a string the result is |
| 290 | a string, otherwise the result is a JavaScript object or array, |
| 291 | as described in the relevant REST API documentation. |
| 292 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 293 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 294 | ---- |
| 295 | context.post( |
| 296 | {message: "..."}, |
| 297 | function (result) { |
| 298 | // ... use result here ... |
| 299 | }); |
| 300 | ---- |
| 301 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 302 | [[context_popup]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 303 | context.popup() |
| 304 | ~~~~~~~~~~~~~~~ |
| 305 | |
| 306 | Displays a small popup near the activation button to gather |
| 307 | additional input from the user before executing the REST API RPC. |
| 308 | |
| 309 | The caller is always responsible for closing the popup with |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 310 | link#context_hide[`context.hide()`]. Gerrit will handle closing a |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 311 | popup if the user presses `Escape` while keyboard focus is within |
| 312 | the popup. |
| 313 | |
| 314 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 315 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 316 | ---- |
| 317 | context.popup(element) |
| 318 | ---- |
| 319 | |
| 320 | * element: an HTML DOM element to display as the body of the |
| 321 | popup. This is typically a `div` element but can be any valid HTML |
| 322 | element. CSS can be used to style the element beyond the defaults. |
| 323 | |
| 324 | A common usage is to gather more input: |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 325 | |
| 326 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 327 | ---- |
| 328 | self.onAction('revision', 'start-build', function (c) { |
| 329 | var l = c.checkbox(); |
| 330 | var m = c.checkbox(); |
| 331 | c.popup(c.div( |
| 332 | c.div(c.label(l, 'Linux')), |
| 333 | c.div(c.label(m, 'Mac OS X')), |
| 334 | c.button('Build', {onclick: function() { |
| 335 | c.call( |
| 336 | { |
| 337 | commit: c.revision.name, |
| 338 | linux: l.checked, |
| 339 | mac: m.checked, |
| 340 | }, |
| 341 | function() { c.hide() }); |
| 342 | }); |
| 343 | }); |
| 344 | ---- |
| 345 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 346 | [[context_put]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 347 | context.put() |
| 348 | ~~~~~~~~~~~~~ |
| 349 | Issues a PUT REST API call to the URL associated with the action. |
| 350 | |
| 351 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 352 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 353 | ---- |
| 354 | context.put(input, callback) |
| 355 | ---- |
| 356 | |
| 357 | * input: JavaScript object to serialize as the request payload. |
| 358 | |
| 359 | * callback: JavaScript function to be invoked with the parsed JSON |
| 360 | result of the API call. If the API returns a string the result is |
| 361 | a string, otherwise the result is a JavaScript object or array, |
| 362 | as described in the relevant REST API documentation. |
| 363 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 364 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 365 | ---- |
| 366 | context.put( |
| 367 | {message: "..."}, |
| 368 | function (result) { |
| 369 | // ... use result here ... |
| 370 | }); |
| 371 | ---- |
| 372 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 373 | [[context_refresh]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 374 | context.refresh() |
| 375 | ~~~~~~~~~~~~~~~~~ |
| 376 | Refresh the current display. Shorthand for |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 377 | link:#Gerrit_refresh[`Gerrit.refresh()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 378 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 379 | [[context_revision]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 380 | context.revision |
| 381 | ~~~~~~~~~~~~~~~~ |
| 382 | When the action is invoked on a specific revision of a change, |
| 383 | a link:rest-api-changes.html#revision-info[RevisionInfo] |
| 384 | object instance describing the revision. Available fields of the |
| 385 | RevisionInfo may vary based on the options used by the UI when it |
| 386 | loaded the change. |
| 387 | |
David Ostrovsky | 9e8b2fb | 2013-08-30 08:09:53 +0200 | [diff] [blame] | 388 | [[context_project]] |
| 389 | context.project |
| 390 | ~~~~~~~~~~~~~~~ |
| 391 | When the action is invoked on a specific project, |
| 392 | the name of the project. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 393 | |
| 394 | Action Context HTML Helpers |
| 395 | --------------------------- |
| 396 | The link:#ActionContext[action context] includes some HTML helper |
| 397 | functions to make working with DOM based widgets less painful. |
| 398 | |
| 399 | * `br()`: new `<br>` element. |
| 400 | |
| 401 | * `button(label, options)`: new `<button>` with the string `label` |
| 402 | wrapped inside of a `div`. The optional `options` object may |
| 403 | define `onclick` as a function to be invoked upon clicking. This |
| 404 | calling pattern avoids circular references between the element |
| 405 | and the onclick handler. |
| 406 | |
| 407 | * `checkbox()`: new `<input type='checkbox'>` element. |
| 408 | * `div(...)`: a new `<div>` wrapping the (optional) arguments. |
| 409 | * `hr()`: new `<hr>` element. |
| 410 | |
| 411 | * `label(c, label)`: a new `<label>` element wrapping element `c` |
| 412 | and the string `label`. Used to wrap a checkbox with its label, |
| 413 | `label(checkbox(), 'Click Me')`. |
| 414 | |
David Ostrovsky | a1067a3 | 2013-08-19 09:09:17 +0200 | [diff] [blame] | 415 | * `prependLabel(label, c)`: a new `<label>` element wrapping element `c` |
| 416 | and the string `label`. Used to wrap an input field with its label, |
| 417 | `prependLabel('Greeting message', textfield())`. |
| 418 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 419 | * `textarea(options)`: new `<textarea>` element. The options |
| 420 | object may optionally include `rows` and `cols`. The textarea |
| 421 | comes with an onkeypress handler installed to play nicely with |
| 422 | Gerrit's keyboard binding system. |
| 423 | |
| 424 | * `textfield()`: new `<input type='text'>` element. The text field |
| 425 | comes with an onkeypress handler installed to play nicely with |
| 426 | Gerrit's keyboard binding system. |
| 427 | |
| 428 | * `span(...)`: a new `<span>` wrapping the (optional) arguments. |
| 429 | |
David Ostrovsky | 7dfc802 | 2013-08-25 23:05:50 +0200 | [diff] [blame] | 430 | * `msg(label)`: a new label. |
| 431 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 432 | [[Gerrit]] |
| 433 | Gerrit |
| 434 | ------ |
| 435 | |
| 436 | The `Gerrit` object is the only symbol provided into the global |
| 437 | namespace by Gerrit Code Review. All top-level functions can be |
| 438 | accessed through this name. |
| 439 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 440 | [[Gerrit_delete]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 441 | Gerrit.delete() |
| 442 | ~~~~~~~~~~~~~~~ |
| 443 | Issues a DELETE REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 444 | private REST API URLs see link:#self_delete[self.delete()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 445 | |
| 446 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 447 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 448 | ---- |
| 449 | Gerrit.delete(url, callback) |
| 450 | ---- |
| 451 | |
| 452 | * url: URL relative to the Gerrit server. For example to access the |
| 453 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 454 | |
| 455 | * callback: JavaScript function to be invoked with the parsed |
| 456 | JSON result of the API call. DELETE methods often return |
| 457 | `204 No Content`, which is passed as null. |
| 458 | |
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 | Gerrit.delete( |
| 462 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 463 | function () {}); |
| 464 | ---- |
| 465 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 466 | [[Gerrit_get]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 467 | Gerrit.get() |
| 468 | ~~~~~~~~~~~~ |
| 469 | Issues a GET REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 470 | private REST API URLs see link:#self_get[self.get()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 471 | |
| 472 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 473 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 474 | ---- |
| 475 | Gerrit.get(url, callback) |
| 476 | ---- |
| 477 | |
| 478 | * url: URL relative to the Gerrit server. For example to access the |
| 479 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 480 | |
| 481 | * callback: JavaScript function to be invoked with the parsed JSON |
| 482 | result of the API call. If the API returns a string the result is |
| 483 | a string, otherwise the result is a JavaScript object or array, |
| 484 | as described in the relevant REST API documentation. |
| 485 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 486 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 487 | ---- |
| 488 | Gerrit.get('/changes/?q=status:open', function (open) { |
| 489 | for (var i = 0; i < open.length; i++) { |
| 490 | console.log(open.get(i).change_id); |
| 491 | } |
| 492 | }); |
| 493 | ---- |
| 494 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 495 | [[Gerrit_getPluginName]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 496 | Gerrit.getPluginName() |
| 497 | ~~~~~~~~~~~~~~~~~~~~~~ |
| 498 | Returns the name this plugin was installed as by the server |
| 499 | administrator. The plugin name is required to access REST API |
| 500 | views installed by the plugin, or to access resources. |
| 501 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 502 | Unlike link:#self_getPluginName[`self.getPluginName()`] this method |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 503 | must guess the name from the JavaScript call stack. Plugins are |
| 504 | encouraged to use `self.getPluginName()` whenever possible. |
| 505 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 506 | [[Gerrit_go]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 507 | Gerrit.go() |
| 508 | ~~~~~~~~~~~ |
| 509 | Updates the web UI to display the view identified by the supplied |
| 510 | URL token. The URL token is the text after `#` in the browser URL. |
| 511 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 512 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 513 | ---- |
| 514 | Gerrit.go('/admin/projects/'); |
| 515 | ---- |
| 516 | |
| 517 | If the URL passed matches `http://...`, `https://...`, or `//...` |
| 518 | the current browser window will navigate to the non-Gerrit URL. |
| 519 | The user can return to Gerrit with the back button. |
| 520 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 521 | [[Gerrit_install]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 522 | Gerrit.install() |
| 523 | ~~~~~~~~~~~~~~~~ |
| 524 | Registers a new plugin by invoking the supplied initialization |
| 525 | function. The function is passed the link:#self[plugin instance]. |
| 526 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 527 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 528 | ---- |
| 529 | Gerrit.install(function (self) { |
| 530 | // ... plugin JavaScript code here ... |
| 531 | }); |
| 532 | ---- |
| 533 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 534 | [[Gerrit_post]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 535 | Gerrit.post() |
| 536 | ~~~~~~~~~~~~~ |
| 537 | Issues a POST REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 538 | private REST API URLs see link:#self_post[self.post()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 539 | |
| 540 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 541 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 542 | ---- |
| 543 | Gerrit.post(url, input, callback) |
| 544 | ---- |
| 545 | |
| 546 | * url: URL relative to the Gerrit server. For example to access the |
| 547 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 548 | |
| 549 | * input: JavaScript object to serialize as the request payload. |
| 550 | |
| 551 | * callback: JavaScript function to be invoked with the parsed JSON |
| 552 | result of the API call. If the API returns a string the result is |
| 553 | a string, otherwise the result is a JavaScript object or array, |
| 554 | as described in the relevant REST API documentation. |
| 555 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 556 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 557 | ---- |
| 558 | Gerrit.post( |
| 559 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 560 | {topic: 'tests', message: 'Classify work as for testing.'}, |
| 561 | function (r) {}); |
| 562 | ---- |
| 563 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 564 | [[Gerrit_put]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 565 | Gerrit.put() |
| 566 | ~~~~~~~~~~~~ |
| 567 | Issues a PUT REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 568 | private REST API URLs see link:#self_put[self.put()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 569 | |
| 570 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 571 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 572 | ---- |
| 573 | Gerrit.put(url, input, callback) |
| 574 | ---- |
| 575 | |
| 576 | * url: URL relative to the Gerrit server. For example to access the |
| 577 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 578 | |
| 579 | * input: JavaScript object to serialize as the request payload. |
| 580 | |
| 581 | * callback: JavaScript function to be invoked with the parsed JSON |
| 582 | result of the API call. If the API returns a string the result is |
| 583 | a string, otherwise the result is a JavaScript object or array, |
| 584 | as described in the relevant REST API documentation. |
| 585 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 586 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 587 | ---- |
| 588 | Gerrit.put( |
| 589 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 590 | {topic: 'tests', message: 'Classify work as for testing.'}, |
| 591 | function (r) {}); |
| 592 | ---- |
| 593 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 594 | [[Gerrit_onAction]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 595 | Gerrit.onAction() |
| 596 | ~~~~~~~~~~~~~~~~~ |
| 597 | Register a JavaScript callback to be invoked when the user clicks |
| 598 | on a button associated with a server side `UiAction`. |
| 599 | |
| 600 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 601 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 602 | ---- |
| 603 | Gerrit.onAction(type, view_name, callback); |
| 604 | ---- |
| 605 | |
| 606 | * type: `'change'` or `'revision'`, indicating what sort of resource |
| 607 | the `UiAction` was bound to in the server. |
| 608 | |
| 609 | * view_name: string appearing in URLs to name the view. This is the |
| 610 | second argument of the `get()`, `post()`, `put()`, and `delete()` |
| 611 | binding methods in a `RestApiModule`. |
| 612 | |
| 613 | * callback: JavaScript function to invoke when the user clicks. The |
| 614 | function will be passed a link:#ActionContext[ActionContext]. |
| 615 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 616 | [[Gerrit_refresh]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 617 | Gerrit.refresh() |
| 618 | ~~~~~~~~~~~~~~~~ |
| 619 | Redisplays the current web UI view, refreshing all information. |
| 620 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 621 | [[Gerrit_url]] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 622 | Gerrit.url() |
| 623 | ~~~~~~~~~~~~ |
| 624 | Returns the URL of the Gerrit Code Review server. If invoked with |
| 625 | no parameter the URL of the site is returned. If passed a string |
| 626 | the argument is appended to the site URL. |
| 627 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 628 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 629 | ---- |
| 630 | Gerrit.url(); // "https://gerrit-review.googlesource.com/" |
| 631 | Gerrit.url('/123'); // "https://gerrit-review.googlesource.com/123" |
| 632 | ---- |
| 633 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 634 | For a plugin specific version see link:#self_url()[`self.url()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 635 | |
| 636 | GERRIT |
| 637 | ------ |
| 638 | Part of link:index.html[Gerrit Code Review] |