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);
98 _openInNewTab: function(url)
100 InspectorFrontendHost.openInNewTab(url);
104 * @param {!WebInspector.ContextMenu} contextMenu
105 * @param {!Object} target
107 _appendContentProviderItems: function(contextMenu, target)
109 if (!(target instanceof WebInspector.UISourceCode || target instanceof WebInspector.Resource || target instanceof WebInspector.NetworkRequest))
111 var contentProvider = /** @type {!WebInspector.ContentProvider} */ (target);
112 if (!contentProvider.contentURL())
115 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._openInNewTab.bind(this, contentProvider.contentURL()));
116 // Skip 0th handler, as it's 'Use default panel' one.
117 for (var i = 1; i < this.handlerNames.length; ++i) {
118 var handler = this.handlerNames[i];
119 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open using %s" : "Open Using %s", handler),
120 this.dispatchToHandler.bind(this, handler, { url: contentProvider.contentURL() }));
122 contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
124 if (!contentProvider.contentURL())
127 var contentType = contentProvider.contentType();
128 if (contentType !== WebInspector.resourceTypes.Document &&
129 contentType !== WebInspector.resourceTypes.Stylesheet &&
130 contentType !== WebInspector.resourceTypes.Script)
134 * @param {boolean} forceSaveAs
135 * @param {?string} content
137 function doSave(forceSaveAs, content)
139 var url = contentProvider.contentURL();
140 WebInspector.fileManager.save(url, /** @type {string} */ (content), forceSaveAs);
141 WebInspector.fileManager.close(url);
145 * @param {boolean} forceSaveAs
147 function save(forceSaveAs)
149 if (contentProvider instanceof WebInspector.UISourceCode) {
150 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (contentProvider);
151 uiSourceCode.saveToFileSystem(forceSaveAs);
154 contentProvider.requestContent(doSave.bind(null, forceSaveAs));
157 contextMenu.appendSeparator();
158 contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(null, false));
159 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save as..." : "Save As..."), save.bind(null, true));
163 * @param {!WebInspector.ContextMenu} contextMenu
164 * @param {!Object} target
166 _appendHrefItems: function(contextMenu, target)
168 if (!(target instanceof Node))
170 var targetNode = /** @type {!Node} */ (target);
172 var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html-resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-external-link");
176 var resourceURL = anchorElement.href;
180 // Add resource-related actions.
181 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._openInNewTab.bind(this, resourceURL));
183 function openInResourcesPanel(resourceURL)
185 var resource = WebInspector.resourceForURL(resourceURL);
187 WebInspector.Revealer.reveal(resource);
189 InspectorFrontendHost.openInNewTab(resourceURL);
191 if (WebInspector.resourceForURL(resourceURL))
192 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in Resources panel" : "Open Link in Resources Panel"), openInResourcesPanel.bind(null, resourceURL));
193 contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, resourceURL));
196 __proto__: WebInspector.Object.prototype
200 WebInspector.HandlerRegistry.EventTypes = {
201 HandlersUpdated: "HandlersUpdated"
207 WebInspector.HandlerSelector = function(handlerRegistry)
209 this._handlerRegistry = handlerRegistry;
210 this.element = document.createElement("select");
211 this.element.addEventListener("change", this._onChange.bind(this), false);
213 this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
216 WebInspector.HandlerSelector.prototype =
220 this.element.removeChildren();
221 var names = this._handlerRegistry.handlerNames;
222 var activeHandler = this._handlerRegistry.activeHandler;
224 for (var i = 0; i < names.length; ++i) {
225 var option = document.createElement("option");
226 option.textContent = names[i];
227 option.selected = activeHandler === names[i];
228 this.element.appendChild(option);
230 this.element.disabled = names.length <= 1;
233 _onChange: function(event)
235 var value = event.target.value;
236 this._handlerRegistry.activeHandler = value;
242 * @implements {WebInspector.ContextMenu.Provider}
244 WebInspector.HandlerRegistry.ContextMenuProvider = function()
248 WebInspector.HandlerRegistry.ContextMenuProvider.prototype = {
250 * @param {!WebInspector.ContextMenu} contextMenu
251 * @param {!Object} target
253 appendApplicableItems: function(event, contextMenu, target)
255 WebInspector.openAnchorLocationRegistry._appendContentProviderItems(contextMenu, target);
256 WebInspector.openAnchorLocationRegistry._appendHrefItems(contextMenu, target);
262 * @implements {WebInspector.Linkifier.LinkHandler}
264 WebInspector.HandlerRegistry.LinkHandler = function()
268 WebInspector.HandlerRegistry.LinkHandler.prototype = {
270 * @param {string} url
271 * @param {number=} lineNumber
274 handleLink: function(url, lineNumber)
276 return WebInspector.openAnchorLocationRegistry.dispatch({ url: url, lineNumber: lineNumber});
281 * @type {!WebInspector.HandlerRegistry}
283 WebInspector.openAnchorLocationRegistry;