2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @extends {WebInspector.Object}
35 WebInspector.HandlerRegistry = function(setting)
37 WebInspector.Object.call(this);
39 this._setting = setting;
40 this._activeHandler = this._setting.get();
42 WebInspector.moduleManager.registerModule("handler-registry");
45 WebInspector.HandlerRegistry.prototype = {
48 return Object.getOwnPropertyNames(this._handlers);
53 return this._activeHandler;
56 set activeHandler(value)
58 this._activeHandler = value;
59 this._setting.set(value);
63 * @param {!Object} data
66 dispatch: function(data)
68 return this.dispatchToHandler(this._activeHandler, data);
72 * @param {string} name
73 * @param {!Object} data
76 dispatchToHandler: function(name, data)
78 var handler = this._handlers[name];
79 var result = handler && handler(data);
83 registerHandler: function(name, handler)
85 this._handlers[name] = handler;
86 this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
89 unregisterHandler: function(name)
91 delete this._handlers[name];
92 this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
96 * @param {!WebInspector.ContextMenu} contextMenu
97 * @param {!Object} target
99 _appendContentProviderItems: function(contextMenu, target)
101 if (!(target instanceof WebInspector.UISourceCode || target instanceof WebInspector.Resource || target instanceof WebInspector.NetworkRequest))
103 var contentProvider = /** @type {!WebInspector.ContentProvider} */ (target);
104 if (!contentProvider.contentURL())
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() }));
114 contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
116 if (!contentProvider.contentURL())
119 var contentType = contentProvider.contentType();
120 if (contentType !== WebInspector.resourceTypes.Document &&
121 contentType !== WebInspector.resourceTypes.Stylesheet &&
122 contentType !== WebInspector.resourceTypes.Script)
126 * @param {boolean} forceSaveAs
127 * @param {?string} content
129 function doSave(forceSaveAs, content)
131 var url = contentProvider.contentURL();
132 WebInspector.fileManager.save(url, /** @type {string} */ (content), forceSaveAs);
133 WebInspector.fileManager.close(url);
137 * @param {boolean} forceSaveAs
138 * @this {WebInspector.HandlerRegistry}
140 function save(forceSaveAs)
142 if (contentProvider instanceof WebInspector.UISourceCode) {
143 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (contentProvider);
144 uiSourceCode.saveToFileSystem(forceSaveAs);
147 contentProvider.requestContent(doSave.bind(this, forceSaveAs));
150 contextMenu.appendSeparator();
151 contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(this, false));
152 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save as..." : "Save As..."), save.bind(this, true));
156 * @param {!WebInspector.ContextMenu} contextMenu
157 * @param {!Object} target
159 _appendHrefItems: function(contextMenu, target)
161 if (!(target instanceof Node))
163 var targetNode = /** @type {!Node} */ (target);
165 var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html-resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-external-link");
169 var resourceURL = anchorElement.href;
173 // Add resource-related actions.
174 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, resourceURL, false));
175 if (WebInspector.resourceForURL(resourceURL))
176 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in Resources panel" : "Open Link in Resources Panel"), WebInspector.openResource.bind(null, resourceURL, true));
177 contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, resourceURL));
180 __proto__: WebInspector.Object.prototype
184 WebInspector.HandlerRegistry.EventTypes = {
185 HandlersUpdated: "HandlersUpdated"
191 WebInspector.HandlerSelector = function(handlerRegistry)
193 this._handlerRegistry = handlerRegistry;
194 this.element = document.createElement("select");
195 this.element.addEventListener("change", this._onChange.bind(this), false);
197 this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
200 WebInspector.HandlerSelector.prototype =
204 this.element.removeChildren();
205 var names = this._handlerRegistry.handlerNames;
206 var activeHandler = this._handlerRegistry.activeHandler;
208 for (var i = 0; i < names.length; ++i) {
209 var option = document.createElement("option");
210 option.textContent = names[i];
211 option.selected = activeHandler === names[i];
212 this.element.appendChild(option);
214 this.element.disabled = names.length <= 1;
217 _onChange: function(event)
219 var value = event.target.value;
220 this._handlerRegistry.activeHandler = value;
226 * @implements {WebInspector.ContextMenu.Provider}
228 WebInspector.HandlerRegistry.ContextMenuProvider = function()
232 WebInspector.HandlerRegistry.ContextMenuProvider.prototype = {
234 * @param {!WebInspector.ContextMenu} contextMenu
235 * @param {!Object} target
237 appendApplicableItems: function(event, contextMenu, target)
239 WebInspector.openAnchorLocationRegistry._appendContentProviderItems(contextMenu, target);
240 WebInspector.openAnchorLocationRegistry._appendHrefItems(contextMenu, target);
245 * @type {!WebInspector.HandlerRegistry}
247 WebInspector.openAnchorLocationRegistry;