FileSystemView.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2012 Google 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 are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.SidebarView}
  33. * @param {WebInspector.FileSystemModel.FileSystem} fileSystem
  34. */
  35. WebInspector.FileSystemView = function(fileSystem)
  36. {
  37. WebInspector.SidebarView.call(this, WebInspector.SidebarView.SidebarPosition.Start, "FileSystemViewSidebarWidth");
  38. this.element.addStyleClass("file-system-view");
  39. this.element.addStyleClass("storage-view");
  40. var directoryTreeElement = this.element.createChild("ol", "filesystem-directory-tree");
  41. this._directoryTree = new TreeOutline(directoryTreeElement);
  42. this.sidebarElement.appendChild(directoryTreeElement);
  43. this.sidebarElement.addStyleClass("outline-disclosure");
  44. this.sidebarElement.addStyleClass("sidebar");
  45. var rootItem = new WebInspector.FileSystemView.EntryTreeElement(this, fileSystem.root);
  46. rootItem.expanded = true;
  47. this._directoryTree.appendChild(rootItem);
  48. this._visibleView = null;
  49. this._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
  50. this._refreshButton.visible = true;
  51. this._refreshButton.addEventListener("click", this._refresh, this);
  52. this._deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
  53. this._deleteButton.visible = true;
  54. this._deleteButton.addEventListener("click", this._confirmDelete, this);
  55. }
  56. WebInspector.FileSystemView.prototype = {
  57. /**
  58. * @type {Array.<Element>}
  59. */
  60. get statusBarItems()
  61. {
  62. return [this._refreshButton.element, this._deleteButton.element];
  63. },
  64. /**
  65. * @type {WebInspector.View}
  66. */
  67. get visibleView()
  68. {
  69. return this._visibleView;
  70. },
  71. /**
  72. * @param {WebInspector.View} view
  73. */
  74. showView: function(view)
  75. {
  76. if (this._visibleView === view)
  77. return;
  78. if (this._visibleView)
  79. this._visibleView.detach();
  80. this._visibleView = view;
  81. view.show(this.mainElement);
  82. },
  83. _refresh: function()
  84. {
  85. this._directoryTree.children[0].refresh();
  86. },
  87. _confirmDelete: function()
  88. {
  89. if (confirm(WebInspector.UIString("Are you sure you want to delete the selected entry?")))
  90. this._delete();
  91. },
  92. _delete: function()
  93. {
  94. this._directoryTree.selectedTreeElement.deleteEntry();
  95. },
  96. __proto__: WebInspector.SidebarView.prototype
  97. }
  98. /**
  99. * @constructor
  100. * @extends {TreeElement}
  101. * @param {WebInspector.FileSystemView} fileSystemView
  102. * @param {WebInspector.FileSystemModel.Entry} entry
  103. */
  104. WebInspector.FileSystemView.EntryTreeElement = function(fileSystemView, entry)
  105. {
  106. TreeElement.call(this, entry.name, null, entry.isDirectory);
  107. this._entry = entry;
  108. this._fileSystemView = fileSystemView;
  109. }
  110. WebInspector.FileSystemView.EntryTreeElement.prototype = {
  111. onattach: function()
  112. {
  113. var selection = this.listItemElement.createChild("div", "selection");
  114. this.listItemElement.insertBefore(selection, this.listItemElement.firstChild);
  115. },
  116. onselect: function()
  117. {
  118. if (!this._view) {
  119. if (this._entry.isDirectory)
  120. this._view = new WebInspector.DirectoryContentView();
  121. else {
  122. var file = /** @type {WebInspector.FileSystemModel.File} */ (this._entry);
  123. this._view = new WebInspector.FileContentView(file);
  124. }
  125. }
  126. this._fileSystemView.showView(this._view);
  127. this.refresh();
  128. },
  129. onpopulate: function()
  130. {
  131. this.refresh();
  132. },
  133. /**
  134. * @param {number} errorCode
  135. * @param {Array.<WebInspector.FileSystemModel.Entry>=} entries
  136. */
  137. _directoryContentReceived: function(errorCode, entries)
  138. {
  139. if (errorCode === FileError.NOT_FOUND_ERR) {
  140. if (this.parent !== this.treeOutline)
  141. this.parent.refresh();
  142. return;
  143. }
  144. if (errorCode !== 0 || !entries) {
  145. console.error("Failed to read directory: " + errorCode);
  146. return;
  147. }
  148. entries.sort(WebInspector.FileSystemModel.Entry.compare);
  149. if (this._view)
  150. this._view.showEntries(entries);
  151. var oldChildren = this.children.slice(0);
  152. var newEntryIndex = 0;
  153. var oldChildIndex = 0;
  154. var currentTreeItem = 0;
  155. while (newEntryIndex < entries.length && oldChildIndex < oldChildren.length) {
  156. var newEntry = entries[newEntryIndex];
  157. var oldChild = oldChildren[oldChildIndex];
  158. var order = newEntry.name.compareTo(oldChild._entry.name);
  159. if (order === 0) {
  160. if (oldChild._entry.isDirectory)
  161. oldChild.shouldRefreshChildren = true;
  162. else
  163. oldChild.refresh();
  164. ++newEntryIndex;
  165. ++oldChildIndex;
  166. ++currentTreeItem;
  167. continue;
  168. }
  169. if (order < 0) {
  170. this.insertChild(new WebInspector.FileSystemView.EntryTreeElement(this._fileSystemView, newEntry), currentTreeItem);
  171. ++newEntryIndex;
  172. ++currentTreeItem;
  173. continue;
  174. }
  175. this.removeChildAtIndex(currentTreeItem);
  176. ++oldChildIndex;
  177. }
  178. for (; newEntryIndex < entries.length; ++newEntryIndex)
  179. this.appendChild(new WebInspector.FileSystemView.EntryTreeElement(this._fileSystemView, entries[newEntryIndex]));
  180. for (; oldChildIndex < oldChildren.length; ++oldChildIndex)
  181. this.removeChild(oldChildren[oldChildIndex]);
  182. },
  183. refresh: function()
  184. {
  185. if (!this._entry.isDirectory) {
  186. if (this._view && this._view === this._fileSystemView.visibleView) {
  187. var fileContentView = /** @type {WebInspector.FileContentView} */ (this._view);
  188. fileContentView.refresh();
  189. }
  190. } else
  191. this._entry.requestDirectoryContent(this._directoryContentReceived.bind(this));
  192. },
  193. deleteEntry: function()
  194. {
  195. this._entry.deleteEntry(this._deletionCompleted.bind(this));
  196. },
  197. _deletionCompleted: function()
  198. {
  199. if (this._entry != this._entry.fileSystem.root)
  200. this.parent.refresh();
  201. },
  202. __proto__: TreeElement.prototype
  203. }