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 /** @override */ | |
| 35 attached: function() { | |
| 36 this.documentListeners_ = { | |
| 37 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | |
| 38 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | |
| 39 }; | |
| 40 for (var event in this.documentListeners_) | |
| 41 document.addEventListener(event, this.documentListeners_[event]); | |
| 42 }, | |
| 43 | |
| 44 /** @override */ | |
| 45 detached: function() { | |
| 46 for (var event in this.documentListeners_) | |
| 47 document.removeEventListener(event, this.documentListeners_[event]); | |
| 48 }, | |
| 49 | |
| 50 /** | |
| 51 * Initializes the store with data from the bookmarks API. | |
| 52 * Called by app on attached. | |
| 53 */ | |
| 54 initializeStore: function() { | |
| 55 chrome.bookmarks.getTree(function(results) { | |
| 56 this.setupStore_(results[0]); | |
| 57 }.bind(this)); | |
| 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 onSelectedFolderChanged_: 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 * Closes or opens a folder as specified by the event. | |
| 91 * Selects a closed folder if it was an ancestor of a selected. | |
|
calamity
2016/12/29 03:47:21
I think:
Handles events that open and close folde
angelayang
2016/12/29 05:00:15
Done.
| |
| 92 * @param {CustomEvent} e | |
| 93 * @private | |
| 94 */ | |
| 95 onFolderOpenChanged_: function(e) { | |
| 96 var folder = this.idToNodeMap_[e.detail.id]; | |
| 97 this.set(folder.path + '.isOpen', e.detail.open); | |
| 98 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) | |
| 99 this.fire('selected-folder-changed', folder.id); | |
| 100 }, | |
| 101 | |
| 102 /** | |
| 103 * @param {BookmarkTreeNode} folder | |
| 104 * @private | |
| 105 * @return {boolean} | |
| 106 */ | |
| 107 isAncestorOfSelected_: function(folder) { | |
| 108 return this.selectedNode.path.startsWith(folder.path); | |
| 109 }, | |
| 110 | |
| 111 /** @private */ | |
| 112 updateSelectedNode_: function() { | |
| 113 var selectedNode = this.idToNodeMap_[this.selectedId]; | |
| 114 this.linkPaths('selectedNode', selectedNode.path); | |
| 115 this._setSelectedNode(selectedNode); | |
| 116 }, | |
| 117 | |
| 118 /** | |
| 119 * Initializes the nodes in the bookmarks tree as follows: | |
| 120 * - Populates |idToNodeMap_| with a mapping of all node ids to their | |
| 121 * corresponding BookmarkTreeNode. | |
| 122 * - Stores the path from the store to a node inside the node. | |
| 123 * - Sets all the nodes to not selected and open by default. | |
| 124 * @param {BookmarkTreeNode} bookmarkNode | |
| 125 * @private | |
| 126 */ | |
| 127 initNodes_: function(bookmarkNode) { | |
| 128 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | |
| 129 if (bookmarkNode.url) | |
| 130 return; | |
| 131 | |
| 132 bookmarkNode.isSelected = false; | |
| 133 bookmarkNode.isOpen = true; | |
| 134 for (var i = 0; i < bookmarkNode.children.length; i++) { | |
| 135 bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i; | |
| 136 this.initNodes_(bookmarkNode.children[i]); | |
| 137 } | |
| 138 }, | |
| 139 }); | |
| OLD | NEW |