Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 Polymer({ | |
| 6 is: 'bookmarks-store', | |
| 7 | |
| 8 properties: { | |
| 9 /** @type {BookmarkTreeNode} */ | |
| 10 rootNode: { | |
| 11 type: Object, | |
| 12 notify: true, | |
| 13 }, | |
| 14 | |
| 15 selectedId: { | |
| 16 type: String, | |
| 17 observer: 'updateSelectedNode_', | |
| 18 notify: true, | |
| 19 }, | |
| 20 | |
| 21 /** @type {BookmarkTreeNode} */ | |
| 22 selectedNode: { | |
| 23 type: Object, | |
| 24 notify: true, | |
| 25 readOnly: true, | |
| 26 }, | |
| 27 | |
| 28 idToNodeMap_: Object, | |
| 29 }, | |
| 30 | |
| 31 /** @private {Object} */ | |
| 32 documentListeners_: null, | |
| 33 | |
| 34 /** | |
| 35 * Initializes the store with data from the bookmarks API. | |
| 36 * Called by app on attached. | |
| 37 */ | |
| 38 initializeStore: function() { | |
|
tsergeant
2016/12/28 23:51:50
Move initializeStore below attached/detached
(the
angelayang
2016/12/29 02:53:34
Done.
| |
| 39 chrome.bookmarks.getTree(function(results) { | |
| 40 this.setupStore_(results[0]); | |
| 41 }.bind(this)); | |
| 42 }, | |
| 43 | |
| 44 /** @override */ | |
| 45 attached: function() { | |
| 46 this.documentListeners_ = { | |
| 47 'selected-folder-changed': this.selectedFolderChanged_.bind(this), | |
| 48 'folder-open-changed': this.folderOpenChanged_.bind(this), | |
| 49 }; | |
| 50 for (var event in this.documentListeners_) | |
| 51 document.addEventListener(event, this.documentListeners_[event]); | |
| 52 }, | |
| 53 | |
| 54 /** @override */ | |
| 55 detached: function() { | |
| 56 for (var event in this.documentListeners_) | |
| 57 document.removeEventListener(event, this.documentListeners_[event]); | |
| 58 }, | |
| 59 | |
| 60 /** | |
| 61 * @param {BookmarkTreeNode} rootNode | |
| 62 * @private | |
| 63 */ | |
| 64 setupStore_: function(rootNode) { | |
| 65 this.rootNode = rootNode; | |
| 66 this.idToNodeMap_ = {}; | |
| 67 this.rootNode.path = 'rootNode'; | |
| 68 this.initNodes_(this.rootNode); | |
| 69 this.fire('selected-folder-changed', this.rootNode.children[0].id); | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * Selects the folder specified by the event and deselects the previously | |
| 74 * selected folder. | |
| 75 * @param {CustomEvent} e | |
| 76 * @private | |
| 77 */ | |
| 78 selectedFolderChanged_: function(e) { | |
| 79 // Deselect the old folder if defined. | |
| 80 if (this.selectedId) | |
| 81 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | |
| 82 | |
| 83 var selectedId = /** @type {string} */ (e.detail); | |
| 84 var newFolder = this.idToNodeMap_[selectedId]; | |
| 85 this.set(newFolder.path + '.isSelected', true); | |
| 86 this.selectedId = selectedId; | |
| 87 }, | |
| 88 | |
| 89 /** | |
| 90 * @param {CustomEvent} e | |
|
calamity
2016/12/29 00:54:50
This could use with a comment since the method nam
angelayang
2016/12/29 02:53:34
Done.
| |
| 91 * @private | |
| 92 */ | |
| 93 folderOpenChanged_: function(e) { | |
|
calamity
2016/12/29 00:54:50
nit: Both these event handlers should be onEventNa
angelayang
2016/12/29 02:53:34
Done.
| |
| 94 var folder = this.idToNodeMap_[e.detail.id]; | |
| 95 this.set(folder.path + '.isOpen', e.detail.open); | |
| 96 if (folder.isOpen) | |
| 97 return; | |
| 98 | |
| 99 if (this.isAncestorOfSelected_(folder)) | |
|
tsergeant
2016/12/28 23:51:50
Personal style, I guess, but I would write this wi
angelayang
2016/12/29 02:53:34
Done.
| |
| 100 this.fire('selected-folder-changed', folder.id); | |
| 101 }, | |
| 102 | |
| 103 /** | |
| 104 * @param {BookmarkTreeNode} folder | |
| 105 * @private | |
| 106 * @return {boolean} | |
| 107 */ | |
| 108 isAncestorOfSelected_: function(folder) { | |
| 109 return this.selectedNode.path.startsWith(folder.path); | |
| 110 }, | |
| 111 | |
| 112 /** @private */ | |
| 113 updateSelectedNode_: function() { | |
| 114 var selectedNode = this.idToNodeMap_[this.selectedId]; | |
| 115 this.linkPaths('selectedNode', selectedNode.path); | |
| 116 this._setSelectedNode(selectedNode); | |
| 117 }, | |
| 118 | |
| 119 /** | |
| 120 * Initializes the nodes in the bookmarks tree as follows: | |
| 121 * - Populates |idToNodeMap_| with a mapping of all node ids to their | |
| 122 * corresponding BookmarkTreeNode. | |
| 123 * - Stores the path from the store to a node inside the node. | |
| 124 * - Sets all the nodes to not selected and open by default. | |
| 125 * @param {BookmarkTreeNode} bookmarkNode | |
| 126 * @private | |
| 127 */ | |
| 128 initNodes_: function(bookmarkNode) { | |
| 129 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | |
| 130 if (bookmarkNode.url) | |
| 131 return; | |
| 132 | |
| 133 bookmarkNode.isSelected = false; | |
| 134 bookmarkNode.isOpen = true; | |
| 135 for (var i = 0; i < bookmarkNode.children.length; i++) { | |
| 136 bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i; | |
| 137 this.initNodes_(bookmarkNode.children[i]); | |
| 138 } | |
| 139 }, | |
| 140 }); | |
| OLD | NEW |