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