Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 cr.define('ui', function() { | |
| 6 const Tree = cr.ui.Tree; | |
| 7 const TreeItem = cr.ui.TreeItem; | |
| 8 | |
| 9 /** | |
| 10 * Creates a new tree item for sites data. | |
| 11 * @param {Object=} data Data used to create a cookie tree item. | |
| 12 * @constructor | |
| 13 * @extends {TreeItem} | |
| 14 */ | |
| 15 function CookiesTreeItem(data) { | |
| 16 var treeItem = new TreeItem({ | |
| 17 label: data.title, | |
| 18 data: data | |
| 19 }); | |
| 20 treeItem.__proto__ = CookiesTreeItem.prototype; | |
| 21 | |
| 22 if (data.icon) { | |
|
Evan Stade
2011/03/10 18:27:59
no curlies
xiyuan
2011/03/10 19:24:00
Done.
| |
| 23 treeItem.icon = data.icon; | |
| 24 } | |
| 25 | |
| 26 treeItem.decorate(); | |
| 27 return treeItem; | |
| 28 } | |
| 29 | |
| 30 CookiesTreeItem.prototype = { | |
| 31 __proto__: TreeItem.prototype, | |
| 32 | |
| 33 /** @inheritDoc */ | |
| 34 decorate: function() { | |
| 35 this.hasChildren = this.data.hasChildren; | |
| 36 }, | |
| 37 | |
| 38 /** @inheritDoc */ | |
| 39 addAt: function(child, index) { | |
| 40 TreeItem.prototype.addAt.call(this, child, index); | |
| 41 if (child.data && child.data.id) | |
| 42 this.tree.treeLookup[child.data.id] = child; | |
| 43 }, | |
| 44 | |
| 45 /** @inheritDoc */ | |
| 46 remove: function(child) { | |
| 47 TreeItem.prototype.remove.call(this, child); | |
| 48 if (child.data && child.data.id) | |
| 49 delete this.tree.treeLookup[child.data.id]; | |
| 50 }, | |
| 51 | |
| 52 /** | |
| 53 * Clears all children. | |
| 54 */ | |
| 55 clear: function() { | |
| 56 // We might leave some garbage in treeLookup for removed children. | |
| 57 // But that should be okay because treeLookup is cleared when we | |
| 58 // reload the tree. | |
| 59 this.lastElementChild.textContent = ''; | |
| 60 }, | |
| 61 | |
| 62 /** | |
| 63 * The tree path id. | |
| 64 * @type {string} | |
| 65 */ | |
| 66 get pathId() { | |
| 67 var parent = this.parentItem; | |
| 68 if (parent && parent instanceof CookiesTreeItem) { | |
|
Evan Stade
2011/03/10 18:27:59
no curlies
xiyuan
2011/03/10 19:24:00
Done.
| |
| 69 return parent.pathId + ',' + this.data.id; | |
| 70 } else { | |
| 71 return this.data.id; | |
| 72 } | |
| 73 }, | |
| 74 | |
| 75 /** @inheritDoc */ | |
| 76 get expanded() { | |
| 77 return TreeItem.prototype.__lookupGetter__('expanded').call(this); | |
| 78 }, | |
| 79 set expanded(b) { | |
| 80 if (b && this.expanded != b) | |
| 81 chrome.send(this.tree.loadChildrenMessage, [this.pathId]); | |
| 82 | |
| 83 TreeItem.prototype.__lookupSetter__('expanded').call(this, b); | |
| 84 } | |
| 85 }; | |
| 86 | |
| 87 /** | |
| 88 * Creates a new cookies tree. | |
| 89 * @param {Object=} opt_propertyBag Optional properties. | |
| 90 * @constructor | |
| 91 * @extends {Tree} | |
| 92 */ | |
| 93 var CookiesTree = cr.ui.define('tree'); | |
| 94 | |
| 95 CookiesTree.prototype = { | |
| 96 __proto__: Tree.prototype, | |
| 97 | |
| 98 /* | |
| 99 * Per-tree dict to map from data.id to tree node. | |
| 100 */ | |
| 101 treeLookup_: 0, | |
| 102 get treeLookup() { | |
| 103 if (!this.treeLookup_) | |
| 104 this.treeLookup_ = {}; | |
| 105 return this.treeLookup_; | |
| 106 }, | |
| 107 | |
| 108 /** @inheritDoc */ | |
| 109 addAt: function(child, index) { | |
| 110 Tree.prototype.addAt.call(this, child, index); | |
| 111 if (child.data && child.data.id) | |
| 112 this.treeLookup[child.data.id] = child; | |
| 113 }, | |
| 114 | |
| 115 /** @inheritDoc */ | |
| 116 remove: function(child) { | |
| 117 Tree.prototype.remove.call(this, child); | |
| 118 if (child.data && child.data.id) | |
| 119 delete this.treeLookup[child.data.id]; | |
| 120 }, | |
| 121 | |
| 122 /** | |
| 123 * Add tree nodes by given parent. | |
| 124 * @param {Object} parent Parent node. | |
| 125 * @param {int} start Start index of where to insert nodes. | |
| 126 * @param {Array} nodesData Nodes data array. | |
| 127 */ | |
| 128 addByParent: function(parent, start, nodesData) { | |
| 129 if (!parent) { | |
|
Evan Stade
2011/03/10 18:27:59
no curlies
xiyuan
2011/03/10 19:24:00
Done.
| |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 for (var i = 0; i < nodesData.length; ++i) { | |
| 134 parent.addAt(new CookiesTreeItem(nodesData[i]), start + i); | |
| 135 } | |
| 136 | |
| 137 cr.dispatchSimpleEvent(this, 'change'); | |
| 138 }, | |
| 139 | |
| 140 /** | |
| 141 * Add tree nodes by parent id. | |
| 142 * @param {string} parentId Id of the parent node. | |
| 143 * @param {int} start Start index of where to insert nodes. | |
| 144 * @param {Array} nodesData Nodes data array. | |
| 145 */ | |
| 146 addByParentId: function(parentId, start, nodesData) { | |
| 147 var parent = parentId ? this.treeLookup[parentId] : this; | |
| 148 this.addByParent(parent, start, nodesData); | |
| 149 }, | |
| 150 | |
| 151 /** | |
| 152 * Removes tree nodes by parent id. | |
| 153 * @param {string} parentId Id of the parent node. | |
| 154 * @param {int} start Start index of nodes to remove. | |
| 155 * @param {int} count Number of nodes to remove. | |
| 156 */ | |
| 157 removeByParentId: function(parentId, start, count) { | |
| 158 var parent = parentId ? this.treeLookup[parentId] : this; | |
| 159 if (!parent) { | |
|
Evan Stade
2011/03/10 18:27:59
no curlies
xiyuan
2011/03/10 19:24:00
Done.
| |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 for (; count > 0 && parent.items.length; --count) { | |
| 164 parent.remove(parent.items[start]); | |
| 165 } | |
| 166 | |
| 167 cr.dispatchSimpleEvent(this, 'change'); | |
| 168 }, | |
| 169 | |
| 170 /** | |
| 171 * Clears the tree. | |
| 172 */ | |
| 173 clear: function() { | |
| 174 // Remove all fields without recreating the object since other code | |
| 175 // references it. | |
| 176 for (var id in this.treeLookup){ | |
| 177 delete this.treeLookup[id]; | |
| 178 } | |
| 179 this.textContent = ''; | |
| 180 }, | |
| 181 | |
| 182 /** | |
| 183 * Unique 'loadChildren' callback message name to send request to | |
| 184 * underlying CookiesTreeAdapter. | |
| 185 * @type {String} | |
| 186 */ | |
| 187 loadChildrenMessage_ : 0, | |
| 188 get loadChildrenMessage() { | |
| 189 return this.loadChildrenMessage_; | |
| 190 }, | |
| 191 | |
| 192 /** | |
| 193 * Set callback message name. | |
| 194 * @param {string} loadChildren Message name for 'loadChildren' request. | |
| 195 */ | |
| 196 setCallback: function(loadChildren) { | |
| 197 this.loadChildrenMessage_ = loadChildren; | |
| 198 }, | |
| 199 | |
| 200 /** | |
| 201 * Loads the immediate children of given parent node. | |
| 202 * @param {string} parentId Id of the parent node. | |
| 203 * @param {Array} children The immediate children of parent node. | |
| 204 */ | |
| 205 loadChildren: function(parentId, children) { | |
| 206 var parent = parentId ? this.treeLookup[parentId] : this; | |
| 207 if (!parent) { | |
|
Evan Stade
2011/03/10 18:27:59
no curlies
is there a legitimate case where paren
xiyuan
2011/03/10 19:24:00
Think you are right. If all code runs as expected,
| |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 parent.clear(); | |
| 212 this.addByParent(parent, 0, children); | |
| 213 } | |
| 214 }; | |
| 215 | |
| 216 // CookiesTreeAdapter callbacks. | |
| 217 CookiesTree.setCallback = function(args) { | |
| 218 $(args[0]).setCallback(args[1]); | |
| 219 } | |
| 220 | |
| 221 CookiesTree.onTreeItemAdded = function(args) { | |
| 222 $(args[0]).addByParentId(args[1], args[2], args[3]); | |
| 223 } | |
| 224 | |
| 225 CookiesTree.onTreeItemRemoved = function(args) { | |
| 226 $(args[0]).removeByParentId(args[1], args[2], args[3]); | |
| 227 } | |
| 228 | |
| 229 CookiesTree.loadChildren = function(args) { | |
|
Evan Stade
2011/03/10 18:27:59
this name is a little over-used, can you think of
xiyuan
2011/03/10 19:24:00
Good suggestions. Done.
| |
| 230 $(args[0]).loadChildren(args[1], args[2]); | |
| 231 } | |
| 232 | |
| 233 return { | |
| 234 CookiesTree: CookiesTree | |
| 235 }; | |
| 236 }); | |
| OLD | NEW |