Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/components/HandlerRegistry.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the 12 * in the documentation and/or other materials provided with the
13 * distribution. 13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its 14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from 15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission. 16 * this software without specific prior written permission.
17 * 17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30 /**
31 /** 31 * @unrestricted
32 * @constructor 32 */
33 * @extends {WebInspector.Object} 33 WebInspector.HandlerRegistry = class extends WebInspector.Object {
34 */ 34 constructor(setting) {
35 WebInspector.HandlerRegistry = function(setting) 35 super();
36 {
37 WebInspector.Object.call(this);
38 this._handlers = {}; 36 this._handlers = {};
39 this._setting = setting; 37 this._setting = setting;
40 this._activeHandler = this._setting.get(); 38 this._activeHandler = this._setting.get();
41 }; 39 }
42 40
43 WebInspector.HandlerRegistry.prototype = { 41 get handlerNames() {
44 get handlerNames() 42 return Object.getOwnPropertyNames(this._handlers);
45 { 43 }
46 return Object.getOwnPropertyNames(this._handlers); 44
47 }, 45 get activeHandler() {
48 46 return this._activeHandler;
49 get activeHandler() 47 }
50 { 48
51 return this._activeHandler; 49 set activeHandler(value) {
52 }, 50 this._activeHandler = value;
53 51 this._setting.set(value);
54 set activeHandler(value) 52 }
55 { 53
56 this._activeHandler = value; 54 /**
57 this._setting.set(value); 55 * @param {!Object} data
58 }, 56 * @return {boolean}
57 */
58 dispatch(data) {
59 return this.dispatchToHandler(this._activeHandler, data);
60 }
61
62 /**
63 * @param {string} name
64 * @param {!Object} data
65 * @return {boolean}
66 */
67 dispatchToHandler(name, data) {
68 var handler = this._handlers[name];
69 var result = handler && handler(data);
70 return !!result;
71 }
72
73 registerHandler(name, handler) {
74 this._handlers[name] = handler;
75 this.dispatchEventToListeners(WebInspector.HandlerRegistry.Events.HandlersUp dated);
76 }
77
78 unregisterHandler(name) {
79 delete this._handlers[name];
80 this.dispatchEventToListeners(WebInspector.HandlerRegistry.Events.HandlersUp dated);
81 }
82
83 /**
84 * @param {string} url
85 */
86 _openInNewTab(url) {
87 InspectorFrontendHost.openInNewTab(url);
88 }
89
90 /**
91 * @param {!WebInspector.ContextMenu} contextMenu
92 * @param {!Object} target
93 */
94 _appendContentProviderItems(contextMenu, target) {
95 if (!(target instanceof WebInspector.UISourceCode || target instanceof WebIn spector.Resource ||
96 target instanceof WebInspector.NetworkRequest))
97 return;
98 var contentProvider = /** @type {!WebInspector.ContentProvider} */ (target);
99 if (!contentProvider.contentURL())
100 return;
101
102 contextMenu.appendItem(
103 WebInspector.openLinkExternallyLabel(), this._openInNewTab.bind(this, co ntentProvider.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 WebInspector.UIString.capitalize('Open ^using %s', handler),
109 this.dispatchToHandler.bind(this, handler, {url: contentProvider.conte ntURL()}));
110 }
111
112 if (!contentProvider.contentURL() || contentProvider instanceof WebInspector .NetworkRequest)
113 return;
114
115 contextMenu.appendItem(
116 WebInspector.copyLinkAddressLabel(),
117 InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvid er.contentURL()));
118
119 if (!contentProvider.contentType().isDocumentOrScriptOrStyleSheet())
120 return;
59 121
60 /** 122 /**
61 * @param {!Object} data 123 * @param {boolean} forceSaveAs
62 * @return {boolean} 124 * @param {?string} content
63 */ 125 */
64 dispatch: function(data) 126 function doSave(forceSaveAs, content) {
65 { 127 var url = contentProvider.contentURL();
66 return this.dispatchToHandler(this._activeHandler, data); 128 WebInspector.fileManager.save(url, /** @type {string} */ (content), forceS aveAs);
67 }, 129 WebInspector.fileManager.close(url);
130 }
68 131
69 /** 132 /**
70 * @param {string} name 133 * @param {boolean} forceSaveAs
71 * @param {!Object} data
72 * @return {boolean}
73 */ 134 */
74 dispatchToHandler: function(name, data) 135 function save(forceSaveAs) {
75 { 136 if (contentProvider instanceof WebInspector.UISourceCode) {
76 var handler = this._handlers[name]; 137 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (contentPro vider);
77 var result = handler && handler(data); 138 if (forceSaveAs)
78 return !!result; 139 uiSourceCode.saveAs();
79 }, 140 else
80 141 uiSourceCode.commitWorkingCopy();
81 registerHandler: function(name, handler) 142 return;
82 { 143 }
83 this._handlers[name] = handler; 144 contentProvider.requestContent().then(doSave.bind(null, forceSaveAs));
84 this.dispatchEventToListeners(WebInspector.HandlerRegistry.Events.Handle rsUpdated); 145 }
85 }, 146
86 147 contextMenu.appendSeparator();
87 unregisterHandler: function(name) 148 contextMenu.appendItem(WebInspector.UIString('Save'), save.bind(null, false) );
88 { 149
89 delete this._handlers[name]; 150 if (contentProvider instanceof WebInspector.UISourceCode) {
90 this.dispatchEventToListeners(WebInspector.HandlerRegistry.Events.Handle rsUpdated); 151 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (contentProvi der);
91 }, 152 if (uiSourceCode.project().type() !== WebInspector.projectTypes.FileSystem &&
153 uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
154 contextMenu.appendItem(WebInspector.UIString.capitalize('Save ^as...'), save.bind(null, true));
155 }
156 }
157
158 /**
159 * @param {!WebInspector.ContextMenu} contextMenu
160 * @param {!Object} target
161 */
162 _appendHrefItems(contextMenu, target) {
163 if (!(target instanceof Node))
164 return;
165 var targetNode = /** @type {!Node} */ (target);
166
167 var anchorElement = targetNode.enclosingNodeOrSelfWithClass('webkit-html-res ource-link') ||
168 targetNode.enclosingNodeOrSelfWithClass('webkit-html-external-link');
169 if (!anchorElement)
170 return;
171
172 var uiLocation = WebInspector.Linkifier.uiLocationByAnchor(anchorElement);
173 var resourceURL = uiLocation ? uiLocation.uiSourceCode.contentURL() : anchor Element.href;
174 var uiSourceCode = uiLocation ?
175 uiLocation.uiSourceCode :
176 (resourceURL ? WebInspector.networkMapping.uiSourceCodeForURLForAnyTarge t(resourceURL) : null);
177 function open() {
178 WebInspector.Revealer.reveal(uiSourceCode);
179 }
180 if (uiSourceCode)
181 contextMenu.appendItem('Open', open);
182
183 if (!resourceURL)
184 return;
185 // Add resource-related actions.
186 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._openInN ewTab.bind(this, resourceURL));
92 187
93 /** 188 /**
94 * @param {string} url 189 * @param {string} resourceURL
95 */ 190 */
96 _openInNewTab: function(url) 191 function openInResourcesPanel(resourceURL) {
97 { 192 var resource = WebInspector.resourceForURL(resourceURL);
98 InspectorFrontendHost.openInNewTab(url); 193 if (resource)
99 }, 194 WebInspector.Revealer.reveal(resource);
100 195 else
101 /** 196 InspectorFrontendHost.openInNewTab(resourceURL);
102 * @param {!WebInspector.ContextMenu} contextMenu 197 }
103 * @param {!Object} target 198 if (!targetNode.enclosingNodeOrSelfWithClassList(['resources', 'panel']) &&
104 */ 199 WebInspector.resourceForURL(resourceURL))
105 _appendContentProviderItems: function(contextMenu, target) 200 contextMenu.appendItem(
106 { 201 WebInspector.UIString.capitalize('Open ^link in Application ^panel'),
107 if (!(target instanceof WebInspector.UISourceCode || target instanceof W ebInspector.Resource || target instanceof WebInspector.NetworkRequest)) 202 openInResourcesPanel.bind(null, resourceURL));
108 return; 203
109 var contentProvider = /** @type {!WebInspector.ContentProvider} */ (targ et); 204 contextMenu.appendItem(
110 if (!contentProvider.contentURL()) 205 WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind (InspectorFrontendHost, resourceURL));
111 return; 206 }
112
113 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._ope nInNewTab.bind(this, contentProvider.contentURL()));
114 // Skip 0th handler, as it's 'Use default panel' one.
115 for (var i = 1; i < this.handlerNames.length; ++i) {
116 var handler = this.handlerNames[i];
117 contextMenu.appendItem(WebInspector.UIString.capitalize("Open ^using %s", handler),
118 this.dispatchToHandler.bind(this, handler, { url: contentProvide r.contentURL() }));
119 }
120
121 if (!contentProvider.contentURL() || contentProvider instanceof WebInspe ctor.NetworkRequest)
122 return;
123
124 contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFro ntendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
125
126 if (!contentProvider.contentType().isDocumentOrScriptOrStyleSheet())
127 return;
128
129 /**
130 * @param {boolean} forceSaveAs
131 * @param {?string} content
132 */
133 function doSave(forceSaveAs, content)
134 {
135 var url = contentProvider.contentURL();
136 WebInspector.fileManager.save(url, /** @type {string} */ (content), forceSaveAs);
137 WebInspector.fileManager.close(url);
138 }
139
140 /**
141 * @param {boolean} forceSaveAs
142 */
143 function save(forceSaveAs)
144 {
145 if (contentProvider instanceof WebInspector.UISourceCode) {
146 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (co ntentProvider);
147 if (forceSaveAs)
148 uiSourceCode.saveAs();
149 else
150 uiSourceCode.commitWorkingCopy();
151 return;
152 }
153 contentProvider.requestContent().then(doSave.bind(null, forceSaveAs) );
154 }
155
156 contextMenu.appendSeparator();
157 contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(null, fa lse));
158
159 if (contentProvider instanceof WebInspector.UISourceCode) {
160 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (conten tProvider);
161 if (uiSourceCode.project().type() !== WebInspector.projectTypes.File System && uiSourceCode.project().type() !== WebInspector.projectTypes.Snippets)
162 contextMenu.appendItem(WebInspector.UIString.capitalize("Save ^a s..."), save.bind(null, true));
163 }
164 },
165
166 /**
167 * @param {!WebInspector.ContextMenu} contextMenu
168 * @param {!Object} target
169 */
170 _appendHrefItems: function(contextMenu, target)
171 {
172 if (!(target instanceof Node))
173 return;
174 var targetNode = /** @type {!Node} */ (target);
175
176 var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html -resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-externa l-link");
177 if (!anchorElement)
178 return;
179
180 var uiLocation = WebInspector.Linkifier.uiLocationByAnchor(anchorElement );
181 var resourceURL = uiLocation ? uiLocation.uiSourceCode.contentURL() : an chorElement.href;
182 var uiSourceCode = uiLocation ? uiLocation.uiSourceCode : (resourceURL ? WebInspector.networkMapping.uiSourceCodeForURLForAnyTarget(resourceURL) : null) ;
183 function open()
184 {
185 WebInspector.Revealer.reveal(uiSourceCode);
186 }
187 if (uiSourceCode)
188 contextMenu.appendItem("Open", open);
189
190 if (!resourceURL)
191 return;
192 // Add resource-related actions.
193 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._ope nInNewTab.bind(this, resourceURL));
194
195 /**
196 * @param {string} resourceURL
197 */
198 function openInResourcesPanel(resourceURL)
199 {
200 var resource = WebInspector.resourceForURL(resourceURL);
201 if (resource)
202 WebInspector.Revealer.reveal(resource);
203 else
204 InspectorFrontendHost.openInNewTab(resourceURL);
205 }
206 if (!targetNode.enclosingNodeOrSelfWithClassList(["resources", "panel"]) && WebInspector.resourceForURL(resourceURL))
207 contextMenu.appendItem(WebInspector.UIString.capitalize("Open ^link in Application ^panel"), openInResourcesPanel.bind(null, resourceURL));
208
209
210 contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFro ntendHost.copyText.bind(InspectorFrontendHost, resourceURL));
211 },
212
213 __proto__: WebInspector.Object.prototype
214 }; 207 };
215 208
216 /** @enum {symbol} */ 209 /** @enum {symbol} */
217 WebInspector.HandlerRegistry.Events = { 210 WebInspector.HandlerRegistry.Events = {
218 HandlersUpdated: Symbol("HandlersUpdated") 211 HandlersUpdated: Symbol('HandlersUpdated')
219 }; 212 };
220 213
221 /** 214 /**
222 * @constructor 215 * @unrestricted
223 */ 216 */
224 WebInspector.HandlerSelector = function(handlerRegistry) 217 WebInspector.HandlerSelector = class {
225 { 218 constructor(handlerRegistry) {
226 this._handlerRegistry = handlerRegistry; 219 this._handlerRegistry = handlerRegistry;
227 this.element = createElementWithClass("select", "chrome-select"); 220 this.element = createElementWithClass('select', 'chrome-select');
228 this.element.addEventListener("change", this._onChange.bind(this), false); 221 this.element.addEventListener('change', this._onChange.bind(this), false);
229 this._update(); 222 this._update();
230 this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.Events.H andlersUpdated, this._update.bind(this)); 223 this._handlerRegistry.addEventListener(
231 }; 224 WebInspector.HandlerRegistry.Events.HandlersUpdated, this._update.bind(t his));
232 225 }
233 WebInspector.HandlerSelector.prototype = 226
234 { 227 _update() {
235 _update: function() 228 this.element.removeChildren();
236 { 229 var names = this._handlerRegistry.handlerNames;
237 this.element.removeChildren(); 230 var activeHandler = this._handlerRegistry.activeHandler;
238 var names = this._handlerRegistry.handlerNames; 231
239 var activeHandler = this._handlerRegistry.activeHandler; 232 for (var i = 0; i < names.length; ++i) {
240 233 var option = createElement('option');
241 for (var i = 0; i < names.length; ++i) { 234 option.textContent = names[i];
242 var option = createElement("option"); 235 option.selected = activeHandler === names[i];
243 option.textContent = names[i]; 236 this.element.appendChild(option);
244 option.selected = activeHandler === names[i]; 237 }
245 this.element.appendChild(option); 238 this.element.disabled = names.length <= 1;
246 } 239 }
247 this.element.disabled = names.length <= 1; 240
248 }, 241 _onChange(event) {
249 242 var value = event.target.value;
250 _onChange: function(event) 243 this._handlerRegistry.activeHandler = value;
251 { 244 }
252 var value = event.target.value; 245 };
253 this._handlerRegistry.activeHandler = value; 246
254 } 247 /**
255 };
256
257 /**
258 * @constructor
259 * @implements {WebInspector.ContextMenu.Provider} 248 * @implements {WebInspector.ContextMenu.Provider}
260 */ 249 * @unrestricted
261 WebInspector.HandlerRegistry.ContextMenuProvider = function() 250 */
262 { 251 WebInspector.HandlerRegistry.ContextMenuProvider = class {
263 }; 252 /**
264 253 * @override
265 WebInspector.HandlerRegistry.ContextMenuProvider.prototype = { 254 * @param {!Event} event
266 /** 255 * @param {!WebInspector.ContextMenu} contextMenu
267 * @override 256 * @param {!Object} target
268 * @param {!Event} event 257 */
269 * @param {!WebInspector.ContextMenu} contextMenu 258 appendApplicableItems(event, contextMenu, target) {
270 * @param {!Object} target 259 WebInspector.openAnchorLocationRegistry._appendContentProviderItems(contextM enu, target);
271 */ 260 WebInspector.openAnchorLocationRegistry._appendHrefItems(contextMenu, target );
272 appendApplicableItems: function(event, contextMenu, target) 261 }
273 { 262 };
274 WebInspector.openAnchorLocationRegistry._appendContentProviderItems(cont extMenu, target); 263
275 WebInspector.openAnchorLocationRegistry._appendHrefItems(contextMenu, ta rget); 264 /**
276 }
277 };
278
279 /**
280 * @constructor
281 * @implements {WebInspector.Linkifier.LinkHandler} 265 * @implements {WebInspector.Linkifier.LinkHandler}
282 */ 266 * @unrestricted
283 WebInspector.HandlerRegistry.LinkHandler = function() 267 */
284 { 268 WebInspector.HandlerRegistry.LinkHandler = class {
285 }; 269 /**
286 270 * @override
287 WebInspector.HandlerRegistry.LinkHandler.prototype = { 271 * @param {string} url
288 /** 272 * @param {number=} lineNumber
289 * @override 273 * @return {boolean}
290 * @param {string} url 274 */
291 * @param {number=} lineNumber 275 handleLink(url, lineNumber) {
292 * @return {boolean} 276 return WebInspector.openAnchorLocationRegistry.dispatch({url: url, lineNumbe r: lineNumber});
293 */ 277 }
294 handleLink: function(url, lineNumber) 278 };
295 { 279
296 return WebInspector.openAnchorLocationRegistry.dispatch({ url: url, line Number: lineNumber}); 280 /**
297 }
298 };
299
300 /**
301 * @constructor
302 * @implements {WebInspector.SettingUI} 281 * @implements {WebInspector.SettingUI}
303 */ 282 * @unrestricted
304 WebInspector.HandlerRegistry.OpenAnchorLocationSettingUI = function() 283 */
305 { 284 WebInspector.HandlerRegistry.OpenAnchorLocationSettingUI = class {
306 }; 285 /**
307 286 * @override
308 WebInspector.HandlerRegistry.OpenAnchorLocationSettingUI.prototype = { 287 * @return {?Element}
309 /** 288 */
310 * @override 289 settingElement() {
311 * @return {?Element} 290 if (!WebInspector.openAnchorLocationRegistry.handlerNames.length)
312 */ 291 return null;
313 settingElement: function() 292
314 { 293 var handlerSelector = new WebInspector.HandlerSelector(WebInspector.openAnch orLocationRegistry);
315 if (!WebInspector.openAnchorLocationRegistry.handlerNames.length) 294 return WebInspector.SettingsUI.createCustomSetting(
316 return null; 295 WebInspector.UIString('Link handling:'), handlerSelector.element);
317 296 }
318 var handlerSelector = new WebInspector.HandlerSelector(WebInspector.open AnchorLocationRegistry); 297 };
319 return WebInspector.SettingsUI.createCustomSetting(WebInspector.UIString ("Link handling:"), handlerSelector.element); 298
320 } 299 /**
321 };
322
323 /**
324 * @type {!WebInspector.HandlerRegistry} 300 * @type {!WebInspector.HandlerRegistry}
325 */ 301 */
326 WebInspector.openAnchorLocationRegistry; 302 WebInspector.openAnchorLocationRegistry;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698