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");
38 this.element.classList.add("table");
40 this.deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
41 this.deleteButton.visible = false;
42 this.deleteButton.addEventListener("click", this._deleteButtonClicked, this);
44 this.refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
45 this.refreshButton.addEventListener("click", this._refreshButtonClicked, this);
47 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemsCleared, this._domStorageItemsCleared, this);
48 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemRemoved, this._domStorageItemRemoved, this);
49 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemAdded, this._domStorageItemAdded, this);
50 this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemUpdated, this._domStorageItemUpdated, this);
53 WebInspector.DOMStorageItemsView.prototype = {
56 return [this.refreshButton.element, this.deleteButton.element];
66 this.deleteButton.visible = false;
70 * @param {!WebInspector.Event} event
72 _domStorageItemsCleared: function(event)
74 if (!this.isShowing() || !this._dataGrid)
77 this._dataGrid.rootNode().removeChildren();
78 this._dataGrid.addCreationNode(false);
79 this.deleteButton.visible = false;
84 * @param {!WebInspector.Event} event
86 _domStorageItemRemoved: function(event)
88 if (!this.isShowing() || !this._dataGrid)
91 var storageData = event.data;
92 var rootNode = this._dataGrid.rootNode();
93 var children = rootNode.children;
97 for (var i = 0; i < children.length; ++i) {
98 var childNode = children[i];
99 if (childNode.data.key === storageData.key) {
100 rootNode.removeChild(childNode);
101 this.deleteButton.visible = (children.length > 1);
108 * @param {!WebInspector.Event} event
110 _domStorageItemAdded: function(event)
112 if (!this.isShowing() || !this._dataGrid)
115 var storageData = event.data;
116 var rootNode = this._dataGrid.rootNode();
117 var children = rootNode.children;
120 this.deleteButton.visible = true;
122 for (var i = 0; i < children.length; ++i)
123 if (children[i].data.key === storageData.key)
126 var childNode = new WebInspector.DataGridNode({key: storageData.key, value: storageData.value}, false);
127 rootNode.insertChild(childNode, children.length - 1);
131 * @param {!WebInspector.Event} event
133 _domStorageItemUpdated: function(event)
135 if (!this.isShowing() || !this._dataGrid)
138 var storageData = event.data;
139 var rootNode = this._dataGrid.rootNode();
140 var children = rootNode.children;
144 var keyFound = false;
145 for (var i = 0; i < children.length; ++i) {
146 var childNode = children[i];
147 if (childNode.data.key === storageData.key) {
149 rootNode.removeChild(childNode);
153 if (childNode.data.value !== storageData.value) {
154 childNode.data.value = storageData.value;
159 this.deleteButton.visible = true;
166 this.detachChildViews();
167 this.domStorage.getItems(this._showDOMStorageItems.bind(this));
170 _showDOMStorageItems: function(error, items)
175 this._dataGrid = this._dataGridForDOMStorageItems(items);
176 this._dataGrid.show(this.element);
177 this.deleteButton.visible = (this._dataGrid.rootNode().children.length > 1);
180 _dataGridForDOMStorageItems: function(items)
183 {id: "key", title: WebInspector.UIString("Key"), editable: true, weight: 50},
184 {id: "value", title: WebInspector.UIString("Value"), editable: true, weight: 50}
190 var length = items.length;
191 for (var i = 0; i < items.length; i++) {
192 var key = items[i][0];
193 var value = items[i][1];
194 var node = new WebInspector.DataGridNode({key: key, value: value}, false);
195 node.selectable = true;
200 var dataGrid = new WebInspector.DataGrid(columns, this._editingCallback.bind(this), this._deleteCallback.bind(this));
201 dataGrid.setName("DOMStorageItemsView");
202 length = nodes.length;
203 for (var i = 0; i < length; ++i)
204 dataGrid.rootNode().appendChild(nodes[i]);
205 dataGrid.addCreationNode(false);
207 nodes[0].selected = true;
211 _deleteButtonClicked: function(event)
213 if (!this._dataGrid || !this._dataGrid.selectedNode)
216 this._deleteCallback(this._dataGrid.selectedNode);
217 this._dataGrid.changeNodeAfterDeletion();
220 _refreshButtonClicked: function(event)
225 _editingCallback: function(editingNode, columnIdentifier, oldText, newText)
227 var domStorage = this.domStorage;
228 if ("key" === columnIdentifier) {
229 if (typeof oldText === "string")
230 domStorage.removeItem(oldText);
231 domStorage.setItem(newText, editingNode.data.value || '');
232 this._removeDupes(editingNode);
234 domStorage.setItem(editingNode.data.key || '', newText);
238 * @param {!WebInspector.DataGridNode} masterNode
240 _removeDupes: function(masterNode)
242 var rootNode = this._dataGrid.rootNode();
243 var children = rootNode.children;
244 for (var i = children.length - 1; i >= 0; --i) {
245 var childNode = children[i];
246 if ((childNode.data.key === masterNode.data.key) && (masterNode !== childNode))
247 rootNode.removeChild(childNode);
251 _deleteCallback: function(node)
253 if (!node || node.isCreationNode)
257 this.domStorage.removeItem(node.data.key);
260 __proto__: WebInspector.VBox.prototype