| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 /** | |
| 6 * @fileoverview | |
| 7 * 'site-data-details-dialog' provides a dialog to show details of site data | |
| 8 * stored by a given site. | |
| 9 */ | |
| 10 Polymer({ | |
| 11 is: 'site-data-details-dialog', | |
| 12 | |
| 13 behaviors: [SiteSettingsBehavior], | |
| 14 | |
| 15 properties: { | |
| 16 /** | |
| 17 * The title of the dialog. | |
| 18 */ | |
| 19 title_: String, | |
| 20 | |
| 21 /** | |
| 22 * The site to show details for. | |
| 23 * @type {!settings.CookieTreeNode} | |
| 24 * @private | |
| 25 */ | |
| 26 site_: Object, | |
| 27 | |
| 28 /** | |
| 29 * The cookie entries for the given site. | |
| 30 * @type {!Array<!CookieDataItem>} | |
| 31 * @private | |
| 32 */ | |
| 33 entries_: Array, | |
| 34 | |
| 35 /** | |
| 36 * The cookie nodes to show in the dialog. | |
| 37 * @type {!Array<CookieDataForDisplay>} | |
| 38 * @private | |
| 39 */ | |
| 40 cookieNodes_: Object, | |
| 41 | |
| 42 /** | |
| 43 * The index of the last selected item. | |
| 44 */ | |
| 45 lastSelectedIndex_: Number, | |
| 46 | |
| 47 /** | |
| 48 * Our WebUI listener. | |
| 49 * @type {?WebUIListener} | |
| 50 */ | |
| 51 listener_: Object, | |
| 52 }, | |
| 53 | |
| 54 /** | |
| 55 * Opens the dialog. | |
| 56 * @param {!settings.CookieTreeNode} site The site to show data for. | |
| 57 */ | |
| 58 open: function(site) { | |
| 59 this.site_ = site; | |
| 60 this.populateDialog_(); | |
| 61 this.listener_ = cr.addWebUIListener( | |
| 62 'onTreeItemRemoved', this.onTreeItemRemoved_.bind(this)); | |
| 63 this.$.dialog.showModal(); | |
| 64 }, | |
| 65 | |
| 66 /** | |
| 67 * Closes the dialog, if open. | |
| 68 */ | |
| 69 close: function() { | |
| 70 var dialog = /** @type {!CrDialogElement} */(this.$.dialog); | |
| 71 if (dialog.open) | |
| 72 dialog.close(); | |
| 73 }, | |
| 74 | |
| 75 /** | |
| 76 * Populates the dialog with the data about the site. | |
| 77 * @private | |
| 78 */ | |
| 79 populateDialog_: function() { | |
| 80 this.title_ = loadTimeData.getStringF('siteSettingsCookieDialog', | |
| 81 this.site_.data.title); | |
| 82 | |
| 83 this.entries_ = this.site_.getCookieList(); | |
| 84 if (this.entries_.length < 2) { | |
| 85 // When there's only one item to show, hide the picker and change the | |
| 86 // 'Remove All' button to read 'Remove' instead. | |
| 87 this.$.container.hidden = true; | |
| 88 this.$.clear.textContent = | |
| 89 loadTimeData.getString('siteSettingsCookieRemove'); | |
| 90 } else { | |
| 91 this.$.picker.value = this.entries_[0].id; | |
| 92 this.lastSelectedIndex_ = 0; | |
| 93 } | |
| 94 | |
| 95 this.populateItem_(this.entries_[0].id, this.site_); | |
| 96 }, | |
| 97 | |
| 98 /** | |
| 99 * Recursively look up a node path for a leaf node with a given id. | |
| 100 * @param {!settings.CookieTreeNode} node The node to start with. | |
| 101 * @param {string} currentPath The path constructed so far. | |
| 102 * @param {string} targetId The id of the target leaf node to look for. | |
| 103 * @return {string} The path of the node returned (or blank if not found). | |
| 104 * @private | |
| 105 */ | |
| 106 nodePath_: function(node, currentPath, targetId) { | |
| 107 if (node.data.id == targetId) | |
| 108 return currentPath; | |
| 109 | |
| 110 for (var i = 0; i < node.children_.length; ++i) { | |
| 111 var child = node.children_[i]; | |
| 112 var path = this.nodePath_( | |
| 113 child, currentPath + ',' + child.data.id, targetId); | |
| 114 if (path.length > 0) | |
| 115 return path; | |
| 116 } | |
| 117 | |
| 118 return ''; | |
| 119 }, | |
| 120 | |
| 121 /** | |
| 122 * Add the cookie data to the content section of this dialog. | |
| 123 * @param {string} id The id of the cookie node to display. | |
| 124 * @param {!settings.CookieTreeNode} site The current site. | |
| 125 * @private | |
| 126 */ | |
| 127 populateItem_: function(id, site) { | |
| 128 var node = site.fetchNodeById(id, true); | |
| 129 if (node) | |
| 130 this.cookieNodes_ = getCookieData(node.data); | |
| 131 }, | |
| 132 | |
| 133 /** | |
| 134 * Called when a single item has been removed. | |
| 135 * @param {!CookieRemovePacket} args The details about what to remove. | |
| 136 * @private | |
| 137 */ | |
| 138 onTreeItemRemoved_: function(args) { | |
| 139 this.entries_ = this.site_.getCookieList(); | |
| 140 if (this.site_.children_.length == 0 || this.entries_.length == 0) { | |
| 141 this.close(); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 if (this.entries_.length <= this.lastSelectedIndex_) | |
| 146 this.lastSelectedIndex_ = this.entries_.length - 1; | |
| 147 var selectedId = this.entries_[this.lastSelectedIndex_].id; | |
| 148 this.$.picker.value = selectedId; | |
| 149 this.populateItem_(selectedId, this.site_); | |
| 150 }, | |
| 151 | |
| 152 /** | |
| 153 * A handler for when the user changes the dropdown box (switches cookies). | |
| 154 * @private | |
| 155 */ | |
| 156 onItemSelected_: function() { | |
| 157 var selectedItem = this.$.picker.value; | |
| 158 this.populateItem_(selectedItem, this.site_); | |
| 159 | |
| 160 // Store the index of what was selected so we can re-select the next value | |
| 161 // when things get deleted. | |
| 162 for (var i = 0; i < this.entries_.length; ++i) { | |
| 163 if (this.entries_[i].data.id == selectedItem) { | |
| 164 this.lastSelectedIndex_ = i; | |
| 165 break; | |
| 166 } | |
| 167 } | |
| 168 }, | |
| 169 | |
| 170 getEntryDescription: function(item) { | |
| 171 // Frequently there are multiple cookies per site. To avoid showing a list | |
| 172 // of '1 cookie', '1 cookie', ... etc, it is better to show the title of the | |
| 173 // cookie to differentiate them. | |
| 174 if (item.data.type == 'cookie') | |
| 175 return loadTimeData.getStringF('siteSettingsCookie', item.title); | |
| 176 | |
| 177 return getCookieDataCategoryText(item.data.type, item.data.totalUsage); | |
| 178 }, | |
| 179 | |
| 180 /** | |
| 181 * A handler for when the user opts to remove a single cookie. | |
| 182 * @private | |
| 183 */ | |
| 184 onRemove_: function(event) { | |
| 185 this.browserProxy.removeCookie(this.nodePath_( | |
| 186 this.site_, this.site_.data.id, this.$.picker.value)); | |
| 187 }, | |
| 188 | |
| 189 /** | |
| 190 * A handler for when the user opts to remove all cookies. | |
| 191 * @private | |
| 192 */ | |
| 193 onRemoveAll_: function(event) { | |
| 194 cr.removeWebUIListener(this.listener_); | |
| 195 this.browserProxy.removeCookie(this.site_.data.id); | |
| 196 this.close(); | |
| 197 }, | |
| 198 | |
| 199 /** @private */ | |
| 200 onCancelTap_: function() { | |
| 201 this.$.dialog.cancel(); | |
| 202 }, | |
| 203 }); | |
| OLD | NEW |