1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('options', function() {
const Tree = cr.ui.Tree;
const TreeItem = cr.ui.TreeItem;
var treeLookup = {};
/**
* Creates a new tree item for sites data.
* @param {Object=} data Data used to create a cookie tree item.
* @constructor
* @extends {TreeItem}
*/
function CookieTreeItem(data) {
var treeItem = new TreeItem({
label: data.title,
data: data
});
treeItem.__proto__ = CookieTreeItem.prototype;
if (data.icon) {
treeItem.icon = data.icon;
}
treeItem.decorate();
return treeItem;
}
CookieTreeItem.prototype = {
__proto__: TreeItem.prototype,
/** @inheritDoc */
decorate: function() {
this.hasChildren = this.data.hasChildren;
},
/** @inheritDoc */
addAt: function(child, index) {
TreeItem.prototype.addAt.call(this, child, index);
if (child.data && child.data.id)
treeLookup[child.data.id] = child;
},
/** @inheritDoc */
remove: function(child) {
TreeItem.prototype.remove.call(this, child);
if (child.data && child.data.id)
delete treeLookup[child.data.id];
},
/**
* Clears all children.
*/
clear: function() {
// We might leave some garbage in treeLookup for removed children.
// But that should be okay because treeLookup is cleared when we
// reload the tree.
this.lastElementChild.textContent = '';
},
/**
* The tree path id.
* @type {string}
*/
get pathId() {
var parent = this.parentItem;
if (parent && parent instanceof CookieTreeItem) {
return parent.pathId + ',' + this.data.id;
} else {
return this.data.id;
}
},
/** @inheritDoc */
get expanded() {
return TreeItem.prototype.__lookupGetter__('expanded').call(this);
},
set expanded(b) {
if (b && this.expanded != b)
chrome.send('loadCookie', [this.pathId]);
TreeItem.prototype.__lookupSetter__('expanded').call(this, b);
}
};
/**
* Creates a new cookies tree.
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {Tree}
*/
var CookiesTree = cr.ui.define('tree');
CookiesTree.prototype = {
__proto__: Tree.prototype,
/** @inheritDoc */
addAt: function(child, index) {
Tree.prototype.addAt.call(this, child, index);
if (child.data && child.data.id)
treeLookup[child.data.id] = child;
},
/** @inheritDoc */
remove: function(child) {
Tree.prototype.remove.call(this, child);
if (child.data && child.data.id)
delete treeLookup[child.data.id];
},
/**
* Clears the tree.
*/
clear: function() {
// Remove all fields without recreating the object since other code
// references it.
for (var id in treeLookup) {
delete treeLookup[id];
}
this.textContent = '';
},
/**
* Add tree nodes by given parent.
* @param {Object} parent Parent node.
* @param {int} start Start index of where to insert nodes.
* @param {Array} nodesData Nodes data array.
*/
addByParent: function(parent, start, nodesData) {
if (!parent) {
return;
}
for (var i = 0; i < nodesData.length; ++i) {
parent.addAt(new CookieTreeItem(nodesData[i]), start + i);
}
cr.dispatchSimpleEvent(this, 'change');
},
/**
* Add tree nodes by parent id.
* @param {string} parentId Id of the parent node.
* @param {int} start Start index of where to insert nodes.
* @param {Array} nodesData Nodes data array.
*/
addByParentId: function(parentId, start, nodesData) {
var parent = parentId ? treeLookup[parentId] : this;
this.addByParent(parent, start, nodesData);
},
/**
* Removes tree nodes by parent id.
* @param {string} parentId Id of the parent node.
* @param {int} start Start index of nodes to remove.
* @param {int} count Number of nodes to remove.
*/
removeByParentId: function(parentId, start, count) {
var parent = parentId ? treeLookup[parentId] : this;
if (!parent) {
return;
}
for (; count > 0 && parent.items.length; --count) {
parent.remove(parent.items[start]);
}
cr.dispatchSimpleEvent(this, 'change');
},
/**
* Clears the tree.
*/
clear: function() {
// Remove all fields without recreating the object since other code
// references it.
for (var id in treeLookup){
delete treeLookup[id];
}
this.textContent = '';
},
/**
* Loads the immediate children of given parent node.
* @param {string} parentId Id of the parent node.
* @param {Array} children The immediate children of parent node.
*/
loadChildren: function(parentId, children) {
var parent = parentId ? treeLookup[parentId] : this;
if (!parent) {
return;
}
parent.clear();
this.addByParent(parent, 0, children);
}
};
return {
CookiesTree: CookiesTree
};
});
|