HandlerRegistry.js 8.4 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. if (event.hasBeenHandledByHandlerRegistry)
  91. return;
  92. event.hasBeenHandledByHandlerRegistry = true;
  93. this._appendContentProviderItems(contextMenu, target);
  94. this._appendHrefItems(contextMenu, target);
  95. },
  96. /**
  97. * @param {WebInspector.ContextMenu} contextMenu
  98. * @param {Object} target
  99. */
  100. _appendContentProviderItems: function(contextMenu, target)
  101. {
  102. if (!(target instanceof WebInspector.UISourceCode || target instanceof WebInspector.Resource || target instanceof WebInspector.NetworkRequest))
  103. return;
  104. var contentProvider = /** @type {WebInspector.ContentProvider} */ (target);
  105. if (!contentProvider.contentURL())
  106. return;
  107. contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, contentProvider.contentURL(), false));
  108. // Skip 0th handler, as it's 'Use default panel' one.
  109. for (var i = 1; i < this.handlerNames.length; ++i) {
  110. var handler = this.handlerNames[i];
  111. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open using %s" : "Open Using %s", handler),
  112. this.dispatchToHandler.bind(this, handler, { url: contentProvider.contentURL() }));
  113. }
  114. contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
  115. if (!InspectorFrontendHost.canSave() || !contentProvider.contentURL())
  116. return;
  117. var contentType = contentProvider.contentType();
  118. if (contentType !== WebInspector.resourceTypes.Document &&
  119. contentType !== WebInspector.resourceTypes.Stylesheet &&
  120. contentType !== WebInspector.resourceTypes.Script)
  121. return;
  122. function doSave(forceSaveAs, content)
  123. {
  124. var url = contentProvider.contentURL();
  125. WebInspector.fileManager.save(url, content, forceSaveAs);
  126. WebInspector.fileManager.close(url);
  127. }
  128. function save(forceSaveAs)
  129. {
  130. if (contentProvider instanceof WebInspector.UISourceCode) {
  131. var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (contentProvider);
  132. if (uiSourceCode.isDirty()) {
  133. doSave(forceSaveAs, uiSourceCode.workingCopy());
  134. uiSourceCode.commitWorkingCopy(function() { });
  135. return;
  136. }
  137. }
  138. contentProvider.requestContent(doSave.bind(this, forceSaveAs));
  139. }
  140. contextMenu.appendSeparator();
  141. contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(this, false));
  142. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save as..." : "Save As..."), save.bind(this, true));
  143. },
  144. /**
  145. * @param {WebInspector.ContextMenu} contextMenu
  146. * @param {Object} target
  147. */
  148. _appendHrefItems: function(contextMenu, target)
  149. {
  150. if (!(target instanceof Node))
  151. return;
  152. var targetNode = /** @type {Node} */ (target);
  153. var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html-resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-external-link");
  154. if (!anchorElement)
  155. return;
  156. var resourceURL = anchorElement.href;
  157. if (!resourceURL)
  158. return;
  159. // Add resource-related actions.
  160. contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, resourceURL, false));
  161. if (WebInspector.resourceForURL(resourceURL))
  162. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in Resources panel" : "Open Link in Resources Panel"), WebInspector.openResource.bind(null, resourceURL, true));
  163. contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, resourceURL));
  164. },
  165. __proto__: WebInspector.Object.prototype
  166. }
  167. WebInspector.HandlerRegistry.EventTypes = {
  168. HandlersUpdated: "HandlersUpdated"
  169. }
  170. /**
  171. * @constructor
  172. */
  173. WebInspector.HandlerSelector = function(handlerRegistry)
  174. {
  175. this._handlerRegistry = handlerRegistry;
  176. this.element = document.createElement("select");
  177. this.element.addEventListener("change", this._onChange.bind(this), false);
  178. this._update();
  179. this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
  180. }
  181. WebInspector.HandlerSelector.prototype =
  182. {
  183. _update: function()
  184. {
  185. this.element.removeChildren();
  186. var names = this._handlerRegistry.handlerNames;
  187. var activeHandler = this._handlerRegistry.activeHandler;
  188. for (var i = 0; i < names.length; ++i) {
  189. var option = document.createElement("option");
  190. option.textContent = names[i];
  191. option.selected = activeHandler === names[i];
  192. this.element.appendChild(option);
  193. }
  194. this.element.disabled = names.length <= 1;
  195. },
  196. _onChange: function(event)
  197. {
  198. var value = event.target.value;
  199. this._handlerRegistry.activeHandler = value;
  200. }
  201. }
  202. /**
  203. * @type {WebInspector.HandlerRegistry}
  204. */
  205. WebInspector.openAnchorLocationRegistry = null;