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 | |
David Ostrovsky | 0a4c0ff | 2014-09-21 23:06:52 +0200 | [diff] [blame] | 63 | [[self_getCurrentUser]] |
| 64 | === self.getCurrentUser() |
| 65 | Returns the currently signed in user's AccountInfo data; empty account |
| 66 | data if no user is currently signed in. |
| 67 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 68 | [[self_getPluginName]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 69 | === self.getPluginName() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 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]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 75 | === self.post() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 76 | Issues a POST REST API request to the Gerrit server. |
| 77 | |
| 78 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 79 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 80 | ---- |
| 81 | self.post(url, input, callback) |
| 82 | ---- |
| 83 | |
| 84 | * url: URL relative to the plugin's URL space. The JavaScript |
| 85 | library prefixes the supplied URL with `/plugins/{getPluginName}/`. |
| 86 | |
| 87 | * input: JavaScript object to serialize as the request payload. |
| 88 | |
| 89 | * callback: JavaScript function to be invoked with the parsed JSON |
| 90 | result of the API call. If the API returns a string the result is |
| 91 | a string, otherwise the result is a JavaScript object or array, |
| 92 | as described in the relevant REST API documentation. |
| 93 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 94 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 95 | ---- |
| 96 | self.post( |
| 97 | '/my-servlet', |
| 98 | {start_build: true, platform_type: 'Linux'}, |
| 99 | function (r) {}); |
| 100 | ---- |
| 101 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 102 | [[self_put]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 103 | === self.put() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 104 | Issues a PUT REST API request to the Gerrit server. |
| 105 | |
| 106 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 107 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 108 | ---- |
| 109 | self.put(url, input, callback) |
| 110 | ---- |
| 111 | |
| 112 | * url: URL relative to the plugin's URL space. The JavaScript |
| 113 | library prefixes the supplied URL with `/plugins/{getPluginName}/`. |
| 114 | |
| 115 | * input: JavaScript object to serialize as the request payload. |
| 116 | |
| 117 | * callback: JavaScript function to be invoked with the parsed JSON |
| 118 | result of the API call. If the API returns a string the result is |
| 119 | a string, otherwise the result is a JavaScript object or array, |
| 120 | as described in the relevant REST API documentation. |
| 121 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 122 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 123 | ---- |
| 124 | self.put( |
| 125 | '/builds', |
| 126 | {start_build: true, platform_type: 'Linux'}, |
| 127 | function (r) {}); |
| 128 | ---- |
| 129 | |
Shawn Pearce | db284cd | 2013-11-29 20:48:21 -0800 | [diff] [blame] | 130 | [[self_on]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 131 | === self.on() |
Shawn Pearce | db284cd | 2013-11-29 20:48:21 -0800 | [diff] [blame] | 132 | Register a JavaScript callback to be invoked when events occur within |
| 133 | the web interface. |
| 134 | |
| 135 | .Signature |
| 136 | [source,javascript] |
| 137 | ---- |
| 138 | Gerrit.on(event, callback); |
| 139 | ---- |
| 140 | |
| 141 | * event: A supported event type. See below for description. |
| 142 | |
| 143 | * callback: JavaScript function to be invoked when event happens. |
| 144 | Arguments may be passed to this function, depending on the event. |
| 145 | |
| 146 | Supported events: |
| 147 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 148 | * `history`: Invoked when the view is changed to a new screen within |
| 149 | the Gerrit web application. The token after "#" is passed as the |
Shawn Pearce | a48826e | 2013-11-29 20:38:55 -0800 | [diff] [blame] | 150 | argument to the callback function, for example "/c/42/" while |
| 151 | showing change 42. |
| 152 | |
Shawn Pearce | db284cd | 2013-11-29 20:48:21 -0800 | [diff] [blame] | 153 | * `showchange`: Invoked when a change is made visible. A |
| 154 | link:rest-api-changes.html#change-info[ChangeInfo] and |
| 155 | link:rest-api-changes.html#revision-info[RevisionInfo] |
| 156 | are passed as arguments. |
| 157 | |
Shawn Pearce | 7c41899 | 2013-11-29 20:34:28 -0800 | [diff] [blame] | 158 | * `submitchange`: Invoked when the submit button is clicked |
| 159 | on a change. A link:rest-api-changes.html#change-info[ChangeInfo] |
| 160 | and link:rest-api-changes.html#revision-info[RevisionInfo] are |
| 161 | passed as arguments. Similar to a form submit validation, the |
| 162 | function must return true to allow the operation to continue, or |
| 163 | false to prevent it. |
| 164 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 165 | [[self_onAction]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 166 | === self.onAction() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 167 | Register a JavaScript callback to be invoked when the user clicks |
| 168 | on a button associated with a server side `UiAction`. |
| 169 | |
| 170 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 171 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 172 | ---- |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 173 | self.onAction(type, view_name, callback); |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 174 | ---- |
| 175 | |
David Ostrovsky | cdda331 | 2014-08-03 14:03:13 +0200 | [diff] [blame] | 176 | * type: `'change'`, `'edit'`, `'revision'`, `'project'`, or `'branch'` |
Shawn Pearce | d1cf962 | 2014-03-24 16:21:51 -0700 | [diff] [blame] | 177 | indicating which type of resource the `UiAction` was bound to |
| 178 | in the server. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 179 | |
| 180 | * view_name: string appearing in URLs to name the view. This is the |
| 181 | second argument of the `get()`, `post()`, `put()`, and `delete()` |
| 182 | binding methods in a `RestApiModule`. |
| 183 | |
| 184 | * callback: JavaScript function to invoke when the user clicks. The |
| 185 | function will be passed a link:#ActionContext[action context]. |
| 186 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 187 | [[self_screen]] |
| 188 | === self.screen() |
| 189 | Register a JavaScript callback to be invoked when the user navigates |
| 190 | to an extension screen provided by the plugin. Extension screens are |
| 191 | usually linked from the link:dev-plugins.html#top-menu-extensions[top menu]. |
| 192 | The callback can populate the DOM with the screen's contents. |
| 193 | |
| 194 | .Signature |
| 195 | [source,javascript] |
| 196 | ---- |
| 197 | self.screen(pattern, callback); |
| 198 | ---- |
| 199 | |
| 200 | * pattern: URL token pattern to identify the screen. Argument can be |
| 201 | either a string (`'index'`) or a RegExp object (`/list\/(.*)/`). |
| 202 | If a RegExp is used the matching groups will be available inside of |
| 203 | the context as `token_match`. |
| 204 | |
| 205 | * callback: JavaScript function to invoke when the user navigates to |
| 206 | the screen. The function will be passed a link:#ScreenContext[screen context]. |
| 207 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 208 | [[self_url]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 209 | === self.url() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 210 | 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] | 211 | parameter the URL of the plugin is returned. If passed a string |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 212 | the argument is appended to the plugin URL. |
| 213 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 214 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 215 | ---- |
| 216 | self.url(); // "https://gerrit-review.googlesource.com/plugins/demo/" |
| 217 | self.url('/static/icon.png'); // "https://gerrit-review.googlesource.com/plugins/demo/static/icon.png" |
| 218 | ---- |
| 219 | |
| 220 | |
| 221 | [[ActionContext]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 222 | == Action Context |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 223 | A new action context is passed to the `onAction` callback function |
| 224 | each time the associated action button is clicked by the user. A |
| 225 | context is initialized with sufficient state to issue the associated |
| 226 | REST API RPC. |
| 227 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 228 | [[context_action]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 229 | === context.action |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 230 | An link:rest-api-changes.html#action-info[ActionInfo] object instance |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 231 | supplied by the server describing the UI button the user used to |
| 232 | invoke the action. |
| 233 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 234 | [[context_call]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 235 | === context.call() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 236 | Issues the REST API call associated with the action. The HTTP method |
| 237 | used comes from `context.action.method`, hiding the JavaScript from |
| 238 | needing to care. |
| 239 | |
| 240 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 241 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 242 | ---- |
| 243 | context.call(input, callback) |
| 244 | ---- |
| 245 | |
| 246 | * input: JavaScript object to serialize as the request payload. This |
| 247 | parameter is ignored for GET and DELETE methods. |
| 248 | |
| 249 | * callback: JavaScript function to be invoked with the parsed JSON |
| 250 | result of the API call. If the API returns a string the result is |
| 251 | a string, otherwise the result is a JavaScript object or array, |
| 252 | as described in the relevant REST API documentation. |
| 253 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 254 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 255 | ---- |
| 256 | context.call( |
| 257 | {message: "..."}, |
| 258 | function (result) { |
| 259 | // ... use result here ... |
| 260 | }); |
| 261 | ---- |
| 262 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 263 | [[context_change]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 264 | === context.change |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 265 | When the action is invoked on a change a |
| 266 | link:rest-api-changes.html#change-info[ChangeInfo] object instance |
| 267 | describing the change. Available fields of the ChangeInfo may vary |
| 268 | based on the options used by the UI when it loaded the change. |
| 269 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 270 | [[context_delete]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 271 | === context.delete() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 272 | Issues a DELETE REST API call to the URL associated with the action. |
| 273 | |
| 274 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 275 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 276 | ---- |
| 277 | context.delete(callback) |
| 278 | ---- |
| 279 | |
| 280 | * callback: JavaScript function to be invoked with the parsed |
| 281 | JSON result of the API call. DELETE methods often return |
| 282 | `204 No Content`, which is passed as null. |
| 283 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 284 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 285 | ---- |
| 286 | context.delete(function () {}); |
| 287 | ---- |
| 288 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 289 | [[context_get]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 290 | === context.get() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 291 | Issues a GET REST API call to the URL associated with the action. |
| 292 | |
| 293 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 294 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 295 | ---- |
| 296 | context.get(callback) |
| 297 | ---- |
| 298 | |
| 299 | * callback: JavaScript function to be invoked with the parsed JSON |
| 300 | result of the API call. If the API returns a string the result is |
| 301 | a string, otherwise the result is a JavaScript object or array, |
| 302 | as described in the relevant REST API documentation. |
| 303 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 304 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 305 | ---- |
| 306 | context.get(function (result) { |
| 307 | // ... use result here ... |
| 308 | }); |
| 309 | ---- |
| 310 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 311 | [[context_go]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 312 | === context.go() |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 313 | Go to a screen. Shorthand for link:#Gerrit_go[`Gerrit.go()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 314 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 315 | [[context_hide]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 316 | === context.hide() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 317 | Hide the currently visible popup displayed by |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 318 | link:#context_popup[`context.popup()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 319 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 320 | [[context_post]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 321 | === context.post() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 322 | Issues a POST REST API call to the URL associated with the action. |
| 323 | |
| 324 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 325 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 326 | ---- |
| 327 | context.post(input, callback) |
| 328 | ---- |
| 329 | |
| 330 | * input: JavaScript object to serialize as the request payload. |
| 331 | |
| 332 | * callback: JavaScript function to be invoked with the parsed JSON |
| 333 | result of the API call. If the API returns a string the result is |
| 334 | a string, otherwise the result is a JavaScript object or array, |
| 335 | as described in the relevant REST API documentation. |
| 336 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 337 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 338 | ---- |
| 339 | context.post( |
| 340 | {message: "..."}, |
| 341 | function (result) { |
| 342 | // ... use result here ... |
| 343 | }); |
| 344 | ---- |
| 345 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 346 | [[context_popup]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 347 | === context.popup() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 348 | |
| 349 | Displays a small popup near the activation button to gather |
| 350 | additional input from the user before executing the REST API RPC. |
| 351 | |
| 352 | The caller is always responsible for closing the popup with |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 353 | link#context_hide[`context.hide()`]. Gerrit will handle closing a |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 354 | popup if the user presses `Escape` while keyboard focus is within |
| 355 | the popup. |
| 356 | |
| 357 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 358 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 359 | ---- |
| 360 | context.popup(element) |
| 361 | ---- |
| 362 | |
| 363 | * element: an HTML DOM element to display as the body of the |
| 364 | popup. This is typically a `div` element but can be any valid HTML |
| 365 | element. CSS can be used to style the element beyond the defaults. |
| 366 | |
| 367 | A common usage is to gather more input: |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 368 | |
| 369 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 370 | ---- |
| 371 | self.onAction('revision', 'start-build', function (c) { |
| 372 | var l = c.checkbox(); |
| 373 | var m = c.checkbox(); |
| 374 | c.popup(c.div( |
| 375 | c.div(c.label(l, 'Linux')), |
| 376 | c.div(c.label(m, 'Mac OS X')), |
| 377 | c.button('Build', {onclick: function() { |
| 378 | c.call( |
| 379 | { |
| 380 | commit: c.revision.name, |
| 381 | linux: l.checked, |
| 382 | mac: m.checked, |
| 383 | }, |
| 384 | function() { c.hide() }); |
| 385 | }); |
| 386 | }); |
| 387 | ---- |
| 388 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 389 | [[context_put]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 390 | === context.put() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 391 | Issues a PUT REST API call to the URL associated with the action. |
| 392 | |
| 393 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 394 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 395 | ---- |
| 396 | context.put(input, callback) |
| 397 | ---- |
| 398 | |
| 399 | * input: JavaScript object to serialize as the request payload. |
| 400 | |
| 401 | * callback: JavaScript function to be invoked with the parsed JSON |
| 402 | result of the API call. If the API returns a string the result is |
| 403 | a string, otherwise the result is a JavaScript object or array, |
| 404 | as described in the relevant REST API documentation. |
| 405 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 406 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 407 | ---- |
| 408 | context.put( |
| 409 | {message: "..."}, |
| 410 | function (result) { |
| 411 | // ... use result here ... |
| 412 | }); |
| 413 | ---- |
| 414 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 415 | [[context_refresh]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 416 | === context.refresh() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 417 | Refresh the current display. Shorthand for |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 418 | link:#Gerrit_refresh[`Gerrit.refresh()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 419 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 420 | [[context_revision]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 421 | === context.revision |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 422 | When the action is invoked on a specific revision of a change, |
| 423 | a link:rest-api-changes.html#revision-info[RevisionInfo] |
| 424 | object instance describing the revision. Available fields of the |
| 425 | RevisionInfo may vary based on the options used by the UI when it |
| 426 | loaded the change. |
| 427 | |
David Ostrovsky | 9e8b2fb | 2013-08-30 08:09:53 +0200 | [diff] [blame] | 428 | [[context_project]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 429 | === context.project |
David Ostrovsky | 9e8b2fb | 2013-08-30 08:09:53 +0200 | [diff] [blame] | 430 | When the action is invoked on a specific project, |
| 431 | the name of the project. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 432 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 433 | === HTML Helpers |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 434 | The link:#ActionContext[action context] includes some HTML helper |
| 435 | functions to make working with DOM based widgets less painful. |
| 436 | |
| 437 | * `br()`: new `<br>` element. |
| 438 | |
| 439 | * `button(label, options)`: new `<button>` with the string `label` |
| 440 | wrapped inside of a `div`. The optional `options` object may |
| 441 | define `onclick` as a function to be invoked upon clicking. This |
| 442 | calling pattern avoids circular references between the element |
| 443 | and the onclick handler. |
| 444 | |
| 445 | * `checkbox()`: new `<input type='checkbox'>` element. |
| 446 | * `div(...)`: a new `<div>` wrapping the (optional) arguments. |
| 447 | * `hr()`: new `<hr>` element. |
| 448 | |
| 449 | * `label(c, label)`: a new `<label>` element wrapping element `c` |
| 450 | and the string `label`. Used to wrap a checkbox with its label, |
| 451 | `label(checkbox(), 'Click Me')`. |
| 452 | |
David Ostrovsky | a1067a3 | 2013-08-19 09:09:17 +0200 | [diff] [blame] | 453 | * `prependLabel(label, c)`: a new `<label>` element wrapping element `c` |
| 454 | and the string `label`. Used to wrap an input field with its label, |
| 455 | `prependLabel('Greeting message', textfield())`. |
| 456 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 457 | * `textarea(options)`: new `<textarea>` element. The options |
| 458 | object may optionally include `rows` and `cols`. The textarea |
| 459 | comes with an onkeypress handler installed to play nicely with |
| 460 | Gerrit's keyboard binding system. |
| 461 | |
| 462 | * `textfield()`: new `<input type='text'>` element. The text field |
| 463 | comes with an onkeypress handler installed to play nicely with |
| 464 | Gerrit's keyboard binding system. |
| 465 | |
Edwin Kempin | 15ce80c | 2013-11-14 17:16:24 +0100 | [diff] [blame] | 466 | * `select(a,i)`: a new `<select>` element containing one `<option>` |
| 467 | element for each entry in the provided array `a`. The option with |
| 468 | the index `i` will be pre-selected in the drop-down-list. |
| 469 | |
| 470 | * `selected(s)`: returns the text of the `<option>` element that is |
| 471 | currently selected in the provided `<select>` element `s`. |
| 472 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 473 | * `span(...)`: a new `<span>` wrapping the (optional) arguments. |
| 474 | |
David Ostrovsky | 7dfc802 | 2013-08-25 23:05:50 +0200 | [diff] [blame] | 475 | * `msg(label)`: a new label. |
| 476 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 477 | |
| 478 | [[ScreenContext]] |
| 479 | == Screen Context |
| 480 | A new screen context is passed to the `screen` callback function |
| 481 | each time the user navigates to a matching URL. |
| 482 | |
| 483 | [[screen_body]] |
| 484 | === screen.body |
| 485 | Empty HTML `<div>` node the plugin should add its content to. The |
| 486 | node is already attached to the document, but is invisible. Plugins |
| 487 | must call `screen.show()` to display the DOM node. Deferred display |
| 488 | allows an implementor to partially populate the DOM, make remote HTTP |
| 489 | requests, finish populating when the callbacks arrive, and only then |
| 490 | make the view visible to the user. |
| 491 | |
| 492 | [[screen_token]] |
| 493 | === screen.token |
| 494 | URL token fragment that activated this screen. The value is identical |
| 495 | to `screen.token_match[0]`. If the URL is `/#/x/hello/list` the token |
| 496 | will be `"list"`. |
| 497 | |
| 498 | [[screen_token_match]] |
| 499 | === screen.token_match |
| 500 | Array of matching subgroups from the pattern specified to `screen()`. |
| 501 | This is identical to the result of RegExp.exec. Index 0 contains the |
| 502 | entire matching expression; index 1 the first matching group, etc. |
| 503 | |
| 504 | [[screen_onUnload]] |
| 505 | === screen.onUnload() |
| 506 | Configures an optional callback to be invoked just before the screen |
| 507 | is deleted from the browser DOM. Plugins can use this callback to |
| 508 | remove event listeners from DOM nodes, preventing memory leaks. |
| 509 | |
| 510 | .Signature |
| 511 | [source,javascript] |
| 512 | ---- |
| 513 | screen.onUnload(callback) |
| 514 | ---- |
| 515 | |
| 516 | * callback: JavaScript function to be invoked just before the |
| 517 | `screen.body` DOM element is removed from the browser DOM. |
| 518 | This event happens when the user navigates to another screen. |
| 519 | |
| 520 | [[screen.setTitle]] |
| 521 | === screen.setTitle() |
| 522 | Sets the heading text to be displayed when the screen is visible. |
| 523 | This is presented in a large bold font below the menus, but above the |
| 524 | content in `screen.body`. Setting the title also sets the window |
| 525 | title to the same string, if it has not already been set. |
| 526 | |
| 527 | .Signature |
| 528 | [source,javascript] |
| 529 | ---- |
| 530 | screen.setPageTitle(titleText) |
| 531 | ---- |
| 532 | |
| 533 | [[screen.setWindowTitle]] |
| 534 | === screen.setWindowTitle() |
| 535 | Sets the text to be displayed in the browser's title bar when the |
| 536 | screen is visible. Plugins should always prefer this method over |
| 537 | trying to set `window.title` directly. The window title defaults to |
| 538 | the title given to `setTitle`. |
| 539 | |
| 540 | .Signature |
| 541 | [source,javascript] |
| 542 | ---- |
| 543 | screen.setWindowTitle(titleText) |
| 544 | ---- |
| 545 | |
| 546 | [[screen_show]] |
| 547 | === screen.show() |
| 548 | Destroy the currently visible screen and display the plugin's screen. |
| 549 | This method must be called after adding content to `screen.body`. |
| 550 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 551 | [[Gerrit]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 552 | == Gerrit |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 553 | |
| 554 | The `Gerrit` object is the only symbol provided into the global |
| 555 | namespace by Gerrit Code Review. All top-level functions can be |
| 556 | accessed through this name. |
| 557 | |
Shawn Pearce | 210b539 | 2013-12-07 22:41:36 -0800 | [diff] [blame] | 558 | [[Gerrit_css]] |
| 559 | Gerrit.css() |
| 560 | ~~~~~~~~~~~~ |
| 561 | Creates a new unique CSS class and injects it into the document. |
| 562 | The name of the class is returned and can be used by the plugin. |
| 563 | See link:#Gerrit_html[`Gerrit.html()`] for an easy way to use |
| 564 | generated class names. |
| 565 | |
| 566 | Classes created with this function should be created once at install |
| 567 | time and reused throughout the plugin. Repeatedly creating the same |
| 568 | class will explode the global stylesheet. |
| 569 | |
| 570 | .Signature |
| 571 | [source,javascript] |
| 572 | ---- |
| 573 | Gerrit.install(function(self)) { |
| 574 | var style = { |
| 575 | name: Gerrit.css('background: #fff; color: #000;'), |
| 576 | }; |
| 577 | }); |
| 578 | ---- |
| 579 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 580 | [[Gerrit_delete]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 581 | === Gerrit.delete() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 582 | Issues a DELETE REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 583 | private REST API URLs see link:#self_delete[self.delete()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 584 | |
| 585 | .Signature |
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.delete(url, callback) |
| 589 | ---- |
| 590 | |
| 591 | * url: URL relative to the Gerrit server. For example to access the |
| 592 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 593 | |
| 594 | * callback: JavaScript function to be invoked with the parsed |
| 595 | JSON result of the API call. DELETE methods often return |
| 596 | `204 No Content`, which is passed as null. |
| 597 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 598 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 599 | ---- |
| 600 | Gerrit.delete( |
| 601 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 602 | function () {}); |
| 603 | ---- |
| 604 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 605 | [[Gerrit_get]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 606 | === Gerrit.get() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 607 | Issues a GET REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 608 | private REST API URLs see link:#self_get[self.get()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 609 | |
| 610 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 611 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 612 | ---- |
| 613 | Gerrit.get(url, callback) |
| 614 | ---- |
| 615 | |
| 616 | * url: URL relative to the Gerrit server. For example to access the |
| 617 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 618 | |
| 619 | * callback: JavaScript function to be invoked with the parsed JSON |
| 620 | result of the API call. If the API returns a string the result is |
| 621 | a string, otherwise the result is a JavaScript object or array, |
| 622 | as described in the relevant REST API documentation. |
| 623 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 624 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 625 | ---- |
| 626 | Gerrit.get('/changes/?q=status:open', function (open) { |
| 627 | for (var i = 0; i < open.length; i++) { |
| 628 | console.log(open.get(i).change_id); |
| 629 | } |
| 630 | }); |
| 631 | ---- |
| 632 | |
David Ostrovsky | 0a4c0ff | 2014-09-21 23:06:52 +0200 | [diff] [blame] | 633 | [[Gerrit_getCurrentUser]] |
| 634 | === Gerrit.getCurrentUser() |
| 635 | Returns the currently signed in user's AccountInfo data; empty account |
| 636 | data if no user is currently signed in. |
| 637 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 638 | [[Gerrit_getPluginName]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 639 | === Gerrit.getPluginName() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 640 | Returns the name this plugin was installed as by the server |
| 641 | administrator. The plugin name is required to access REST API |
| 642 | views installed by the plugin, or to access resources. |
| 643 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 644 | Unlike link:#self_getPluginName[`self.getPluginName()`] this method |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 645 | must guess the name from the JavaScript call stack. Plugins are |
| 646 | encouraged to use `self.getPluginName()` whenever possible. |
| 647 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 648 | [[Gerrit_go]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 649 | === Gerrit.go() |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 650 | Updates the web UI to display the screen identified by the supplied |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 651 | URL token. The URL token is the text after `#` in the browser URL. |
| 652 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 653 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 654 | ---- |
| 655 | Gerrit.go('/admin/projects/'); |
| 656 | ---- |
| 657 | |
| 658 | If the URL passed matches `http://...`, `https://...`, or `//...` |
| 659 | the current browser window will navigate to the non-Gerrit URL. |
| 660 | The user can return to Gerrit with the back button. |
| 661 | |
Shawn Pearce | 210b539 | 2013-12-07 22:41:36 -0800 | [diff] [blame] | 662 | [[Gerrit_html]] |
| 663 | Gerrit.html() |
| 664 | ~~~~~~~~~~~~~ |
| 665 | Parses an HTML fragment after performing template replacements. If |
| 666 | the HTML has a single root element or node that node is returned, |
| 667 | otherwise it is wrapped inside a `<div>` and the div is returned. |
| 668 | |
| 669 | .Signature |
| 670 | [source,javascript] |
| 671 | ---- |
| 672 | Gerrit.html(htmlText, options, wantElements); |
| 673 | ---- |
| 674 | |
| 675 | * htmlText: string of HTML to be parsed. A new unattached `<div>` is |
| 676 | created in the browser's document and the innerHTML property is |
| 677 | assigned to the passed string, after performing replacements. If |
| 678 | the div has exactly one child, that child will be returned instead |
| 679 | of the div. |
| 680 | |
| 681 | * options: optional object reference supplying replacements for any |
| 682 | `{name}` references in htmlText. Navigation through objects is |
| 683 | supported permitting `{style.bar}` to be replaced with `"foo"` if |
| 684 | options was `{style: {bar: "foo"}}`. Value replacements are HTML |
| 685 | escaped before being inserted into the document fragment. |
| 686 | |
| 687 | * wantElements: if options is given and wantElements is also true |
| 688 | an object consisting of `{root: parsedElement, elements: {...}}` is |
| 689 | returned instead of the parsed element. The elements object contains |
| 690 | a property for each element using `id={name}` in htmlText. |
| 691 | |
| 692 | .Example |
| 693 | [source,javascript] |
| 694 | ---- |
| 695 | var style = {bar: Gerrit.css('background: yellow')}; |
| 696 | Gerrit.html( |
| 697 | '<span class="{style.bar}">Hello {name}!</span>', |
| 698 | {style: style, name: "World"}); |
| 699 | ---- |
| 700 | |
| 701 | Event handlers can be automatically attached to elements referenced |
| 702 | through an attribute id. Object navigation is not supported for ids, |
| 703 | and the parser strips the id attribute before returning the result. |
| 704 | Handler functions must begin with `on` and be a function to be |
| 705 | installed on the element. This approach is useful for onclick and |
| 706 | other handlers that do not want to create circular references that |
| 707 | will eventually leak browser memory. |
| 708 | |
| 709 | .Example |
| 710 | [source,javascript] |
| 711 | ---- |
| 712 | var options = { |
| 713 | link: { |
| 714 | onclick: function(e) { window.close() }, |
| 715 | }, |
| 716 | }; |
| 717 | Gerrit.html('<a href="javascript:;" id="{link}">Close</a>', options); |
| 718 | ---- |
| 719 | |
| 720 | When using options to install handlers care must be taken to not |
| 721 | accidentally include the returned element into the event handler's |
| 722 | closure. This is why options is built before calling `Gerrit.html()` |
| 723 | and not inline as a shown above with "Hello World". |
| 724 | |
| 725 | DOM nodes can optionally be returned, allowing handlers to access the |
| 726 | elements identified by `id={name}` at a later point in time. |
| 727 | |
| 728 | .Example |
| 729 | [source,javascript] |
| 730 | ---- |
| 731 | var w = Gerrit.html( |
| 732 | '<div>Name: <input type="text" id="{name}"></div>' |
| 733 | + '<div>Age: <input type="text" id="{age}"></div>' |
| 734 | + '<button id="{submit}"><div>Save</div></button>', |
| 735 | { |
| 736 | submit: { |
| 737 | onclick: function(s) { |
| 738 | var e = w.elements; |
| 739 | window.alert(e.name.value + " is " + e.age.value); |
| 740 | }, |
| 741 | }, |
| 742 | }, true); |
| 743 | ---- |
| 744 | |
| 745 | To prevent memory leaks `w.root` and `w.elements` should be set to |
| 746 | null when the elements are no longer necessary. Screens can use |
| 747 | link:#screen_onUnload[screen.onUnload()] to define a callback function |
| 748 | to perform this cleanup: |
| 749 | |
| 750 | [source,javascript] |
| 751 | ---- |
| 752 | var w = Gerrit.html(...); |
| 753 | screen.body.appendElement(w.root); |
| 754 | screen.onUnload(function() { w.clear() }); |
| 755 | ---- |
| 756 | |
| 757 | [[Gerrit_injectCss]] |
| 758 | Gerrit.injectCss() |
| 759 | ~~~~~~~~~~~~~~~~~~ |
| 760 | Injects CSS rules into the document by appending onto the end of the |
| 761 | existing rule list. CSS rules are global to the entire application |
| 762 | and must be manually scoped by each plugin. For an automatic scoping |
| 763 | alternative see link:#Gerrit_css[`css()`]. |
| 764 | |
| 765 | [source,javascript] |
| 766 | ---- |
| 767 | Gerrit.injectCss('.myplugin_bg {background: #000}'); |
| 768 | ---- |
| 769 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 770 | [[Gerrit_install]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 771 | === Gerrit.install() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 772 | Registers a new plugin by invoking the supplied initialization |
| 773 | function. The function is passed the link:#self[plugin instance]. |
| 774 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 775 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 776 | ---- |
| 777 | Gerrit.install(function (self) { |
| 778 | // ... plugin JavaScript code here ... |
| 779 | }); |
| 780 | ---- |
| 781 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 782 | [[Gerrit_post]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 783 | === Gerrit.post() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 784 | Issues a POST REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 785 | private REST API URLs see link:#self_post[self.post()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 786 | |
| 787 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 788 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 789 | ---- |
| 790 | Gerrit.post(url, input, callback) |
| 791 | ---- |
| 792 | |
| 793 | * url: URL relative to the Gerrit server. For example to access the |
| 794 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 795 | |
| 796 | * input: JavaScript object to serialize as the request payload. |
| 797 | |
| 798 | * callback: JavaScript function to be invoked with the parsed JSON |
| 799 | result of the API call. If the API returns a string the result is |
| 800 | a string, otherwise the result is a JavaScript object or array, |
| 801 | as described in the relevant REST API documentation. |
| 802 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 803 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 804 | ---- |
| 805 | Gerrit.post( |
| 806 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 807 | {topic: 'tests', message: 'Classify work as for testing.'}, |
| 808 | function (r) {}); |
| 809 | ---- |
| 810 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 811 | [[Gerrit_put]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 812 | === Gerrit.put() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 813 | Issues a PUT REST API request to the Gerrit server. For plugin |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 814 | private REST API URLs see link:#self_put[self.put()]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 815 | |
| 816 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 817 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 818 | ---- |
| 819 | Gerrit.put(url, input, callback) |
| 820 | ---- |
| 821 | |
| 822 | * url: URL relative to the Gerrit server. For example to access the |
| 823 | link:rest-api-changes.html[changes REST API] use `'/changes/'`. |
| 824 | |
| 825 | * input: JavaScript object to serialize as the request payload. |
| 826 | |
| 827 | * callback: JavaScript function to be invoked with the parsed JSON |
| 828 | result of the API call. If the API returns a string the result is |
| 829 | a string, otherwise the result is a JavaScript object or array, |
| 830 | as described in the relevant REST API documentation. |
| 831 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 832 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 833 | ---- |
| 834 | Gerrit.put( |
| 835 | '/changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/topic', |
| 836 | {topic: 'tests', message: 'Classify work as for testing.'}, |
| 837 | function (r) {}); |
| 838 | ---- |
| 839 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 840 | [[Gerrit_onAction]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 841 | === Gerrit.onAction() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 842 | Register a JavaScript callback to be invoked when the user clicks |
| 843 | on a button associated with a server side `UiAction`. |
| 844 | |
| 845 | .Signature |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 846 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 847 | ---- |
| 848 | Gerrit.onAction(type, view_name, callback); |
| 849 | ---- |
| 850 | |
David Ostrovsky | cdda331 | 2014-08-03 14:03:13 +0200 | [diff] [blame] | 851 | * type: `'change'`, `'edit'`, `'revision'`, `'project'` or `'branch'` |
| 852 | 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] | 853 | |
| 854 | * view_name: string appearing in URLs to name the view. This is the |
| 855 | second argument of the `get()`, `post()`, `put()`, and `delete()` |
| 856 | binding methods in a `RestApiModule`. |
| 857 | |
| 858 | * callback: JavaScript function to invoke when the user clicks. The |
| 859 | function will be passed a link:#ActionContext[ActionContext]. |
| 860 | |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 861 | [[Gerrit_screen]] |
| 862 | === Gerrit.screen() |
| 863 | Register a JavaScript callback to be invoked when the user navigates |
| 864 | to an extension screen provided by the plugin. Extension screens are |
| 865 | usually linked from the link:dev-plugins.html#top-menu-extensions[top menu]. |
| 866 | The callback can populate the DOM with the screen's contents. |
| 867 | |
| 868 | .Signature |
| 869 | [source,javascript] |
| 870 | ---- |
| 871 | Gerrit.screen(pattern, callback); |
| 872 | ---- |
| 873 | |
| 874 | * pattern: URL token pattern to identify the screen. Argument can be |
| 875 | either a string (`'index'`) or a RegExp object (`/list\/(.*)/`). |
| 876 | If a RegExp is used the matching groups will be available inside of |
| 877 | the context as `token_match`. |
| 878 | |
| 879 | * callback: JavaScript function to invoke when the user navigates to |
| 880 | the screen. The function will be passed link:#ScreenContext[screen context]. |
| 881 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 882 | [[Gerrit_refresh]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 883 | === Gerrit.refresh() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 884 | Redisplays the current web UI view, refreshing all information. |
| 885 | |
Edwin Kempin | 049c355 | 2014-04-15 11:26:23 +0200 | [diff] [blame] | 886 | [[Gerrit_refreshMenuBar]] |
| 887 | === Gerrit.refreshMenuBar() |
| 888 | Refreshes Gerrit's menu bar. |
| 889 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 890 | [[Gerrit_url]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 891 | === Gerrit.url() |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 892 | Returns the URL of the Gerrit Code Review server. If invoked with |
| 893 | no parameter the URL of the site is returned. If passed a string |
| 894 | the argument is appended to the site URL. |
| 895 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 896 | [source,javascript] |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 897 | ---- |
| 898 | Gerrit.url(); // "https://gerrit-review.googlesource.com/" |
| 899 | Gerrit.url('/123'); // "https://gerrit-review.googlesource.com/123" |
| 900 | ---- |
| 901 | |
David Pursehouse | 8d0b520 | 2013-08-01 14:13:17 +0900 | [diff] [blame] | 902 | For a plugin specific version see link:#self_url()[`self.url()`]. |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 903 | |
Edwin Kempin | 792c8d8 | 2014-01-31 16:16:52 +0100 | [diff] [blame] | 904 | [[Gerrit_showError]] |
| 905 | === Gerrit.showError(message) |
| 906 | Displays the given message in the Gerrit ErrorDialog. |
| 907 | |
Shawn Pearce | 92a1fa8 | 2013-07-16 15:01:40 -0700 | [diff] [blame] | 908 | GERRIT |
| 909 | ------ |
| 910 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 911 | |
| 912 | SEARCHBOX |
| 913 | --------- |