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.
5 cr
.define('options', function() {
6 /** @const */ var DeletableItemList
= options
.DeletableItemList
;
7 /** @const */ var DeletableItem
= options
.DeletableItem
;
8 /** @const */ var ArrayDataModel
= cr
.ui
.ArrayDataModel
;
9 /** @const */ var ListSingleSelectionModel
= cr
.ui
.ListSingleSelectionModel
;
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 */ var 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 'channel_id': [['serverId', 'label_channel_id_server_id'],
41 ['certType', 'label_channel_id_type'],
42 ['created', 'label_channel_id_created']],
43 'service_worker': [['origin', 'label_service_worker_origin'],
44 ['size', 'label_service_worker_size'],
45 ['scopes', 'label_service_worker_scopes']],
46 'flash_lso': [['domain', 'label_cookie_domain']],
50 * Returns the item's height, like offsetHeight but such that it works better
51 * when the page is zoomed. See the similar calculation in @{code cr.ui.List}.
52 * This version also accounts for the animation done in this file.
53 * @param {Element} item The item to get the height of.
54 * @return {number} The height of the item, calculated with zooming in mind.
56 function getItemHeight(item
) {
57 var height
= item
.style
.height
;
58 // Use the fixed animation target height if set, in case the element is
59 // currently being animated and we'd get an intermediate height below.
60 if (height
&& height
.substr(-2) == 'px')
61 return parseInt(height
.substr(0, height
.length
- 2), 10);
62 return item
.getBoundingClientRect().height
;
66 * Create tree nodes for the objects in the data array, and insert them all
67 * into the given list using its @{code splice} method at the given index.
68 * @param {Array<Object>} data The data objects for the nodes to add.
69 * @param {number} start The index at which to start inserting the nodes.
70 * @return {Array<options.CookieTreeNode>} An array of CookieTreeNodes added.
72 function spliceTreeNodes(data
, start
, list
) {
73 var nodes
= data
.map(function(x
) { return new CookieTreeNode(x
); });
74 // Insert [start, 0] at the beginning of the array of nodes, making it
75 // into the arguments we want to pass to @{code list.splice} below.
76 nodes
.splice(0, 0, start
, 0);
77 list
.splice
.apply(list
, nodes
);
78 // Remove the [start, 0] prefix and return the array of nodes.
84 * Adds information about an app that protects this data item to the
86 * @param {Element} element The DOM element the information should be
88 * @param {{id: string, name: string}} appInfo Information about an app.
90 function addAppInfo(element
, appInfo
) {
91 var img
= element
.ownerDocument
.createElement('img');
92 img
.src
= 'chrome://extension-icon/' + appInfo
.id
+ '/16/1';
93 element
.title
= loadTimeData
.getString('label_protected_by_apps') +
95 img
.className
= 'protecting-app';
96 element
.appendChild(img
);
99 var parentLookup
= {};
100 var lookupRequests
= {};
103 * Creates a new list item for sites data. Note that these are created and
104 * destroyed lazily as they scroll into and out of view, so they must be
105 * stateless. We cache the expanded item in @{code CookiesList} though, so it
106 * can keep state. (Mostly just which item is selected.)
107 * @param {Object} origin Data used to create a cookie list item.
108 * @param {options.CookiesList} list The list that will contain this item.
110 * @extends {options.DeletableItem}
112 function CookieListItem(origin
, list
) {
113 var listItem
= new DeletableItem();
114 listItem
.__proto__
= CookieListItem
.prototype;
116 listItem
.origin
= origin
;
117 listItem
.list
= list
;
120 // This hooks up updateOrigin() to the list item, makes the top-level
121 // tree nodes (i.e., origins) register their IDs in parentLookup, and
122 // causes them to request their children if they have none. Note that we
123 // have special logic in the setter for the parent property to make sure
124 // that we can still garbage collect list items when they scroll out of
125 // view, even though it appears that we keep a direct reference.
127 origin
.parent
= listItem
;
128 origin
.updateOrigin();
134 CookieListItem
.prototype = {
135 __proto__
: DeletableItem
.prototype,
138 decorate: function() {
139 this.siteChild
= this.ownerDocument
.createElement('div');
140 this.siteChild
.className
= 'cookie-site';
141 this.dataChild
= this.ownerDocument
.createElement('div');
142 this.dataChild
.className
= 'cookie-data';
143 this.sizeChild
= this.ownerDocument
.createElement('div');
144 this.sizeChild
.className
= 'cookie-size';
145 this.itemsChild
= this.ownerDocument
.createElement('div');
146 this.itemsChild
.className
= 'cookie-items';
147 this.infoChild
= this.ownerDocument
.createElement('div');
148 this.infoChild
.className
= 'cookie-details';
149 this.infoChild
.hidden
= true;
151 var remove
= this.ownerDocument
.createElement('button');
152 remove
.textContent
= loadTimeData
.getString('remove_cookie');
153 remove
.onclick
= this.removeCookie_
.bind(this);
154 this.infoChild
.appendChild(remove
);
155 var content
= this.contentElement
;
156 content
.appendChild(this.siteChild
);
157 content
.appendChild(this.dataChild
);
158 content
.appendChild(this.sizeChild
);
159 content
.appendChild(this.itemsChild
);
160 this.itemsChild
.appendChild(this.infoChild
);
161 if (this.origin
&& this.origin
.data
) {
162 this.siteChild
.textContent
= this.origin
.data
.title
;
163 this.siteChild
.setAttribute('title', this.origin
.data
.title
);
168 /** @type {boolean} */
170 return this.expanded_
;
172 set expanded(expanded
) {
173 if (this.expanded_
== expanded
)
175 this.expanded_
= expanded
;
177 this.classList
.add('show-items');
178 var oldExpanded
= this.list
.expandedItem
;
179 this.list
.expandedItem
= this;
182 oldExpanded
.expanded
= false;
184 if (this.list
.expandedItem
== this) {
185 this.list
.expandedItem
= null;
187 this.style
.height
= '';
188 this.itemsChild
.style
.height
= '';
189 this.classList
.remove('show-items');
194 * The callback for the "remove" button shown when an item is selected.
195 * Requests that the currently selected cookie be removed.
198 removeCookie_: function() {
199 if (this.selectedIndex_
>= 0) {
200 var item
= this.itemList_
[this.selectedIndex_
];
201 if (item
&& item
.node
)
202 chrome
.send('removeCookie', [item
.node
.pathId
]);
207 * Disable animation within this cookie list item, in preparation for making
208 * changes that will need to be animated. Makes it possible to measure the
209 * contents without displaying them, to set animation targets.
212 disableAnimation_: function() {
213 this.itemsHeight_
= getItemHeight(this.itemsChild
);
214 this.classList
.add('measure-items');
218 * Enable animation after changing the contents of this cookie list item.
219 * See @{code disableAnimation_}.
222 enableAnimation_: function() {
223 if (!this.classList
.contains('measure-items'))
224 this.disableAnimation_();
225 this.itemsChild
.style
.height
= '';
226 // This will force relayout in order to calculate the new heights.
227 var itemsHeight
= getItemHeight(this.itemsChild
);
228 var fixedHeight
= getItemHeight(this) + itemsHeight
- this.itemsHeight_
;
229 this.itemsChild
.style
.height
= this.itemsHeight_
+ 'px';
230 // Force relayout before enabling animation, so that if we have
231 // changed things since the last layout, they will not be animated
232 // during subsequent layouts.
233 /** @suppress {suspiciousCode} */
234 this.itemsChild
.offsetHeight
;
235 this.classList
.remove('measure-items');
236 this.itemsChild
.style
.height
= itemsHeight
+ 'px';
237 this.style
.height
= fixedHeight
+ 'px';
241 * Updates the origin summary to reflect changes in its items.
242 * Both CookieListItem and CookieTreeNode implement this API.
243 * This implementation scans the descendants to update the text.
245 updateOrigin: function() {
254 serviceWorker
: false,
257 this.origin
.collectSummaryInfo(info
);
260 if (info
.cookies
> 1)
261 list
.push(loadTimeData
.getStringF('cookie_plural', info
.cookies
));
262 else if (info
.cookies
> 0)
263 list
.push(loadTimeData
.getString('cookie_singular'));
264 if (info
.database
|| info
.indexedDb
)
265 list
.push(loadTimeData
.getString('cookie_database_storage'));
266 if (info
.localStorage
)
267 list
.push(loadTimeData
.getString('cookie_local_storage'));
269 list
.push(loadTimeData
.getString('cookie_app_cache'));
271 list
.push(loadTimeData
.getString('cookie_file_system'));
273 list
.push(loadTimeData
.getString('cookie_channel_id'));
274 if (info
.serviceWorker
)
275 list
.push(loadTimeData
.getString('cookie_service_worker'));
277 list
.push(loadTimeData
.getString('cookie_flash_lso'));
280 for (var i
= 0; i
< list
.length
; ++i
) {
282 text
+= ', ' + list
[i
];
286 this.dataChild
.textContent
= text
;
288 var apps
= info
.appsProtectingThis
;
289 for (var key
in apps
) {
290 addAppInfo(this.dataChild
, apps
[key
]);
293 if (info
.quota
&& info
.quota
.totalUsage
)
294 this.sizeChild
.textContent
= info
.quota
.totalUsage
;
301 * Updates the items section to reflect changes, animating to the new state.
302 * Removes existing contents and calls @{code CookieTreeNode.createItems}.
305 updateItems_: function() {
306 this.disableAnimation_();
307 this.itemsChild
.textContent
= '';
308 this.infoChild
.hidden
= true;
309 this.selectedIndex_
= -1;
312 this.origin
.createItems(this);
313 this.itemsChild
.appendChild(this.infoChild
);
314 this.enableAnimation_();
318 * Append a new cookie node "bubble" to this list item.
319 * @param {options.CookieTreeNode} node The cookie node to add a bubble for.
320 * @param {Element} div The DOM element for the bubble itself.
321 * @return {number} The index the bubble was added at.
323 appendItem: function(node
, div
) {
324 this.itemList_
.push({node
: node
, div
: div
});
325 this.itemsChild
.appendChild(div
);
326 return this.itemList_
.length
- 1;
330 * The currently selected cookie node ("cookie bubble") index.
337 * Get the currently selected cookie node ("cookie bubble") index.
340 get selectedIndex() {
341 return this.selectedIndex_
;
345 * Set the currently selected cookie node ("cookie bubble") index to
346 * |itemIndex|, unselecting any previously selected node first.
347 * @param {number} itemIndex The index to set as the selected index.
349 set selectedIndex(itemIndex
) {
350 // Get the list index up front before we change anything.
351 var index
= this.list
.getIndexOfListItem(this);
352 // Unselect any previously selected item.
353 if (this.selectedIndex_
>= 0) {
354 var item
= this.itemList_
[this.selectedIndex_
];
355 if (item
&& item
.div
)
356 item
.div
.removeAttribute('selected');
358 // Special case: decrementing -1 wraps around to the end of the list.
360 itemIndex
= this.itemList_
.length
- 1;
361 // Check if we're going out of bounds and hide the item details.
362 if (itemIndex
< 0 || itemIndex
>= this.itemList_
.length
) {
363 this.selectedIndex_
= -1;
364 this.disableAnimation_();
365 this.infoChild
.hidden
= true;
366 this.enableAnimation_();
369 // Set the new selected item and show the item details for it.
370 this.selectedIndex_
= itemIndex
;
371 this.itemList_
[itemIndex
].div
.setAttribute('selected', '');
372 this.disableAnimation_();
373 this.itemList_
[itemIndex
].node
.setDetailText(this.infoChild
,
374 this.list
.infoNodes
);
375 this.infoChild
.hidden
= false;
376 this.enableAnimation_();
377 // If we're near the bottom of the list this may cause the list item to go
378 // beyond the end of the visible area. Fix it after the animation is done.
379 var list
= this.list
;
380 window
.setTimeout(function() { list
.scrollIndexIntoView(index
); }, 150);
385 * {@code CookieTreeNode}s mirror the structure of the cookie tree lazily, and
386 * contain all the actual data used to generate the {@code CookieListItem}s.
387 * @param {Object} data The data object for this node.
390 function CookieTreeNode(data
) {
395 CookieTreeNode
.prototype = {
397 * Insert the given list of cookie tree nodes at the given index.
398 * Both CookiesList and CookieTreeNode implement this API.
399 * @param {Array<Object>} data The data objects for the nodes to add.
400 * @param {number} start The index at which to start inserting the nodes.
402 insertAt: function(data
, start
) {
403 var nodes
= spliceTreeNodes(data
, start
, this.children
);
404 for (var i
= 0; i
< nodes
.length
; i
++)
405 nodes
[i
].parent
= this;
410 * Remove a cookie tree node from the given index.
411 * Both CookiesList and CookieTreeNode implement this API.
412 * @param {number} index The index of the tree node to remove.
414 remove: function(index
) {
415 if (index
< this.children
.length
) {
416 this.children
.splice(index
, 1);
422 * Clears all children.
423 * Both CookiesList and CookieTreeNode implement this API.
424 * It is used by CookiesList.loadChildren().
427 // We might leave some garbage in parentLookup for removed children.
428 // But that should be OK because parentLookup is cleared when we
435 * The counter used by startBatchUpdates() and endBatchUpdates().
441 * See cr.ui.List.startBatchUpdates().
442 * Both CookiesList (via List) and CookieTreeNode implement this API.
444 startBatchUpdates: function() {
449 * See cr.ui.List.endBatchUpdates().
450 * Both CookiesList (via List) and CookieTreeNode implement this API.
452 endBatchUpdates: function() {
453 if (!--this.batchCount_
)
458 * Requests updating the origin summary to reflect changes in this item.
459 * Both CookieListItem and CookieTreeNode implement this API.
461 updateOrigin: function() {
462 if (!this.batchCount_
&& this.parent
)
463 this.parent
.updateOrigin();
467 * Summarize the information in this node and update @{code info}.
468 * This will recurse into child nodes to summarize all descendants.
469 * @param {Object} info The info object from @{code updateOrigin}.
471 collectSummaryInfo: function(info
) {
472 if (this.children
.length
> 0) {
473 for (var i
= 0; i
< this.children
.length
; ++i
)
474 this.children
[i
].collectSummaryInfo(info
);
475 } else if (this.data
&& !this.data
.hasChildren
) {
476 if (this.data
.type
== 'cookie') {
478 } else if (this.data
.type
== 'database') {
479 info
.database
= true;
480 } else if (this.data
.type
== 'local_storage') {
481 info
.localStorage
= true;
482 } else if (this.data
.type
== 'app_cache') {
483 info
.appCache
= true;
484 } else if (this.data
.type
== 'indexed_db') {
485 info
.indexedDb
= true;
486 } else if (this.data
.type
== 'file_system') {
487 info
.fileSystem
= true;
488 } else if (this.data
.type
== 'quota') {
489 info
.quota
= this.data
;
490 } else if (this.data
.type
== 'channel_id') {
492 } else if (this.data
.type
== 'service_worker') {
493 info
.serviceWorker
= true;
494 } else if (this.data
.type
== 'flash_lso') {
495 info
.flashLSO
= true;
498 var apps
= this.data
.appsProtectingThis
;
500 if (!info
.appsProtectingThis
)
501 info
.appsProtectingThis
= {};
502 apps
.forEach(function(appInfo
) {
503 info
.appsProtectingThis
[appInfo
.id
] = appInfo
;
510 * Create the cookie "bubbles" for this node, recursing into children
511 * if there are any. Append the cookie bubbles to @{code item}.
512 * @param {options.CookieListItem} item The cookie list item to create items
515 createItems: function(item
) {
516 if (this.children
.length
> 0) {
517 for (var i
= 0; i
< this.children
.length
; ++i
)
518 this.children
[i
].createItems(item
);
522 if (!this.data
|| this.data
.hasChildren
)
526 switch (this.data
.type
) {
529 text
= this.data
.name
;
532 text
= loadTimeData
.getString('cookie_' + this.data
.type
);
537 var div
= item
.ownerDocument
.createElement('div');
538 div
.className
= 'cookie-item';
539 // Help out screen readers and such: this is a clickable thing.
540 div
.setAttribute('role', 'button');
541 div
.textContent
= text
;
542 var apps
= this.data
.appsProtectingThis
;
544 apps
.forEach(addAppInfo
.bind(null, div
));
546 var index
= item
.appendItem(this, div
);
547 div
.onclick = function() {
548 item
.selectedIndex
= (item
.selectedIndex
== index
) ? -1 : index
;
553 * Set the detail text to be displayed to that of this cookie tree node.
554 * Uses preallocated DOM elements for each cookie node type from @{code
555 * infoNodes}, and inserts the appropriate elements to @{code element}.
556 * @param {Element} element The DOM element to insert elements to.
557 * @param {Object<string, {table: Element, info: Object<string,
558 * Element>}>} infoNodes The map from cookie node types to maps from
559 * cookie attribute names to DOM elements to display cookie attribute
560 * values, created by @{code CookiesList.decorate}.
562 setDetailText: function(element
, infoNodes
) {
564 if (this.data
&& !this.data
.hasChildren
&& cookieInfo
[this.data
.type
]) {
565 var info
= cookieInfo
[this.data
.type
];
566 var nodes
= infoNodes
[this.data
.type
].info
;
567 for (var i
= 0; i
< info
.length
; ++i
) {
568 var name
= info
[i
][0];
569 if (name
!= 'id' && this.data
[name
])
570 nodes
[name
].textContent
= this.data
[name
];
572 nodes
[name
].textContent
= '';
574 table
= infoNodes
[this.data
.type
].table
;
577 while (element
.childNodes
.length
> 1)
578 element
.removeChild(element
.firstChild
);
581 element
.insertBefore(table
, element
.firstChild
);
585 * The parent of this cookie tree node.
586 * @type {?(options.CookieTreeNode|options.CookieListItem)}
589 // See below for an explanation of this special case.
590 if (typeof this.parent_
== 'number')
591 return this.list_
.getListItemByIndex(this.parent_
);
595 if (parent
== this.parent
)
598 if (parent
instanceof CookieListItem
) {
599 // If the parent is to be a CookieListItem, then we keep the reference
600 // to it by its containing list and list index, rather than directly.
601 // This allows the list items to be garbage collected when they scroll
602 // out of view (except the expanded item, which we cache). This is
603 // transparent except in the setter and getter, where we handle it.
604 if (this.parent_
== undefined || parent
.listIndex
!= -1) {
605 // Setting the parent is somewhat tricky because the CookieListItem
606 // constructor has side-effects on the |origin| that it wraps. Every
607 // time a CookieListItem is created for an |origin|, it registers
608 // itself as the parent of the |origin|.
609 // The List implementation may create a temporary CookieListItem item
610 // that wraps the |origin| of the very first entry of the CokiesList,
611 // when the List is redrawn the first time. This temporary
612 // CookieListItem is fresh (has listIndex = -1) and is never inserted
613 // into the List. Therefore it gets never updated. This destroys the
614 // chain of parent pointers.
615 // This is the stack trace:
617 // CookiesList.createItem
619 // List.getDefaultItemSize_
620 // List.getDefaultItemHeight_
621 // List.getIndexForListOffset_
622 // List.getItemsInViewPort
624 // List.endBatchUpdates
625 // CookiesList.loadChildren
626 this.parent_
= parent
.listIndex
;
628 this.list_
= parent
.list
;
629 parent
.addEventListener('listIndexChange',
630 this.parentIndexChanged_
.bind(this));
632 this.parent_
= parent
;
635 if (this.data
&& this.data
.id
) {
637 parentLookup
[this.data
.id
] = this;
639 delete parentLookup
[this.data
.id
];
642 if (this.data
&& this.data
.hasChildren
&&
643 !this.children
.length
&& !lookupRequests
[this.data
.id
]) {
644 lookupRequests
[this.data
.id
] = true;
645 chrome
.send('loadCookie', [this.pathId
]);
650 * Called when the parent is a CookieListItem whose index has changed.
651 * See the code above that avoids keeping a direct reference to
652 * CookieListItem parents, to allow them to be garbage collected.
655 parentIndexChanged_: function(event
) {
656 if (typeof this.parent_
== 'number') {
657 this.parent_
= event
.newValue
;
658 // We set a timeout to update the origin, rather than doing it right
659 // away, because this callback may occur while the list items are
660 // being repopulated following a scroll event. Calling updateOrigin()
661 // immediately could trigger relayout that would reset the scroll
662 // position within the list, among other things.
663 window
.setTimeout(this.updateOrigin
.bind(this), 0);
668 * The cookie tree path id.
672 var parent
= this.parent
;
673 if (parent
&& parent
instanceof CookieTreeNode
)
674 return parent
.pathId
+ ',' + this.data
.id
;
680 * Creates a new cookies list.
681 * @param {Object=} opt_propertyBag Optional properties.
683 * @extends {options.DeletableItemList}
685 var CookiesList
= cr
.ui
.define('list');
687 CookiesList
.prototype = {
688 __proto__
: DeletableItemList
.prototype,
691 decorate: function() {
692 DeletableItemList
.prototype.decorate
.call(this);
693 this.classList
.add('cookie-list');
694 this.dataModel
= new ArrayDataModel([]);
695 this.addEventListener('keydown', this.handleKeyLeftRight_
.bind(this));
696 var sm
= new ListSingleSelectionModel();
697 sm
.addEventListener('change', this.cookieSelectionChange_
.bind(this));
698 sm
.addEventListener('leadIndexChange', this.cookieLeadChange_
.bind(this));
699 this.selectionModel
= sm
;
701 this.fixedHeight
= false;
702 var doc
= this.ownerDocument
;
703 // Create a table for each type of site data (e.g. cookies, databases,
704 // etc.) and save it so that we can reuse it for all origins.
705 for (var type
in cookieInfo
) {
706 var table
= doc
.createElement('table');
707 table
.className
= 'cookie-details-table';
708 var tbody
= doc
.createElement('tbody');
709 table
.appendChild(tbody
);
711 for (var i
= 0; i
< cookieInfo
[type
].length
; i
++) {
712 var tr
= doc
.createElement('tr');
713 var name
= doc
.createElement('td');
714 var data
= doc
.createElement('td');
715 var pair
= cookieInfo
[type
][i
];
716 name
.className
= 'cookie-details-label';
717 name
.textContent
= loadTimeData
.getString(pair
[1]);
718 data
.className
= 'cookie-details-value';
719 data
.textContent
= '';
720 tr
.appendChild(name
);
721 tr
.appendChild(data
);
722 tbody
.appendChild(tr
);
723 info
[pair
[0]] = data
;
725 this.infoNodes
[type
] = {table
: table
, info
: info
};
730 * Handles key down events and looks for left and right arrows, then
731 * dispatches to the currently expanded item, if any.
732 * @param {Event} e The keydown event.
735 handleKeyLeftRight_: function(e
) {
736 var id
= e
.keyIdentifier
;
737 if (e
.altKey
|| e
.ctrlKey
|| e
.metaKey
|| e
.shiftKey
)
739 if ((id
== 'Left' || id
== 'Right') && this.expandedItem
) {
740 var cs
= this.ownerDocument
.defaultView
.getComputedStyle(this);
741 var rtl
= cs
.direction
== 'rtl';
742 if ((!rtl
&& id
== 'Left') || (rtl
&& id
== 'Right'))
743 this.expandedItem
.selectedIndex
--;
745 this.expandedItem
.selectedIndex
++;
746 this.scrollIndexIntoView(this.expandedItem
.listIndex
);
747 // Prevent the page itself from scrolling.
753 * Called on selection model selection changes.
754 * @param {Event} ce The selection change event.
757 cookieSelectionChange_: function(ce
) {
758 ce
.changes
.forEach(function(change
) {
759 var listItem
= this.getListItemByIndex(change
.index
);
761 if (!change
.selected
) {
762 // We set a timeout here, rather than setting the item unexpanded
763 // immediately, so that if another item gets set expanded right
764 // away, it will be expanded before this item is unexpanded. It
765 // will notice that, and unexpand this item in sync with its own
766 // expansion. Later, this callback will end up having no effect.
767 window
.setTimeout(function() {
768 if (!listItem
.selected
|| !listItem
.lead
)
769 listItem
.expanded
= false;
771 } else if (listItem
.lead
) {
772 listItem
.expanded
= true;
779 * Called on selection model lead changes.
780 * @param {Event} pe The lead change event.
783 cookieLeadChange_: function(pe
) {
784 if (pe
.oldValue
!= -1) {
785 var listItem
= this.getListItemByIndex(pe
.oldValue
);
787 // See cookieSelectionChange_ above for why we use a timeout here.
788 window
.setTimeout(function() {
789 if (!listItem
.lead
|| !listItem
.selected
)
790 listItem
.expanded
= false;
794 if (pe
.newValue
!= -1) {
795 var listItem
= this.getListItemByIndex(pe
.newValue
);
796 if (listItem
&& listItem
.selected
)
797 listItem
.expanded
= true;
802 * The currently expanded item. Used by CookieListItem above.
803 * @type {?options.CookieListItem}
810 * @param {Object} data
812 createItem: function(data
) {
813 // We use the cached expanded item in order to allow it to maintain some
814 // state (like its fixed height, and which bubble is selected).
815 if (this.expandedItem
&& this.expandedItem
.origin
== data
)
816 return this.expandedItem
;
817 return new CookieListItem(data
, this);
820 // from options.DeletableItemList
822 deleteItemAtIndex: function(index
) {
823 var item
= this.dataModel
.item(index
);
825 var pathId
= item
.pathId
;
827 chrome
.send('removeCookie', [pathId
]);
832 * Insert the given list of cookie tree nodes at the given index.
833 * Both CookiesList and CookieTreeNode implement this API.
834 * @param {Array<Object>} data The data objects for the nodes to add.
835 * @param {number} start The index at which to start inserting the nodes.
837 insertAt: function(data
, start
) {
838 spliceTreeNodes(data
, start
, this.dataModel
);
842 * Remove a cookie tree node from the given index.
843 * Both CookiesList and CookieTreeNode implement this API.
844 * @param {number} index The index of the tree node to remove.
846 remove: function(index
) {
847 if (index
< this.dataModel
.length
)
848 this.dataModel
.splice(index
, 1);
853 * Both CookiesList and CookieTreeNode implement this API.
854 * It is used by CookiesList.loadChildren().
858 this.dataModel
.splice(0, this.dataModel
.length
);
863 * Add tree nodes by given parent.
864 * @param {Object} parent The parent node.
865 * @param {number} start The index at which to start inserting the nodes.
866 * @param {Array} nodesData Nodes data array.
869 addByParent_: function(parent
, start
, nodesData
) {
873 parent
.startBatchUpdates();
874 parent
.insertAt(nodesData
, start
);
875 parent
.endBatchUpdates();
877 cr
.dispatchSimpleEvent(this, 'change');
881 * Add tree nodes by parent id.
882 * This is used by cookies_view.js.
883 * @param {string} parentId Id of the parent node.
884 * @param {number} start The index at which to start inserting the nodes.
885 * @param {Array} nodesData Nodes data array.
887 addByParentId: function(parentId
, start
, nodesData
) {
888 var parent
= parentId
? parentLookup
[parentId
] : this;
889 this.addByParent_(parent
, start
, nodesData
);
893 * Removes tree nodes by parent id.
894 * This is used by cookies_view.js.
895 * @param {string} parentId Id of the parent node.
896 * @param {number} start The index at which to start removing the nodes.
897 * @param {number} count Number of nodes to remove.
899 removeByParentId: function(parentId
, start
, count
) {
900 var parent
= parentId
? parentLookup
[parentId
] : this;
904 parent
.startBatchUpdates();
906 parent
.remove(start
);
907 parent
.endBatchUpdates();
909 cr
.dispatchSimpleEvent(this, 'change');
913 * Loads the immediate children of given parent node.
914 * This is used by cookies_view.js.
915 * @param {string} parentId Id of the parent node.
916 * @param {Array} children The immediate children of parent node.
918 loadChildren: function(parentId
, children
) {
920 delete lookupRequests
[parentId
];
921 var parent
= parentId
? parentLookup
[parentId
] : this;
925 parent
.startBatchUpdates();
927 this.addByParent_(parent
, 0, children
);
928 parent
.endBatchUpdates();
933 CookiesList
: CookiesList
,
934 CookieListItem
: CookieListItem
,
935 CookieTreeNode
: CookieTreeNode
,