Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Listener functions which translate events from the | 6 * @fileoverview Listener functions which translate events from the |
| 7 * chrome.bookmarks API into actions to modify the local page state. | 7 * chrome.bookmarks API into actions to modify the local page state. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('bookmarks.ApiListener', function() { | 10 cr.define('bookmarks.ApiListener', function() { |
| 11 | 11 |
| 12 /** @type {?number} */ | 12 /** @type {?number} */ |
| 13 var timerHandle; | 13 var timerHandle; |
| 14 /** @type {boolean} */ | |
| 15 var trackUpdates = false; | |
| 16 /** @type {!Array<string>} */ | |
| 17 var updatedItems = []; | |
| 14 | 18 |
| 15 /** | 19 /** |
| 16 * Batches UI updates so that no changes will be made to UI until the next | 20 * Batches UI updates so that no changes will be made to UI until the next |
| 17 * task after the last call to this method. This is useful for listeners which | 21 * task after the last call to this method. This is useful for listeners which |
| 18 * can be called in a tight loop by UI actions. | 22 * can be called in a tight loop by UI actions. |
| 19 */ | 23 */ |
| 20 function batchUIUpdates() { | 24 function batchUIUpdates() { |
| 21 if (timerHandle) | 25 if (timerHandle) |
| 22 clearTimeout(timerHandle); | 26 clearTimeout(timerHandle); |
| 23 else | 27 else |
| 24 bookmarks.Store.getInstance().beginBatchUpdate(); | 28 bookmarks.Store.getInstance().beginBatchUpdate(); |
| 25 | 29 |
| 26 timerHandle = setTimeout(function() { | 30 timerHandle = setTimeout(function() { |
| 27 bookmarks.Store.getInstance().endBatchUpdate(); | 31 bookmarks.Store.getInstance().endBatchUpdate(); |
| 28 timerHandle = null; | 32 timerHandle = null; |
| 29 }); | 33 }); |
| 30 } | 34 } |
| 31 | 35 |
| 36 /** | |
| 37 * Tracks any items that are created or moved. | |
| 38 */ | |
| 39 function trackUpdatedItems() { | |
| 40 trackUpdates = true; | |
| 41 } | |
| 42 | |
| 43 /** | |
| 44 * Highlights any items that have been updated since |trackUpdatedItems| was | |
| 45 * called. Should be called after a user action causes new items to appear in | |
| 46 * the main list. | |
| 47 */ | |
| 48 function highlightUpdatedItems() { | |
| 49 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.
| |
| 50 // Ensure that the items are highlighted after the current batch update (if | |
| 51 // there is one) is completed. | |
| 52 setTimeout(function() { | |
| 53 document.dispatchEvent(new CustomEvent('highlight-items', { | |
| 54 detail: updatedItems, | |
| 55 })); | |
| 56 updatedItems = []; | |
| 57 trackUpdates = false; | |
| 58 }); | |
| 59 } | |
| 60 | |
| 32 /** @param {Action} action */ | 61 /** @param {Action} action */ |
| 33 function dispatch(action) { | 62 function dispatch(action) { |
| 34 bookmarks.Store.getInstance().dispatch(action); | 63 bookmarks.Store.getInstance().dispatch(action); |
| 35 } | 64 } |
| 36 | 65 |
| 37 /** | 66 /** |
| 38 * @param {string} id | 67 * @param {string} id |
| 39 * @param {{title: string, url: (string|undefined)}} changeInfo | 68 * @param {{title: string, url: (string|undefined)}} changeInfo |
| 40 */ | 69 */ |
| 41 function onBookmarkChanged(id, changeInfo) { | 70 function onBookmarkChanged(id, changeInfo) { |
| 42 dispatch(bookmarks.actions.editBookmark(id, changeInfo)); | 71 dispatch(bookmarks.actions.editBookmark(id, changeInfo)); |
| 43 } | 72 } |
| 44 | 73 |
| 45 /** | 74 /** |
| 46 * @param {string} id | 75 * @param {string} id |
| 47 * @param {BookmarkTreeNode} treeNode | 76 * @param {BookmarkTreeNode} treeNode |
| 48 */ | 77 */ |
| 49 function onBookmarkCreated(id, treeNode) { | 78 function onBookmarkCreated(id, treeNode) { |
| 50 batchUIUpdates(); | 79 batchUIUpdates(); |
| 80 if (trackUpdatedItems) | |
| 81 updatedItems.push(id); | |
| 51 dispatch(bookmarks.actions.createBookmark(id, treeNode)); | 82 dispatch(bookmarks.actions.createBookmark(id, treeNode)); |
| 52 } | 83 } |
| 53 | 84 |
| 54 /** | 85 /** |
| 55 * @param {string} id | 86 * @param {string} id |
| 56 * @param {{parentId: string, index: number}} removeInfo | 87 * @param {{parentId: string, index: number}} removeInfo |
| 57 */ | 88 */ |
| 58 function onBookmarkRemoved(id, removeInfo) { | 89 function onBookmarkRemoved(id, removeInfo) { |
| 59 batchUIUpdates(); | 90 batchUIUpdates(); |
| 60 var nodes = bookmarks.Store.getInstance().data.nodes; | 91 var nodes = bookmarks.Store.getInstance().data.nodes; |
| 61 dispatch(bookmarks.actions.removeBookmark( | 92 dispatch(bookmarks.actions.removeBookmark( |
| 62 id, removeInfo.parentId, removeInfo.index, nodes)); | 93 id, removeInfo.parentId, removeInfo.index, nodes)); |
| 63 } | 94 } |
| 64 | 95 |
| 65 /** | 96 /** |
| 66 * @param {string} id | 97 * @param {string} id |
| 67 * @param {{ | 98 * @param {{ |
| 68 * parentId: string, | 99 * parentId: string, |
| 69 * index: number, | 100 * index: number, |
| 70 * oldParentId: string, | 101 * oldParentId: string, |
| 71 * oldIndex: number | 102 * oldIndex: number |
| 72 * }} moveInfo | 103 * }} moveInfo |
| 73 */ | 104 */ |
| 74 function onBookmarkMoved(id, moveInfo) { | 105 function onBookmarkMoved(id, moveInfo) { |
| 75 batchUIUpdates(); | 106 batchUIUpdates(); |
| 107 if (trackUpdatedItems) | |
| 108 updatedItems.push(id); | |
| 76 dispatch(bookmarks.actions.moveBookmark( | 109 dispatch(bookmarks.actions.moveBookmark( |
| 77 id, moveInfo.parentId, moveInfo.index, moveInfo.oldParentId, | 110 id, moveInfo.parentId, moveInfo.index, moveInfo.oldParentId, |
| 78 moveInfo.oldIndex)); | 111 moveInfo.oldIndex)); |
| 79 } | 112 } |
| 80 | 113 |
| 81 /** | 114 /** |
| 82 * @param {string} id | 115 * @param {string} id |
| 83 * @param {{childIds: !Array<string>}} reorderInfo | 116 * @param {{childIds: !Array<string>}} reorderInfo |
| 84 */ | 117 */ |
| 85 function onChildrenReordered(id, reorderInfo) { | 118 function onChildrenReordered(id, reorderInfo) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 109 dispatch(bookmarks.actions.setIncognitoAvailability(availability)); | 142 dispatch(bookmarks.actions.setIncognitoAvailability(availability)); |
| 110 } | 143 } |
| 111 | 144 |
| 112 /** | 145 /** |
| 113 * @param {boolean} canEdit | 146 * @param {boolean} canEdit |
| 114 */ | 147 */ |
| 115 function onCanEditBookmarksChanged(canEdit) { | 148 function onCanEditBookmarksChanged(canEdit) { |
| 116 dispatch(bookmarks.actions.setCanEditBookmarks(canEdit)); | 149 dispatch(bookmarks.actions.setCanEditBookmarks(canEdit)); |
| 117 } | 150 } |
| 118 | 151 |
| 152 var listeners = [ | |
| 153 {api: chrome.bookmarks.onChanged, fn: onBookmarkChanged}, | |
| 154 {api: chrome.bookmarks.onChildrenReordered, fn: onChildrenReordered}, | |
| 155 {api: chrome.bookmarks.onCreated, fn: onBookmarkCreated}, | |
| 156 {api: chrome.bookmarks.onMoved, fn: onBookmarkMoved}, | |
| 157 {api: chrome.bookmarks.onRemoved, fn: onBookmarkRemoved}, | |
| 158 {api: chrome.bookmarks.onImportBegan, fn: onImportBegan}, | |
| 159 {api: chrome.bookmarks.onImportEnded, fn: onImportEnded}, | |
| 160 ]; | |
| 161 | |
| 119 function init() { | 162 function init() { |
| 120 chrome.bookmarks.onChanged.addListener(onBookmarkChanged); | 163 listeners.forEach((listener) => listener.api.addListener(listener.fn)); |
| 121 chrome.bookmarks.onChildrenReordered.addListener(onChildrenReordered); | |
| 122 chrome.bookmarks.onCreated.addListener(onBookmarkCreated); | |
| 123 chrome.bookmarks.onMoved.addListener(onBookmarkMoved); | |
| 124 chrome.bookmarks.onRemoved.addListener(onBookmarkRemoved); | |
| 125 chrome.bookmarks.onImportBegan.addListener(onImportBegan); | |
| 126 chrome.bookmarks.onImportEnded.addListener(onImportEnded); | |
| 127 | 164 |
| 128 cr.sendWithPromise('getIncognitoAvailability') | 165 cr.sendWithPromise('getIncognitoAvailability') |
| 129 .then(onIncognitoAvailabilityChanged); | 166 .then(onIncognitoAvailabilityChanged); |
| 130 cr.addWebUIListener( | 167 cr.addWebUIListener( |
| 131 'incognito-availability-changed', onIncognitoAvailabilityChanged); | 168 'incognito-availability-changed', onIncognitoAvailabilityChanged); |
| 132 | 169 |
| 133 cr.sendWithPromise('getCanEditBookmarks').then(onCanEditBookmarksChanged); | 170 cr.sendWithPromise('getCanEditBookmarks').then(onCanEditBookmarksChanged); |
| 134 cr.addWebUIListener( | 171 cr.addWebUIListener( |
| 135 'can-edit-bookmarks-changed', onCanEditBookmarksChanged); | 172 'can-edit-bookmarks-changed', onCanEditBookmarksChanged); |
| 136 } | 173 } |
| 137 | 174 |
| 175 function destroy() { | |
| 176 listeners.forEach((listener) => listener.api.removeListener(listener.fn)); | |
| 177 cr.removeWebUIListener( | |
| 178 'incognito-availability-changed', onIncognitoAvailabilityChanged); | |
| 179 cr.removeWebUIListener( | |
| 180 'can-edit-bookmarks-changed', onCanEditBookmarksChanged); | |
| 181 } | |
| 182 | |
| 138 return { | 183 return { |
| 139 init: init, | 184 init: init, |
| 185 destroy: destroy, | |
| 186 trackUpdatedItems: trackUpdatedItems, | |
| 187 highlightUpdatedItems: highlightUpdatedItems, | |
| 140 }; | 188 }; |
| 141 }); | 189 }); |
| OLD | NEW |