Unreviewed one line fix for DOMStorageItemsView.
[WebKit-https.git] / Source / WebCore / inspector / front-end / DOMStorageItemsView.js
1 /*
2  * Copyright (C) 2008 Nokia Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 /**
27  * @constructor
28  * @extends {WebInspector.View}
29  */
30 WebInspector.DOMStorageItemsView = function(domStorage)
31 {
32     WebInspector.View.call(this);
33
34     this.domStorage = domStorage;
35
36     this.element.addStyleClass("storage-view");
37     this.element.addStyleClass("table");
38
39     this.deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
40     this.deleteButton.visible = false;
41     this.deleteButton.addEventListener("click", this._deleteButtonClicked, this);
42
43     this.refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
44     this.refreshButton.addEventListener("click", this._refreshButtonClicked, this);
45 }
46
47 WebInspector.DOMStorageItemsView.prototype = {
48     get statusBarItems()
49     {
50         return [this.refreshButton.element, this.deleteButton.element];
51     },
52
53     wasShown: function()
54     {
55         this.update();
56     },
57
58     willHide: function()
59     {
60         this.deleteButton.visible = false;
61     },
62
63     update: function()
64     {
65         this.detachChildViews();
66         this.domStorage.getEntries(this._showDOMStorageEntries.bind(this));
67     },
68
69     _showDOMStorageEntries: function(error, entries)
70     {
71         if (error)
72             return;
73
74         this._dataGrid = this._dataGridForDOMStorageEntries(entries);
75         this._dataGrid.show(this.element);
76         this._dataGrid.autoSizeColumns(10);
77         this.deleteButton.visible = true;
78     },
79
80     _dataGridForDOMStorageEntries: function(entries)
81     {
82         var columns = {};
83         columns[0] = {};
84         columns[1] = {};
85         columns[0].title = WebInspector.UIString("Key");
86         columns[1].title = WebInspector.UIString("Value");
87
88         var nodes = [];
89
90         var keys = [];
91         var length = entries.length;
92         for (var i = 0; i < entries.length; i++) {
93             var data = {};
94
95             var key = entries[i][0];
96             data[0] = key;
97             var value = entries[i][1];
98             data[1] = value;
99             var node = new WebInspector.DataGridNode(data, false);
100             node.selectable = true;
101             nodes.push(node);
102             keys.push(key);
103         }
104
105         var dataGrid = new WebInspector.DataGrid(columns, this._editingCallback.bind(this), this._deleteCallback.bind(this));
106         length = nodes.length;
107         for (var i = 0; i < length; ++i)
108             dataGrid.appendChild(nodes[i]);
109         dataGrid.addCreationNode(false);
110         if (length > 0)
111             nodes[0].selected = true;
112         return dataGrid;
113     },
114
115     _deleteButtonClicked: function(event)
116     {
117         if (!this._dataGrid || !this._dataGrid.selectedNode)
118             return;
119
120         this._deleteCallback(this._dataGrid.selectedNode);
121     },
122
123     _refreshButtonClicked: function(event)
124     {
125         this.update();
126     },
127
128     _editingCallback: function(editingNode, columnIdentifier, oldText, newText)
129     {
130         var domStorage = this.domStorage;
131         if (columnIdentifier === 0) {
132             if (oldText)
133                 domStorage.removeItem(oldText);
134
135             domStorage.setItem(newText, editingNode.data[1]);
136         } else {
137             domStorage.setItem(editingNode.data[0], newText);
138         }
139
140         this.update();
141     },
142
143     _deleteCallback: function(node)
144     {
145         if (!node || node.isCreationNode)
146             return;
147
148         if (this.domStorage)
149             this.domStorage.removeItem(node.data[0]);
150
151         this.update();
152     }
153 }
154
155 WebInspector.DOMStorageItemsView.prototype.__proto__ = WebInspector.View.prototype;