1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * @implements {WebInspector.TabbedEditorContainerDelegate}
8 * @implements {WebInspector.Searchable}
9 * @implements {WebInspector.Replaceable}
10 * @extends {WebInspector.VBox}
11 * @param {!WebInspector.Workspace} workspace
12 * @param {!WebInspector.SourcesPanel} sourcesPanel
14 WebInspector.SourcesView = function(workspace, sourcesPanel)
16 WebInspector.VBox.call(this);
17 this.registerRequiredCSS("sourcesView.css");
18 this.element.id = "sources-panel-sources-view";
19 this.setMinimumSize(50, 25);
21 this._workspace = workspace;
22 this._sourcesPanel = sourcesPanel;
24 this._searchableView = new WebInspector.SearchableView(this);
25 this._searchableView.setMinimalSearchQuerySize(0);
26 this._searchableView.show(this.element);
28 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.SourceFrame>} */
29 this._sourceFramesByUISourceCode = new Map();
31 var tabbedEditorPlaceholderText = WebInspector.isMac() ? WebInspector.UIString("Hit Cmd+O to open a file") : WebInspector.UIString("Hit Ctrl+O to open a file");
32 this._editorContainer = new WebInspector.TabbedEditorContainer(this, "previouslyViewedFiles", tabbedEditorPlaceholderText);
33 this._editorContainer.show(this._searchableView.element);
34 this._editorContainer.addEventListener(WebInspector.TabbedEditorContainer.Events.EditorSelected, this._editorSelected, this);
35 this._editorContainer.addEventListener(WebInspector.TabbedEditorContainer.Events.EditorClosed, this._editorClosed, this);
37 this._historyManager = new WebInspector.EditingLocationHistoryManager(this, this.currentSourceFrame.bind(this));
39 this._scriptViewStatusBarItemsContainer = document.createElement("div");
40 this._scriptViewStatusBarItemsContainer.className = "inline-block";
42 this._scriptViewStatusBarTextContainer = document.createElement("div");
43 this._scriptViewStatusBarTextContainer.className = "hbox";
45 this._statusBarContainerElement = this.element.createChild("div", "sources-status-bar");
48 * @this {WebInspector.SourcesView}
49 * @param {!WebInspector.SourcesView.EditorAction} EditorAction
51 function appendButtonForExtension(EditorAction)
53 this._statusBarContainerElement.appendChild(EditorAction.button(this));
55 var editorActions = /** @type {!Array.<!WebInspector.SourcesView.EditorAction>} */ (WebInspector.moduleManager.instances(WebInspector.SourcesView.EditorAction));
56 editorActions.forEach(appendButtonForExtension.bind(this));
58 this._statusBarContainerElement.appendChild(this._scriptViewStatusBarItemsContainer);
59 this._statusBarContainerElement.appendChild(this._scriptViewStatusBarTextContainer);
61 WebInspector.startBatchUpdate();
62 this._workspace.uiSourceCodes().forEach(this._addUISourceCode.bind(this));
63 WebInspector.endBatchUpdate();
65 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAdded, this);
66 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeRemoved, this._uiSourceCodeRemoved, this);
67 this._workspace.addEventListener(WebInspector.Workspace.Events.ProjectWillReset, this._projectWillReset.bind(this), this);
69 function handleBeforeUnload(event)
71 if (event.returnValue)
73 var unsavedSourceCodes = WebInspector.workspace.unsavedSourceCodes();
74 if (!unsavedSourceCodes.length)
77 event.returnValue = WebInspector.UIString("DevTools have unsaved changes that will be permanently lost.");
78 WebInspector.inspectorView.showPanel("sources");
79 for (var i = 0; i < unsavedSourceCodes.length; ++i)
80 WebInspector.panels.sources.showUISourceCode(unsavedSourceCodes[i]);
82 window.addEventListener("beforeunload", handleBeforeUnload, true);
85 this.element.addEventListener("keydown", this._handleKeyDown.bind(this), false);
88 WebInspector.SourcesView.Events = {
89 EditorClosed: "EditorClosed",
90 EditorSelected: "EditorSelected",
93 WebInspector.SourcesView.prototype = {
95 * @param {function(!Array.<!WebInspector.KeyboardShortcut.Descriptor>, function(?Event=):boolean)} registerShortcutDelegate
97 registerShortcuts: function(registerShortcutDelegate)
100 * @this {WebInspector.SourcesView}
101 * @param {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} shortcuts
102 * @param {function(?Event=):boolean} handler
104 function registerShortcut(shortcuts, handler)
106 registerShortcutDelegate(shortcuts, handler);
107 this._registerShortcuts(shortcuts, handler);
110 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.JumpToPreviousLocation, this._onJumpToPreviousLocation.bind(this));
111 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.JumpToNextLocation, this._onJumpToNextLocation.bind(this));
112 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.CloseEditorTab, this._onCloseEditorTab.bind(this));
113 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.GoToLine, this._showGoToLineDialog.bind(this));
114 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.GoToMember, this._showOutlineDialog.bind(this));
115 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.ToggleBreakpoint, this._toggleBreakpoint.bind(this));
116 registerShortcut.call(this, WebInspector.ShortcutsScreen.SourcesPanelShortcuts.Save, this._save.bind(this));
120 * @param {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} keys
121 * @param {function(?Event=):boolean} handler
123 _registerShortcuts: function(keys, handler)
125 for (var i = 0; i < keys.length; ++i)
126 this._shortcuts[keys[i].key] = handler;
129 _handleKeyDown: function(event)
131 var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
132 var handler = this._shortcuts[shortcutKey];
133 if (handler && handler())
140 statusBarContainerElement: function()
142 return this._statusBarContainerElement;
148 defaultFocusedElement: function()
150 return this._editorContainer.view.defaultFocusedElement();
154 * @return {!WebInspector.SearchableView}
156 searchableView: function()
158 return this._searchableView;
162 * @return {!WebInspector.View}
164 visibleView: function()
166 return this._editorContainer.visibleView;
170 * @return {?WebInspector.SourceFrame}
172 currentSourceFrame: function()
174 var view = this.visibleView();
175 if (!(view instanceof WebInspector.SourceFrame))
177 return /** @type {!WebInspector.SourceFrame} */ (view);
181 * @return {?WebInspector.UISourceCode}
183 currentUISourceCode: function()
185 return this._currentUISourceCode;
189 * @param {?Event=} event
191 _onCloseEditorTab: function(event)
193 var uiSourceCode = this.currentUISourceCode();
196 this._editorContainer.closeFile(uiSourceCode);
201 * @param {?Event=} event
203 _onJumpToPreviousLocation: function(event)
205 this._historyManager.rollback();
210 * @param {?Event=} event
212 _onJumpToNextLocation: function(event)
214 this._historyManager.rollover();
219 * @param {!WebInspector.Event} event
221 _uiSourceCodeAdded: function(event)
223 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
224 this._addUISourceCode(uiSourceCode);
228 * @param {!WebInspector.UISourceCode} uiSourceCode
230 _addUISourceCode: function(uiSourceCode)
232 if (uiSourceCode.project().isServiceProject())
234 this._editorContainer.addUISourceCode(uiSourceCode);
235 // Replace debugger script-based uiSourceCode with a network-based one.
236 var currentUISourceCode = this._currentUISourceCode;
237 if (currentUISourceCode && currentUISourceCode.project().isServiceProject() && currentUISourceCode !== uiSourceCode && currentUISourceCode.url === uiSourceCode.url) {
238 this._showFile(uiSourceCode);
239 this._editorContainer.removeUISourceCode(currentUISourceCode);
243 _uiSourceCodeRemoved: function(event)
245 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
246 this._removeUISourceCodes([uiSourceCode]);
250 * @param {!Array.<!WebInspector.UISourceCode>} uiSourceCodes
252 _removeUISourceCodes: function(uiSourceCodes)
254 for (var i = 0; i < uiSourceCodes.length; ++i) {
255 this._removeSourceFrame(uiSourceCodes[i]);
256 this._historyManager.removeHistoryForSourceCode(uiSourceCodes[i]);
258 this._editorContainer.removeUISourceCodes(uiSourceCodes);
261 _projectWillReset: function(event)
263 var project = event.data;
264 var uiSourceCodes = project.uiSourceCodes();
265 this._removeUISourceCodes(uiSourceCodes);
266 if (project.type() === WebInspector.projectTypes.Network)
267 this._editorContainer.reset();
270 _updateScriptViewStatusBarItems: function()
272 this._scriptViewStatusBarItemsContainer.removeChildren();
273 this._scriptViewStatusBarTextContainer.removeChildren();
274 var sourceFrame = this.currentSourceFrame();
278 var statusBarItems = sourceFrame.statusBarItems() || [];
279 for (var i = 0; i < statusBarItems.length; ++i)
280 this._scriptViewStatusBarItemsContainer.appendChild(statusBarItems[i]);
281 var statusBarText = sourceFrame.statusBarText();
283 this._scriptViewStatusBarTextContainer.appendChild(statusBarText);
287 * @param {!WebInspector.UISourceCode} uiSourceCode
288 * @param {number=} lineNumber
289 * @param {number=} columnNumber
290 * @param {boolean=} omitFocus
291 * @param {boolean=} omitHighlight
293 showSourceLocation: function(uiSourceCode, lineNumber, columnNumber, omitFocus, omitHighlight)
295 this._historyManager.updateCurrentState();
296 var sourceFrame = this._showFile(uiSourceCode);
297 if (typeof lineNumber === "number")
298 sourceFrame.revealPosition(lineNumber, columnNumber, !omitHighlight);
299 this._historyManager.pushNewState();
302 WebInspector.notifications.dispatchEventToListeners(WebInspector.UserMetrics.UserAction, {
303 action: WebInspector.UserMetrics.UserActionNames.OpenSourceLink,
304 url: uiSourceCode.originURL(),
305 lineNumber: lineNumber
310 * @param {!WebInspector.UISourceCode} uiSourceCode
311 * @return {!WebInspector.SourceFrame}
313 _showFile: function(uiSourceCode)
315 var sourceFrame = this._getOrCreateSourceFrame(uiSourceCode);
316 if (this._currentUISourceCode === uiSourceCode)
319 this._currentUISourceCode = uiSourceCode;
320 this._editorContainer.showFile(uiSourceCode);
321 this._updateScriptViewStatusBarItems();
326 * @param {!WebInspector.UISourceCode} uiSourceCode
327 * @return {!WebInspector.SourceFrame}
329 _createSourceFrame: function(uiSourceCode)
332 switch (uiSourceCode.contentType()) {
333 case WebInspector.resourceTypes.Script:
334 sourceFrame = new WebInspector.JavaScriptSourceFrame(this._sourcesPanel, uiSourceCode);
336 case WebInspector.resourceTypes.Document:
337 sourceFrame = new WebInspector.JavaScriptSourceFrame(this._sourcesPanel, uiSourceCode);
339 case WebInspector.resourceTypes.Stylesheet:
340 sourceFrame = new WebInspector.CSSSourceFrame(uiSourceCode);
343 sourceFrame = new WebInspector.UISourceCodeFrame(uiSourceCode);
346 sourceFrame.setHighlighterType(uiSourceCode.highlighterType());
347 this._sourceFramesByUISourceCode.put(uiSourceCode, sourceFrame);
348 this._historyManager.trackSourceFrameCursorJumps(sourceFrame);
353 * @param {!WebInspector.UISourceCode} uiSourceCode
354 * @return {!WebInspector.SourceFrame}
356 _getOrCreateSourceFrame: function(uiSourceCode)
358 return this._sourceFramesByUISourceCode.get(uiSourceCode) || this._createSourceFrame(uiSourceCode);
362 * @param {!WebInspector.SourceFrame} sourceFrame
363 * @param {!WebInspector.UISourceCode} uiSourceCode
366 _sourceFrameMatchesUISourceCode: function(sourceFrame, uiSourceCode)
368 switch (uiSourceCode.contentType()) {
369 case WebInspector.resourceTypes.Script:
370 case WebInspector.resourceTypes.Document:
371 return sourceFrame instanceof WebInspector.JavaScriptSourceFrame;
372 case WebInspector.resourceTypes.Stylesheet:
373 return sourceFrame instanceof WebInspector.CSSSourceFrame;
375 return !(sourceFrame instanceof WebInspector.JavaScriptSourceFrame);
380 * @param {!WebInspector.UISourceCode} uiSourceCode
382 _recreateSourceFrameIfNeeded: function(uiSourceCode)
384 var oldSourceFrame = this._sourceFramesByUISourceCode.get(uiSourceCode);
387 if (this._sourceFrameMatchesUISourceCode(oldSourceFrame, uiSourceCode)) {
388 oldSourceFrame.setHighlighterType(uiSourceCode.highlighterType());
390 this._editorContainer.removeUISourceCode(uiSourceCode);
391 this._removeSourceFrame(uiSourceCode);
396 * @param {!WebInspector.UISourceCode} uiSourceCode
397 * @return {!WebInspector.SourceFrame}
399 viewForFile: function(uiSourceCode)
401 return this._getOrCreateSourceFrame(uiSourceCode);
405 * @param {!WebInspector.UISourceCode} uiSourceCode
407 _removeSourceFrame: function(uiSourceCode)
409 var sourceFrame = this._sourceFramesByUISourceCode.get(uiSourceCode);
412 this._sourceFramesByUISourceCode.remove(uiSourceCode);
413 sourceFrame.dispose();
416 clearCurrentExecutionLine: function()
418 if (this._executionSourceFrame)
419 this._executionSourceFrame.clearExecutionLine();
420 delete this._executionSourceFrame;
423 setExecutionLine: function(uiLocation)
425 var sourceFrame = this._getOrCreateSourceFrame(uiLocation.uiSourceCode);
426 sourceFrame.setExecutionLine(uiLocation.lineNumber);
427 this._executionSourceFrame = sourceFrame;
430 _editorClosed: function(event)
432 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
433 this._historyManager.removeHistoryForSourceCode(uiSourceCode);
435 var wasSelected = false;
436 if (this._currentUISourceCode === uiSourceCode) {
437 delete this._currentUISourceCode;
441 // SourcesNavigator does not need to update on EditorClosed.
442 this._updateScriptViewStatusBarItems();
443 this._searchableView.resetSearch();
446 data.uiSourceCode = uiSourceCode;
447 data.wasSelected = wasSelected;
448 this.dispatchEventToListeners(WebInspector.SourcesView.Events.EditorClosed, data);
451 _editorSelected: function(event)
453 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data.currentFile);
454 var shouldUseHistoryManager = uiSourceCode !== this._currentUISourceCode && event.data.userGesture;
455 if (shouldUseHistoryManager)
456 this._historyManager.updateCurrentState();
457 var sourceFrame = this._showFile(uiSourceCode);
458 if (shouldUseHistoryManager)
459 this._historyManager.pushNewState();
461 this._searchableView.setReplaceable(!!sourceFrame && sourceFrame.canEditSource());
462 this._searchableView.resetSearch();
464 this.dispatchEventToListeners(WebInspector.SourcesView.Events.EditorSelected, uiSourceCode);
468 * @param {!WebInspector.UISourceCode} uiSourceCode
470 sourceRenamed: function(uiSourceCode)
472 this._recreateSourceFrameIfNeeded(uiSourceCode);
475 searchCanceled: function()
477 if (this._searchView)
478 this._searchView.searchCanceled();
480 delete this._searchView;
481 delete this._searchQuery;
485 * @param {string} query
486 * @param {boolean} shouldJump
488 performSearch: function(query, shouldJump)
490 this._searchableView.updateSearchMatchesCount(0);
492 var sourceFrame = this.currentSourceFrame();
496 this._searchView = sourceFrame;
497 this._searchQuery = query;
500 * @param {!WebInspector.View} view
501 * @param {number} searchMatches
502 * @this {WebInspector.SourcesView}
504 function finishedCallback(view, searchMatches)
509 this._searchableView.updateSearchMatchesCount(searchMatches);
513 * @param {number} currentMatchIndex
514 * @this {WebInspector.SourcesView}
516 function currentMatchChanged(currentMatchIndex)
518 this._searchableView.updateCurrentMatchIndex(currentMatchIndex);
522 * @this {WebInspector.SourcesView}
524 function searchResultsChanged()
526 this._searchableView.cancelSearch();
529 this._searchView.performSearch(query, shouldJump, finishedCallback.bind(this), currentMatchChanged.bind(this), searchResultsChanged.bind(this));
532 jumpToNextSearchResult: function()
534 if (!this._searchView)
537 if (this._searchView !== this.currentSourceFrame()) {
538 this.performSearch(this._searchQuery, true);
542 this._searchView.jumpToNextSearchResult();
545 jumpToPreviousSearchResult: function()
547 if (!this._searchView)
550 if (this._searchView !== this.currentSourceFrame()) {
551 this.performSearch(this._searchQuery, true);
552 if (this._searchView)
553 this._searchView.jumpToLastSearchResult();
557 this._searchView.jumpToPreviousSearchResult();
561 * @param {string} text
563 replaceSelectionWith: function(text)
565 var sourceFrame = this.currentSourceFrame();
567 console.assert(sourceFrame);
570 sourceFrame.replaceSelectionWith(text);
574 * @param {string} query
575 * @param {string} text
577 replaceAllWith: function(query, text)
579 var sourceFrame = this.currentSourceFrame();
581 console.assert(sourceFrame);
584 sourceFrame.replaceAllWith(query, text);
588 * @param {?Event=} event
591 _showOutlineDialog: function(event)
593 var uiSourceCode = this._editorContainer.currentFile();
597 switch (uiSourceCode.contentType()) {
598 case WebInspector.resourceTypes.Document:
599 case WebInspector.resourceTypes.Script:
600 WebInspector.JavaScriptOutlineDialog.show(this, uiSourceCode, this.showSourceLocation.bind(this, uiSourceCode));
602 case WebInspector.resourceTypes.Stylesheet:
603 WebInspector.StyleSheetOutlineDialog.show(this, uiSourceCode, this.showSourceLocation.bind(this, uiSourceCode));
610 * @param {string=} query
612 showOpenResourceDialog: function(query)
614 var uiSourceCodes = this._editorContainer.historyUISourceCodes();
615 /** @type {!Map.<!WebInspector.UISourceCode, number>} */
616 var defaultScores = new Map();
617 for (var i = 1; i < uiSourceCodes.length; ++i) // Skip current element
618 defaultScores.put(uiSourceCodes[i], uiSourceCodes.length - i);
619 WebInspector.OpenResourceDialog.show(this, this.element, query, defaultScores);
623 * @param {?Event=} event
626 _showGoToLineDialog: function(event)
628 if (this._currentUISourceCode)
629 this.showOpenResourceDialog(":");
638 var sourceFrame = this.currentSourceFrame();
641 if (!(sourceFrame instanceof WebInspector.UISourceCodeFrame))
643 var uiSourceCodeFrame = /** @type {!WebInspector.UISourceCodeFrame} */ (sourceFrame);
644 uiSourceCodeFrame.commitEditing();
651 _toggleBreakpoint: function()
653 var sourceFrame = this.currentSourceFrame();
657 if (sourceFrame instanceof WebInspector.JavaScriptSourceFrame) {
658 var javaScriptSourceFrame = /** @type {!WebInspector.JavaScriptSourceFrame} */ (sourceFrame);
659 javaScriptSourceFrame.toggleBreakpointOnCurrentLine();
666 * @param {boolean} active
668 toggleBreakpointsActiveState: function(active)
670 this._editorContainer.view.element.classList.toggle("breakpoints-deactivated", !active);
673 __proto__: WebInspector.VBox.prototype
679 WebInspector.SourcesView.EditorAction = function()
683 WebInspector.SourcesView.EditorAction.prototype = {
685 * @param {!WebInspector.SourcesView} sourcesView
688 button: function(sourcesView) { }