OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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 | |
arv (Not doing code reviews)
2011/03/09 18:42:47
Do you mind calling out what was changed here? I c
xiyuan
2011/03/09 21:33:31
Here's summary of what I changed:
- Change namespa
| |
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) { | |
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) { | |
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) { | |
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) { | |
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) { | |
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) { | |
230 $(args[0]).loadChildren(args[1], args[2]); | |
231 } | |
232 | |
233 return { | |
234 CookiesTree: CookiesTree | |
235 }; | |
236 }); | |
OLD | NEW |