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..b4aaf5d97ace0d01056ec7738f93719c286df374 100644 |
--- a/chrome/browser/resources/md_bookmarks/api_listener.js |
+++ b/chrome/browser/resources/md_bookmarks/api_listener.js |
@@ -11,6 +11,10 @@ cr.define('bookmarks.ApiListener', function() { |
/** @type {?number} */ |
var timerHandle; |
+ /** @type {boolean} */ |
+ var trackUpdates = false; |
+ /** @type {!Array<string>} */ |
+ var updatedItems = []; |
/** |
* Batches UI updates so that no changes will be made to UI until the next |
@@ -29,6 +33,31 @@ cr.define('bookmarks.ApiListener', function() { |
}); |
} |
+ /** |
+ * Tracks any items that are created or moved. |
+ */ |
+ function trackUpdatedItems() { |
+ trackUpdates = true; |
+ } |
+ |
+ /** |
+ * 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() { |
+ assert(trackUpdates); |
calamity
2017/07/19 05:25:14
Try mashing Ctrl+V while you drop something. I get
tsergeant
2017/07/19 06:53:12
Yeah, I think I was able to get this to trigger.
|
+ // Ensure that the items are highlighted after the current batch update (if |
+ // there is one) is completed. |
+ setTimeout(function() { |
+ document.dispatchEvent(new CustomEvent('highlight-items', { |
+ detail: updatedItems, |
+ })); |
+ updatedItems = []; |
+ trackUpdates = false; |
+ }); |
+ } |
+ |
/** @param {Action} action */ |
function dispatch(action) { |
bookmarks.Store.getInstance().dispatch(action); |
@@ -48,6 +77,8 @@ cr.define('bookmarks.ApiListener', function() { |
*/ |
function onBookmarkCreated(id, treeNode) { |
batchUIUpdates(); |
+ if (trackUpdatedItems) |
+ updatedItems.push(id); |
dispatch(bookmarks.actions.createBookmark(id, treeNode)); |
} |
@@ -73,6 +104,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 +149,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 +172,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, |
}; |
}); |