SimpleWorkspaceProvider.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.ContentProviderBasedProjectDelegate}
  33. * @param {string} name
  34. * @param {string} type
  35. */
  36. WebInspector.SimpleProjectDelegate = function(name, type)
  37. {
  38. WebInspector.ContentProviderBasedProjectDelegate.call(this, type);
  39. this._name = name;
  40. this._lastUniqueSuffix = 0;
  41. }
  42. WebInspector.SimpleProjectDelegate.projectId = function(name, type)
  43. {
  44. var typePrefix = type !== WebInspector.projectTypes.Network ? (type + ":") : "";
  45. return typePrefix + name;
  46. }
  47. WebInspector.SimpleProjectDelegate.prototype = {
  48. /**
  49. * @return {string}
  50. */
  51. id: function()
  52. {
  53. return WebInspector.SimpleProjectDelegate.projectId(this._name, this.type());
  54. },
  55. /**
  56. * @return {string}
  57. */
  58. displayName: function()
  59. {
  60. if (typeof this._displayName !== "undefined")
  61. return this._displayName;
  62. if (!this._name) {
  63. this._displayName = this.type() !== WebInspector.projectTypes.Snippets ? WebInspector.UIString("(no domain)") : "";
  64. return this._displayName;
  65. }
  66. var parsedURL = new WebInspector.ParsedURL(this._name);
  67. if (parsedURL.isValid) {
  68. this._displayName = parsedURL.host + (parsedURL.port ? (":" + parsedURL.port) : "");
  69. if (!this._displayName)
  70. this._displayName = this._name;
  71. }
  72. else
  73. this._displayName = this._name;
  74. return this._displayName;
  75. },
  76. /**
  77. * @param {string} parentPath
  78. * @param {string} name
  79. * @param {string} url
  80. * @param {WebInspector.ContentProvider} contentProvider
  81. * @param {boolean} isEditable
  82. * @param {boolean=} isContentScript
  83. * @return {string}
  84. */
  85. addFile: function(parentPath, name, forceUniquePath, url, contentProvider, isEditable, isContentScript)
  86. {
  87. if (forceUniquePath)
  88. name = this._ensureUniqueName(parentPath, name);
  89. return this.addContentProvider(parentPath, name, url, contentProvider, isEditable, isContentScript);
  90. },
  91. /**
  92. * @param {string} parentPath
  93. * @param {string} name
  94. * @return {string}
  95. */
  96. _ensureUniqueName: function(parentPath, name)
  97. {
  98. var path = parentPath ? parentPath + "/" + name : name;
  99. var uniquePath = path;
  100. var suffix = "";
  101. var contentProviders = this.contentProviders();
  102. while (contentProviders[uniquePath]) {
  103. suffix = " (" + (++this._lastUniqueSuffix) + ")";
  104. uniquePath = path + suffix;
  105. }
  106. return name + suffix;
  107. },
  108. __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
  109. }
  110. /**
  111. * @constructor
  112. * @extends {WebInspector.Object}
  113. * @param {WebInspector.Workspace} workspace
  114. * @param {string} type
  115. */
  116. WebInspector.SimpleWorkspaceProvider = function(workspace, type)
  117. {
  118. this._workspace = workspace;
  119. this._type = type;
  120. this._simpleProjectDelegates = {};
  121. }
  122. WebInspector.SimpleWorkspaceProvider.prototype = {
  123. /**
  124. * @param {string} projectName
  125. * @return {WebInspector.SimpleProjectDelegate}
  126. */
  127. _projectDelegate: function(projectName)
  128. {
  129. if (this._simpleProjectDelegates[projectName])
  130. return this._simpleProjectDelegates[projectName];
  131. var simpleProjectDelegate = new WebInspector.SimpleProjectDelegate(projectName, this._type);
  132. this._simpleProjectDelegates[projectName] = simpleProjectDelegate;
  133. this._workspace.addProject(simpleProjectDelegate);
  134. return simpleProjectDelegate;
  135. },
  136. /**
  137. * @param {string} url
  138. * @param {WebInspector.ContentProvider} contentProvider
  139. * @param {boolean} isEditable
  140. * @param {boolean=} isContentScript
  141. * @return {WebInspector.UISourceCode}
  142. */
  143. addFileForURL: function(url, contentProvider, isEditable, isContentScript)
  144. {
  145. return this._innerAddFileForURL(url, contentProvider, isEditable, false, isContentScript);
  146. },
  147. /**
  148. * @param {string} url
  149. * @param {WebInspector.ContentProvider} contentProvider
  150. * @param {boolean} isEditable
  151. * @param {boolean=} isContentScript
  152. * @return {WebInspector.UISourceCode}
  153. */
  154. addUniqueFileForURL: function(url, contentProvider, isEditable, isContentScript)
  155. {
  156. return this._innerAddFileForURL(url, contentProvider, isEditable, true, isContentScript);
  157. },
  158. /**
  159. * @param {string} url
  160. * @param {WebInspector.ContentProvider} contentProvider
  161. * @param {boolean} isEditable
  162. * @param {boolean} forceUnique
  163. * @param {boolean=} isContentScript
  164. * @return {WebInspector.UISourceCode}
  165. */
  166. _innerAddFileForURL: function(url, contentProvider, isEditable, forceUnique, isContentScript)
  167. {
  168. var splitURL = WebInspector.ParsedURL.splitURL(url);
  169. var projectName = splitURL[0];
  170. var parentPath = splitURL.slice(1, splitURL.length - 1).join("/");
  171. var name = splitURL[splitURL.length - 1];
  172. var projectDelegate = this._projectDelegate(projectName);
  173. var path = projectDelegate.addFile(parentPath, name, forceUnique, url, contentProvider, isEditable, isContentScript);
  174. return this._workspace.uiSourceCode(projectDelegate.id(), path);
  175. },
  176. reset: function()
  177. {
  178. for (var projectName in this._simpleProjectDelegates)
  179. this._simpleProjectDelegates[projectName].reset();
  180. this._simpleProjectDelegates = {};
  181. },
  182. __proto__: WebInspector.Object.prototype
  183. }