DOMStorageItemsView.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2008 Nokia Inc. All rights reserved.
  3. * Copyright (C) 2013 Samsung Electronics. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  13. *
  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.
  25. */
  26. /**
  27. * @constructor
  28. * @extends {WebInspector.View}
  29. */
  30. WebInspector.DOMStorageItemsView = function(domStorage)
  31. {
  32. WebInspector.View.call(this);
  33. this.domStorage = domStorage;
  34. this.element.addStyleClass("storage-view");
  35. this.element.addStyleClass("table");
  36. this.deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
  37. this.deleteButton.visible = false;
  38. this.deleteButton.addEventListener("click", this._deleteButtonClicked, this);
  39. this.refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
  40. this.refreshButton.addEventListener("click", this._refreshButtonClicked, this);
  41. this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemsCleared, this._domStorageItemsCleared, this);
  42. this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemRemoved, this._domStorageItemRemoved, this);
  43. this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemAdded, this._domStorageItemAdded, this);
  44. this.domStorage.addEventListener(WebInspector.DOMStorage.Events.DOMStorageItemUpdated, this._domStorageItemUpdated, this);
  45. }
  46. WebInspector.DOMStorageItemsView.prototype = {
  47. get statusBarItems()
  48. {
  49. return [this.refreshButton.element, this.deleteButton.element];
  50. },
  51. wasShown: function()
  52. {
  53. this._update();
  54. },
  55. willHide: function()
  56. {
  57. this.deleteButton.visible = false;
  58. },
  59. /**
  60. * @param {WebInspector.Event} event
  61. */
  62. _domStorageItemsCleared: function(event)
  63. {
  64. if (!this.isShowing() || !this._dataGrid)
  65. return;
  66. this._dataGrid.rootNode().removeChildren();
  67. this._dataGrid.addCreationNode(false);
  68. this.deleteButton.visible = false;
  69. event.consume(true);
  70. },
  71. /**
  72. * @param {WebInspector.Event} event
  73. */
  74. _domStorageItemRemoved: function(event)
  75. {
  76. if (!this.isShowing() || !this._dataGrid)
  77. return;
  78. var storageData = event.data;
  79. var rootNode = this._dataGrid.rootNode();
  80. var children = rootNode.children;
  81. event.consume(true);
  82. for (var i = 0; i < children.length; ++i) {
  83. var childNode = children[i];
  84. if (childNode.data.key === storageData.key) {
  85. rootNode.removeChild(childNode);
  86. this.deleteButton.visible = (children.length > 1);
  87. return;
  88. }
  89. }
  90. },
  91. /**
  92. * @param {WebInspector.Event} event
  93. */
  94. _domStorageItemAdded: function(event)
  95. {
  96. if (!this.isShowing() || !this._dataGrid)
  97. return;
  98. var storageData = event.data;
  99. var rootNode = this._dataGrid.rootNode();
  100. var children = rootNode.children;
  101. event.consume(true);
  102. this.deleteButton.visible = true;
  103. for (var i = 0; i < children.length; ++i)
  104. if (children[i].data.key === storageData.key)
  105. return;
  106. var childNode = new WebInspector.DataGridNode({key: storageData.key, value: storageData.value}, false);
  107. rootNode.insertChild(childNode, children.length - 1);
  108. },
  109. /**
  110. * @param {WebInspector.Event} event
  111. */
  112. _domStorageItemUpdated: function(event)
  113. {
  114. if (!this.isShowing() || !this._dataGrid)
  115. return;
  116. var storageData = event.data;
  117. var rootNode = this._dataGrid.rootNode();
  118. var children = rootNode.children;
  119. event.consume(true);
  120. var keyFound = false;
  121. for (var i = 0; i < children.length; ++i) {
  122. var childNode = children[i];
  123. if (childNode.data.key === storageData.key) {
  124. if (keyFound) {
  125. rootNode.removeChild(childNode);
  126. return;
  127. }
  128. keyFound = true;
  129. if (childNode.data.value !== storageData.value) {
  130. childNode.data.value = storageData.value;
  131. childNode.refresh();
  132. childNode.select();
  133. childNode.reveal();
  134. }
  135. this.deleteButton.visible = true;
  136. }
  137. }
  138. },
  139. _update: function()
  140. {
  141. this.detachChildViews();
  142. this.domStorage.getItems(this._showDOMStorageItems.bind(this));
  143. },
  144. _showDOMStorageItems: function(error, items)
  145. {
  146. if (error)
  147. return;
  148. this._dataGrid = this._dataGridForDOMStorageItems(items);
  149. this._dataGrid.show(this.element);
  150. this.deleteButton.visible = (this._dataGrid.rootNode().children.length > 1);
  151. },
  152. _dataGridForDOMStorageItems: function(items)
  153. {
  154. var columns = [
  155. {id: "key", title: WebInspector.UIString("Key"), editable: true, weight: 50},
  156. {id: "value", title: WebInspector.UIString("Value"), editable: true, weight: 50}
  157. ];
  158. var nodes = [];
  159. var keys = [];
  160. var length = items.length;
  161. for (var i = 0; i < items.length; i++) {
  162. var key = items[i][0];
  163. var value = items[i][1];
  164. var node = new WebInspector.DataGridNode({key: key, value: value}, false);
  165. node.selectable = true;
  166. nodes.push(node);
  167. keys.push(key);
  168. }
  169. var dataGrid = new WebInspector.DataGrid(columns, this._editingCallback.bind(this), this._deleteCallback.bind(this));
  170. dataGrid.setName("DOMStorageItemsView");
  171. length = nodes.length;
  172. for (var i = 0; i < length; ++i)
  173. dataGrid.rootNode().appendChild(nodes[i]);
  174. dataGrid.addCreationNode(false);
  175. if (length > 0)
  176. nodes[0].selected = true;
  177. return dataGrid;
  178. },
  179. _deleteButtonClicked: function(event)
  180. {
  181. if (!this._dataGrid || !this._dataGrid.selectedNode)
  182. return;
  183. this._deleteCallback(this._dataGrid.selectedNode);
  184. this._dataGrid.changeNodeAfterDeletion();
  185. },
  186. _refreshButtonClicked: function(event)
  187. {
  188. this._update();
  189. },
  190. _editingCallback: function(editingNode, columnIdentifier, oldText, newText)
  191. {
  192. var domStorage = this.domStorage;
  193. if ("key" === columnIdentifier) {
  194. if (typeof oldText === "string")
  195. domStorage.removeItem(oldText);
  196. domStorage.setItem(newText, editingNode.data.value || '');
  197. this._removeDupes(editingNode);
  198. } else
  199. domStorage.setItem(editingNode.data.key || '', newText);
  200. },
  201. /**
  202. * @param {!WebInspector.DataGridNode} masterNode
  203. */
  204. _removeDupes: function(masterNode)
  205. {
  206. var rootNode = this._dataGrid.rootNode();
  207. var children = rootNode.children;
  208. for (var i = children.length - 1; i >= 0; --i) {
  209. var childNode = children[i];
  210. if ((childNode.data.key === masterNode.data.key) && (masterNode !== childNode))
  211. rootNode.removeChild(childNode);
  212. }
  213. },
  214. _deleteCallback: function(node)
  215. {
  216. if (!node || node.isCreationNode)
  217. return;
  218. if (this.domStorage)
  219. this.domStorage.removeItem(node.data.key);
  220. },
  221. __proto__: WebInspector.View.prototype
  222. }