Index: chrome/browser/resources/md_bookmarks/api_listener.js |
diff --git a/chrome/browser/resources/md_bookmarks/api_listener.js b/chrome/browser/resources/md_bookmarks/api_listener.js |
index 1e8852cbe7d587a12e13c8fe04c9ef2a0978a0bb..4556caa685e8da4d7343518fbde683272d962191 100644 |
--- a/chrome/browser/resources/md_bookmarks/api_listener.js |
+++ b/chrome/browser/resources/md_bookmarks/api_listener.js |
@@ -11,6 +11,12 @@ cr.define('bookmarks.ApiListener', function() { |
/** @type {?number} */ |
var timerHandle; |
+ /** @type {boolean} */ |
+ var trackUpdates = false; |
+ /** @type {!Array<string>} */ |
+ var updatedItems = []; |
+ /** @type {boolean} */ |
+ var shouldHighlightAfterBatch = false; |
calamity
2017/07/20 05:49:03
As discussed, it would be nice to have a resettabl
tsergeant
2017/07/25 00:13:00
Done.
|
/** |
* Batches UI updates so that no changes will be made to UI until the next |
@@ -26,9 +32,45 @@ cr.define('bookmarks.ApiListener', function() { |
timerHandle = setTimeout(function() { |
bookmarks.Store.getInstance().endBatchUpdate(); |
timerHandle = null; |
+ |
+ if (shouldHighlightAfterBatch) |
+ highlightUpdatedItemsImpl(); |
}); |
} |
+ /** |
+ * Tracks any items that are created or moved. |
+ */ |
+ function trackUpdatedItems() { |
+ trackUpdates = true; |
+ } |
+ |
+ function highlightUpdatedItemsImpl() { |
+ if (!trackUpdates) |
+ return; |
+ |
+ document.dispatchEvent(new CustomEvent('highlight-items', { |
+ detail: updatedItems, |
+ })); |
+ updatedItems = []; |
+ trackUpdates = false; |
+ shouldHighlightAfterBatch = false; |
+ } |
+ |
+ /** |
+ * Highlights any items that have been updated since |trackUpdatedItems| was |
+ * called. Should be called after a user action causes new items to appear in |
+ * the main list. |
+ */ |
+ function highlightUpdatedItems() { |
+ // Ensure that the items are highlighted after the current batch update (if |
+ // there is one) is completed. |
+ if (timerHandle) |
+ shouldHighlightAfterBatch = true; |
+ else |
+ highlightUpdatedItemsImpl(); |
+ } |
+ |
/** @param {Action} action */ |
function dispatch(action) { |
bookmarks.Store.getInstance().dispatch(action); |
@@ -48,6 +90,8 @@ cr.define('bookmarks.ApiListener', function() { |
*/ |
function onBookmarkCreated(id, treeNode) { |
batchUIUpdates(); |
+ if (trackUpdatedItems) |
+ updatedItems.push(id); |
dispatch(bookmarks.actions.createBookmark(id, treeNode)); |
} |
@@ -73,6 +117,8 @@ cr.define('bookmarks.ApiListener', function() { |
*/ |
function onBookmarkMoved(id, moveInfo) { |
batchUIUpdates(); |
+ if (trackUpdatedItems) |
+ updatedItems.push(id); |
dispatch(bookmarks.actions.moveBookmark( |
id, moveInfo.parentId, moveInfo.index, moveInfo.oldParentId, |
moveInfo.oldIndex)); |
@@ -116,14 +162,18 @@ cr.define('bookmarks.ApiListener', function() { |
dispatch(bookmarks.actions.setCanEditBookmarks(canEdit)); |
} |
+ var listeners = [ |
+ {api: chrome.bookmarks.onChanged, fn: onBookmarkChanged}, |
+ {api: chrome.bookmarks.onChildrenReordered, fn: onChildrenReordered}, |
+ {api: chrome.bookmarks.onCreated, fn: onBookmarkCreated}, |
+ {api: chrome.bookmarks.onMoved, fn: onBookmarkMoved}, |
+ {api: chrome.bookmarks.onRemoved, fn: onBookmarkRemoved}, |
+ {api: chrome.bookmarks.onImportBegan, fn: onImportBegan}, |
+ {api: chrome.bookmarks.onImportEnded, fn: onImportEnded}, |
+ ]; |
+ |
function init() { |
- chrome.bookmarks.onChanged.addListener(onBookmarkChanged); |
- chrome.bookmarks.onChildrenReordered.addListener(onChildrenReordered); |
- chrome.bookmarks.onCreated.addListener(onBookmarkCreated); |
- chrome.bookmarks.onMoved.addListener(onBookmarkMoved); |
- chrome.bookmarks.onRemoved.addListener(onBookmarkRemoved); |
- chrome.bookmarks.onImportBegan.addListener(onImportBegan); |
- chrome.bookmarks.onImportEnded.addListener(onImportEnded); |
+ listeners.forEach((listener) => listener.api.addListener(listener.fn)); |
cr.sendWithPromise('getIncognitoAvailability') |
.then(onIncognitoAvailabilityChanged); |
@@ -135,7 +185,18 @@ cr.define('bookmarks.ApiListener', function() { |
'can-edit-bookmarks-changed', onCanEditBookmarksChanged); |
} |
+ function destroy() { |
+ listeners.forEach((listener) => listener.api.removeListener(listener.fn)); |
+ cr.removeWebUIListener( |
+ 'incognito-availability-changed', onIncognitoAvailabilityChanged); |
+ cr.removeWebUIListener( |
+ 'can-edit-bookmarks-changed', onCanEditBookmarksChanged); |
+ } |
+ |
return { |
init: init, |
+ destroy: destroy, |
+ trackUpdatedItems: trackUpdatedItems, |
+ highlightUpdatedItems: highlightUpdatedItems, |
}; |
}); |