blob: 8eb3989114ddaddcd8db043f7cd9a45da680abdf [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
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 Bonventre78792e82016-03-04 17:48:22 -050017(function() {
18 'use strict';
19
Viktar Donich5b9f7bc2018-02-14 12:00:41 -080020 const CHANGE_SIZE = {
21 XS: 10,
22 SMALL: 50,
23 MEDIUM: 250,
24 LARGE: 1000,
25 };
26
Andrew Bonventre78792e82016-03-04 17:48:22 -050027 Polymer({
28 is: 'gr-change-list-item',
29
30 properties: {
Becky Siegel1a44b342016-11-14 08:37:06 -080031 visibleChangeTableColumns: Array,
Andrew Bonventre78792e82016-03-04 17:48:22 -050032 labelNames: {
33 type: Array,
34 },
Becky Siegel8d92d532017-08-11 16:32:47 -070035
36 /** @type {?} */
Andrew Bonventre78792e82016-03-04 17:48:22 -050037 change: Object,
38 changeURL: {
39 type: String,
Wyatt Allendb6afdc2017-08-15 15:03:51 -070040 computed: '_computeChangeURL(change)',
Andrew Bonventre78792e82016-03-04 17:48:22 -050041 },
Kasper Nilssone15afc92018-02-27 14:22:21 -080042 statuses: {
43 type: Array,
44 computed: 'changeStatuses(change)',
Kasper Nilsson114c7762018-02-20 14:22:11 -080045 },
Dave Borowitz4b5f6c72017-10-26 15:02:48 +000046 showStar: {
47 type: Boolean,
48 value: false,
49 },
Becky Siegel8d92d532017-08-11 16:32:47 -070050 showNumber: Boolean,
Kasper Nilssonffc011c2018-03-07 16:29:27 -080051 _changeSize: {
52 type: String,
53 computed: '_computeChangeSize(change)',
54 },
Thomas Shafer03275862019-02-26 15:39:16 -080055 _dynamicCellEndpoints: {
56 type: Array,
57 },
Andrew Bonventre78792e82016-03-04 17:48:22 -050058 },
59
60 behaviors: [
Paladox none8b0d0462017-03-31 14:37:00 +000061 Gerrit.BaseUrlBehavior,
Becky Siegel1a44b342016-11-14 08:37:06 -080062 Gerrit.ChangeTableBehavior,
Kasper Nilssone4202362018-02-15 13:02:34 -080063 Gerrit.PathListBehavior,
Andrew Bonventre78792e82016-03-04 17:48:22 -050064 Gerrit.RESTClientBehavior,
Kasper Nilssonf99ce6e2016-09-23 15:14:59 -070065 Gerrit.URLEncodingBehavior,
Andrew Bonventre78792e82016-03-04 17:48:22 -050066 ],
67
Thomas Shafer03275862019-02-26 15:39:16 -080068 attached() {
69 Gerrit.awaitPluginsLoaded().then(() => {
70 this._dynamicCellEndpoints = Gerrit._endpoints.getDynamicEndpoints(
71 'change-list-item-cell');
72 });
73 },
74
Wyatt Allendb6afdc2017-08-15 15:03:51 -070075 _computeChangeURL(change) {
76 return Gerrit.Nav.getUrlForChange(change);
Andrew Bonventre78792e82016-03-04 17:48:22 -050077 },
78
Kasper Nilsson178d4f82017-05-14 17:47:02 -070079 _computeLabelTitle(change, labelName) {
80 const label = change.labels[labelName];
Urs Wolferde6620f2016-03-20 18:23:43 +010081 if (!label) { return 'Label not applicable'; }
Kasper Nilsson178d4f82017-05-14 17:47:02 -070082 const significantLabel = label.rejected || label.approved ||
Andrew Bonventre78792e82016-03-04 17:48:22 -050083 label.disliked || label.recommended;
84 if (significantLabel && significantLabel.name) {
85 return labelName + '\nby ' + significantLabel.name;
86 }
87 return labelName;
88 },
89
Kasper Nilsson178d4f82017-05-14 17:47:02 -070090 _computeLabelClass(change, labelName) {
91 const label = change.labels[labelName];
Andrew Bonventre78792e82016-03-04 17:48:22 -050092 // Mimic a Set.
Kasper Nilsson178d4f82017-05-14 17:47:02 -070093 const classes = {
94 cell: true,
95 label: true,
Andrew Bonventre78792e82016-03-04 17:48:22 -050096 };
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 Wolferde6620f2016-03-20 18:23:43 +0100111 } else {
112 classes['u-gray-background'] = true;
Andrew Bonventre78792e82016-03-04 17:48:22 -0500113 }
114 return Object.keys(classes).sort().join(' ');
115 },
116
Kasper Nilsson178d4f82017-05-14 17:47:02 -0700117 _computeLabelValue(change, labelName) {
118 const label = change.labels[labelName];
Andrew Bonventre78792e82016-03-04 17:48:22 -0500119 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 Allen06a7d0e2018-07-26 12:02:41 -0700135 _computeRepoUrl(change) {
Kasper Nilsson92058de2018-07-10 11:15:07 -0700136 return Gerrit.Nav.getUrlForProjectChanges(change.project, true,
137 change.internalHost);
Andrew Bonventre78792e82016-03-04 17:48:22 -0500138 },
139
Wyatt Allen06a7d0e2018-07-26 12:02:41 -0700140 _computeRepoBranchURL(change) {
Kasper Nilsson92058de2018-07-10 11:15:07 -0700141 return Gerrit.Nav.getUrlForBranch(change.branch, change.project, null,
142 change.internalHost);
Kasper Nilssondee79f72017-06-09 17:08:11 -0700143 },
144
Wyatt Allendb6afdc2017-08-15 15:03:51 -0700145 _computeTopicURL(change) {
146 if (!change.topic) { return ''; }
Kasper Nilsson92058de2018-07-10 11:15:07 -0700147 return Gerrit.Nav.getUrlForTopic(change.topic, change.internalHost);
Andrew Bonventre78792e82016-03-04 17:48:22 -0500148 },
Kasper Nilssone4202362018-02-15 13:02:34 -0800149
Kasper Nilsson6f898a72018-07-11 11:26:41 -0700150 /**
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 Allen06a7d0e2018-07-26 12:02:41 -0700159 _computeRepoDisplay(change, truncate) {
Kasper Nilsson6f898a72018-07-11 11:26:41 -0700160 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 Nilssone4202362018-02-15 13:02:34 -0800165 },
Kasper Nilsson33e38552018-02-23 16:39:10 -0800166
Viktar Donich5b9f7bc2018-02-14 12:00:41 -0800167 _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 Nilssonffc011c2018-03-07 16:29:27 -0800183 return null; // Unknown
Viktar Donich5b9f7bc2018-02-14 12:00:41 -0800184 }
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 Egan3278d7b2018-07-06 11:02:31 -0400197
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 Rehmsenc82baba2019-05-16 14:43:01 +0200203 composed: true,
Sean Egan3278d7b2018-07-06 11:02:31 -0400204 detail: {change: this.change, reviewed: newVal},
205 }));
206 },
Andrew Bonventre78792e82016-03-04 17:48:22 -0500207 });
208})();