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 var callback = this._showDOMStorageEntries.bind(this); 65 this.domStorage.getEntries(callback); 66 }, 67 68 _showDOMStorageEntries: function(entries) 69 { 70 this._dataGrid = this._dataGridForDOMStorageEntries(entries); 71 this.element.appendChild(this._dataGrid.element); 72 this._dataGrid.autoSizeColumns(10); 73 this.deleteButton.visible = true; 74 }, 75 76 resize: function() 77 { 78 if (this._dataGrid) 79 this._dataGrid.updateWidths(); 80 }, 81 82 _dataGridForDOMStorageEntries: function(entries) 83 { 84 var columns = {}; 85 columns[0] = {}; 86 columns[1] = {}; 87 columns[0].title = WebInspector.UIString("Key"); 88 columns[1].title = WebInspector.UIString("Value"); 89 90 var nodes = []; 91 92 var keys = []; 93 var length = entries.length; 94 for (var i = 0; i < entries.length; i++) { 95 var data = {}; 96 97 var key = entries[i][0]; 98 data[0] = key; 99 var value = entries[i][1]; 100 data[1] = value; 101 var node = new WebInspector.DataGridNode(data, false); 102 node.selectable = true; 103 nodes.push(node); 104 keys.push(key); 105 } 106 107 var dataGrid = new WebInspector.DataGrid(columns, this._editingCallback.bind(this), this._deleteCallback.bind(this)); 108 var length = nodes.length; 109 for (var i = 0; i < length; ++i) 110 dataGrid.appendChild(nodes[i]); 111 dataGrid.addCreationNode(false); 112 if (length > 0) 113 nodes[0].selected = true; 114 return dataGrid; 115 }, 116 117 _deleteButtonClicked: function(event) 118 { 119 if (!this._dataGrid || !this._dataGrid.selectedNode) 120 return; 121 122 this._deleteCallback(this._dataGrid.selectedNode); 123 }, 124 125 _refreshButtonClicked: function(event) 126 { 127 this.update(); 128 }, 129 130 _editingCallback: function(editingNode, columnIdentifier, oldText, newText) 131 { 132 var domStorage = this.domStorage; 133 if (columnIdentifier === 0) { 134 if (oldText) 135 domStorage.removeItem(oldText); 136 137 domStorage.setItem(newText, editingNode.data[1]); 138 } else { 139 domStorage.setItem(editingNode.data[0], newText); 140 } 141 142 this.update(); 143 }, 144 145 _deleteCallback: function(node) 146 { 147 if (!node || node.isCreationNode) 148 return; 149 150 if (this.domStorage) 151 this.domStorage.removeItem(node.data[0]); 152 153 this.update(); 154 } 155} 156 157WebInspector.DOMStorageItemsView.prototype.__proto__ = WebInspector.View.prototype; 158