Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 17 | (function() { |
| 18 | 'use strict'; |
| 19 | |
Viktar Donich | 5b9f7bc | 2018-02-14 12:00:41 -0800 | [diff] [blame] | 20 | const CHANGE_SIZE = { |
| 21 | XS: 10, |
| 22 | SMALL: 50, |
| 23 | MEDIUM: 250, |
| 24 | LARGE: 1000, |
| 25 | }; |
| 26 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 27 | Polymer({ |
| 28 | is: 'gr-change-list-item', |
| 29 | |
| 30 | properties: { |
Becky Siegel | 1a44b34 | 2016-11-14 08:37:06 -0800 | [diff] [blame] | 31 | visibleChangeTableColumns: Array, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 32 | labelNames: { |
| 33 | type: Array, |
| 34 | }, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 35 | |
| 36 | /** @type {?} */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 37 | change: Object, |
| 38 | changeURL: { |
| 39 | type: String, |
Wyatt Allen | db6afdc | 2017-08-15 15:03:51 -0700 | [diff] [blame] | 40 | computed: '_computeChangeURL(change)', |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 41 | }, |
Kasper Nilsson | e15afc9 | 2018-02-27 14:22:21 -0800 | [diff] [blame] | 42 | statuses: { |
| 43 | type: Array, |
| 44 | computed: 'changeStatuses(change)', |
Kasper Nilsson | 114c776 | 2018-02-20 14:22:11 -0800 | [diff] [blame] | 45 | }, |
Dave Borowitz | 4b5f6c7 | 2017-10-26 15:02:48 +0000 | [diff] [blame] | 46 | showStar: { |
| 47 | type: Boolean, |
| 48 | value: false, |
| 49 | }, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 50 | showNumber: Boolean, |
Kasper Nilsson | ffc011c | 2018-03-07 16:29:27 -0800 | [diff] [blame] | 51 | _changeSize: { |
| 52 | type: String, |
| 53 | computed: '_computeChangeSize(change)', |
| 54 | }, |
Thomas Shafer | 0327586 | 2019-02-26 15:39:16 -0800 | [diff] [blame] | 55 | _dynamicCellEndpoints: { |
| 56 | type: Array, |
| 57 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 58 | }, |
| 59 | |
| 60 | behaviors: [ |
Paladox none | 8b0d046 | 2017-03-31 14:37:00 +0000 | [diff] [blame] | 61 | Gerrit.BaseUrlBehavior, |
Becky Siegel | 1a44b34 | 2016-11-14 08:37:06 -0800 | [diff] [blame] | 62 | Gerrit.ChangeTableBehavior, |
Kasper Nilsson | e420236 | 2018-02-15 13:02:34 -0800 | [diff] [blame] | 63 | Gerrit.PathListBehavior, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 64 | Gerrit.RESTClientBehavior, |
Kasper Nilsson | f99ce6e | 2016-09-23 15:14:59 -0700 | [diff] [blame] | 65 | Gerrit.URLEncodingBehavior, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 66 | ], |
| 67 | |
Thomas Shafer | 0327586 | 2019-02-26 15:39:16 -0800 | [diff] [blame] | 68 | attached() { |
| 69 | Gerrit.awaitPluginsLoaded().then(() => { |
| 70 | this._dynamicCellEndpoints = Gerrit._endpoints.getDynamicEndpoints( |
| 71 | 'change-list-item-cell'); |
| 72 | }); |
| 73 | }, |
| 74 | |
Wyatt Allen | db6afdc | 2017-08-15 15:03:51 -0700 | [diff] [blame] | 75 | _computeChangeURL(change) { |
| 76 | return Gerrit.Nav.getUrlForChange(change); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 77 | }, |
| 78 | |
Kasper Nilsson | 178d4f8 | 2017-05-14 17:47:02 -0700 | [diff] [blame] | 79 | _computeLabelTitle(change, labelName) { |
| 80 | const label = change.labels[labelName]; |
Urs Wolfer | de6620f | 2016-03-20 18:23:43 +0100 | [diff] [blame] | 81 | if (!label) { return 'Label not applicable'; } |
Kasper Nilsson | 178d4f8 | 2017-05-14 17:47:02 -0700 | [diff] [blame] | 82 | const significantLabel = label.rejected || label.approved || |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 83 | label.disliked || label.recommended; |
| 84 | if (significantLabel && significantLabel.name) { |
| 85 | return labelName + '\nby ' + significantLabel.name; |
| 86 | } |
| 87 | return labelName; |
| 88 | }, |
| 89 | |
Kasper Nilsson | 178d4f8 | 2017-05-14 17:47:02 -0700 | [diff] [blame] | 90 | _computeLabelClass(change, labelName) { |
| 91 | const label = change.labels[labelName]; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 92 | // Mimic a Set. |
Kasper Nilsson | 178d4f8 | 2017-05-14 17:47:02 -0700 | [diff] [blame] | 93 | const classes = { |
| 94 | cell: true, |
| 95 | label: true, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 96 | }; |
| 97 | if (label) { |
| 98 | if (label.approved) { |
| 99 | classes['u-green'] = true; |
| 100 | } |
| 101 | if (label.value == 1) { |
| 102 | classes['u-monospace'] = true; |
| 103 | classes['u-green'] = true; |
| 104 | } else if (label.value == -1) { |
| 105 | classes['u-monospace'] = true; |
| 106 | classes['u-red'] = true; |
| 107 | } |
| 108 | if (label.rejected) { |
| 109 | classes['u-red'] = true; |
| 110 | } |
Urs Wolfer | de6620f | 2016-03-20 18:23:43 +0100 | [diff] [blame] | 111 | } else { |
| 112 | classes['u-gray-background'] = true; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 113 | } |
| 114 | return Object.keys(classes).sort().join(' '); |
| 115 | }, |
| 116 | |
Kasper Nilsson | 178d4f8 | 2017-05-14 17:47:02 -0700 | [diff] [blame] | 117 | _computeLabelValue(change, labelName) { |
| 118 | const label = change.labels[labelName]; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 119 | if (!label) { return ''; } |
| 120 | if (label.approved) { |
| 121 | return '✓'; |
| 122 | } |
| 123 | if (label.rejected) { |
| 124 | return '✕'; |
| 125 | } |
| 126 | if (label.value > 0) { |
| 127 | return '+' + label.value; |
| 128 | } |
| 129 | if (label.value < 0) { |
| 130 | return label.value; |
| 131 | } |
| 132 | return ''; |
| 133 | }, |
| 134 | |
Wyatt Allen | 06a7d0e | 2018-07-26 12:02:41 -0700 | [diff] [blame] | 135 | _computeRepoUrl(change) { |
Kasper Nilsson | 92058de | 2018-07-10 11:15:07 -0700 | [diff] [blame] | 136 | return Gerrit.Nav.getUrlForProjectChanges(change.project, true, |
| 137 | change.internalHost); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 138 | }, |
| 139 | |
Wyatt Allen | 06a7d0e | 2018-07-26 12:02:41 -0700 | [diff] [blame] | 140 | _computeRepoBranchURL(change) { |
Kasper Nilsson | 92058de | 2018-07-10 11:15:07 -0700 | [diff] [blame] | 141 | return Gerrit.Nav.getUrlForBranch(change.branch, change.project, null, |
| 142 | change.internalHost); |
Kasper Nilsson | dee79f7 | 2017-06-09 17:08:11 -0700 | [diff] [blame] | 143 | }, |
| 144 | |
Wyatt Allen | db6afdc | 2017-08-15 15:03:51 -0700 | [diff] [blame] | 145 | _computeTopicURL(change) { |
| 146 | if (!change.topic) { return ''; } |
Kasper Nilsson | 92058de | 2018-07-10 11:15:07 -0700 | [diff] [blame] | 147 | return Gerrit.Nav.getUrlForTopic(change.topic, change.internalHost); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 148 | }, |
Kasper Nilsson | e420236 | 2018-02-15 13:02:34 -0800 | [diff] [blame] | 149 | |
Kasper Nilsson | 6f898a7 | 2018-07-11 11:26:41 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Computes the display string for the project column. If there is a host |
| 152 | * specified in the change detail, the string will be prefixed with it. |
| 153 | * |
| 154 | * @param {!Object} change |
| 155 | * @param {string=} truncate whether or not the project name should be |
| 156 | * truncated. If this value is truthy, the name will be truncated. |
| 157 | * @return {string} |
| 158 | */ |
Wyatt Allen | 06a7d0e | 2018-07-26 12:02:41 -0700 | [diff] [blame] | 159 | _computeRepoDisplay(change, truncate) { |
Kasper Nilsson | 6f898a7 | 2018-07-11 11:26:41 -0700 | [diff] [blame] | 160 | if (!change || !change.project) { return ''; } |
| 161 | let str = ''; |
| 162 | if (change.internalHost) { str += change.internalHost + '/'; } |
| 163 | str += truncate ? this.truncatePath(change.project, 2) : change.project; |
| 164 | return str; |
Kasper Nilsson | e420236 | 2018-02-15 13:02:34 -0800 | [diff] [blame] | 165 | }, |
Kasper Nilsson | 33e3855 | 2018-02-23 16:39:10 -0800 | [diff] [blame] | 166 | |
Viktar Donich | 5b9f7bc | 2018-02-14 12:00:41 -0800 | [diff] [blame] | 167 | _computeSizeTooltip(change) { |
| 168 | if (change.insertions + change.deletions === 0 || |
| 169 | isNaN(change.insertions + change.deletions)) { |
| 170 | return 'Size unknown'; |
| 171 | } else { |
| 172 | return `+${change.insertions}, -${change.deletions}`; |
| 173 | } |
| 174 | }, |
| 175 | |
| 176 | /** |
| 177 | * TShirt sizing is based on the following paper: |
| 178 | * http://dirkriehle.com/wp-content/uploads/2008/09/hicss-42-csdistr-final-web.pdf |
| 179 | */ |
| 180 | _computeChangeSize(change) { |
| 181 | const delta = change.insertions + change.deletions; |
| 182 | if (isNaN(delta) || delta === 0) { |
Kasper Nilsson | ffc011c | 2018-03-07 16:29:27 -0800 | [diff] [blame] | 183 | return null; // Unknown |
Viktar Donich | 5b9f7bc | 2018-02-14 12:00:41 -0800 | [diff] [blame] | 184 | } |
| 185 | if (delta < CHANGE_SIZE.XS) { |
| 186 | return 'XS'; |
| 187 | } else if (delta < CHANGE_SIZE.SMALL) { |
| 188 | return 'S'; |
| 189 | } else if (delta < CHANGE_SIZE.MEDIUM) { |
| 190 | return 'M'; |
| 191 | } else if (delta < CHANGE_SIZE.LARGE) { |
| 192 | return 'L'; |
| 193 | } else { |
| 194 | return 'XL'; |
| 195 | } |
| 196 | }, |
Sean Egan | 3278d7b | 2018-07-06 11:02:31 -0400 | [diff] [blame] | 197 | |
| 198 | toggleReviewed() { |
| 199 | const newVal = !this.change.reviewed; |
| 200 | this.set('change.reviewed', newVal); |
| 201 | this.dispatchEvent(new CustomEvent('toggle-reviewed', { |
| 202 | bubbles: true, |
Ole Rehmsen | c82baba | 2019-05-16 14:43:01 +0200 | [diff] [blame] | 203 | composed: true, |
Sean Egan | 3278d7b | 2018-07-06 11:02:31 -0400 | [diff] [blame] | 204 | detail: {change: this.change, reviewed: newVal}, |
| 205 | })); |
| 206 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 207 | }); |
| 208 | })(); |