Chromium Code Reviews| Index: chrome/browser/resources/md_bookmarks/store.js |
| diff --git a/chrome/browser/resources/md_bookmarks/store.js b/chrome/browser/resources/md_bookmarks/store.js |
| index 15b270fbe23c31b922c2bb02f27f902a2a59c9db..6c3fbdad0eddf1faf11a67570def95c137e08c64 100644 |
| --- a/chrome/browser/resources/md_bookmarks/store.js |
| +++ b/chrome/browser/resources/md_bookmarks/store.js |
| @@ -37,6 +37,8 @@ var BookmarksStore = Polymer({ |
| }, |
| idToNodeMap_: Object, |
| + |
| + prevSelectedItemIndex_: Number, |
| }, |
| /** @private {Object} */ |
| @@ -45,7 +47,12 @@ var BookmarksStore = Polymer({ |
| /** @override */ |
| attached: function() { |
| this.documentListeners_ = { |
| + 'ctrl-select-multiple-items': |
| + this.onMultipleItemsCtrlSelected_.bind(this), |
| + 'select-single-item': this.onSingleItemSelected_.bind(this), |
| 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| + 'shift-select-multiple-items': |
| + this.onMultipleItemsShiftSelected_.bind(this), |
| 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
|
angelayang
2017/01/17 00:46:51
Nice, since you've got yours in alphabetical order
jiaxi
2017/01/17 01:53:33
Done.
|
| 'search-term-changed': this.onSearchTermChanged_.bind(this), |
| }; |
| @@ -72,8 +79,8 @@ var BookmarksStore = Polymer({ |
| chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); |
| }, |
| -//////////////////////////////////////////////////////////////////////////////// |
| -// bookmarks-store, private: |
| + ////////////////////////////////////////////////////////////////////////////// |
| + // bookmarks-store, private: |
| /** |
| * @param {BookmarkTreeNode} rootNode |
| @@ -91,7 +98,8 @@ var BookmarksStore = Polymer({ |
| /** @private */ |
| deselectFolders_: function() { |
| this.unlinkPaths('displayedList'); |
| - this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| + this.set( |
| + this.idToNodeMap_[this.selectedId].path + '.isSelectedFolder', false); |
| this.selectedId = null; |
| }, |
| @@ -114,10 +122,23 @@ var BookmarksStore = Polymer({ |
| this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| } else { |
| chrome.bookmarks.search(this.searchTerm, function(results) { |
| + if (this.displayedList) |
| + this.clearSelectedItems_(); |
| + |
| + this.prevSelectedItemIndex_ = undefined; |
| + |
| if (this.selectedId) |
| this.deselectFolders_(); |
| - this._setDisplayedList(results); |
| + var searchedList = []; |
| + if (results) { |
|
angelayang
2017/01/17 00:46:51
Can we remove this outer if? or does no results gi
jiaxi
2017/01/17 01:53:33
It returns an empty array []
|
| + for (var i = 0; i < results.length; i++) { |
| + searchedList.push(this.idToNodeMap_[results[i].id]); |
| + this.linkPaths( |
| + 'displayedList.#' + i, this.idToNodeMap_[results[i].id].path); |
| + } |
| + } |
| + this._setDisplayedList(searchedList); |
| }.bind(this)); |
| } |
| }, |
| @@ -128,6 +149,11 @@ var BookmarksStore = Polymer({ |
| if (!this.selectedId) |
| return; |
| + if (this.displayedList) |
| + this.clearSelectedItems_(); |
| + |
| + this.prevSelectedItemIndex_ = undefined; |
| + |
| var selectedNode = this.idToNodeMap_[this.selectedId]; |
| this.linkPaths('displayedList', selectedNode.path + '.children'); |
| this._setDisplayedList(selectedNode.children); |
| @@ -150,8 +176,38 @@ var BookmarksStore = Polymer({ |
| delete this.idToNodeMap_[id]; |
| }, |
| -//////////////////////////////////////////////////////////////////////////////// |
| -// bookmarks-store, bookmarks API event listeners: |
| + /** |
| + * Remove all selected items in the list. |
| + * @private |
| + */ |
| + clearSelectedItems_: function() { |
| + for (var i = 0; i < this.displayedList.length; i++) { |
| + if (this.displayedList[i].isSelected == true) { |
|
angelayang
2017/01/17 00:46:51
can we make this, if (this.displayedList[i].isSele
jiaxi
2017/01/17 01:53:33
Done.
|
| + this.set( |
| + this.idToNodeMap_[this.displayedList[i].id].path + |
| + '.isSelected', |
| + false); |
| + } |
| + } |
| + }, |
| + |
| + /** |
| + * Return the index in the search result of an item. |
| + * @param {BookmarkTreeNode} item |
| + * @return {number} |
| + * @private |
| + */ |
| + getSelectedIndex_: function(item) { |
| + if (this.searchTerm == '') |
| + return item.index; |
| + |
| + for (var i = 0; i < this.displayedList.length; i++) { |
| + if (this.displayedList[i].id == item.id) |
| + return i; |
| + } |
| + }, |
| + ////////////////////////////////////////////////////////////////////////////// |
| + //////// bookmarks-store, bookmarks API event listeners: |
| /** |
| * Callback for when a bookmark node is removed. |
| @@ -192,8 +248,8 @@ var BookmarksStore = Polymer({ |
| this.updateSearchDisplay_(); |
| }, |
| -//////////////////////////////////////////////////////////////////////////////// |
| -// bookmarks-store, bookmarks app event listeners: |
| + ////////////////////////////////////////////////////////////////////////////// |
| + // bookmarks-store, bookmarks app event listeners: |
| /** |
| * @param {Event} e |
| @@ -215,11 +271,12 @@ var BookmarksStore = Polymer({ |
| // Deselect the old folder if defined. |
| if (this.selectedId) |
| - this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| + this.set( |
| + this.idToNodeMap_[this.selectedId].path + '.isSelectedFolder', false); |
|
jiaxi
2017/01/16 23:48:39
?
jiaxi
2017/01/16 23:49:22
Oops wrong click, ignore me.
|
| var selectedId = /** @type {string} */ (e.detail); |
| var newFolder = this.idToNodeMap_[selectedId]; |
| - this.set(newFolder.path + '.isSelected', true); |
| + this.set(newFolder.path + '.isSelectedFolder', true); |
| this.selectedId = selectedId; |
| }, |
| @@ -234,15 +291,62 @@ var BookmarksStore = Polymer({ |
| if (!folder.isOpen && this.isAncestorOfSelected_(folder)) |
| this.fire('selected-folder-changed', folder.id); |
| }, |
| + |
| + /** |
| + * Select a single item in the list. |
| + * @param {CustomEvent} e |
| + * @private |
| + */ |
| + onSingleItemSelected_: function(e) { |
| + this.clearSelectedItems_(); |
| + this.set(this.idToNodeMap_[e.detail.item.id].path + '.isSelected', true); |
| + this.prevSelectedItemIndex_ = this.getSelectedIndex_(e.detail.item); |
| + }, |
| + |
| + /** |
| + * Selected multiple items based on |prevSelectedItemIndex_| and the selected |
| + * item. If |prevSelectedItemIndex_| is not set, single select the item. |
| + * @param {CustomEvent} e |
| + * @private |
| + */ |
| + onMultipleItemsShiftSelected_: function(e) { |
| + this.clearSelectedItems_(); |
| + var startIndex, endIndex; |
| + if (this.prevSelectedItemIndex_ == undefined) { |
| + this.prevSelectedItemIndex_ = this.getSelectedIndex_(e.detail.item); |
| + startIndex = this.prevSelectedItemIndex_; |
| + endIndex = this.prevSelectedItemIndex_; |
| + } else { |
| + var currIndex = this.getSelectedIndex_(e.detail.item); |
| + startIndex = Math.min(this.prevSelectedItemIndex_, currIndex); |
| + endIndex = Math.max(this.prevSelectedItemIndex_, currIndex); |
| + } |
| + for (var i = startIndex; i <= endIndex; i++) |
| + this.set( |
| + this.idToNodeMap_[this.displayedList[i].id].path + |
| + '.isSelected', |
| + true); |
| + }, |
| + |
| + /** |
| + * Select multiple items with the index of the last elected item as |
| + * |prevSelectedItemIndex_|. |
| + * @param {CustomEvent} e |
| + * @private |
| + */ |
| + onMultipleItemsCtrlSelected_: function(e) { |
| + this.set(this.idToNodeMap_[e.detail.item.id].path + '.isSelected', true); |
| + this.prevSelectedItemIndex_ = this.getSelectedIndex_(e.detail.item); |
| + }, |
| }); |
| //////////////////////////////////////////////////////////////////////////////// |
| // bookmarks-store, static methods: |
| /** |
| - * Stores the path from the store to a node inside the node. |
| - * @param {BookmarkTreeNode} bookmarkNode |
| - * @param {number} startIndex |
| +* Stores the path from the store to a node inside the node. |
| +* @param {BookmarkTreeNode} bookmarkNode |
| +* @param {number} startIndex |
| */ |
| BookmarksStore.generatePaths = function(bookmarkNode, startIndex) { |
| if (!bookmarkNode.children) |
| @@ -263,13 +367,14 @@ BookmarksStore.generatePaths = function(bookmarkNode, startIndex) { |
| * @param {Object=} idToNodeMap |
| */ |
| BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) { |
| + bookmarkNode.isSelected = false; |
| if (idToNodeMap) |
| idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| if (bookmarkNode.url) |
| return; |
| - bookmarkNode.isSelected = false; |
| + bookmarkNode.isSelectedFolder = false; |
| bookmarkNode.isOpen = true; |
| for (var i = 0; i < bookmarkNode.children.length; i++) |
| BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |