ES6ify /gr-diff-comment/*
Bug: Issue 6179
Change-Id: Id103711e6a7ca24785a301dbb8341bb0c8f63617
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js b/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js
index a7a2db3..f87c14a 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js
@@ -14,7 +14,7 @@
(function() {
'use strict';
- var STORAGE_DEBOUNCE_INTERVAL = 400;
+ const STORAGE_DEBOUNCE_INTERVAL = 400;
Polymer({
is: 'gr-diff-comment',
@@ -116,55 +116,55 @@
'_calculateActionstoShow(showActions, isRobotComment)',
],
- attached: function() {
+ attached() {
if (this.editing) {
this.collapsed = false;
} else if (this.comment) {
this.collapsed = this.comment.collapsed;
}
- this._getIsAdmin().then(function(isAdmin) {
+ this._getIsAdmin().then(isAdmin => {
this._isAdmin = isAdmin;
- }.bind(this));
+ });
},
- detached: function() {
+ detached() {
this.cancelDebouncer('fire-update');
},
- _computeShowHideText: function(collapsed) {
+ _computeShowHideText(collapsed) {
return collapsed ? '◀' : '▼';
},
- _calculateActionstoShow: function(showActions, isRobotComment) {
+ _calculateActionstoShow(showActions, isRobotComment) {
this._showHumanActions = showActions && !isRobotComment;
this._showRobotActions = showActions && isRobotComment;
},
- _isRobotComment: function(comment) {
+ _isRobotComment(comment) {
this.isRobotComment = !!comment.robot_id;
},
- isOnParent: function() {
+ isOnParent() {
return this.side === 'PARENT';
},
- _getIsAdmin: function() {
+ _getIsAdmin() {
return this.$.restAPI.getIsAdmin();
},
- save: function() {
+ save() {
this.comment.message = this._messageText;
this.disabled = true;
this._eraseDraftComment();
- this._xhrPromise = this._saveDraft(this.comment).then(function(response) {
+ this._xhrPromise = this._saveDraft(this.comment).then(response => {
this.disabled = false;
if (!response.ok) { return response; }
- return this.$.restAPI.getResponseObject(response).then(function(obj) {
- var comment = obj;
+ return this.$.restAPI.getResponseObject(response).then(obj => {
+ const comment = obj;
comment.__draft = true;
// Maintain the ephemeral draft ID for identification by other
// elements.
@@ -176,17 +176,18 @@
this.editing = false;
this._fireSave();
return obj;
- }.bind(this));
- }.bind(this)).catch(function(err) {
+ });
+ }).catch(err => {
this.disabled = false;
throw err;
- }.bind(this));
+ });
},
- _eraseDraftComment: function() {
+ _eraseDraftComment() {
// Prevents a race condition in which removing the draft comment occurs
// prior to it being saved.
this.cancelDebouncer('store');
+
this.$.storage.eraseDraftComment({
changeNum: this.changeNum,
patchNum: this._getPatchNum(),
@@ -196,7 +197,7 @@
});
},
- _commentChanged: function(comment) {
+ _commentChanged(comment) {
this.editing = !!comment.__editing;
this.resolved = !comment.unresolved;
if (this.editing) { // It's a new draft/reply, notify.
@@ -204,41 +205,37 @@
}
},
- _getEventPayload: function(opt_mixin) {
- var payload = {
+ _getEventPayload(opt_mixin) {
+ return Object.assign({}, opt_mixin, {
comment: this.comment,
patchNum: this.patchNum,
- };
- for (var k in opt_mixin) {
- payload[k] = opt_mixin[k];
- }
- return payload;
+ });
},
- _fireSave: function() {
+ _fireSave() {
this.fire('comment-save', this._getEventPayload());
},
- _fireUpdate: function() {
- this.debounce('fire-update', function() {
+ _fireUpdate() {
+ this.debounce('fire-update', () => {
this.fire('comment-update', this._getEventPayload());
});
},
- _draftChanged: function(draft) {
+ _draftChanged(draft) {
this.$.container.classList.toggle('draft', draft);
},
- _editingChanged: function(editing, previousValue) {
+ _editingChanged(editing, previousValue) {
this.$.container.classList.toggle('editing', editing);
if (editing) {
- var textarea = this.$.editTextarea.textarea;
+ const textarea = this.$.editTextarea.textarea;
// Put the cursor at the end always.
textarea.selectionStart = textarea.value.length;
textarea.selectionEnd = textarea.selectionStart;
- this.async(function() {
+ this.async(() => {
textarea.focus();
- }.bind(this));
+ });
}
if (this.comment && this.comment.id) {
this.$$('.cancel').hidden = !editing;
@@ -252,15 +249,15 @@
}
},
- _computeLinkToComment: function(comment) {
+ _computeLinkToComment(comment) {
return '#' + comment.line;
},
- _computeSaveDisabled: function(draft) {
+ _computeSaveDisabled(draft) {
return draft == null || draft.trim() == '';
},
- _handleTextareaKeydown: function(e) {
+ _handleTextareaKeydown(e) {
switch (e.keyCode) {
case 13: // 'enter'
if (this._messageText.length !== 0 && (e.metaKey || e.ctrlKey)) {
@@ -280,11 +277,11 @@
}
},
- _handleToggleCollapsed: function() {
+ _handleToggleCollapsed() {
this.collapsed = !this.collapsed;
},
- _toggleCollapseClass: function(collapsed) {
+ _toggleCollapseClass(collapsed) {
if (collapsed) {
this.$.container.classList.add('collapsed');
} else {
@@ -292,20 +289,20 @@
}
},
- _commentMessageChanged: function(message) {
+ _commentMessageChanged(message) {
this._messageText = message || '';
},
- _messageTextChanged: function(newValue, oldValue) {
+ _messageTextChanged(newValue, oldValue) {
if (!this.comment || (this.comment && this.comment.id)) { return; }
// Keep comment.message in sync so that gr-diff-comment-thread is aware
// of the current message in the case that another comment is deleted.
this.comment.message = this._messageText || '';
- this.debounce('store', function() {
- var message = this._messageText;
+ this.debounce('store', () => {
+ const message = this._messageText;
- var commentLocation = {
+ const commentLocation = {
changeNum: this.changeNum,
patchNum: this._getPatchNum(),
path: this.comment.path,
@@ -324,9 +321,9 @@
}, STORAGE_DEBOUNCE_INTERVAL);
},
- _handleLinkTap: function(e) {
+ _handleLinkTap(e) {
e.preventDefault();
- var hash = this._computeLinkToComment(this.comment);
+ const hash = this._computeLinkToComment(this.comment);
// Don't add the hash to the window history if it's already there.
// Otherwise you mess up expected back button behavior.
if (window.location.hash == hash) { return; }
@@ -335,52 +332,51 @@
page.show(window.location.pathname + hash, null, false);
},
- _handleReply: function(e) {
+ _handleReply(e) {
e.preventDefault();
this.fire('create-reply-comment', this._getEventPayload(),
{bubbles: false});
},
- _handleQuote: function(e) {
+ _handleQuote(e) {
e.preventDefault();
this.fire('create-reply-comment', this._getEventPayload({quote: true}),
{bubbles: false});
},
- _handleFix: function(e) {
+ _handleFix(e) {
e.preventDefault();
this.fire('create-fix-comment', this._getEventPayload({quote: true}),
{bubbles: false});
},
- _handleAck: function(e) {
+ _handleAck(e) {
e.preventDefault();
this.fire('create-ack-comment', this._getEventPayload(),
{bubbles: false});
},
- _handleDone: function(e) {
+ _handleDone(e) {
e.preventDefault();
this.fire('create-done-comment', this._getEventPayload(),
{bubbles: false});
},
- _handleEdit: function(e) {
+ _handleEdit(e) {
e.preventDefault();
this._messageText = this.comment.message;
this.editing = true;
},
- _handleSave: function(e) {
+ _handleSave(e) {
e.preventDefault();
this.set('comment.__editing', false);
this.save();
},
- _handleCancel: function(e) {
+ _handleCancel(e) {
e.preventDefault();
- if (!this.comment.message ||
- this.comment.message.trim().length === 0) {
+ if (!this.comment.message || this.comment.message.trim().length === 0) {
this._fireDiscard();
return;
}
@@ -388,12 +384,12 @@
this.editing = false;
},
- _fireDiscard: function() {
+ _fireDiscard() {
this.cancelDebouncer('fire-update');
this.fire('comment-discard', this._getEventPayload());
},
- _handleDiscard: function(e) {
+ _handleDiscard(e) {
e.preventDefault();
if (!this.comment.__draft) {
throw Error('Cannot discard a non-draft comment.');
@@ -408,32 +404,31 @@
return;
}
- this._xhrPromise = this._deleteDraft(this.comment).then(
- function(response) {
- this.disabled = false;
- if (!response.ok) { return response; }
+ this._xhrPromise = this._deleteDraft(this.comment).then(response => {
+ this.disabled = false;
+ if (!response.ok) { return response; }
- this._fireDiscard();
- }.bind(this)).catch(function(err) {
- this.disabled = false;
- throw err;
- }.bind(this));
+ this._fireDiscard();
+ }).catch(err => {
+ this.disabled = false;
+ throw err;
+ });
},
- _saveDraft: function(draft) {
+ _saveDraft(draft) {
return this.$.restAPI.saveDiffDraft(this.changeNum, this.patchNum, draft);
},
- _deleteDraft: function(draft) {
+ _deleteDraft(draft) {
return this.$.restAPI.deleteDiffDraft(this.changeNum, this.patchNum,
draft);
},
- _getPatchNum: function() {
+ _getPatchNum() {
return this.isOnParent() ? 'PARENT' : this.patchNum;
},
- _loadLocalDraft: function(changeNum, patchNum, comment) {
+ _loadLocalDraft(changeNum, patchNum, comment) {
// Only apply local drafts to comments that haven't been saved
// remotely, and haven't been given a default message already.
//
@@ -444,8 +439,8 @@
return;
}
- var draft = this.$.storage.getDraftComment({
- changeNum: changeNum,
+ const draft = this.$.storage.getDraftComment({
+ changeNum,
patchNum: this._getPatchNum(),
path: comment.path,
line: comment.line,
@@ -457,40 +452,40 @@
}
},
- _handleMouseEnter: function(e) {
+ _handleMouseEnter(e) {
this.fire('comment-mouse-over', this._getEventPayload());
},
- _handleMouseLeave: function(e) {
+ _handleMouseLeave(e) {
this.fire('comment-mouse-out', this._getEventPayload());
},
- _handleToggleResolved: function() {
+ _handleToggleResolved() {
this.resolved = !this.resolved;
},
- _toggleResolved: function(resolved) {
+ _toggleResolved(resolved) {
this.comment.unresolved = !resolved;
this.fire('comment-update', this._getEventPayload());
},
- _handleCommentDelete: function() {
+ _handleCommentDelete() {
Polymer.dom(Gerrit.getRootElement()).appendChild(this.$.overlay);
this.$.overlay.open();
},
- _handleCancelDeleteComment: function() {
+ _handleCancelDeleteComment() {
Polymer.dom(Gerrit.getRootElement()).removeChild(this.$.overlay);
this.$.overlay.close();
},
- _handleConfirmDeleteComment: function() {
+ _handleConfirmDeleteComment() {
this.$.restAPI.deleteComment(
this.changeNum, this.patchNum, this.comment.id,
- this.$.confirmDeleteComment.message).then(function(newComment) {
- this._handleCancelDeleteComment();
- this.comment = newComment;
- }.bind(this));
+ this.$.confirmDeleteComment.message).then(newComment => {
+ this._handleCancelDeleteComment();
+ this.comment = newComment;
+ });
},
});
})();