EditFileSystemDialog.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (C) 2013 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.DialogDelegate}
  33. * @param {string} fileSystemPath
  34. */
  35. WebInspector.EditFileSystemDialog = function(fileSystemPath)
  36. {
  37. WebInspector.DialogDelegate.call(this);
  38. this._fileSystemPath = fileSystemPath;
  39. this.element = document.createElement("div");
  40. this.element.className = "edit-file-system-dialog";
  41. var header = this.element.createChild("div", "header");
  42. var headerText = header.createChild("span");
  43. headerText.textContent = "Edit file system";
  44. var closeButton = header.createChild("div", "close-button-gray done-button");
  45. closeButton.addEventListener("click", this._onDoneClick.bind(this), false);
  46. var contents = this.element.createChild("div", "contents");
  47. WebInspector.isolatedFileSystemManager.mapping().addEventListener(WebInspector.FileSystemMapping.Events.FileMappingAdded, this._fileMappingAdded, this);
  48. WebInspector.isolatedFileSystemManager.mapping().addEventListener(WebInspector.FileSystemMapping.Events.FileMappingRemoved, this._fileMappingRemoved, this);
  49. WebInspector.isolatedFileSystemManager.mapping().addEventListener(WebInspector.FileSystemMapping.Events.ExcludedFolderAdded, this._excludedFolderAdded, this);
  50. WebInspector.isolatedFileSystemManager.mapping().addEventListener(WebInspector.FileSystemMapping.Events.ExcludedFolderRemoved, this._excludedFolderRemoved, this);
  51. var blockHeader = contents.createChild("div", "block-header");
  52. blockHeader.textContent = "Mappings";
  53. this._fileMappingsSection = contents.createChild("div", "file-mappings-section");
  54. this._fileMappingsListContainer = this._fileMappingsSection.createChild("div", "settings-list-container");
  55. var entries = WebInspector.isolatedFileSystemManager.mapping().mappingEntries(this._fileSystemPath);
  56. this._fileMappingsList = new WebInspector.EditableSettingsList(["url", "path"], this._fileMappingValuesProvider.bind(this), this._fileMappingValidate.bind(this), this._fileMappingEdit.bind(this));
  57. this._fileMappingsList.addEventListener(WebInspector.SettingsList.Events.Removed, this._fileMappingRemovedfromList.bind(this));
  58. this._fileMappingsList.element.addStyleClass("file-mappings-list");
  59. this._fileMappingsListContainer.appendChild(this._fileMappingsList.element);
  60. this._entries = {};
  61. for (var i = 0; i < entries.length; ++i)
  62. this._addMappingRow(entries[i]);
  63. blockHeader = contents.createChild("div", "block-header");
  64. blockHeader.textContent = "Excluded folders";
  65. this._excludedFolderListSection = contents.createChild("div", "excluded-folders-section");
  66. this._excludedFolderListContainer = this._excludedFolderListSection.createChild("div", "settings-list-container");
  67. var excludedFolderEntries = WebInspector.isolatedFileSystemManager.mapping().excludedFolders(fileSystemPath);
  68. this._excludedFolderList = new WebInspector.EditableSettingsList(["path"], this._excludedFolderValueProvider.bind(this), this._excludedFolderValidate.bind(this), this._excludedFolderEdit.bind(this));
  69. this._excludedFolderList.addEventListener(WebInspector.SettingsList.Events.Removed, this._excludedFolderRemovedfromList.bind(this));
  70. this._excludedFolderList.element.addStyleClass("excluded-folders-list");
  71. this._excludedFolderListContainer.appendChild(this._excludedFolderList.element);
  72. this._excludedFolderEntries = new StringMap();
  73. for (var i = 0; i < excludedFolderEntries.length; ++i)
  74. this._addExcludedFolderRow(excludedFolderEntries[i]);
  75. this.element.tabIndex = 0;
  76. }
  77. WebInspector.EditFileSystemDialog.show = function(element, fileSystemPath)
  78. {
  79. WebInspector.Dialog.show(element, new WebInspector.EditFileSystemDialog(fileSystemPath));
  80. var glassPane = document.getElementById("glass-pane");
  81. glassPane.addStyleClass("settings-glass-pane");
  82. }
  83. WebInspector.EditFileSystemDialog.prototype = {
  84. /**
  85. * @param {Element} element
  86. */
  87. show: function(element)
  88. {
  89. element.appendChild(this.element);
  90. this.element.addStyleClass("dialog-contents");
  91. element.addStyleClass("settings-dialog");
  92. element.addStyleClass("settings-tab");
  93. this._dialogElement = element;
  94. },
  95. _resize: function()
  96. {
  97. if (!this._dialogElement)
  98. return;
  99. const width = 540;
  100. const minHeight = 150;
  101. var maxHeight = document.body.offsetHeight - 10;
  102. maxHeight = Math.max(minHeight, maxHeight);
  103. this._dialogElement.style.maxHeight = maxHeight + "px";
  104. this._dialogElement.style.width = width + "px";
  105. },
  106. /**
  107. * @param {Element} element
  108. * @param {Element} relativeToElement
  109. */
  110. position: function(element, relativeToElement)
  111. {
  112. this._resize();
  113. },
  114. willHide: function(event)
  115. {
  116. },
  117. _fileMappingAdded: function(event)
  118. {
  119. var entry = /** @type {WebInspector.FileSystemMapping.Entry} */ (event.data);
  120. this._addMappingRow(entry);
  121. },
  122. _fileMappingRemoved: function(event)
  123. {
  124. var entry = /** @type {WebInspector.FileSystemMapping.Entry} */ (event.data);
  125. if (this._fileSystemPath !== entry.fileSystemPath)
  126. return;
  127. delete this._entries[entry.urlPrefix];
  128. if (this._fileMappingsList.itemForId(entry.urlPrefix))
  129. this._fileMappingsList.removeItem(entry.urlPrefix);
  130. this._resize();
  131. },
  132. _fileMappingValuesProvider: function(itemId, columnId)
  133. {
  134. if (!itemId)
  135. return "";
  136. var entry = this._entries[itemId];
  137. switch (columnId) {
  138. case "url":
  139. return entry.urlPrefix;
  140. case "path":
  141. return entry.pathPrefix;
  142. default:
  143. console.assert("Should not be reached.");
  144. }
  145. return "";
  146. },
  147. /**
  148. * @param {?string} itemId
  149. * @param {Object} data
  150. */
  151. _fileMappingValidate: function(itemId, data)
  152. {
  153. var oldPathPrefix = itemId ? this._entries[itemId].pathPrefix : null;
  154. return this._validateMapping(data["url"], itemId, data["path"], oldPathPrefix);
  155. },
  156. /**
  157. * @param {?string} itemId
  158. * @param {Object} data
  159. */
  160. _fileMappingEdit: function(itemId, data)
  161. {
  162. if (itemId) {
  163. var urlPrefix = itemId;
  164. var pathPrefix = this._entries[itemId].pathPrefix;
  165. var fileSystemPath = this._entries[itemId].fileSystemPath;
  166. WebInspector.isolatedFileSystemManager.mapping().removeFileMapping(fileSystemPath, urlPrefix, pathPrefix);
  167. }
  168. this._addFileMapping(data["url"], data["path"]);
  169. },
  170. /**
  171. * @param {string} urlPrefix
  172. * @param {?string} allowedURLPrefix
  173. * @param {string} path
  174. * @param {?string} allowedPathPrefix
  175. */
  176. _validateMapping: function(urlPrefix, allowedURLPrefix, path, allowedPathPrefix)
  177. {
  178. var columns = [];
  179. if (!this._checkURLPrefix(urlPrefix, allowedURLPrefix))
  180. columns.push("url");
  181. if (!this._checkPathPrefix(path, allowedPathPrefix))
  182. columns.push("path");
  183. return columns;
  184. },
  185. /**
  186. * @param {WebInspector.Event} event
  187. */
  188. _fileMappingRemovedfromList: function(event)
  189. {
  190. var urlPrefix = /** @type{?string} */ (event.data);
  191. if (!urlPrefix)
  192. return;
  193. var entry = this._entries[urlPrefix];
  194. WebInspector.isolatedFileSystemManager.mapping().removeFileMapping(entry.fileSystemPath, entry.urlPrefix, entry.pathPrefix);
  195. },
  196. /**
  197. * @param {string} urlPrefix
  198. * @param {string} pathPrefix
  199. * @return {boolean}
  200. */
  201. _addFileMapping: function(urlPrefix, pathPrefix)
  202. {
  203. var normalizedURLPrefix = this._normalizePrefix(urlPrefix);
  204. var normalizedPathPrefix = this._normalizePrefix(pathPrefix);
  205. WebInspector.isolatedFileSystemManager.mapping().addFileMapping(this._fileSystemPath, normalizedURLPrefix, normalizedPathPrefix);
  206. this._fileMappingsList.selectItem(normalizedURLPrefix);
  207. return true;
  208. },
  209. /**
  210. * @param {string} prefix
  211. * @return {string}
  212. */
  213. _normalizePrefix: function(prefix)
  214. {
  215. if (!prefix)
  216. return "";
  217. return prefix + (prefix[prefix.length - 1] === "/" ? "" : "/");
  218. },
  219. _addMappingRow: function(entry)
  220. {
  221. var fileSystemPath = entry.fileSystemPath;
  222. var urlPrefix = entry.urlPrefix;
  223. if (!this._fileSystemPath || this._fileSystemPath !== fileSystemPath)
  224. return;
  225. this._entries[urlPrefix] = entry;
  226. var fileMappingListItem = this._fileMappingsList.addItem(urlPrefix, null);
  227. this._resize();
  228. },
  229. _excludedFolderAdded: function(event)
  230. {
  231. var entry = /** @type {WebInspector.FileSystemMapping.ExcludedFolderEntry} */ (event.data);
  232. this._addExcludedFolderRow(entry);
  233. },
  234. _excludedFolderRemoved: function(event)
  235. {
  236. var entry = /** @type {WebInspector.FileSystemMapping.ExcludedFolderEntry} */ (event.data);
  237. var fileSystemPath = entry.fileSystemPath;
  238. if (!fileSystemPath || this._fileSystemPath !== fileSystemPath)
  239. return;
  240. delete this._excludedFolderEntries[entry.path];
  241. if (this._excludedFolderList.itemForId(entry.path))
  242. this._excludedFolderList.removeItem(entry.path);
  243. },
  244. _excludedFolderValueProvider: function(itemId, columnId)
  245. {
  246. return itemId;
  247. },
  248. /**
  249. * @param {?string} itemId
  250. * @param {Object} data
  251. */
  252. _excludedFolderValidate: function(itemId, data)
  253. {
  254. var fileSystemPath = this._fileSystemPath;
  255. var columns = [];
  256. if (!this._validateExcludedFolder(data["path"], itemId))
  257. columns.push("path");
  258. return columns;
  259. },
  260. /**
  261. * @param {string} path
  262. * @param {?string} allowedPath
  263. * @return {boolean}
  264. */
  265. _validateExcludedFolder: function(path, allowedPath)
  266. {
  267. return !!path && (path === allowedPath || !this._excludedFolderEntries.contains(path));
  268. },
  269. /**
  270. * @param {?string} itemId
  271. * @param {Object} data
  272. */
  273. _excludedFolderEdit: function(itemId, data)
  274. {
  275. var fileSystemPath = this._fileSystemPath;
  276. if (itemId)
  277. WebInspector.isolatedFileSystemManager.mapping().removeExcludedFolder(fileSystemPath, itemId);
  278. var excludedFolderPath = data["path"];
  279. WebInspector.isolatedFileSystemManager.mapping().addExcludedFolder(fileSystemPath, excludedFolderPath);
  280. },
  281. /**
  282. * @param {WebInspector.Event} event
  283. */
  284. _excludedFolderRemovedfromList: function(event)
  285. {
  286. var itemId = /** @type{?string} */ (event.data);
  287. if (!itemId)
  288. return;
  289. WebInspector.isolatedFileSystemManager.mapping().removeExcludedFolder(this._fileSystemPath, itemId);
  290. },
  291. /**
  292. * @param {WebInspector.FileSystemMapping.ExcludedFolderEntry} entry
  293. */
  294. _addExcludedFolderRow: function(entry)
  295. {
  296. var fileSystemPath = entry.fileSystemPath;
  297. if (!fileSystemPath || this._fileSystemPath !== fileSystemPath)
  298. return;
  299. var path = entry.path;
  300. this._excludedFolderEntries.put(path, entry);
  301. this._excludedFolderList.addItem(path, null);
  302. },
  303. /**
  304. * @param {string} value
  305. * @param {?string} allowedPrefix
  306. * @return {boolean}
  307. */
  308. _checkURLPrefix: function(value, allowedPrefix)
  309. {
  310. var prefix = this._normalizePrefix(value);
  311. return !!prefix && (prefix === allowedPrefix || !this._entries[prefix]);
  312. },
  313. /**
  314. * @param {string} value
  315. * @param {?string} allowedPrefix
  316. * @return {boolean}
  317. */
  318. _checkPathPrefix: function(value, allowedPrefix)
  319. {
  320. var prefix = this._normalizePrefix(value);
  321. if (!prefix)
  322. return false;
  323. if (prefix === allowedPrefix)
  324. return true;
  325. for (var urlPrefix in this._entries) {
  326. var entry = this._entries[urlPrefix];
  327. if (urlPrefix && entry.pathPrefix === prefix)
  328. return false;
  329. }
  330. return true;
  331. },
  332. focus: function()
  333. {
  334. WebInspector.setCurrentFocusElement(this.element);
  335. },
  336. _onDoneClick: function()
  337. {
  338. WebInspector.Dialog.hide();
  339. },
  340. onEnter: function()
  341. {
  342. },
  343. __proto__: WebInspector.DialogDelegate.prototype
  344. }