• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26WebInspector.DOMStorageItemsView = function(domStorage)
27{
28    WebInspector.View.call(this);
29
30    this.domStorage = domStorage;
31
32    this.element.addStyleClass("storage-view");
33    this.element.addStyleClass("table");
34
35    this.deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
36    this.deleteButton.visible = false;
37    this.deleteButton.addEventListener("click", this._deleteButtonClicked.bind(this), false);
38
39    this.refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
40    this.refreshButton.addEventListener("click", this._refreshButtonClicked.bind(this), false);
41}
42
43WebInspector.DOMStorageItemsView.prototype = {
44    get statusBarItems()
45    {
46        return [this.refreshButton.element, this.deleteButton.element];
47    },
48
49    show: function(parentElement)
50    {
51        WebInspector.View.prototype.show.call(this, parentElement);
52        this.update();
53    },
54
55    hide: function()
56    {
57        WebInspector.View.prototype.hide.call(this);
58        this.deleteButton.visible = false;
59    },
60
61    update: function()
62    {
63        this.element.removeChildren();
64        this.domStorage.getEntries(this._showDOMStorageEntries.bind(this));
65    },
66
67    _showDOMStorageEntries: function(error, entries)
68    {
69        if (error)
70            return;
71
72        this._dataGrid = this._dataGridForDOMStorageEntries(entries);
73        this.element.appendChild(this._dataGrid.element);
74        this._dataGrid.autoSizeColumns(10);
75        this.deleteButton.visible = true;
76    },
77
78    resize: function()
79    {
80        if (this._dataGrid)
81            this._dataGrid.updateWidths();
82    },
83
84    _dataGridForDOMStorageEntries: function(entries)
85    {
86        var columns = {};
87        columns[0] = {};
88        columns[1] = {};
89        columns[0].title = WebInspector.UIString("Key");
90        columns[1].title = WebInspector.UIString("Value");
91
92        var nodes = [];
93
94        var keys = [];
95        var length = entries.length;
96        for (var i = 0; i < entries.length; i++) {
97            var data = {};
98
99            var key = entries[i][0];
100            data[0] = key;
101            var value = entries[i][1];
102            data[1] = value;
103            var node = new WebInspector.DataGridNode(data, false);
104            node.selectable = true;
105            nodes.push(node);
106            keys.push(key);
107        }
108
109        var dataGrid = new WebInspector.DataGrid(columns, this._editingCallback.bind(this), this._deleteCallback.bind(this));
110        var length = nodes.length;
111        for (var i = 0; i < length; ++i)
112            dataGrid.appendChild(nodes[i]);
113        dataGrid.addCreationNode(false);
114        if (length > 0)
115            nodes[0].selected = true;
116        return dataGrid;
117    },
118
119    _deleteButtonClicked: function(event)
120    {
121        if (!this._dataGrid || !this._dataGrid.selectedNode)
122            return;
123
124        this._deleteCallback(this._dataGrid.selectedNode);
125    },
126
127    _refreshButtonClicked: function(event)
128    {
129        this.update();
130    },
131
132    _editingCallback: function(editingNode, columnIdentifier, oldText, newText)
133    {
134        var domStorage = this.domStorage;
135        if (columnIdentifier === 0) {
136            if (oldText)
137                domStorage.removeItem(oldText);
138
139            domStorage.setItem(newText, editingNode.data[1]);
140        } else {
141            domStorage.setItem(editingNode.data[0], newText);
142        }
143
144        this.update();
145    },
146
147    _deleteCallback: function(node)
148    {
149        if (!node || node.isCreationNode)
150            return;
151
152        if (this.domStorage)
153            this.domStorage.removeItem(node.data[0]);
154
155        this.update();
156    }
157}
158
159WebInspector.DOMStorageItemsView.prototype.__proto__ = WebInspector.View.prototype;
160