ScriptsNavigator.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2011 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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
  20. * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @extends {WebInspector.Object}
  30. * @constructor
  31. */
  32. WebInspector.ScriptsNavigator = function()
  33. {
  34. WebInspector.Object.call(this);
  35. this._tabbedPane = new WebInspector.TabbedPane();
  36. this._tabbedPane.shrinkableTabs = true;
  37. this._tabbedPane.element.addStyleClass("navigator-tabbed-pane");
  38. this._scriptsView = new WebInspector.NavigatorView();
  39. this._scriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._scriptSelected, this);
  40. this._scriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemSearchStarted, this._itemSearchStarted, this);
  41. this._scriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamingRequested, this._itemRenamingRequested, this);
  42. this._scriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemCreationRequested, this._itemCreationRequested, this);
  43. this._contentScriptsView = new WebInspector.NavigatorView();
  44. this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._scriptSelected, this);
  45. this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemSearchStarted, this._itemSearchStarted, this);
  46. this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamingRequested, this._itemRenamingRequested, this);
  47. this._contentScriptsView.addEventListener(WebInspector.NavigatorView.Events.ItemCreationRequested, this._itemCreationRequested, this);
  48. this._snippetsView = new WebInspector.SnippetsNavigatorView();
  49. this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemSelected, this._scriptSelected, this);
  50. this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemSearchStarted, this._itemSearchStarted, this);
  51. this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemRenamingRequested, this._itemRenamingRequested, this);
  52. this._snippetsView.addEventListener(WebInspector.NavigatorView.Events.ItemCreationRequested, this._itemCreationRequested, this);
  53. this._tabbedPane.appendTab(WebInspector.ScriptsNavigator.ScriptsTab, WebInspector.UIString("Sources"), this._scriptsView);
  54. this._tabbedPane.selectTab(WebInspector.ScriptsNavigator.ScriptsTab);
  55. this._tabbedPane.appendTab(WebInspector.ScriptsNavigator.ContentScriptsTab, WebInspector.UIString("Content scripts"), this._contentScriptsView);
  56. this._tabbedPane.appendTab(WebInspector.ScriptsNavigator.SnippetsTab, WebInspector.UIString("Snippets"), this._snippetsView);
  57. }
  58. WebInspector.ScriptsNavigator.Events = {
  59. ScriptSelected: "ScriptSelected",
  60. ItemCreationRequested: "ItemCreationRequested",
  61. ItemRenamingRequested: "ItemRenamingRequested",
  62. ItemSearchStarted: "ItemSearchStarted",
  63. }
  64. WebInspector.ScriptsNavigator.ScriptsTab = "scripts";
  65. WebInspector.ScriptsNavigator.ContentScriptsTab = "contentScripts";
  66. WebInspector.ScriptsNavigator.SnippetsTab = "snippets";
  67. WebInspector.ScriptsNavigator.prototype = {
  68. /*
  69. * @return {WebInspector.View}
  70. */
  71. get view()
  72. {
  73. return this._tabbedPane;
  74. },
  75. /**
  76. * @param {WebInspector.UISourceCode} uiSourceCode
  77. */
  78. _navigatorViewForUISourceCode: function(uiSourceCode)
  79. {
  80. if (uiSourceCode.isContentScript)
  81. return this._contentScriptsView;
  82. else if (uiSourceCode.project().type() === WebInspector.projectTypes.Snippets)
  83. return this._snippetsView;
  84. else
  85. return this._scriptsView;
  86. },
  87. /**
  88. * @param {WebInspector.UISourceCode} uiSourceCode
  89. */
  90. addUISourceCode: function(uiSourceCode)
  91. {
  92. this._navigatorViewForUISourceCode(uiSourceCode).addUISourceCode(uiSourceCode);
  93. },
  94. /**
  95. * @param {WebInspector.UISourceCode} uiSourceCode
  96. */
  97. removeUISourceCode: function(uiSourceCode)
  98. {
  99. this._navigatorViewForUISourceCode(uiSourceCode).removeUISourceCode(uiSourceCode);
  100. },
  101. /**
  102. * @param {WebInspector.UISourceCode} uiSourceCode
  103. * @param {boolean=} select
  104. */
  105. revealUISourceCode: function(uiSourceCode, select)
  106. {
  107. this._navigatorViewForUISourceCode(uiSourceCode).revealUISourceCode(uiSourceCode, select);
  108. if (uiSourceCode.isContentScript)
  109. this._tabbedPane.selectTab(WebInspector.ScriptsNavigator.ContentScriptsTab);
  110. else if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
  111. this._tabbedPane.selectTab(WebInspector.ScriptsNavigator.ScriptsTab);
  112. },
  113. /**
  114. * @param {WebInspector.UISourceCode} uiSourceCode
  115. * @param {function(boolean)=} callback
  116. */
  117. rename: function(uiSourceCode, callback)
  118. {
  119. this._navigatorViewForUISourceCode(uiSourceCode).rename(uiSourceCode, callback);
  120. },
  121. /**
  122. * @param {WebInspector.Event} event
  123. */
  124. _scriptSelected: function(event)
  125. {
  126. this.dispatchEventToListeners(WebInspector.ScriptsNavigator.Events.ScriptSelected, event.data);
  127. },
  128. /**
  129. * @param {WebInspector.Event} event
  130. */
  131. _itemSearchStarted: function(event)
  132. {
  133. this.dispatchEventToListeners(WebInspector.ScriptsNavigator.Events.ItemSearchStarted, event.data);
  134. },
  135. /**
  136. * @param {WebInspector.Event} event
  137. */
  138. _itemRenamingRequested: function(event)
  139. {
  140. this.dispatchEventToListeners(WebInspector.ScriptsNavigator.Events.ItemRenamingRequested, event.data);
  141. },
  142. /**
  143. * @param {WebInspector.Event} event
  144. */
  145. _itemCreationRequested: function(event)
  146. {
  147. this.dispatchEventToListeners(WebInspector.ScriptsNavigator.Events.ItemCreationRequested, event.data);
  148. },
  149. __proto__: WebInspector.Object.prototype
  150. }
  151. /**
  152. * @constructor
  153. * @extends {WebInspector.NavigatorView}
  154. */
  155. WebInspector.SnippetsNavigatorView = function()
  156. {
  157. WebInspector.NavigatorView.call(this);
  158. }
  159. WebInspector.SnippetsNavigatorView.prototype = {
  160. /**
  161. * @param {Event} event
  162. */
  163. handleContextMenu: function(event)
  164. {
  165. var contextMenu = new WebInspector.ContextMenu(event);
  166. contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this));
  167. contextMenu.show();
  168. },
  169. /**
  170. * @param {Event} event
  171. * @param {WebInspector.UISourceCode} uiSourceCode
  172. */
  173. handleFileContextMenu: function(event, uiSourceCode)
  174. {
  175. var contextMenu = new WebInspector.ContextMenu(event);
  176. contextMenu.appendItem(WebInspector.UIString("Run"), this._handleEvaluateSnippet.bind(this, uiSourceCode));
  177. contextMenu.appendItem(WebInspector.UIString("Rename"), this.requestRename.bind(this, uiSourceCode));
  178. contextMenu.appendItem(WebInspector.UIString("Remove"), this._handleRemoveSnippet.bind(this, uiSourceCode));
  179. contextMenu.appendSeparator();
  180. contextMenu.appendItem(WebInspector.UIString("New"), this._handleCreateSnippet.bind(this));
  181. contextMenu.show();
  182. },
  183. /**
  184. * @param {WebInspector.UISourceCode} uiSourceCode
  185. */
  186. _handleEvaluateSnippet: function(uiSourceCode)
  187. {
  188. if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
  189. return;
  190. WebInspector.scriptSnippetModel.evaluateScriptSnippet(uiSourceCode);
  191. },
  192. /**
  193. * @param {WebInspector.UISourceCode} uiSourceCode
  194. */
  195. _handleRemoveSnippet: function(uiSourceCode)
  196. {
  197. if (uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
  198. return;
  199. uiSourceCode.project().deleteFile(uiSourceCode);
  200. },
  201. _handleCreateSnippet: function()
  202. {
  203. var data = {};
  204. data.project = WebInspector.scriptSnippetModel.project();
  205. data.path = "";
  206. this.dispatchEventToListeners(WebInspector.NavigatorView.Events.ItemCreationRequested, data);
  207. },
  208. __proto__: WebInspector.NavigatorView.prototype
  209. }