Upgrade gr-editable-content with alert

Editable content now shows an alert when it reloads with content cached
in gr-storage.

Bug: Issue 6309
Change-Id: Id38b7eddf26e223a2899a0ffb1b3fa72398da0f0
This commit is contained in:
Kasper Nilsson
2018-01-02 16:12:07 -08:00
parent 4b20dafe89
commit 9e54b7d4a9
2 changed files with 22 additions and 4 deletions

View File

@@ -14,6 +14,7 @@
(function() { (function() {
'use strict'; 'use strict';
const RESTORED_MESSAGE = 'Content restored from a previous edit.';
const STORAGE_DEBOUNCE_INTERVAL_MS = 400; const STORAGE_DEBOUNCE_INTERVAL_MS = 400;
Polymer({ Polymer({
@@ -31,6 +32,12 @@
* @event editable-content-cancel * @event editable-content-cancel
*/ */
/**
* Fired when content is restored from storage.
*
* @event show-alert
*/
properties: { properties: {
content: { content: {
notify: true, notify: true,
@@ -81,10 +88,16 @@
let content; let content;
if (this.storageKey) { if (this.storageKey) {
content = this.$.storage.getEditableContentItem(this.storageKey); const storedContent =
this.$.storage.getEditableContentItem(this.storageKey);
if (storedContent && storedContent.message) {
content = storedContent.message;
this.dispatchEvent(new CustomEvent('show-alert',
{detail: {message: RESTORED_MESSAGE}, bubbles: true}));
}
} }
if (!content) { if (!content) {
content = this.content; content = this.content || '';
} }
// TODO(wyatta) switch linkify sequence, see issue 5526. // TODO(wyatta) switch linkify sequence, see issue 5526.

View File

@@ -100,25 +100,30 @@ limitations under the License.
}); });
suite('storageKey and related behavior', () => { suite('storageKey and related behavior', () => {
let dispatchSpy;
setup(() => { setup(() => {
element.content = 'current content'; element.content = 'current content';
element.storageKey = 'test'; element.storageKey = 'test';
dispatchSpy = sandbox.spy(element, 'dispatchEvent');
}); });
test('editing toggled to true, has stored data', () => { test('editing toggled to true, has stored data', () => {
sandbox.stub(element.$.storage, 'getEditableContentItem') sandbox.stub(element.$.storage, 'getEditableContentItem')
.returns('stored content'); .returns({message: 'stored content'});
element.editing = true; element.editing = true;
assert.equal(element._newContent, 'stored content'); assert.equal(element._newContent, 'stored content');
assert.isTrue(dispatchSpy.called);
assert.equal(dispatchSpy.lastCall.args[0].type, 'show-alert');
}); });
test('editing toggled to true, has no stored data', () => { test('editing toggled to true, has no stored data', () => {
sandbox.stub(element.$.storage, 'getEditableContentItem') sandbox.stub(element.$.storage, 'getEditableContentItem')
.returns(''); .returns({});
element.editing = true; element.editing = true;
assert.equal(element._newContent, 'current content'); assert.equal(element._newContent, 'current content');
assert.isFalse(dispatchSpy.called);
}); });
test('edits are cached', () => { test('edits are cached', () => {