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('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 } |
| 522 table = infoNodes[this.data.type].table; |
| 523 } |
| 524 } |
| 525 while (element.childNodes.length > 1) |
| 526 element.removeChild(element.firstChild); |
| 527 if (table) |
| 528 element.insertBefore(table, element.firstChild); |
| 529 }, |
| 530 |
| 531 /** |
| 532 * The parent of this cookie tree node. |
| 533 * @type {?CookieTreeNode|CookieListItem} |
| 534 */ |
| 535 get parent(parent) { |
| 536 // See below for an explanation of this special case. |
| 537 if (typeof this.parent_ == 'number') |
| 538 return this.list_.getListItemByIndex(this.parent_); |
| 539 return this.parent_; |
| 540 }, |
| 541 set parent(parent) { |
| 542 if (parent == this.parent) |
| 543 return; |
| 544 if (parent instanceof CookieListItem) { |
| 545 // If the parent is to be a CookieListItem, then we keep the reference |
| 546 // to it by its containing list and list index, rather than directly. |
| 547 // This allows the list items to be garbage collected when they scroll |
| 548 // out of view (except the expanded item, which we cache). This is |
| 549 // transparent except in the setter and getter, where we handle it. |
| 550 this.parent_ = parent.listIndex; |
| 551 this.list_ = parent.list; |
| 552 parent.addEventListener('listIndexChange', |
| 553 this.parentIndexChanged_.bind(this)); |
| 554 } else { |
| 555 this.parent_ = parent; |
| 556 } |
| 557 if (this.data && this.data.id) { |
| 558 if (parent) |
| 559 parentLookup[this.data.id] = this; |
| 560 else |
| 561 delete parentLookup[this.data.id]; |
| 562 } |
| 563 if (this.data && this.data.hasChildren && |
| 564 !this.children.length && !lookupRequests[this.data.id]) { |
| 565 lookupRequests[this.data.id] = true; |
| 566 chrome.send('loadCookie', [this.pathId]); |
| 567 } |
| 568 }, |
| 569 |
| 570 /** |
| 571 * Called when the parent is a CookieListItem whose index has changed. |
| 572 * See the code above that avoids keeping a direct reference to |
| 573 * CookieListItem parents, to allow them to be garbage collected. |
| 574 * @private |
| 575 */ |
| 576 parentIndexChanged_: function(event) { |
| 577 if (typeof this.parent_ == 'number') { |
| 578 this.parent_ = event.newValue; |
| 579 // We set a timeout to update the origin, rather than doing it right |
| 580 // away, because this callback may occur while the list items are |
| 581 // being repopulated following a scroll event. Calling updateOrigin() |
| 582 // immediately could trigger relayout that would reset the scroll |
| 583 // position within the list, among other things. |
| 584 window.setTimeout(this.updateOrigin.bind(this), 0); |
| 585 } |
| 586 }, |
| 587 |
| 588 /** |
| 589 * The cookie tree path id. |
| 590 * @type {string} |
| 591 */ |
| 592 get pathId() { |
| 593 var parent = this.parent; |
| 594 if (parent && parent instanceof CookieTreeNode) |
| 595 return parent.pathId + ',' + this.data.id; |
| 596 return this.data.id; |
| 597 }, |
| 598 }; |
| 599 |
| 600 /** |
| 601 * Creates a new cookies list. |
| 602 * @param {Object=} opt_propertyBag Optional properties. |
| 603 * @constructor |
| 604 * @extends {DeletableItemList} |
| 605 */ |
| 606 var CookiesList = cr.ui.define('list'); |
| 607 |
| 608 CookiesList.prototype = { |
| 609 __proto__: DeletableItemList.prototype, |
| 610 |
| 611 /** @inheritDoc */ |
| 612 decorate: function() { |
| 613 DeletableItemList.prototype.decorate.call(this); |
| 614 this.classList.add('cookie-list'); |
| 615 this.data_ = []; |
| 616 this.dataModel = new ArrayDataModel(this.data_); |
| 617 this.addEventListener('keydown', this.handleKeyLeftRight_.bind(this)); |
| 618 var sm = new ListSingleSelectionModel(); |
| 619 sm.addEventListener('change', this.cookieSelectionChange_.bind(this)); |
| 620 sm.addEventListener('leadIndexChange', this.cookieLeadChange_.bind(this)); |
| 621 this.selectionModel = sm; |
| 622 this.infoNodes = {}; |
| 623 this.fixedHeight = false; |
| 624 var doc = this.ownerDocument; |
| 625 // Create a table for each type of site data (e.g. cookies, databases, |
| 626 // etc.) and save it so that we can reuse it for all origins. |
| 627 for (var type in cookieInfo) { |
| 628 var table = doc.createElement('table'); |
| 629 table.className = 'cookie-details-table'; |
| 630 var tbody = doc.createElement('tbody'); |
| 631 table.appendChild(tbody); |
| 632 var info = {}; |
| 633 for (var i = 0; i < cookieInfo[type].length; i++) { |
| 634 var tr = doc.createElement('tr'); |
| 635 var name = doc.createElement('td'); |
| 636 var data = doc.createElement('td'); |
| 637 var pair = cookieInfo[type][i]; |
| 638 name.className = 'cookie-details-label'; |
| 639 name.textContent = localStrings.getString(pair[1]); |
| 640 data.className = 'cookie-details-value'; |
| 641 data.textContent = ''; |
| 642 tr.appendChild(name); |
| 643 tr.appendChild(data); |
| 644 tbody.appendChild(tr); |
| 645 info[pair[0]] = data; |
| 646 } |
| 647 this.infoNodes[type] = {table: table, info: info}; |
| 648 } |
| 649 }, |
| 650 |
| 651 /** |
| 652 * Handles key down events and looks for left and right arrows, then |
| 653 * dispatches to the currently expanded item, if any. |
| 654 * @param {Event} e The keydown event. |
| 655 * @private |
| 656 */ |
| 657 handleKeyLeftRight_: function(e) { |
| 658 var id = e.keyIdentifier; |
| 659 if ((id == 'Left' || id == 'Right') && this.expandedItem) { |
| 660 var cs = this.ownerDocument.defaultView.getComputedStyle(this); |
| 661 var rtl = cs.direction == 'rtl'; |
| 662 if ((!rtl && id == 'Left') || (rtl && id == 'Right')) |
| 663 this.expandedItem.selectedIndex--; |
| 664 else |
| 665 this.expandedItem.selectedIndex++; |
| 666 this.scrollIndexIntoView(this.expandedItem.listIndex); |
| 667 // Prevent the page itself from scrolling. |
| 668 e.preventDefault(); |
| 669 } |
| 670 }, |
| 671 |
| 672 /** |
| 673 * Called on selection model selection changes. |
| 674 * @param {Event} ce The selection change event. |
| 675 * @private |
| 676 */ |
| 677 cookieSelectionChange_: function(ce) { |
| 678 ce.changes.forEach(function(change) { |
| 679 var listItem = this.getListItemByIndex(change.index); |
| 680 if (listItem) { |
| 681 if (!change.selected) { |
| 682 // We set a timeout here, rather than setting the item unexpanded |
| 683 // immediately, so that if another item gets set expanded right |
| 684 // away, it will be expanded before this item is unexpanded. It |
| 685 // will notice that, and unexpand this item in sync with its own |
| 686 // expansion. Later, this callback will end up having no effect. |
| 687 window.setTimeout(function() { |
| 688 if (!listItem.selected || !listItem.lead) |
| 689 listItem.expanded = false; |
| 690 }, 0); |
| 691 } else if (listItem.lead) { |
| 692 listItem.expanded = true; |
| 693 } |
| 694 } |
| 695 }, this); |
| 696 }, |
| 697 |
| 698 /** |
| 699 * Called on selection model lead changes. |
| 700 * @param {Event} pe The lead change event. |
| 701 * @private |
| 702 */ |
| 703 cookieLeadChange_: function(pe) { |
| 704 if (pe.oldValue != -1) { |
| 705 var listItem = this.getListItemByIndex(pe.oldValue); |
| 706 if (listItem) { |
| 707 // See cookieSelectionChange_ above for why we use a timeout here. |
| 708 window.setTimeout(function() { |
| 709 if (!listItem.lead || !listItem.selected) |
| 710 listItem.expanded = false; |
| 711 }, 0); |
| 712 } |
| 713 } |
| 714 if (pe.newValue != -1) { |
| 715 var listItem = this.getListItemByIndex(pe.newValue); |
| 716 if (listItem && listItem.selected) |
| 717 listItem.expanded = true; |
| 718 } |
| 719 }, |
| 720 |
| 721 /** |
| 722 * The currently expanded item. Used by CookieListItem above. |
| 723 * @type {?CookieListItem} |
| 724 */ |
| 725 expandedItem: null, |
| 726 |
| 727 // from cr.ui.List |
| 728 /** @inheritDoc */ |
| 729 createItem: function(data) { |
| 730 // We use the cached expanded item in order to allow it to maintain some |
| 731 // state (like its fixed height, and which bubble is selected). |
| 732 if (this.expandedItem && this.expandedItem.origin == data) |
| 733 return this.expandedItem; |
| 734 return new CookieListItem(data, this); |
| 735 }, |
| 736 |
| 737 // from options.DeletableItemList |
| 738 /** @inheritDoc */ |
| 739 deleteItemAtIndex: function(index) { |
| 740 var item = this.data_[index]; |
| 741 if (item) { |
| 742 var pathId = item.pathId; |
| 743 if (pathId) |
| 744 chrome.send('removeCookie', [pathId]); |
| 745 } |
| 746 }, |
| 747 |
| 748 /** |
| 749 * Insert the given list of cookie tree nodes at the given index. |
| 750 * Both CookiesList and CookieTreeNode implement this API. |
| 751 * @param {Array.<Object>} data The data objects for the nodes to add. |
| 752 * @param {number} start The index at which to start inserting the nodes. |
| 753 */ |
| 754 insertAt: function(data, start) { |
| 755 spliceTreeNodes(data, start, this.dataModel); |
| 756 }, |
| 757 |
| 758 /** |
| 759 * Remove a cookie tree node from the given index. |
| 760 * Both CookiesList and CookieTreeNode implement this API. |
| 761 * @param {number} index The index of the tree node to remove. |
| 762 */ |
| 763 remove: function(index) { |
| 764 if (index < this.data_.length) |
| 765 this.dataModel.splice(index, 1); |
| 766 }, |
| 767 |
| 768 /** |
| 769 * Clears the list. |
| 770 * Both CookiesList and CookieTreeNode implement this API. |
| 771 * It is used by CookiesList.loadChildren(). |
| 772 */ |
| 773 clear: function() { |
| 774 parentLookup = {}; |
| 775 this.data_ = []; |
| 776 this.dataModel = new ArrayDataModel(this.data_); |
| 777 this.redraw(); |
| 778 }, |
| 779 |
| 780 /** |
| 781 * Add tree nodes by given parent. |
| 782 * @param {Object} parent The parent node. |
| 783 * @param {number} start The index at which to start inserting the nodes. |
| 784 * @param {Array} nodesData Nodes data array. |
| 785 * @private |
| 786 */ |
| 787 addByParent_: function(parent, start, nodesData) { |
| 788 if (!parent) |
| 789 return; |
| 790 |
| 791 parent.startBatchUpdates(); |
| 792 parent.insertAt(nodesData, start); |
| 793 parent.endBatchUpdates(); |
| 794 |
| 795 cr.dispatchSimpleEvent(this, 'change'); |
| 796 }, |
| 797 |
| 798 /** |
| 799 * Add tree nodes by parent id. |
| 800 * This is used by cookies_view.js. |
| 801 * @param {string} parentId Id of the parent node. |
| 802 * @param {number} start The index at which to start inserting the nodes. |
| 803 * @param {Array} nodesData Nodes data array. |
| 804 */ |
| 805 addByParentId: function(parentId, start, nodesData) { |
| 806 var parent = parentId ? parentLookup[parentId] : this; |
| 807 this.addByParent_(parent, start, nodesData); |
| 808 }, |
| 809 |
| 810 /** |
| 811 * Removes tree nodes by parent id. |
| 812 * This is used by cookies_view.js. |
| 813 * @param {string} parentId Id of the parent node. |
| 814 * @param {number} start The index at which to start removing the nodes. |
| 815 * @param {number} count Number of nodes to remove. |
| 816 */ |
| 817 removeByParentId: function(parentId, start, count) { |
| 818 var parent = parentId ? parentLookup[parentId] : this; |
| 819 if (!parent) |
| 820 return; |
| 821 |
| 822 parent.startBatchUpdates(); |
| 823 while (count-- > 0) |
| 824 parent.remove(start); |
| 825 parent.endBatchUpdates(); |
| 826 |
| 827 cr.dispatchSimpleEvent(this, 'change'); |
| 828 }, |
| 829 |
| 830 /** |
| 831 * Loads the immediate children of given parent node. |
| 832 * This is used by cookies_view.js. |
| 833 * @param {string} parentId Id of the parent node. |
| 834 * @param {Array} children The immediate children of parent node. |
| 835 */ |
| 836 loadChildren: function(parentId, children) { |
| 837 if (parentId) |
| 838 delete lookupRequests[parentId]; |
| 839 var parent = parentId ? parentLookup[parentId] : this; |
| 840 if (!parent) |
| 841 return; |
| 842 |
| 843 parent.startBatchUpdates(); |
| 844 parent.clear(); |
| 845 this.addByParent_(parent, 0, children); |
| 846 parent.endBatchUpdates(); |
| 847 }, |
| 848 }; |
| 849 |
| 850 return { |
| 851 CookiesList: CookiesList |
| 852 }; |
| 853 }); |
OLD | NEW |