blob: 9af6aa92247727cf0a4bf1d0f98f3dcf5afc66f3 [file] [log] [blame]
Andrew Bonventre78792e82016-03-04 17:48:22 -05001// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14(function() {
15 'use strict';
16
17 Polymer({
18 is: 'gr-change-view',
19
20 /**
21 * Fired when the title of the page should change.
22 *
23 * @event title-change
24 */
25
26 properties: {
27 /**
28 * URL params passed from the router.
29 */
30 params: {
31 type: Object,
32 observer: '_paramsChanged',
33 },
34 viewState: {
35 type: Object,
36 notify: true,
37 value: function() { return {}; },
38 },
39 serverConfig: Object,
40 keyEventTarget: {
41 type: Object,
42 value: function() { return document.body; },
43 },
44
45 _comments: Object,
46 _change: {
47 type: Object,
48 observer: '_changeChanged',
49 },
50 _commitInfo: Object,
51 _changeNum: String,
Andrew Bonventre015b8042016-03-09 21:04:15 -050052 _diffDrafts: Object,
Andrew Bonventre78792e82016-03-04 17:48:22 -050053 _patchNum: String,
54 _allPatchSets: {
55 type: Array,
56 computed: '_computeAllPatchSets(_change)',
57 },
58 _loggedIn: {
59 type: Boolean,
60 value: false,
61 },
62 _loading: Boolean,
63 _headerContainerEl: Object,
64 _headerEl: Object,
65 _projectConfig: Object,
66 _boundScrollHandler: {
67 type: Function,
68 value: function() { return this._handleBodyScroll.bind(this); },
69 },
Andrew Bonventre015b8042016-03-09 21:04:15 -050070 _replyButtonLabel: {
71 type: String,
72 value: 'Reply',
73 computed: '_computeReplyButtonLabel(_diffDrafts)',
74 },
Andrew Bonventre78792e82016-03-04 17:48:22 -050075 },
76
77 behaviors: [
78 Gerrit.KeyboardShortcutBehavior,
79 Gerrit.RESTClientBehavior,
80 ],
81
82 ready: function() {
Andrew Bonventre78792e82016-03-04 17:48:22 -050083 this._headerEl = this.$$('.header');
84 },
85
86 attached: function() {
Andrew Bonventre015b8042016-03-09 21:04:15 -050087 this._getLoggedIn().then(function(loggedIn) {
88 this._loggedIn = loggedIn;
89 }.bind(this));
90
Andrew Bonventre78792e82016-03-04 17:48:22 -050091 window.addEventListener('scroll', this._boundScrollHandler);
92 },
93
94 detached: function() {
95 window.removeEventListener('scroll', this._boundScrollHandler);
96 },
97
98 _handleBodyScroll: function(e) {
99 var containerEl = this._headerContainerEl ||
100 this.$$('.headerContainer');
101
102 // Calculate where the header is relative to the window.
103 var top = containerEl.offsetTop;
104 for (var offsetParent = containerEl.offsetParent;
105 offsetParent;
106 offsetParent = offsetParent.offsetParent) {
107 top += offsetParent.offsetTop;
108 }
109 // The element may not be displayed yet, in which case do nothing.
110 if (top == 0) { return; }
111
112 this._headerEl.classList.toggle('pinned', window.scrollY >= top);
113 },
114
115 _resetHeaderEl: function() {
116 var el = this._headerEl || this.$$('.header');
117 this._headerEl = el;
118 el.classList.remove('pinned');
119 },
120
121 _handlePatchChange: function(e) {
122 var patchNum = e.target.value;
123 var currentPatchNum =
124 this._change.revisions[this._change.current_revision]._number;
125 if (patchNum == currentPatchNum) {
David Ostrovskye8771402016-02-13 12:23:53 +0100126 page.show(this.changePath(this._changeNum));
Andrew Bonventre78792e82016-03-04 17:48:22 -0500127 return;
128 }
David Ostrovskye8771402016-02-13 12:23:53 +0100129 page.show(this.changePath(this._changeNum) + '/' + patchNum);
Andrew Bonventre78792e82016-03-04 17:48:22 -0500130 },
131
132 _handleReplyTap: function(e) {
133 e.preventDefault();
134 this.$.replyOverlay.open();
135 },
136
137 _handleDownloadTap: function(e) {
138 e.preventDefault();
139 this.$.downloadOverlay.open();
140 },
141
142 _handleDownloadDialogClose: function(e) {
143 this.$.downloadOverlay.close();
144 },
145
146 _handleMessageReply: function(e) {
147 var msg = e.detail.message.message;
148 var quoteStr = msg.split('\n').map(
149 function(line) { return '> ' + line; }).join('\n') + '\n\n';
150 this.$.replyDialog.draft += quoteStr;
151 this.$.replyOverlay.open();
152 },
153
154 _handleReplyOverlayOpen: function(e) {
Andrew Bonventre78792e82016-03-04 17:48:22 -0500155 this.$.replyDialog.focus();
156 },
157
158 _handleReplySent: function(e) {
159 this.$.replyOverlay.close();
160 this._reload();
161 },
162
163 _handleReplyCancel: function(e) {
164 this.$.replyOverlay.close();
165 },
166
167 _paramsChanged: function(value) {
168 if (value.view != this.tagName.toLowerCase()) { return; }
169
170 this._changeNum = value.changeNum;
171 this._patchNum = value.patchNum;
172 if (this.viewState.changeNum != this._changeNum ||
173 this.viewState.patchNum != this._patchNum) {
174 this.set('viewState.selectedFileIndex', 0);
175 this.set('viewState.changeNum', this._changeNum);
176 this.set('viewState.patchNum', this._patchNum);
177 }
178 if (!this._changeNum) {
179 return;
180 }
181 this._reload().then(function() {
182 this.$.messageList.topMargin = this._headerEl.offsetHeight;
183
184 // Allow the message list to render before scrolling.
185 this.async(function() {
186 var msgPrefix = '#message-';
187 var hash = window.location.hash;
188 if (hash.indexOf(msgPrefix) == 0) {
189 this.$.messageList.scrollToMessage(hash.substr(msgPrefix.length));
190 }
191 }.bind(this), 1);
192
Andrew Bonventre015b8042016-03-09 21:04:15 -0500193 this._getLoggedIn().then(function(loggedIn) {
194 if (!loggedIn) { return; }
Andrew Bonventre78792e82016-03-04 17:48:22 -0500195
196 if (this.viewState.showReplyDialog) {
197 this.$.replyOverlay.open();
198 this.set('viewState.showReplyDialog', false);
199 }
200 }.bind(this));
201 }.bind(this));
202 },
203
204 _changeChanged: function(change) {
205 if (!change) { return; }
206 this._patchNum = this._patchNum ||
207 change.revisions[change.current_revision]._number;
208
209 var title = change.subject + ' (' + change.change_id.substr(0, 9) + ')';
210 this.fire('title-change', {title: title});
211 },
212
Andrew Bonventre78792e82016-03-04 17:48:22 -0500213 _computeChangePermalink: function(changeNum) {
214 return '/' + changeNum;
215 },
216
217 _computeChangeStatus: function(change, patchNum) {
218 var status = change.status;
219 if (status == this.ChangeStatus.NEW) {
220 var rev = this._getRevisionNumber(change, patchNum);
221 // TODO(davido): Figure out, why sometimes revision is not there
222 if (rev == undefined || !rev.draft) { return ''; }
223 status = this.ChangeStatus.DRAFT;
224 }
225 return '(' + status.toLowerCase() + ')';
226 },
227
228 _computeDetailPath: function(changeNum) {
229 return '/changes/' + changeNum + '/detail';
230 },
231
232 _computeCommitInfoPath: function(changeNum, patchNum) {
233 return this.changeBaseURL(changeNum, patchNum) + '/commit?links';
234 },
235
236 _computeCommentsPath: function(changeNum) {
237 return '/changes/' + changeNum + '/comments';
238 },
239
240 _computeProjectConfigPath: function(project) {
241 return '/projects/' + encodeURIComponent(project) + '/config';
242 },
243
244 _computeDetailQueryParams: function() {
245 var options = this.listChangesOptionsToHex(
246 this.ListChangesOption.ALL_REVISIONS,
247 this.ListChangesOption.CHANGE_ACTIONS,
248 this.ListChangesOption.DOWNLOAD_COMMANDS
249 );
250 return {O: options};
251 },
252
253 _computeLatestPatchNum: function(change) {
254 return change.revisions[change.current_revision]._number;
255 },
256
257 _computeAllPatchSets: function(change) {
258 var patchNums = [];
259 for (var rev in change.revisions) {
260 patchNums.push(change.revisions[rev]._number);
261 }
262 return patchNums.sort(function(a, b) {
263 return a - b;
264 });
265 },
266
267 _getRevisionNumber: function(change, patchNum) {
268 for (var rev in change.revisions) {
269 if (change.revisions[rev]._number == patchNum) {
270 return change.revisions[rev];
271 }
272 }
273 },
274
275 _computePatchIndexIsSelected: function(index, patchNum) {
276 return this._allPatchSets[index] == patchNum;
277 },
278
279 _computeLabelNames: function(labels) {
280 return Object.keys(labels).sort();
281 },
282
283 _computeLabelValues: function(labelName, labels) {
284 var result = [];
285 var t = labels[labelName];
286 if (!t) { return result; }
287 var approvals = t.all || [];
288 approvals.forEach(function(label) {
289 if (label.value && label.value != labels[labelName].default_value) {
290 var labelClassName;
291 var labelValPrefix = '';
292 if (label.value > 0) {
293 labelValPrefix = '+';
294 labelClassName = 'approved';
295 } else if (label.value < 0) {
296 labelClassName = 'notApproved';
297 }
298 result.push({
299 value: labelValPrefix + label.value,
300 className: labelClassName,
301 account: label,
302 });
303 }
304 });
305 return result;
306 },
307
Andrew Bonventre015b8042016-03-09 21:04:15 -0500308 _computeReplyButtonHighlighted: function(drafts) {
309 return Object.keys(drafts || {}).length > 0;
310 },
311
312 _computeReplyButtonLabel: function(drafts) {
313 drafts = drafts || {};
314 var draftCount = Object.keys(drafts).reduce(function(count, file) {
315 return count + drafts[file].length;
316 }, 0);
317
318 var label = 'Reply';
319 if (draftCount > 0) {
320 label += ' (' + draftCount + ')';
321 }
322 return label;
323 },
324
Andrew Bonventre78792e82016-03-04 17:48:22 -0500325 _handleKey: function(e) {
326 if (this.shouldSupressKeyboardShortcut(e)) { return; }
327
328 switch (e.keyCode) {
329 case 65: // 'a'
Andrew Bonventreebf0cbc2016-03-11 13:02:16 -0500330 if (!this._loggedIn) { return; }
331
Andrew Bonventre78792e82016-03-04 17:48:22 -0500332 e.preventDefault();
333 this.$.replyOverlay.open();
334 break;
335 case 85: // 'u'
336 e.preventDefault();
337 page.show('/');
338 break;
339 }
340 },
341
342 _handleReloadChange: function() {
David Ostrovskye8771402016-02-13 12:23:53 +0100343 page.show(this.changePath(this._changeNum));
Andrew Bonventre78792e82016-03-04 17:48:22 -0500344 },
345
Andrew Bonventre015b8042016-03-09 21:04:15 -0500346 _getDiffDrafts: function() {
347 return this.$.restAPI.getDiffDrafts(this._changeNum).then(
348 function(drafts) { return this._diffDrafts = drafts; }.bind(this));
349 },
350
351 _getLoggedIn: function() {
352 return this.$.restAPI.getLoggedIn();
353 },
354
355 _reloadDiffDrafts: function() {
356 this._diffDrafts = {};
357 this._getDiffDrafts().then(function() {
358 if (this.$.replyOverlay.opened) {
359 this.async(function() { this.$.replyOverlay.center(); }, 1);
360 }
361 }.bind(this));
362 },
363
Andrew Bonventre78792e82016-03-04 17:48:22 -0500364 _reload: function() {
Andrew Bonventre015b8042016-03-09 21:04:15 -0500365 this._getLoggedIn().then(function(loggedIn) {
366 if (!loggedIn) { return; }
367
368 this._reloadDiffDrafts();
369 }.bind(this));
370
Andrew Bonventre78792e82016-03-04 17:48:22 -0500371 var detailCompletes = this.$.detailXHR.generateRequest().completes;
372 this.$.commentsXHR.generateRequest();
Andrew Bonventre015b8042016-03-09 21:04:15 -0500373
Andrew Bonventre78792e82016-03-04 17:48:22 -0500374 var reloadPatchNumDependentResources = function() {
375 return Promise.all([
376 this.$.commitInfoXHR.generateRequest().completes,
377 this.$.actions.reload(),
378 this.$.fileList.reload(),
379 ]);
380 }.bind(this);
381 var reloadDetailDependentResources = function() {
382 return this.$.relatedChanges.reload();
383 }.bind(this);
384
385 this._resetHeaderEl();
386
387 if (this._patchNum) {
388 return reloadPatchNumDependentResources().then(function() {
389 return detailCompletes;
390 }).then(reloadDetailDependentResources);
391 } else {
392 // The patch number is reliant on the change detail request.
393 return detailCompletes.then(reloadPatchNumDependentResources).then(
394 reloadDetailDependentResources);
395 }
396 },
397 });
398})();