| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 var BookmarksStore = Polymer({ | 5 var BookmarksStore = Polymer({ |
| 6 is: 'bookmarks-store', | 6 is: 'bookmarks-store', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 /** @type {BookmarkTreeNode} */ | 9 /** @type {BookmarkTreeNode} */ |
| 10 rootNode: { | 10 rootNode: { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 }, | 48 }, |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Initializes the store with data from the bookmarks API. | 51 * Initializes the store with data from the bookmarks API. |
| 52 * Called by app on attached. | 52 * Called by app on attached. |
| 53 */ | 53 */ |
| 54 initializeStore: function() { | 54 initializeStore: function() { |
| 55 chrome.bookmarks.getTree(function(results) { | 55 chrome.bookmarks.getTree(function(results) { |
| 56 this.setupStore_(results[0]); | 56 this.setupStore_(results[0]); |
| 57 }.bind(this)); | 57 }.bind(this)); |
| 58 // Attach bookmarks API listeners. |
| 58 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); | 59 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); |
| 60 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); |
| 59 }, | 61 }, |
| 60 | 62 |
| 63 //////////////////////////////////////////////////////////////////////////////// |
| 64 // bookmarks-store, private: |
| 65 |
| 61 /** | 66 /** |
| 62 * @param {BookmarkTreeNode} rootNode | 67 * @param {BookmarkTreeNode} rootNode |
| 63 * @private | 68 * @private |
| 64 */ | 69 */ |
| 65 setupStore_: function(rootNode) { | 70 setupStore_: function(rootNode) { |
| 66 this.rootNode = rootNode; | 71 this.rootNode = rootNode; |
| 67 this.idToNodeMap_ = {}; | 72 this.idToNodeMap_ = {}; |
| 68 this.rootNode.path = 'rootNode'; | 73 this.rootNode.path = 'rootNode'; |
| 69 BookmarksStore.generatePaths(rootNode, 0); | 74 BookmarksStore.generatePaths(rootNode, 0); |
| 70 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 75 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
| 71 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 76 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 72 }, | 77 }, |
| 73 | 78 |
| 74 /** | 79 /** |
| 75 * Selects the folder specified by the event and deselects the previously | |
| 76 * selected folder. | |
| 77 * @param {CustomEvent} e | |
| 78 * @private | |
| 79 */ | |
| 80 onSelectedFolderChanged_: function(e) { | |
| 81 // Deselect the old folder if defined. | |
| 82 if (this.selectedId) | |
| 83 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | |
| 84 | |
| 85 var selectedId = /** @type {string} */ (e.detail); | |
| 86 var newFolder = this.idToNodeMap_[selectedId]; | |
| 87 this.set(newFolder.path + '.isSelected', true); | |
| 88 this.selectedId = selectedId; | |
| 89 }, | |
| 90 | |
| 91 /** | |
| 92 * Handles events that open and close folders. | |
| 93 * @param {CustomEvent} e | |
| 94 * @private | |
| 95 */ | |
| 96 onFolderOpenChanged_: function(e) { | |
| 97 var folder = this.idToNodeMap_[e.detail.id]; | |
| 98 this.set(folder.path + '.isOpen', e.detail.open); | |
| 99 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) | |
| 100 this.fire('selected-folder-changed', folder.id); | |
| 101 }, | |
| 102 | |
| 103 /** | |
| 104 * @param {BookmarkTreeNode} folder | 80 * @param {BookmarkTreeNode} folder |
| 105 * @private | 81 * @private |
| 106 * @return {boolean} | 82 * @return {boolean} |
| 107 */ | 83 */ |
| 108 isAncestorOfSelected_: function(folder) { | 84 isAncestorOfSelected_: function(folder) { |
| 109 return this.selectedNode.path.startsWith(folder.path); | 85 return this.selectedNode.path.startsWith(folder.path); |
| 110 }, | 86 }, |
| 111 | 87 |
| 112 /** @private */ | 88 /** @private */ |
| 113 updateSelectedNode_: function() { | 89 updateSelectedNode_: function() { |
| 114 var selectedNode = this.idToNodeMap_[this.selectedId]; | 90 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 115 this.linkPaths('selectedNode', selectedNode.path); | 91 this.linkPaths('selectedNode', selectedNode.path); |
| 116 this._setSelectedNode(selectedNode); | 92 this._setSelectedNode(selectedNode); |
| 117 }, | 93 }, |
| 118 | 94 |
| 119 /** | 95 /** |
| 96 * Remove all descendants of a given node from the map. |
| 97 * @param {string} id |
| 98 * @private |
| 99 */ |
| 100 removeDescendantsFromMap_: function(id) { |
| 101 var node = this.idToNodeMap_[id]; |
| 102 if (!node) |
| 103 return; |
| 104 |
| 105 if (node.children) { |
| 106 for (var i = 0; i < node.children.length; i++) |
| 107 this.removeDescendantsFromMap_(node.children[i].id); |
| 108 } |
| 109 delete this.idToNodeMap_[id]; |
| 110 }, |
| 111 |
| 112 //////////////////////////////////////////////////////////////////////////////// |
| 113 // bookmarks-store, bookmarks API event listeners: |
| 114 |
| 115 /** |
| 120 * Callback for when a bookmark node is removed. | 116 * Callback for when a bookmark node is removed. |
| 121 * If a folder is selected or is an ancestor of a selected folder, the parent | 117 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 122 * of the removed folder will be selected. | 118 * of the removed folder will be selected. |
| 123 * @param {string} id The id of the removed bookmark node. | 119 * @param {string} id The id of the removed bookmark node. |
| 124 * @param {!{index: number, | 120 * @param {!{index: number, |
| 125 * parentId: string, | 121 * parentId: string, |
| 126 * node: BookmarkTreeNode}} removeInfo | 122 * node: BookmarkTreeNode}} removeInfo |
| 127 */ | 123 */ |
| 128 onBookmarkRemoved_: function(id, removeInfo) { | 124 onBookmarkRemoved_: function(id, removeInfo) { |
| 129 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | 125 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) |
| 130 this.fire('selected-folder-changed', removeInfo.parentId); | 126 this.fire('selected-folder-changed', removeInfo.parentId); |
| 131 | 127 |
| 132 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | 128 var parentNode = this.idToNodeMap_[removeInfo.parentId]; |
| 133 this.splice(parentNode.path + '.children', removeInfo.index, 1); | 129 this.splice(parentNode.path + '.children', removeInfo.index, 1); |
| 134 this.removeDescendantsFromMap_(id); | 130 this.removeDescendantsFromMap_(id); |
| 135 BookmarksStore.generatePaths(parentNode, removeInfo.index); | 131 BookmarksStore.generatePaths(parentNode, removeInfo.index); |
| 136 }, | 132 }, |
| 137 | 133 |
| 138 /** | 134 /** |
| 139 * Remove all descendants of a given node from the map. | 135 * Called when the title of a bookmark changes. |
| 140 * @param {string} id | 136 * @param {string} id The id of changed bookmark node. |
| 137 * @param {!Object} changeInfo |
| 138 */ |
| 139 onBookmarkChanged_: function(id, changeInfo) { |
| 140 if (changeInfo.title) |
| 141 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); |
| 142 if (changeInfo.url) |
| 143 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); |
| 144 }, |
| 145 |
| 146 //////////////////////////////////////////////////////////////////////////////// |
| 147 // bookmarks-store, bookmarks app event listeners: |
| 148 |
| 149 /** |
| 150 * Selects the folder specified by the event and deselects the previously |
| 151 * selected folder. |
| 152 * @param {CustomEvent} e |
| 141 * @private | 153 * @private |
| 142 */ | 154 */ |
| 143 removeDescendantsFromMap_: function(id) { | 155 onSelectedFolderChanged_: function(e) { |
| 144 var node = this.idToNodeMap_[id]; | 156 // Deselect the old folder if defined. |
| 145 if (!node) | 157 if (this.selectedId) |
| 146 return; | 158 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 147 | 159 |
| 148 if (node.children) { | 160 var selectedId = /** @type {string} */ (e.detail); |
| 149 for (var i = 0; i < node.children.length; i++) | 161 var newFolder = this.idToNodeMap_[selectedId]; |
| 150 this.removeDescendantsFromMap_(node.children[i].id); | 162 this.set(newFolder.path + '.isSelected', true); |
| 151 } | 163 this.selectedId = selectedId; |
| 152 delete this.idToNodeMap_[id]; | 164 }, |
| 165 |
| 166 /** |
| 167 * Handles events that open and close folders. |
| 168 * @param {CustomEvent} e |
| 169 * @private |
| 170 */ |
| 171 onFolderOpenChanged_: function(e) { |
| 172 var folder = this.idToNodeMap_[e.detail.id]; |
| 173 this.set(folder.path + '.isOpen', e.detail.open); |
| 174 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) |
| 175 this.fire('selected-folder-changed', folder.id); |
| 153 }, | 176 }, |
| 154 }); | 177 }); |
| 155 | 178 |
| 179 //////////////////////////////////////////////////////////////////////////////// |
| 180 // bookmarks-store, static methods: |
| 181 |
| 156 /** | 182 /** |
| 157 * Stores the path from the store to a node inside the node. | 183 * Stores the path from the store to a node inside the node. |
| 158 * @param {BookmarkTreeNode} bookmarkNode | 184 * @param {BookmarkTreeNode} bookmarkNode |
| 159 * @param {number} startIndex | 185 * @param {number} startIndex |
| 160 */ | 186 */ |
| 161 BookmarksStore.generatePaths = function(bookmarkNode, startIndex) { | 187 BookmarksStore.generatePaths = function(bookmarkNode, startIndex) { |
| 162 if (!bookmarkNode.children) | 188 if (!bookmarkNode.children) |
| 163 return; | 189 return; |
| 164 | 190 |
| 165 for (var i = startIndex; i < bookmarkNode.children.length; i++) { | 191 for (var i = startIndex; i < bookmarkNode.children.length; i++) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 181 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 207 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 182 | 208 |
| 183 if (bookmarkNode.url) | 209 if (bookmarkNode.url) |
| 184 return; | 210 return; |
| 185 | 211 |
| 186 bookmarkNode.isSelected = false; | 212 bookmarkNode.isSelected = false; |
| 187 bookmarkNode.isOpen = true; | 213 bookmarkNode.isOpen = true; |
| 188 for (var i = 0; i < bookmarkNode.children.length; i++) | 214 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 189 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 215 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 190 }; | 216 }; |
| OLD | NEW |