OLD | NEW |
| (Empty) |
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 /** | |
32 * @unrestricted | |
33 */ | |
34 Components.HandlerRegistry = class extends Common.Object { | |
35 constructor(setting) { | |
36 super(); | |
37 this._handlers = {}; | |
38 this._setting = setting; | |
39 this._activeHandler = this._setting.get(); | |
40 } | |
41 | |
42 get handlerNames() { | |
43 return Object.getOwnPropertyNames(this._handlers); | |
44 } | |
45 | |
46 get activeHandler() { | |
47 return this._activeHandler; | |
48 } | |
49 | |
50 set activeHandler(value) { | |
51 this._activeHandler = value; | |
52 this._setting.set(value); | |
53 } | |
54 | |
55 /** | |
56 * @param {!Object} data | |
57 * @return {boolean} | |
58 */ | |
59 dispatch(data) { | |
60 return this.dispatchToHandler(this._activeHandler, data); | |
61 } | |
62 | |
63 /** | |
64 * @param {string} name | |
65 * @param {!Object} data | |
66 * @return {boolean} | |
67 */ | |
68 dispatchToHandler(name, data) { | |
69 var handler = this._handlers[name]; | |
70 var result = handler && handler(data); | |
71 return !!result; | |
72 } | |
73 | |
74 registerHandler(name, handler) { | |
75 this._handlers[name] = handler; | |
76 this.dispatchEventToListeners(Components.HandlerRegistry.Events.HandlersUpda
ted); | |
77 } | |
78 | |
79 unregisterHandler(name) { | |
80 delete this._handlers[name]; | |
81 this.dispatchEventToListeners(Components.HandlerRegistry.Events.HandlersUpda
ted); | |
82 } | |
83 | |
84 /** | |
85 * @param {string} url | |
86 */ | |
87 _openInNewTab(url) { | |
88 InspectorFrontendHost.openInNewTab(url); | |
89 } | |
90 | |
91 /** | |
92 * @param {!UI.ContextMenu} contextMenu | |
93 * @param {!Object} target | |
94 */ | |
95 _appendContentProviderItems(contextMenu, target) { | |
96 if (!(target instanceof Workspace.UISourceCode || target instanceof SDK.Reso
urce || | |
97 target instanceof SDK.NetworkRequest)) | |
98 return; | |
99 var contentProvider = /** @type {!Common.ContentProvider} */ (target); | |
100 if (!contentProvider.contentURL()) | |
101 return; | |
102 | |
103 contextMenu.appendItem(UI.openLinkExternallyLabel(), this._openInNewTab.bind
(this, contentProvider.contentURL())); | |
104 // Skip 0th handler, as it's 'Use default panel' one. | |
105 for (var i = 1; i < this.handlerNames.length; ++i) { | |
106 var handler = this.handlerNames[i]; | |
107 contextMenu.appendItem( | |
108 Common.UIString.capitalize('Open ^using %s', handler), | |
109 this.dispatchToHandler.bind(this, handler, {url: contentProvider.conte
ntURL()})); | |
110 } | |
111 | |
112 if (!contentProvider.contentURL() || contentProvider instanceof SDK.NetworkR
equest) | |
113 return; | |
114 | |
115 contextMenu.appendItem( | |
116 UI.copyLinkAddressLabel(), | |
117 InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvid
er.contentURL())); | |
118 | |
119 if (!contentProvider.contentType().isDocumentOrScriptOrStyleSheet()) | |
120 return; | |
121 | |
122 /** | |
123 * @param {boolean} forceSaveAs | |
124 * @param {?string} content | |
125 */ | |
126 function doSave(forceSaveAs, content) { | |
127 var url = contentProvider.contentURL(); | |
128 Workspace.fileManager.save(url, /** @type {string} */ (content), forceSave
As); | |
129 Workspace.fileManager.close(url); | |
130 } | |
131 | |
132 /** | |
133 * @param {boolean} forceSaveAs | |
134 */ | |
135 function save(forceSaveAs) { | |
136 if (contentProvider instanceof Workspace.UISourceCode) { | |
137 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (contentProvid
er); | |
138 if (forceSaveAs) | |
139 uiSourceCode.saveAs(); | |
140 else | |
141 uiSourceCode.commitWorkingCopy(); | |
142 return; | |
143 } | |
144 contentProvider.requestContent().then(doSave.bind(null, forceSaveAs)); | |
145 } | |
146 | |
147 contextMenu.appendSeparator(); | |
148 contextMenu.appendItem(Common.UIString('Save'), save.bind(null, false)); | |
149 | |
150 if (contentProvider instanceof Workspace.UISourceCode) { | |
151 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (contentProvider
); | |
152 if (uiSourceCode.project().type() !== Workspace.projectTypes.FileSystem && | |
153 uiSourceCode.project().type() !== Workspace.projectTypes.Snippets) | |
154 contextMenu.appendItem(Common.UIString.capitalize('Save ^as...'), save.b
ind(null, true)); | |
155 } | |
156 } | |
157 }; | |
158 | |
159 /** @enum {symbol} */ | |
160 Components.HandlerRegistry.Events = { | |
161 HandlersUpdated: Symbol('HandlersUpdated') | |
162 }; | |
163 | |
164 /** | |
165 * @unrestricted | |
166 */ | |
167 Components.HandlerSelector = class { | |
168 constructor(handlerRegistry) { | |
169 this._handlerRegistry = handlerRegistry; | |
170 this.element = createElementWithClass('select', 'chrome-select'); | |
171 this.element.addEventListener('change', this._onChange.bind(this), false); | |
172 this._update(); | |
173 this._handlerRegistry.addEventListener(Components.HandlerRegistry.Events.Han
dlersUpdated, this._update.bind(this)); | |
174 } | |
175 | |
176 _update() { | |
177 this.element.removeChildren(); | |
178 var names = this._handlerRegistry.handlerNames; | |
179 var activeHandler = this._handlerRegistry.activeHandler; | |
180 | |
181 for (var i = 0; i < names.length; ++i) { | |
182 var option = createElement('option'); | |
183 option.textContent = names[i]; | |
184 option.selected = activeHandler === names[i]; | |
185 this.element.appendChild(option); | |
186 } | |
187 this.element.disabled = names.length <= 1; | |
188 } | |
189 | |
190 _onChange(event) { | |
191 var value = event.target.value; | |
192 this._handlerRegistry.activeHandler = value; | |
193 } | |
194 }; | |
195 | |
196 /** | |
197 * @implements {UI.ContextMenu.Provider} | |
198 * @unrestricted | |
199 */ | |
200 Components.HandlerRegistry.ContextMenuProvider = class { | |
201 /** | |
202 * @override | |
203 * @param {!Event} event | |
204 * @param {!UI.ContextMenu} contextMenu | |
205 * @param {!Object} target | |
206 */ | |
207 appendApplicableItems(event, contextMenu, target) { | |
208 Components.openAnchorLocationRegistry._appendContentProviderItems(contextMen
u, target); | |
209 } | |
210 }; | |
211 | |
212 /** | |
213 * @implements {Components.Linkifier.LinkHandler} | |
214 * @unrestricted | |
215 */ | |
216 Components.HandlerRegistry.LinkHandler = class { | |
217 /** | |
218 * @override | |
219 * @param {string} url | |
220 * @param {number=} lineNumber | |
221 * @return {boolean} | |
222 */ | |
223 handleLink(url, lineNumber) { | |
224 return Components.openAnchorLocationRegistry.dispatch({url: url, lineNumber:
lineNumber}); | |
225 } | |
226 }; | |
227 | |
228 /** | |
229 * @implements {UI.SettingUI} | |
230 * @unrestricted | |
231 */ | |
232 Components.HandlerRegistry.OpenAnchorLocationSettingUI = class { | |
233 /** | |
234 * @override | |
235 * @return {?Element} | |
236 */ | |
237 settingElement() { | |
238 if (!Components.openAnchorLocationRegistry.handlerNames.length) | |
239 return null; | |
240 | |
241 var handlerSelector = new Components.HandlerSelector(Components.openAnchorLo
cationRegistry); | |
242 return UI.SettingsUI.createCustomSetting(Common.UIString('Link handling:'),
handlerSelector.element); | |
243 } | |
244 }; | |
245 | |
246 /** | |
247 * @type {!Components.HandlerRegistry} | |
248 */ | |
249 Components.openAnchorLocationRegistry; | |
OLD | NEW |