Gr-diff retrofit: store comment state
Fixing UI data pipe line, re-rendering:
- side, draft text and editing status in UI comment objects
- update gr-diff UI model on comment save/update
Feature: Issue 3910
Change-Id: I96f714c7de9add6e316dcf64bb7d566690b9d3ae
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 525bac0..7a15754 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
@@ -15,6 +15,7 @@
'use strict';
var STORAGE_DEBOUNCE_INTERVAL = 400;
+ var UPDATE_DEBOUNCE_INTERVAL = 500;
Polymer({
is: 'gr-diff-comment',
@@ -43,11 +44,18 @@
* @event comment-save
*/
+ /**
+ * Fired when this comment is updated.
+ *
+ * @event comment-update
+ */
+
properties: {
changeNum: String,
comment: {
type: Object,
notify: true,
+ observer: '_commentChanged',
},
disabled: {
type: Boolean,
@@ -81,6 +89,10 @@
'_loadLocalDraft(changeNum, patchNum, comment)',
],
+ detached: function() {
+ this.flushDebouncer('fire-update');
+ },
+
save: function() {
this.comment.message = this._messageText;
this.disabled = true;
@@ -106,8 +118,7 @@
}
this.comment = comment;
this.editing = false;
- this.fire('comment-save');
-
+ this.fire('comment-save', {comment: this.comment});
return obj;
}.bind(this));
}.bind(this)).catch(function(err) {
@@ -116,6 +127,16 @@
}.bind(this));
},
+ _commentChanged: function(comment) {
+ this.editing = !!comment.__editing;
+ },
+
+ _fireUpdate: function() {
+ this.debounce('fire-update', function() {
+ this.fire('comment-update', {comment: this.comment});
+ }, UPDATE_DEBOUNCE_INTERVAL);
+ },
+
_draftChanged: function(draft) {
this.$.container.classList.toggle('draft', draft);
},
@@ -134,6 +155,10 @@
if (this.comment && this.comment.id) {
this.$$('.cancel').hidden = !editing;
}
+ if (this.comment) {
+ this.comment.__editing = this.editing;
+ }
+ this._fireUpdate();
},
_computeLinkToComment: function(comment) {
@@ -174,6 +199,7 @@
} else {
this.$.storage.setDraftComment(commentLocation, message);
}
+ this._fireUpdate();
}, STORAGE_DEBOUNCE_INTERVAL);
},
@@ -218,7 +244,7 @@
_handleCancel: function(e) {
this._preventDefaultAndBlur(e);
if (this.comment.message == null || this.comment.message.length == 0) {
- this.fire('comment-discard');
+ this.fire('comment-discard', {comment: this.comment});
return;
}
this._messageText = this.comment.message;
@@ -234,20 +260,20 @@
this.disabled = true;
if (!this.comment.id) {
this.disabled = false;
- this.fire('comment-discard');
+ this.fire('comment-discard', {comment: this.comment});
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(
+ function(response) {
+ this.disabled = false;
+ if (!response.ok) { return response; }
- this.fire('comment-discard');
- }.bind(this)).catch(function(err) {
- this.disabled = false;
- throw err;
- }.bind(this));;
+ this.fire('comment-discard', {comment: this.comment});
+ }.bind(this)).catch(function(err) {
+ this.disabled = false;
+ throw err;
+ }.bind(this));
},
_preventDefaultAndBlur: function(e) {
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment_test.html b/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment_test.html
index 8ef222e..6c27a36 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment_test.html
+++ b/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment_test.html
@@ -204,16 +204,50 @@
});
test('draft saving/editing', function(done) {
+ var fireStub = sinon.stub(element, 'fire');
+
element.draft = true;
MockInteractions.tap(element.$$('.edit'));
element._messageText = 'good news, everyone!';
+ element.flushDebouncer('fire-update');
+ element.flushDebouncer('store');
+ assert(fireStub.calledWith('comment-update'),
+ 'comment-update should be sent');
+ assert.deepEqual(fireStub.lastCall.args, [
+ 'comment-update', {
+ comment: {
+ __draft: true,
+ __draftID: 'temp_draft_id',
+ __editing: true,
+ line: 5,
+ path: '/path/to/file',
+ },
+ },
+ ]);
MockInteractions.tap(element.$$('.save'));
+
assert.isTrue(element.disabled,
'Element should be disabled when creating draft.');
element._xhrPromise.then(function(draft) {
+ assert(fireStub.calledWith('comment-save'),
+ 'comment-save should be sent');
+ assert.deepEqual(fireStub.lastCall.args, [
+ 'comment-save', {
+ comment: {
+ __draft: true,
+ __draftID: 'temp_draft_id',
+ __editing: false,
+ id: 'baf0414d_40572e03',
+ line: 5,
+ message: 'saved!',
+ path: '/path/to/file',
+ updated: '2015-12-08 21:52:36.177000000',
+ },
+ },
+ ]);
assert.isFalse(element.disabled,
- 'Element should be enabled when done creating draft.');
+ 'Element should be enabled when done creating draft.');
assert.equal(draft.message, 'saved!');
assert.isFalse(element.editing);
}).then(function() {