OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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('options', function() { | |
6 const DeletableItemList = options.DeletableItemList; | |
7 const DeletableItem = options.DeletableItem; | |
8 const ArrayDataModel = cr.ui.ArrayDataModel; | |
9 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | |
10 | |
11 // This structure maps the various cookie type names from C++ (hence the | |
12 // underscores) to arrays of the different types of data each has, along with | |
13 // the i18n name for the description of that data type. | |
14 const cookieInfo = { | |
15 'cookie': [ ['name', 'label_cookie_name'], | |
16 ['content', 'label_cookie_content'], | |
17 ['domain', 'label_cookie_domain'], | |
18 ['path', 'label_cookie_path'], | |
19 ['sendfor', 'label_cookie_send_for'], | |
20 ['accessibleToScript', 'label_cookie_accessible_to_script'], | |
21 ['created', 'label_cookie_created'], | |
22 ['expires', 'label_cookie_expires'] ], | |
23 'app_cache': [ ['manifest', 'label_app_cache_manifest'], | |
24 ['size', 'label_local_storage_size'], | |
25 ['created', 'label_cookie_created'], | |
26 ['accessed', 'label_cookie_last_accessed'] ], | |
27 'database': [ ['name', 'label_cookie_name'], | |
28 ['desc', 'label_webdb_desc'], | |
29 ['size', 'label_local_storage_size'], | |
30 ['modified', 'label_local_storage_last_modified'] ], | |
31 'local_storage': [ ['origin', 'label_local_storage_origin'], | |
32 ['size', 'label_local_storage_size'], | |
33 ['modified', 'label_local_storage_last_modified'] ], | |
34 'indexed_db': [ ['origin', 'label_indexed_db_origin'], | |
35 ['size', 'label_indexed_db_size'], | |
36 ['modified', 'label_indexed_db_last_modified'] ], | |
37 'file_system': [ ['origin', 'label_file_system_origin'], | |
38 ['persistent', 'label_file_system_persistent_usage' ], | |
39 ['temporary', 'label_file_system_temporary_usage' ] ], | |
40 }; | |
41 | |
42 const localStrings = new LocalStrings(); | |
43 | |
44 /** | |
45 * Returns the item's height, like offsetHeight but such that it works better | |
46 * when the page is zoomed. See the similar calculation in @{code cr.ui.List}. | |
47 * This version also accounts for the animation done in this file. | |
48 * @param {Element} item The item to get the height of. | |
49 * @return {number} The height of the item, calculated with zooming in mind. | |
50 */ | |
51 function getItemHeight(item) { | |
52 var height = item.style.height; | |
53 // Use the fixed animation target height if set, in case the element is | |
54 // currently being animated and we'd get an intermediate height below. | |
55 if (height && height.substr(-2) == 'px') | |
56 return parseInt(height.substr(0, height.length - 2)); | |
57 return item.getBoundingClientRect().height; | |
58 } | |
59 | |
60 /** | |
61 * Create tree nodes for the objects in the data array, and insert them all | |
62 * into the given list using its @{code splice} method at the given index. | |
63 * @param {Array.<Object>} data The data objects for the nodes to add. | |
64 * @param {number} start The index at which to start inserting the nodes. | |
65 * @return {Array.<CookieTreeNode>} An array of CookieTreeNodes added. | |
66 */ | |
67 function spliceTreeNodes(data, start, list) { | |
68 var nodes = data.map(function(x) { return new CookieTreeNode(x); }); | |
69 // Insert [start, 0] at the beginning of the array of nodes, making it | |
70 // into the arguments we want to pass to @{code list.splice} below. | |
71 nodes.splice(0, 0, start, 0); | |
72 list.splice.apply(list, nodes); | |
73 // Remove the [start, 0] prefix and return the array of nodes. | |
74 nodes.splice(0, 2); | |
75 return nodes; | |
76 } | |
77 | |
78 var parentLookup = {}; | |
79 var lookupRequests = {}; | |
80 | |
81 /** | |
82 * Creates a new list item for sites data. Note that these are created and | |
83 * destroyed lazily as they scroll into and out of view, so they must be | |
84 * stateless. We cache the expanded item in @{code CookiesList} though, so it | |
85 * can keep state. (Mostly just which item is selected.) | |
86 * @param {Object} origin Data used to create a cookie list item. | |
87 * @param {CookiesList} list The list that will contain this item. | |
88 * @constructor | |
89 * @extends {DeletableItem} | |
90 */ | |
91 function CookieListItem(origin, list) { | |
92 var listItem = new DeletableItem(null); | |
93 listItem.__proto__ = CookieListItem.prototype; | |
94 | |
95 listItem.origin = origin; | |
96 listItem.list = list; | |
97 listItem.decorate(); | |
98 | |
99 // This hooks up updateOrigin() to the list item, makes the top-level | |
100 // tree nodes (i.e., origins) register their IDs in parentLookup, and | |
101 // causes them to request their children if they have none. Note that we | |
102 // have special logic in the setter for the parent property to make sure | |
103 // that we can still garbage collect list items when they scroll out of | |
104 // view, even though it appears that we keep a direct reference. | |
105 if (origin) { | |
106 origin.parent = listItem; | |
107 origin.updateOrigin(); | |
108 } | |
109 | |
110 return listItem; | |
111 } | |
112 | |
113 CookieListItem.prototype = { | |
114 __proto__: DeletableItem.prototype, | |
115 | |
116 /** @inheritDoc */ | |
117 decorate: function() { | |
118 this.siteChild = this.ownerDocument.createElement('div'); | |
119 this.siteChild.className = 'cookie-site'; | |
120 this.dataChild = this.ownerDocument.createElement('div'); | |
121 this.dataChild.className = 'cookie-data'; | |
122 this.sizeChild = this.ownerDocument.createElement('div'); | |
123 this.sizeChild.className = 'cookie-size'; | |
124 this.itemsChild = this.ownerDocument.createElement('div'); | |
125 this.itemsChild.className = 'cookie-items'; | |
126 this.infoChild = this.ownerDocument.createElement('div'); | |
127 this.infoChild.className = 'cookie-details'; | |
128 this.infoChild.hidden = true; | |
129 var remove = this.ownerDocument.createElement('button'); | |
130 remove.textContent = localStrings.getString('remove_cookie'); | |
131 remove.onclick = this.removeCookie_.bind(this); | |
132 this.infoChild.appendChild(remove); | |
133 var content = this.contentElement; | |
134 content.appendChild(this.siteChild); | |
135 content.appendChild(this.dataChild); | |
136 content.appendChild(this.sizeChild); | |
137 content.appendChild(this.itemsChild); | |
138 this.itemsChild.appendChild(this.infoChild); | |
139 if (this.origin && this.origin.data) { | |
140 this.siteChild.textContent = this.origin.data.title; | |
141 this.siteChild.setAttribute('title', this.origin.data.title); | |
142 } | |
143 this.itemList_ = []; | |
144 }, | |
145 | |
146 /** @type {boolean} */ | |
147 get expanded() { | |
148 return this.expanded_; | |
149 }, | |
150 set expanded(expanded) { | |
151 if (this.expanded_ == expanded) | |
152 return; | |
153 this.expanded_ = expanded; | |
154 if (expanded) { | |
155 var oldExpanded = this.list.expandedItem; | |
156 this.list.expandedItem = this; | |
157 this.updateItems_(); | |
158 if (oldExpanded) | |
159 oldExpanded.expanded = false; | |
160 this.classList.add('show-items'); | |
161 } else { | |
162 if (this.list.expandedItem == this) { | |
163 this.list.expandedItem = null; | |
164 } | |
165 this.style.height = ''; | |
166 this.itemsChild.style.height = ''; | |
167 this.classList.remove('show-items'); | |
168 } | |
169 }, | |
170 | |
171 /** | |
172 * The callback for the "remove" button shown when an item is selected. | |
173 * Requests that the currently selected cookie be removed. | |
174 * @private | |
175 */ | |
176 removeCookie_: function() { | |
177 if (this.selectedIndex_ >= 0) { | |
178 var item = this.itemList_[this.selectedIndex_]; | |
179 if (item && item.node) | |
180 chrome.send('removeCookie', [item.node.pathId]); | |
181 } | |
182 }, | |
183 | |
184 /** | |
185 * Disable animation within this cookie list item, in preparation for making | |
186 * changes that will need to be animated. Makes it possible to measure the | |
187 * contents without displaying them, to set animation targets. | |
188 * @private | |
189 */ | |
190 disableAnimation_: function() { | |
191 this.itemsHeight_ = getItemHeight(this.itemsChild); | |
192 this.classList.add('measure-items'); | |
193 }, | |
194 | |
195 /** | |
196 * Enable animation after changing the contents of this cookie list item. | |
197 * See @{code disableAnimation_}. | |
198 * @private | |
199 */ | |
200 enableAnimation_: function() { | |
201 if (!this.classList.contains('measure-items')) | |
202 this.disableAnimation_(); | |
203 this.itemsChild.style.height = ''; | |
204 // This will force relayout in order to calculate the new heights. | |
205 var itemsHeight = getItemHeight(this.itemsChild); | |
206 var fixedHeight = getItemHeight(this) + itemsHeight - this.itemsHeight_; | |
207 this.itemsChild.style.height = this.itemsHeight_ + 'px'; | |
208 // Force relayout before enabling animation, so that if we have | |
209 // changed things since the last layout, they will not be animated | |
210 // during subsequent layouts. | |
211 this.itemsChild.offsetHeight; | |
212 this.classList.remove('measure-items'); | |
213 this.itemsChild.style.height = itemsHeight + 'px'; | |
214 this.style.height = fixedHeight + 'px'; | |
215 }, | |
216 | |
217 /** | |
218 * Updates the origin summary to reflect changes in its items. | |
219 * Both CookieListItem and CookieTreeNode implement this API. | |
220 * This implementation scans the descendants to update the text. | |
221 */ | |
222 updateOrigin: function() { | |
223 var info = { | |
224 cookies: 0, | |
225 database: false, | |
226 localStorage: false, | |
227 appCache: false, | |
228 indexedDb: false, | |
229 fileSystem: false, | |
230 }; | |
231 if (this.origin) | |
232 this.origin.collectSummaryInfo(info); | |
233 var list = []; | |
234 if (info.cookies > 1) | |
235 list.push(localStrings.getStringF('cookie_plural', info.cookies)); | |
236 else if (info.cookies > 0) | |
237 list.push(localStrings.getString('cookie_singular')); | |
238 if (info.database || info.indexedDb) | |
239 list.push(localStrings.getString('cookie_database_storage')); | |
240 if (info.localStorage) | |
241 list.push(localStrings.getString('cookie_local_storage')); | |
242 if (info.appCache) | |
243 list.push(localStrings.getString('cookie_app_cache')); | |
244 if (info.fileSystem) | |
245 list.push(localStrings.getString('cookie_file_system')); | |
246 var text = ''; | |
247 for (var i = 0; i < list.length; ++i) | |
248 if (text.length > 0) | |
249 text += ', ' + list[i]; | |
250 else | |
251 text = list[i]; | |
252 this.dataChild.textContent = text; | |
253 if (info.quota && info.quota.totalUsage) { | |
254 this.sizeChild.textContent = info.quota.totalUsage; | |
255 } | |
256 | |
257 if (this.expanded) | |
258 this.updateItems_(); | |
259 }, | |
260 | |
261 /** | |
262 * Updates the items section to reflect changes, animating to the new state. | |
263 * Removes existing contents and calls @{code CookieTreeNode.createItems}. | |
264 * @private | |
265 */ | |
266 updateItems_: function() { | |
267 this.disableAnimation_(); | |
268 this.itemsChild.textContent = ''; | |
269 this.infoChild.hidden = true; | |
270 this.selectedIndex_ = -1; | |
271 this.itemList_ = []; | |
272 if (this.origin) | |
273 this.origin.createItems(this); | |
274 this.itemsChild.appendChild(this.infoChild); | |
275 this.enableAnimation_(); | |
276 }, | |
277 | |
278 /** | |
279 * Append a new cookie node "bubble" to this list item. | |
280 * @param {CookieTreeNode} node The cookie node to add a bubble for. | |
281 * @param {Element} div The DOM element for the bubble itself. | |
282 * @return {number} The index the bubble was added at. | |
283 */ | |
284 appendItem: function(node, div) { | |
285 this.itemList_.push({node: node, div: div}); | |
286 this.itemsChild.appendChild(div); | |
287 return this.itemList_.length - 1; | |
288 }, | |
289 | |
290 /** | |
291 * The currently selected cookie node ("cookie bubble") index. | |
292 * @type {number} | |
293 * @private | |
294 */ | |
295 selectedIndex_: -1, | |
296 | |
297 /** | |
298 * Get the currently selected cookie node ("cookie bubble") index. | |
299 * @type {number} | |
300 */ | |
301 get selectedIndex() { | |
302 return this.selectedIndex_; | |
303 }, | |
304 | |
305 /** | |
306 * Set the currently selected cookie node ("cookie bubble") index to | |
307 * @{code itemIndex}, unselecting any previously selected node first. | |
308 * @param {number} itemIndex The index to set as the selected index. | |
309 */ | |
310 set selectedIndex(itemIndex) { | |
311 // Get the list index up front before we change anything. | |
312 var index = this.list.getIndexOfListItem(this); | |
313 // Unselect any previously selected item. | |
314 if (this.selectedIndex_ >= 0) { | |
315 var item = this.itemList_[this.selectedIndex_]; | |
316 if (item && item.div) | |
317 item.div.removeAttribute('selected'); | |
318 } | |
319 // Special case: decrementing -1 wraps around to the end of the list. | |
320 if (itemIndex == -2) | |
321 itemIndex = this.itemList_.length - 1; | |
322 // Check if we're going out of bounds and hide the item details. | |
323 if (itemIndex < 0 || itemIndex >= this.itemList_.length) { | |
324 this.selectedIndex_ = -1; | |
325 this.disableAnimation_(); | |
326 this.infoChild.hidden = true; | |
327 this.enableAnimation_(); | |
328 return; | |
329 } | |
330 // Set the new selected item and show the item details for it. | |
331 this.selectedIndex_ = itemIndex; | |
332 this.itemList_[itemIndex].div.setAttribute('selected', ''); | |
333 this.disableAnimation_(); | |
334 this.itemList_[itemIndex].node.setDetailText(this.infoChild, | |
335 this.list.infoNodes); | |
336 this.infoChild.hidden = false; | |
337 this.enableAnimation_(); | |
338 // If we're near the bottom of the list this may cause the list item to go | |
339 // beyond the end of the visible area. Fix it after the animation is done. | |
340 var list = this.list; | |
341 window.setTimeout(function() { list.scrollIndexIntoView(index); }, 150); | |
342 }, | |
343 }; | |
344 | |
345 /** | |
346 * {@code CookieTreeNode}s mirror the structure of the cookie tree lazily, and | |
347 * contain all the actual data used to generate the {@code CookieListItem}s. | |
348 * @param {Object} data The data object for this node. | |
349 * @constructor | |
350 */ | |
351 function CookieTreeNode(data) { | |
352 this.data = data; | |
353 this.children = []; | |
354 } | |
355 | |
356 CookieTreeNode.prototype = { | |
357 /** | |
358 * Insert the given list of cookie tree nodes at the given index. | |
359 * Both CookiesList and CookieTreeNode implement this API. | |
360 * @param {Array.<Object>} data The data objects for the nodes to add. | |
361 * @param {number} start The index at which to start inserting the nodes. | |
362 */ | |
363 insertAt: function(data, start) { | |
364 var nodes = spliceTreeNodes(data, start, this.children); | |
365 for (var i = 0; i < nodes.length; i++) | |
366 nodes[i].parent = this; | |
367 this.updateOrigin(); | |
368 }, | |
369 | |
370 /** | |
371 * Remove a cookie tree node from the given index. | |
372 * Both CookiesList and CookieTreeNode implement this API. | |
373 * @param {number} index The index of the tree node to remove. | |
374 */ | |
375 remove: function(index) { | |
376 if (index < this.children.length) { | |
377 this.children.splice(index, 1); | |
378 this.updateOrigin(); | |
379 } | |
380 }, | |
381 | |
382 /** | |
383 * Clears all children. | |
384 * Both CookiesList and CookieTreeNode implement this API. | |
385 * It is used by CookiesList.loadChildren(). | |
386 */ | |
387 clear: function() { | |
388 // We might leave some garbage in parentLookup for removed children. | |
389 // But that should be OK because parentLookup is cleared when we | |
390 // reload the tree. | |
391 this.children = []; | |
392 this.updateOrigin(); | |
393 }, | |
394 | |
395 /** | |
396 * The counter used by startBatchUpdates() and endBatchUpdates(). | |
397 * @type {number} | |
398 */ | |
399 batchCount_: 0, | |
400 | |
401 /** | |
402 * See cr.ui.List.startBatchUpdates(). | |
403 * Both CookiesList (via List) and CookieTreeNode implement this API. | |
404 */ | |
405 startBatchUpdates: function() { | |
406 this.batchCount_++; | |
407 }, | |
408 | |
409 /** | |
410 * See cr.ui.List.endBatchUpdates(). | |
411 * Both CookiesList (via List) and CookieTreeNode implement this API. | |
412 */ | |
413 endBatchUpdates: function() { | |
414 if (!--this.batchCount_) | |
415 this.updateOrigin(); | |
416 }, | |
417 | |
418 /** | |
419 * Requests updating the origin summary to reflect changes in this item. | |
420 * Both CookieListItem and CookieTreeNode implement this API. | |
421 */ | |
422 updateOrigin: function() { | |
423 if (!this.batchCount_ && this.parent) | |
424 this.parent.updateOrigin(); | |
425 }, | |
426 | |
427 /** | |
428 * Summarize the information in this node and update @{code info}. | |
429 * This will recurse into child nodes to summarize all descendants. | |
430 * @param {Object} info The info object from @{code updateOrigin}. | |
431 */ | |
432 collectSummaryInfo: function(info) { | |
433 if (this.children.length > 0) { | |
434 for (var i = 0; i < this.children.length; ++i) | |
435 this.children[i].collectSummaryInfo(info); | |
436 } else if (this.data && !this.data.hasChildren) { | |
437 if (this.data.type == 'cookie') { | |
438 info.cookies++; | |
439 } else if (this.data.type == 'database') { | |
440 info.database = true; | |
441 } else if (this.data.type == 'local_storage') { | |
442 info.localStorage = true; | |
443 } else if (this.data.type == 'app_cache') { | |
444 info.appCache = true; | |
445 } else if (this.data.type == 'indexed_db') { | |
446 info.indexedDb = true; | |
447 } else if (this.data.type == 'file_system') { | |
448 info.fileSystem = true; | |
449 } else if (this.data.type == 'quota') { | |
450 info.quota = this.data; | |
451 } | |
452 } | |
453 }, | |
454 | |
455 /** | |
456 * Create the cookie "bubbles" for this node, recursing into children | |
457 * if there are any. Append the cookie bubbles to @{code item}. | |
458 * @param {CookieListItem} item The cookie list item to create items in. | |
459 */ | |
460 createItems: function(item) { | |
461 if (this.children.length > 0) { | |
462 for (var i = 0; i < this.children.length; ++i) | |
463 this.children[i].createItems(item); | |
464 } else if (this.data && !this.data.hasChildren) { | |
465 var text = ''; | |
466 switch (this.data.type) { | |
467 case 'cookie': | |
468 case 'database': | |
469 text = this.data.name; | |
470 break; | |
471 case 'local_storage': | |
472 text = localStrings.getString('cookie_local_storage'); | |
473 break; | |
474 case 'app_cache': | |
475 text = localStrings.getString('cookie_app_cache'); | |
476 break; | |
477 case 'indexed_db': | |
478 text = localStrings.getString('cookie_indexed_db'); | |
479 break; | |
480 case 'file_system': | |
481 text = localStrings.getString('cookie_file_system'); | |
482 break; | |
483 } | |
484 if (!text) | |
485 return; | |
486 var div = item.ownerDocument.createElement('div'); | |
487 div.className = 'cookie-item'; | |
488 // Help out screen readers and such: this is a clickable thing. | |
489 div.setAttribute('role', 'button'); | |
490 div.textContent = text; | |
491 var index = item.appendItem(this, div); | |
492 div.onclick = function() { | |
493 if (item.selectedIndex == index) | |
494 item.selectedIndex = -1; | |
495 else | |
496 item.selectedIndex = index; | |
497 }; | |
498 } | |
499 }, | |
500 | |
501 /** | |
502 * Set the detail text to be displayed to that of this cookie tree node. | |
503 * Uses preallocated DOM elements for each cookie node type from @{code | |
504 * infoNodes}, and inserts the appropriate elements to @{code element}. | |
505 * @param {Element} element The DOM element to insert elements to. | |
506 * @param {Object.<string, {table: Element, info: Object.<string, | |
507 * Element>}>} infoNodes The map from cookie node types to maps from | |
508 * cookie attribute names to DOM elements to display cookie attribute | |
509 * values, created by @{code CookiesList.decorate}. | |
510 */ | |
511 setDetailText: function(element, infoNodes) { | |
512 var table; | |
513 if (this.data && !this.data.hasChildren) { | |
514 if (cookieInfo[this.data.type]) { | |
515 var info = cookieInfo[this.data.type]; | |
516 var nodes = infoNodes[this.data.type].info; | |
517 for (var i = 0; i < info.length; ++i) { | |
518 var name = info[i][0]; | |
519 if (name != 'id' && this.data[name]) | |
520 nodes[name].textContent = this.data[name]; | |
521 else | |
522 nodes[name].textContent = ''; | |
523 } | |
524 table = infoNodes[this.data.type].table; | |
525 } | |
526 } | |
527 while (element.childNodes.length > 1) | |
528 element.removeChild(element.firstChild); | |
529 if (table) | |
530 element.insertBefore(table, element.firstChild); | |
531 }, | |
532 | |
533 /** | |
534 * The parent of this cookie tree node. | |
535 * @type {?CookieTreeNode|CookieListItem} | |
536 */ | |
537 get parent(parent) { | |
538 // See below for an explanation of this special case. | |
539 if (typeof this.parent_ == 'number') | |
540 return this.list_.getListItemByIndex(this.parent_); | |
541 return this.parent_; | |
542 }, | |
543 set parent(parent) { | |
544 if (parent == this.parent) | |
545 return; | |
546 if (parent instanceof CookieListItem) { | |
547 // If the parent is to be a CookieListItem, then we keep the reference | |
548 // to it by its containing list and list index, rather than directly. | |
549 // This allows the list items to be garbage collected when they scroll | |
550 // out of view (except the expanded item, which we cache). This is | |
551 // transparent except in the setter and getter, where we handle it. | |
552 this.parent_ = parent.listIndex; | |
553 this.list_ = parent.list; | |
554 parent.addEventListener('listIndexChange', | |
555 this.parentIndexChanged_.bind(this)); | |
556 } else { | |
557 this.parent_ = parent; | |
558 } | |
559 if (this.data && this.data.id) { | |
560 if (parent) | |
561 parentLookup[this.data.id] = this; | |
562 else | |
563 delete parentLookup[this.data.id]; | |
564 } | |
565 if (this.data && this.data.hasChildren && | |
566 !this.children.length && !lookupRequests[this.data.id]) { | |
567 lookupRequests[this.data.id] = true; | |
568 chrome.send('loadCookie', [this.pathId]); | |
569 } | |
570 }, | |
571 | |
572 /** | |
573 * Called when the parent is a CookieListItem whose index has changed. | |
574 * See the code above that avoids keeping a direct reference to | |
575 * CookieListItem parents, to allow them to be garbage collected. | |
576 * @private | |
577 */ | |
578 parentIndexChanged_: function(event) { | |
579 if (typeof this.parent_ == 'number') { | |
580 this.parent_ = event.newValue; | |
581 // We set a timeout to update the origin, rather than doing it right | |
582 // away, because this callback may occur while the list items are | |
583 // being repopulated following a scroll event. Calling updateOrigin() | |
584 // immediately could trigger relayout that would reset the scroll | |
585 // position within the list, among other things. | |
586 window.setTimeout(this.updateOrigin.bind(this), 0); | |
587 } | |
588 }, | |
589 | |
590 /** | |
591 * The cookie tree path id. | |
592 * @type {string} | |
593 */ | |
594 get pathId() { | |
595 var parent = this.parent; | |
596 if (parent && parent instanceof CookieTreeNode) | |
597 return parent.pathId + ',' + this.data.id; | |
598 return this.data.id; | |
599 }, | |
600 }; | |
601 | |
602 /** | |
603 * Creates a new cookies list. | |
604 * @param {Object=} opt_propertyBag Optional properties. | |
605 * @constructor | |
606 * @extends {DeletableItemList} | |
607 */ | |
608 var CookiesList = cr.ui.define('list'); | |
609 | |
610 CookiesList.prototype = { | |
611 __proto__: DeletableItemList.prototype, | |
612 | |
613 /** @inheritDoc */ | |
614 decorate: function() { | |
615 DeletableItemList.prototype.decorate.call(this); | |
616 this.classList.add('cookie-list'); | |
617 this.data_ = []; | |
618 this.dataModel = new ArrayDataModel(this.data_); | |
619 this.addEventListener('keydown', this.handleKeyLeftRight_.bind(this)); | |
620 var sm = new ListSingleSelectionModel(); | |
621 sm.addEventListener('change', this.cookieSelectionChange_.bind(this)); | |
622 sm.addEventListener('leadIndexChange', this.cookieLeadChange_.bind(this)); | |
623 this.selectionModel = sm; | |
624 this.infoNodes = {}; | |
625 this.fixedHeight = false; | |
626 var doc = this.ownerDocument; | |
627 // Create a table for each type of site data (e.g. cookies, databases, | |
628 // etc.) and save it so that we can reuse it for all origins. | |
629 for (var type in cookieInfo) { | |
630 var table = doc.createElement('table'); | |
631 table.className = 'cookie-details-table'; | |
632 var tbody = doc.createElement('tbody'); | |
633 table.appendChild(tbody); | |
634 var info = {}; | |
635 for (var i = 0; i < cookieInfo[type].length; i++) { | |
636 var tr = doc.createElement('tr'); | |
637 var name = doc.createElement('td'); | |
638 var data = doc.createElement('td'); | |
639 var pair = cookieInfo[type][i]; | |
640 name.className = 'cookie-details-label'; | |
641 name.textContent = localStrings.getString(pair[1]); | |
642 data.className = 'cookie-details-value'; | |
643 data.textContent = ''; | |
644 tr.appendChild(name); | |
645 tr.appendChild(data); | |
646 tbody.appendChild(tr); | |
647 info[pair[0]] = data; | |
648 } | |
649 this.infoNodes[type] = {table: table, info: info}; | |
650 } | |
651 }, | |
652 | |
653 /** | |
654 * Handles key down events and looks for left and right arrows, then | |
655 * dispatches to the currently expanded item, if any. | |
656 * @param {Event} e The keydown event. | |
657 * @private | |
658 */ | |
659 handleKeyLeftRight_: function(e) { | |
660 var id = e.keyIdentifier; | |
661 if ((id == 'Left' || id == 'Right') && this.expandedItem) { | |
662 var cs = this.ownerDocument.defaultView.getComputedStyle(this); | |
663 var rtl = cs.direction == 'rtl'; | |
664 if ((!rtl && id == 'Left') || (rtl && id == 'Right')) | |
665 this.expandedItem.selectedIndex--; | |
666 else | |
667 this.expandedItem.selectedIndex++; | |
668 this.scrollIndexIntoView(this.expandedItem.listIndex); | |
669 // Prevent the page itself from scrolling. | |
670 e.preventDefault(); | |
671 } | |
672 }, | |
673 | |
674 /** | |
675 * Called on selection model selection changes. | |
676 * @param {Event} ce The selection change event. | |
677 * @private | |
678 */ | |
679 cookieSelectionChange_: function(ce) { | |
680 ce.changes.forEach(function(change) { | |
681 var listItem = this.getListItemByIndex(change.index); | |
682 if (listItem) { | |
683 if (!change.selected) { | |
684 // We set a timeout here, rather than setting the item unexpanded | |
685 // immediately, so that if another item gets set expanded right | |
686 // away, it will be expanded before this item is unexpanded. It | |
687 // will notice that, and unexpand this item in sync with its own | |
688 // expansion. Later, this callback will end up having no effect. | |
689 window.setTimeout(function() { | |
690 if (!listItem.selected || !listItem.lead) | |
691 listItem.expanded = false; | |
692 }, 0); | |
693 } else if (listItem.lead) { | |
694 listItem.expanded = true; | |
695 } | |
696 } | |
697 }, this); | |
698 }, | |
699 | |
700 /** | |
701 * Called on selection model lead changes. | |
702 * @param {Event} pe The lead change event. | |
703 * @private | |
704 */ | |
705 cookieLeadChange_: function(pe) { | |
706 if (pe.oldValue != -1) { | |
707 var listItem = this.getListItemByIndex(pe.oldValue); | |
708 if (listItem) { | |
709 // See cookieSelectionChange_ above for why we use a timeout here. | |
710 window.setTimeout(function() { | |
711 if (!listItem.lead || !listItem.selected) | |
712 listItem.expanded = false; | |
713 }, 0); | |
714 } | |
715 } | |
716 if (pe.newValue != -1) { | |
717 var listItem = this.getListItemByIndex(pe.newValue); | |
718 if (listItem && listItem.selected) | |
719 listItem.expanded = true; | |
720 } | |
721 }, | |
722 | |
723 /** | |
724 * The currently expanded item. Used by CookieListItem above. | |
725 * @type {?CookieListItem} | |
726 */ | |
727 expandedItem: null, | |
728 | |
729 // from cr.ui.List | |
730 /** @inheritDoc */ | |
731 createItem: function(data) { | |
732 // We use the cached expanded item in order to allow it to maintain some | |
733 // state (like its fixed height, and which bubble is selected). | |
734 if (this.expandedItem && this.expandedItem.origin == data) | |
735 return this.expandedItem; | |
736 return new CookieListItem(data, this); | |
737 }, | |
738 | |
739 // from options.DeletableItemList | |
740 /** @inheritDoc */ | |
741 deleteItemAtIndex: function(index) { | |
742 var item = this.data_[index]; | |
743 if (item) { | |
744 var pathId = item.pathId; | |
745 if (pathId) | |
746 chrome.send('removeCookie', [pathId]); | |
747 } | |
748 }, | |
749 | |
750 /** | |
751 * Insert the given list of cookie tree nodes at the given index. | |
752 * Both CookiesList and CookieTreeNode implement this API. | |
753 * @param {Array.<Object>} data The data objects for the nodes to add. | |
754 * @param {number} start The index at which to start inserting the nodes. | |
755 */ | |
756 insertAt: function(data, start) { | |
757 spliceTreeNodes(data, start, this.dataModel); | |
758 }, | |
759 | |
760 /** | |
761 * Remove a cookie tree node from the given index. | |
762 * Both CookiesList and CookieTreeNode implement this API. | |
763 * @param {number} index The index of the tree node to remove. | |
764 */ | |
765 remove: function(index) { | |
766 if (index < this.data_.length) | |
767 this.dataModel.splice(index, 1); | |
768 }, | |
769 | |
770 /** | |
771 * Clears the list. | |
772 * Both CookiesList and CookieTreeNode implement this API. | |
773 * It is used by CookiesList.loadChildren(). | |
774 */ | |
775 clear: function() { | |
776 parentLookup = {}; | |
777 this.data_ = []; | |
778 this.dataModel = new ArrayDataModel(this.data_); | |
779 this.redraw(); | |
780 }, | |
781 | |
782 /** | |
783 * Add tree nodes by given parent. | |
784 * @param {Object} parent The parent node. | |
785 * @param {number} start The index at which to start inserting the nodes. | |
786 * @param {Array} nodesData Nodes data array. | |
787 * @private | |
788 */ | |
789 addByParent_: function(parent, start, nodesData) { | |
790 if (!parent) | |
791 return; | |
792 | |
793 parent.startBatchUpdates(); | |
794 parent.insertAt(nodesData, start); | |
795 parent.endBatchUpdates(); | |
796 | |
797 cr.dispatchSimpleEvent(this, 'change'); | |
798 }, | |
799 | |
800 /** | |
801 * Add tree nodes by parent id. | |
802 * This is used by cookies_view.js. | |
803 * @param {string} parentId Id of the parent node. | |
804 * @param {number} start The index at which to start inserting the nodes. | |
805 * @param {Array} nodesData Nodes data array. | |
806 */ | |
807 addByParentId: function(parentId, start, nodesData) { | |
808 var parent = parentId ? parentLookup[parentId] : this; | |
809 this.addByParent_(parent, start, nodesData); | |
810 }, | |
811 | |
812 /** | |
813 * Removes tree nodes by parent id. | |
814 * This is used by cookies_view.js. | |
815 * @param {string} parentId Id of the parent node. | |
816 * @param {number} start The index at which to start removing the nodes. | |
817 * @param {number} count Number of nodes to remove. | |
818 */ | |
819 removeByParentId: function(parentId, start, count) { | |
820 var parent = parentId ? parentLookup[parentId] : this; | |
821 if (!parent) | |
822 return; | |
823 | |
824 parent.startBatchUpdates(); | |
825 while (count-- > 0) | |
826 parent.remove(start); | |
827 parent.endBatchUpdates(); | |
828 | |
829 cr.dispatchSimpleEvent(this, 'change'); | |
830 }, | |
831 | |
832 /** | |
833 * Loads the immediate children of given parent node. | |
834 * This is used by cookies_view.js. | |
835 * @param {string} parentId Id of the parent node. | |
836 * @param {Array} children The immediate children of parent node. | |
837 */ | |
838 loadChildren: function(parentId, children) { | |
839 if (parentId) | |
840 delete lookupRequests[parentId]; | |
841 var parent = parentId ? parentLookup[parentId] : this; | |
842 if (!parent) | |
843 return; | |
844 | |
845 parent.startBatchUpdates(); | |
846 parent.clear(); | |
847 this.addByParent_(parent, 0, children); | |
848 parent.endBatchUpdates(); | |
849 }, | |
850 }; | |
851 | |
852 return { | |
853 CookiesList: CookiesList | |
854 }; | |
855 }); | |
OLD | NEW |