HandlerRegistry.js 8.3 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. * * 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.Object}
  33. * @implements {WebInspector.ContextMenu.Provider}
  34. */
  35. WebInspector.HandlerRegistry = function(setting)
  36. {
  37. WebInspector.Object.call(this);
  38. this._handlers = {};
  39. this._setting = setting;
  40. this._activeHandler = this._setting.get();
  41. WebInspector.ContextMenu.registerProvider(this);
  42. }
  43. WebInspector.HandlerRegistry.prototype = {
  44. get handlerNames()
  45. {
  46. return Object.getOwnPropertyNames(this._handlers);
  47. },
  48. get activeHandler()
  49. {
  50. return this._activeHandler;
  51. },
  52. set activeHandler(value)
  53. {
  54. this._activeHandler = value;
  55. this._setting.set(value);
  56. },
  57. /**
  58. * @param {Object} data
  59. */
  60. dispatch: function(data)
  61. {
  62. return this.dispatchToHandler(this._activeHandler, data);
  63. },
  64. /**
  65. * @param {string} name
  66. * @param {Object} data
  67. */
  68. dispatchToHandler: function(name, data)
  69. {
  70. var handler = this._handlers[name];
  71. var result = handler && handler(data);
  72. return !!result;
  73. },
  74. registerHandler: function(name, handler)
  75. {
  76. this._handlers[name] = handler;
  77. this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
  78. },
  79. unregisterHandler: function(name)
  80. {
  81. delete this._handlers[name];
  82. this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
  83. },
  84. /**
  85. * @param {WebInspector.ContextMenu} contextMenu
  86. * @param {Object} target
  87. */
  88. appendApplicableItems: function(event, contextMenu, target)
  89. {
  90. this._appendContentProviderItems(contextMenu, target);
  91. this._appendHrefItems(contextMenu, target);
  92. },
  93. /**
  94. * @param {WebInspector.ContextMenu} contextMenu
  95. * @param {Object} target
  96. */
  97. _appendContentProviderItems: function(contextMenu, target)
  98. {
  99. if (!(target instanceof WebInspector.UISourceCode || target instanceof WebInspector.Resource || target instanceof WebInspector.NetworkRequest))
  100. return;
  101. var contentProvider = /** @type {WebInspector.ContentProvider} */ (target);
  102. if (!contentProvider.contentURL())
  103. return;
  104. contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, contentProvider.contentURL(), false));
  105. // Skip 0th handler, as it's 'Use default panel' one.
  106. for (var i = 1; i < this.handlerNames.length; ++i) {
  107. var handler = this.handlerNames[i];
  108. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open using %s" : "Open Using %s", handler),
  109. this.dispatchToHandler.bind(this, handler, { url: contentProvider.contentURL() }));
  110. }
  111. contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
  112. if (!contentProvider.contentURL())
  113. return;
  114. var contentType = contentProvider.contentType();
  115. if (contentType !== WebInspector.resourceTypes.Document &&
  116. contentType !== WebInspector.resourceTypes.Stylesheet &&
  117. contentType !== WebInspector.resourceTypes.Script)
  118. return;
  119. /**
  120. * @param {boolean} forceSaveAs
  121. * @param {?string} content
  122. */
  123. function doSave(forceSaveAs, content)
  124. {
  125. var url = contentProvider.contentURL();
  126. WebInspector.fileManager.save(url, content, forceSaveAs);
  127. WebInspector.fileManager.close(url);
  128. }
  129. /**
  130. * @param {boolean} forceSaveAs
  131. */
  132. function save(forceSaveAs)
  133. {
  134. if (contentProvider instanceof WebInspector.UISourceCode) {
  135. var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (contentProvider);
  136. uiSourceCode.saveToFileSystem(forceSaveAs);
  137. return;
  138. }
  139. contentProvider.requestContent(doSave.bind(this, forceSaveAs));
  140. }
  141. contextMenu.appendSeparator();
  142. contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(this, false));
  143. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save as..." : "Save As..."), save.bind(this, true));
  144. },
  145. /**
  146. * @param {WebInspector.ContextMenu} contextMenu
  147. * @param {Object} target
  148. */
  149. _appendHrefItems: function(contextMenu, target)
  150. {
  151. if (!(target instanceof Node))
  152. return;
  153. var targetNode = /** @type {Node} */ (target);
  154. var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html-resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-external-link");
  155. if (!anchorElement)
  156. return;
  157. var resourceURL = anchorElement.href;
  158. if (!resourceURL)
  159. return;
  160. // Add resource-related actions.
  161. contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, resourceURL, false));
  162. if (WebInspector.resourceForURL(resourceURL))
  163. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in Resources panel" : "Open Link in Resources Panel"), WebInspector.openResource.bind(null, resourceURL, true));
  164. contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, resourceURL));
  165. },
  166. __proto__: WebInspector.Object.prototype
  167. }
  168. WebInspector.HandlerRegistry.EventTypes = {
  169. HandlersUpdated: "HandlersUpdated"
  170. }
  171. /**
  172. * @constructor
  173. */
  174. WebInspector.HandlerSelector = function(handlerRegistry)
  175. {
  176. this._handlerRegistry = handlerRegistry;
  177. this.element = document.createElement("select");
  178. this.element.addEventListener("change", this._onChange.bind(this), false);
  179. this._update();
  180. this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
  181. }
  182. WebInspector.HandlerSelector.prototype =
  183. {
  184. _update: function()
  185. {
  186. this.element.removeChildren();
  187. var names = this._handlerRegistry.handlerNames;
  188. var activeHandler = this._handlerRegistry.activeHandler;
  189. for (var i = 0; i < names.length; ++i) {
  190. var option = document.createElement("option");
  191. option.textContent = names[i];
  192. option.selected = activeHandler === names[i];
  193. this.element.appendChild(option);
  194. }
  195. this.element.disabled = names.length <= 1;
  196. },
  197. _onChange: function(event)
  198. {
  199. var value = event.target.value;
  200. this._handlerRegistry.activeHandler = value;
  201. }
  202. }
  203. /**
  204. * @type {WebInspector.HandlerRegistry}
  205. */
  206. WebInspector.openAnchorLocationRegistry = null;