2 * Copyright (C) 2008 Nokia Inc. All rights reserved.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * @extends {WebInspector.VBox}
31 WebInspector.DOMStorageItemsView = function(domStorage)
33 WebInspector.VBox.call(this);
35 this.domStorage = domStorage;
37 this.element.classList.add("storage-view", "table");
39 this.deleteButton = new WebInspector.ToolbarButton(WebInspector.UIString("Delete"), "delete-toolbar-item");
40 this.deleteButton.setVisible(false);
41 this.deleteButton.addEventListener("click", this._deleteButtonClicked, this);
43 this.refreshButton = new WebInspector.ToolbarButton(WebInspector.UIString("Refresh"), "refresh-toolbar-item");
44 this.refreshButton.addEventListener("click", this._refreshButtonClicked, this);
46 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemsCleared, this._domStorageItemsCleared, this);
47 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemRemoved, this._domStorageItemRemoved, this);
48 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemAdded, this._domStorageItemAdded, this);
49 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemUpdated, this._domStorageItemUpdated, this);
52 WebInspector.DOMStorageItemsView.prototype = {
54 * @return {!Array.<!WebInspector.ToolbarItem>}
56 toolbarItems: function()
58 return [this.refreshButton, this.deleteButton];
68 this.deleteButton.setVisible(false);
72 * @param {!WebInspector.Event} event
74 _domStorageItemsCleared: function(event)
76 if (!this.isShowing() || !this._dataGrid)
79 this._dataGrid.rootNode().removeChildren();
80 this._dataGrid.addCreationNode(false);
81 this.deleteButton.setVisible(false);
86 * @param {!WebInspector.Event} event
88 _domStorageItemRemoved: function(event)
90 if (!this.isShowing() || !this._dataGrid)
93 var storageData = event.data;
94 var rootNode = this._dataGrid.rootNode();
95 var children = rootNode.children;
99 for (var i = 0; i < children.length; ++i) {
100 var childNode = children[i];
101 if (childNode.data.key === storageData.key) {
102 rootNode.removeChild(childNode);
103 this.deleteButton.setVisible(children.length > 1);
110 * @param {!WebInspector.Event} event
112 _domStorageItemAdded: function(event)
114 if (!this.isShowing() || !this._dataGrid)
117 var storageData = event.data;
118 var rootNode = this._dataGrid.rootNode();
119 var children = rootNode.children;
122 this.deleteButton.setVisible(true);
124 for (var i = 0; i < children.length; ++i)
125 if (children[i].data.key === storageData.key)
128 var childNode = new WebInspector.DataGridNode({key: storageData.key, value: storageData.value}, false);
129 rootNode.insertChild(childNode, children.length - 1);
133 * @param {!WebInspector.Event} event
135 _domStorageItemUpdated: function(event)
137 if (!this.isShowing() || !this._dataGrid)
140 var storageData = event.data;
141 var rootNode = this._dataGrid.rootNode();
142 var children = rootNode.children;
146 var keyFound = false;
147 for (var i = 0; i < children.length; ++i) {
148 var childNode = children[i];
149 if (childNode.data.key === storageData.key) {
151 rootNode.removeChild(childNode);
155 if (childNode.data.value !== storageData.value) {
156 childNode.data.value = storageData.value;
161 this.deleteButton.setVisible(true);
168 this.detachChildWidgets();
169 this.domStorage.getItems(this._showDOMStorageItems.bind(this));
172 _showDOMStorageItems: function(error, items)
177 this._dataGrid = this._dataGridForDOMStorageItems(items);
178 this._dataGrid.show(this.element);
179 this.deleteButton.setVisible(this._dataGrid.rootNode().children.length > 1);
182 _dataGridForDOMStorageItems: function(items)
185 {id: "key", title: WebInspector.UIString("Key"), editable: true, weight: 50},
186 {id: "value", title: WebInspector.UIString("Value"), editable: true, weight: 50}
192 var length = items.length;
193 for (var i = 0; i < items.length; i++) {
194 var key = items[i][0];
195 var value = items[i][1];
196 var node = new WebInspector.DataGridNode({key: key, value: value}, false);
197 node.selectable = true;
202 var dataGrid = new WebInspector.DataGrid(columns, this._editingCallback.bind(this), this._deleteCallback.bind(this));
203 dataGrid.setName("DOMStorageItemsView");
204 length = nodes.length;
205 for (var i = 0; i < length; ++i)
206 dataGrid.rootNode().appendChild(nodes[i]);
207 dataGrid.addCreationNode(false);
209 nodes[0].selected = true;
213 _deleteButtonClicked: function(event)
215 if (!this._dataGrid || !this._dataGrid.selectedNode)
218 this._deleteCallback(this._dataGrid.selectedNode);
219 this._dataGrid.changeNodeAfterDeletion();
222 _refreshButtonClicked: function(event)
227 _editingCallback: function(editingNode, columnIdentifier, oldText, newText)
229 var domStorage = this.domStorage;
230 if ("key" === columnIdentifier) {
231 if (typeof oldText === "string")
232 domStorage.removeItem(oldText);
233 domStorage.setItem(newText, editingNode.data.value || '');
234 this._removeDupes(editingNode);
236 domStorage.setItem(editingNode.data.key || '', newText);
240 * @param {!WebInspector.DataGridNode} masterNode
242 _removeDupes: function(masterNode)
244 var rootNode = this._dataGrid.rootNode();
245 var children = rootNode.children;
246 for (var i = children.length - 1; i >= 0; --i) {
247 var childNode = children[i];
248 if ((childNode.data.key === masterNode.data.key) && (masterNode !== childNode))
249 rootNode.removeChild(childNode);
253 _deleteCallback: function(node)
255 if (!node || node.isCreationNode)
259 this.domStorage.removeItem(node.data.key);
262 __proto__: WebInspector.VBox.prototype