1/* 2 * Copyright (C) 2009 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 BY APPLE INC. ``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.DOMStorageDataGrid = function(columns) 27{ 28 WebInspector.DataGrid.call(this, columns); 29 this.dataTableBody.addEventListener("dblclick", this._ondblclick.bind(this), false); 30} 31 32WebInspector.DOMStorageDataGrid.prototype = { 33 _ondblclick: function(event) 34 { 35 if (this._editing) 36 return; 37 if (this._editingNode) 38 return; 39 this._startEditing(event); 40 }, 41 42 _startEditingColumnOfDataGridNode: function(node, column) 43 { 44 this._editing = true; 45 this._editingNode = node; 46 this._editingNode.select(); 47 WebInspector.panels.databases._unregisterStorageEventListener(); 48 49 var element = this._editingNode._element.children[column]; 50 WebInspector.startEditing(element, this._editingCommitted.bind(this), this._editingCancelled.bind(this), element.textContent); 51 window.getSelection().setBaseAndExtent(element, 0, element, 1); 52 }, 53 54 _startEditing: function(event) 55 { 56 var element = event.target.enclosingNodeOrSelfWithNodeName("td"); 57 if (!element) 58 return; 59 60 this._editingNode = this.dataGridNodeFromEvent(event); 61 if (!this._editingNode) { 62 if (!this.creationNode) 63 return; 64 this._editingNode = this.creationNode; 65 } 66 67 // Force editing the "Key" column when editing the creation node 68 if (this._editingNode.isCreationNode) 69 return this._startEditingColumnOfDataGridNode(this._editingNode, 0); 70 71 this._editing = true; 72 WebInspector.panels.databases._unregisterStorageEventListener(); 73 WebInspector.startEditing(element, this._editingCommitted.bind(this), this._editingCancelled.bind(this), element.textContent); 74 window.getSelection().setBaseAndExtent(element, 0, element, 1); 75 }, 76 77 _editingCommitted: function(element, newText, oldText, context, moveDirection) 78 { 79 var columnIdentifier = (element.hasStyleClass("0-column") ? 0 : 1); 80 var textBeforeEditing = this._editingNode.data[columnIdentifier]; 81 var currentEditingNode = this._editingNode; 82 83 function moveToNextIfNeeded(wasChange) { 84 if (!moveDirection) 85 return; 86 87 if (moveDirection === "forward") { 88 if (currentEditingNode.isCreationNode && columnIdentifier === 0 && !wasChange) 89 return; 90 91 if (columnIdentifier === 0) 92 return this._startEditingColumnOfDataGridNode(currentEditingNode, 1); 93 94 var nextDataGridNode = currentEditingNode.traverseNextNode(true, null, true); 95 if (nextDataGridNode) 96 return this._startEditingColumnOfDataGridNode(nextDataGridNode, 0); 97 if (currentEditingNode.isCreationNode && wasChange) { 98 addCreationNode(false); 99 return this._startEditingColumnOfDataGridNode(this.creationNode, 0); 100 } 101 return; 102 } 103 104 if (moveDirection === "backward") { 105 if (columnIdentifier === 1) 106 return this._startEditingColumnOfDataGridNode(currentEditingNode, 0); 107 var nextDataGridNode = currentEditingNode.traversePreviousNode(true, null, true); 108 109 if (nextDataGridNode) 110 return this._startEditingColumnOfDataGridNode(nextDataGridNode, 1); 111 return; 112 } 113 } 114 115 if (textBeforeEditing == newText) { 116 this._editingCancelled(element); 117 moveToNextIfNeeded.call(this, false); 118 return; 119 } 120 121 var domStorage = WebInspector.panels.databases.visibleView.domStorage.domStorage; 122 if (domStorage) { 123 if (columnIdentifier == 0) { 124 if (domStorage.getItem(newText) != null) { 125 element.textContent = this._editingNode.data[0]; 126 this._editingCancelled(element); 127 moveToNextIfNeeded.call(this, false); 128 return; 129 } 130 domStorage.removeItem(this._editingNode.data[0]); 131 domStorage.setItem(newText, this._editingNode.data[1]); 132 this._editingNode.data[0] = newText; 133 } else { 134 domStorage.setItem(this._editingNode.data[0], newText); 135 this._editingNode.data[1] = newText; 136 } 137 } 138 139 if (this._editingNode.isCreationNode) 140 this.addCreationNode(false); 141 142 this._editingCancelled(element); 143 moveToNextIfNeeded.call(this, true); 144 }, 145 146 _editingCancelled: function(element, context) 147 { 148 delete this._editing; 149 this._editingNode = null; 150 WebInspector.panels.databases._registerStorageEventListener(); 151 }, 152 153 deleteSelectedRow: function() 154 { 155 var node = this.selectedNode; 156 if (this.selectedNode.isCreationNode) 157 return; 158 159 var domStorage = WebInspector.panels.databases.visibleView.domStorage.domStorage; 160 if (node && domStorage) 161 domStorage.removeItem(node.data[0]); 162 } 163} 164 165WebInspector.DOMStorageDataGrid.prototype.__proto__ = WebInspector.DataGrid.prototype; 166