WebCore:
[WebKit-https.git] / WebCore / inspector / front-end / ScriptsPanel.js
1 /*
2  * Copyright (C) 2008 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.ScriptsPanel = function()
27 {
28     WebInspector.Panel.call(this);
29
30     this.element.addStyleClass("scripts");
31
32     this.topStatusBar = document.createElement("div");
33     this.topStatusBar.className = "status-bar";
34     this.topStatusBar.id = "scripts-status-bar";
35     this.element.appendChild(this.topStatusBar);
36
37     this.backButton = document.createElement("button");
38     this.backButton.className = "status-bar-item";
39     this.backButton.id = "scripts-back";
40     this.backButton.title = WebInspector.UIString("Show the previous script resource.");
41     this.backButton.disabled = true;
42     this.backButton.appendChild(document.createElement("img"));
43     this.backButton.addEventListener("click", this._goBack.bind(this), false);
44     this.topStatusBar.appendChild(this.backButton);
45
46     this.forwardButton = document.createElement("button");
47     this.forwardButton.className = "status-bar-item";
48     this.forwardButton.id = "scripts-forward";
49     this.forwardButton.title = WebInspector.UIString("Show the next script resource.");
50     this.forwardButton.disabled = true;
51     this.forwardButton.appendChild(document.createElement("img"));
52     this.forwardButton.addEventListener("click", this._goForward.bind(this), false);
53     this.topStatusBar.appendChild(this.forwardButton);
54
55     this.filesSelectElement = document.createElement("select");
56     this.filesSelectElement.className = "status-bar-item";
57     this.filesSelectElement.id = "scripts-files";
58     this.filesSelectElement.addEventListener("change", this._changeVisibleFile.bind(this), false);
59     this.topStatusBar.appendChild(this.filesSelectElement);
60
61     this.functionsSelectElement = document.createElement("select");
62     this.functionsSelectElement.className = "status-bar-item";
63     this.functionsSelectElement.id = "scripts-functions";
64
65     // FIXME: append the functions select element to the top status bar when it is implemented.
66     // this.topStatusBar.appendChild(this.functionsSelectElement);
67
68     this.sidebarButtonsElement = document.createElement("div");
69     this.sidebarButtonsElement.id = "scripts-sidebar-buttons";
70     this.topStatusBar.appendChild(this.sidebarButtonsElement);
71
72     this.pauseButton = document.createElement("button");
73     this.pauseButton.className = "status-bar-item";
74     this.pauseButton.id = "scripts-pause";
75     this.pauseButton.title = WebInspector.UIString("Pause script execution.");
76     this.pauseButton.disabled = true;
77     this.pauseButton.appendChild(document.createElement("img"));
78     this.pauseButton.addEventListener("click", this._togglePause.bind(this), false);
79     this.sidebarButtonsElement.appendChild(this.pauseButton);
80
81     this.stepOverButton = document.createElement("button");
82     this.stepOverButton.className = "status-bar-item";
83     this.stepOverButton.id = "scripts-step-over";
84     this.stepOverButton.title = WebInspector.UIString("Step over next function call.");
85     this.stepOverButton.disabled = true;
86     this.stepOverButton.addEventListener("click", this._stepOverClicked.bind(this), false);
87     this.stepOverButton.appendChild(document.createElement("img"));
88     this.sidebarButtonsElement.appendChild(this.stepOverButton);
89
90     this.stepIntoButton = document.createElement("button");
91     this.stepIntoButton.className = "status-bar-item";
92     this.stepIntoButton.id = "scripts-step-into";
93     this.stepIntoButton.title = WebInspector.UIString("Step into next function call.");
94     this.stepIntoButton.disabled = true;
95     this.stepIntoButton.addEventListener("click", this._stepIntoClicked.bind(this), false);
96     this.stepIntoButton.appendChild(document.createElement("img"));
97     this.sidebarButtonsElement.appendChild(this.stepIntoButton);
98
99     this.stepOutButton = document.createElement("button");
100     this.stepOutButton.className = "status-bar-item";
101     this.stepOutButton.id = "scripts-step-out";
102     this.stepOutButton.title = WebInspector.UIString("Step out of current function.");
103     this.stepOutButton.disabled = true;
104     this.stepOutButton.addEventListener("click", this._stepOutClicked.bind(this), false);
105     this.stepOutButton.appendChild(document.createElement("img"));
106     this.sidebarButtonsElement.appendChild(this.stepOutButton);
107
108     this.debuggerStatusElement = document.createElement("div");
109     this.debuggerStatusElement.id = "scripts-debugger-status";
110     this.sidebarButtonsElement.appendChild(this.debuggerStatusElement);
111
112     this.viewsContainerElement = document.createElement("div");
113     this.viewsContainerElement.id = "script-resource-views";
114
115     this.sidebarElement = document.createElement("div");
116     this.sidebarElement.id = "scripts-sidebar";
117
118     this.sidebarResizeElement = document.createElement("div");
119     this.sidebarResizeElement.className = "sidebar-resizer-vertical";
120     this.sidebarResizeElement.addEventListener("mousedown", this._startSidebarResizeDrag.bind(this), false);
121
122     this.sidebarResizeWidgetElement = document.createElement("div");
123     this.sidebarResizeWidgetElement.id = "scripts-sidebar-resizer-widget";
124     this.sidebarResizeWidgetElement.addEventListener("mousedown", this._startSidebarResizeDrag.bind(this), false);
125     this.topStatusBar.appendChild(this.sidebarResizeWidgetElement);
126
127     this.sidebarPanes = {};
128     this.sidebarPanes.callstack = new WebInspector.CallStackSidebarPane();
129     this.sidebarPanes.scopechain = new WebInspector.ScopeChainSidebarPane();
130     this.sidebarPanes.breakpoints = new WebInspector.BreakpointsSidebarPane();
131
132     for (var pane in this.sidebarPanes)
133         this.sidebarElement.appendChild(this.sidebarPanes[pane].element);
134
135     // FIXME: remove the following line of code when the Breakpoints pane has content.
136     this.sidebarElement.removeChild(this.sidebarPanes.breakpoints.element);
137
138     this.sidebarPanes.callstack.expanded = true;
139     this.sidebarPanes.callstack.addEventListener("call frame selected", this._callFrameSelected, this);
140
141     this.sidebarPanes.scopechain.expanded = true;
142
143     var panelEnablerHeading = WebInspector.UIString("You need to enable debugging before you can use the Scripts panel.");
144     var panelEnablerDisclaimer = WebInspector.UIString("Enabling debugging will make scripts run slower.");
145     var panelEnablerButton = WebInspector.UIString("Enable Debugging");
146
147     this.panelEnablerView = new WebInspector.PanelEnablerView("scripts", panelEnablerHeading, panelEnablerDisclaimer, panelEnablerButton);
148     this.panelEnablerView.addEventListener("enable clicked", this._enableDebugging, this);
149
150     this.element.appendChild(this.panelEnablerView.element);
151     this.element.appendChild(this.viewsContainerElement);
152     this.element.appendChild(this.sidebarElement);
153     this.element.appendChild(this.sidebarResizeElement);
154
155     this.enableToggleButton = document.createElement("button");
156     this.enableToggleButton.className = "enable-toggle-status-bar-item status-bar-item";
157     this.enableToggleButton.addEventListener("click", this._toggleDebugging.bind(this), false);
158
159     this.pauseOnExceptionButton = document.createElement("button");
160     this.pauseOnExceptionButton.id = "scripts-pause-on-exceptions-status-bar-item";
161     this.pauseOnExceptionButton.className = "status-bar-item";
162     this.pauseOnExceptionButton.addEventListener("click", this._togglePauseOnExceptions.bind(this), false);
163
164     this._breakpointsURLMap = {};
165
166     this._shortcuts = {};
167
168     var isMac = InspectorController.platform().indexOf("mac-") === 0;
169     var platformSpecificModifier = isMac ? WebInspector.KeyboardShortcut.Modifiers.Meta : WebInspector.KeyboardShortcut.Modifiers.Ctrl;
170
171     // Continue.
172     var handler = this.pauseButton.click.bind(this.pauseButton);
173     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.F5);
174     this._shortcuts[shortcut] = handler;
175     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.Slash, platformSpecificModifier);
176     this._shortcuts[shortcut] = handler;
177
178     // Step over.
179     var handler = this.stepOverButton.click.bind(this.stepOverButton);
180     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.F10);
181     this._shortcuts[shortcut] = handler;
182     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.SingleQuote, platformSpecificModifier);
183     this._shortcuts[shortcut] = handler;
184
185     // Step into.
186     var handler = this.stepIntoButton.click.bind(this.stepIntoButton);
187     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.F11);
188     this._shortcuts[shortcut] = handler;
189     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.Semicolon, platformSpecificModifier);
190     this._shortcuts[shortcut] = handler;
191
192     // Step out.
193     var handler = this.stepOutButton.click.bind(this.stepOutButton);
194     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.F11, WebInspector.KeyboardShortcut.Modifiers.Shift);
195     this._shortcuts[shortcut] = handler;
196     var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.Semicolon, WebInspector.KeyboardShortcut.Modifiers.Shift, platformSpecificModifier);
197     this._shortcuts[shortcut] = handler;
198
199     this.reset();
200 }
201
202 WebInspector.ScriptsPanel.prototype = {
203     toolbarItemClass: "scripts",
204
205     get toolbarItemLabel()
206     {
207         return WebInspector.UIString("Scripts");
208     },
209
210     get statusBarItems()
211     {
212         return [this.enableToggleButton, this.pauseOnExceptionButton];
213     },
214
215     get paused()
216     {
217         return this._paused;
218     },
219
220     show: function()
221     {
222         WebInspector.Panel.prototype.show.call(this);
223         this.sidebarResizeElement.style.right = (this.sidebarElement.offsetWidth - 3) + "px";
224
225         if (this.visibleView) {
226             if (this.visibleView instanceof WebInspector.ResourceView)
227                 this.visibleView.headersVisible = false;
228             this.visibleView.show(this.viewsContainerElement);
229         }
230
231         // Hide any views that are visible that are not this panel's current visible view.
232         // This can happen when a ResourceView is visible in the Resources panel then switched
233         // to the this panel.
234         for (var sourceID in this._sourceIDMap) {
235             var scriptOrResource = this._sourceIDMap[sourceID];
236             var view = this._sourceViewForScriptOrResource(scriptOrResource);
237             if (!view || view === this.visibleView)
238                 continue;
239             view.visible = false;
240         }
241         if (this._attachDebuggerWhenShown) {
242             InspectorController.enableDebuggerFromFrontend(false);
243             delete this._attachDebuggerWhenShown;
244         }
245     },
246
247     get searchableViews()
248     {
249         var views = [];
250
251         const visibleView = this.visibleView;
252         if (visibleView && visibleView.performSearch) {
253             visibleView.alreadySearching = true;
254             views.push(visibleView);
255         }
256
257         for (var sourceID in this._sourceIDMap) {
258             var scriptOrResource = this._sourceIDMap[sourceID];
259             var view = this._sourceViewForScriptOrResource(scriptOrResource);
260             if (!view || !view.performSearch || view.alreadySearching)
261                 continue;
262
263             view.alreadySearching = true;
264             views.push(view);
265         }
266
267         for (var i = 0; i < views.length; ++i)
268             delete views[i].alreadySearching;
269
270         return views;
271     },
272
273     addScript: function(sourceID, sourceURL, source, startingLine, errorLine, errorMessage)
274     {
275         var script = new WebInspector.Script(sourceID, sourceURL, source, startingLine, errorLine, errorMessage);
276
277         if (sourceURL in WebInspector.resourceURLMap) {
278             var resource = WebInspector.resourceURLMap[sourceURL];
279             resource.addScript(script);
280         }
281
282         if (sourceURL in this._breakpointsURLMap && sourceID) {
283             var breakpoints = this._breakpointsURLMap[sourceURL];
284             var breakpointsLength = breakpoints.length;
285             for (var i = 0; i < breakpointsLength; ++i) {
286                 var breakpoint = breakpoints[i];
287                 if (startingLine <= breakpoint.line) {
288                     breakpoint.sourceID = sourceID;
289                     if (breakpoint.enabled)
290                         InspectorController.addBreakpoint(breakpoint.sourceID, breakpoint.line);
291                 }
292             }
293         }
294
295         if (sourceID)
296             this._sourceIDMap[sourceID] = (resource || script);
297
298         this._addScriptToFilesMenu(script);
299     },
300
301     addBreakpoint: function(breakpoint)
302     {
303         this.sidebarPanes.breakpoints.addBreakpoint(breakpoint);
304
305         var sourceFrame;
306         if (breakpoint.url) {
307             if (!(breakpoint.url in this._breakpointsURLMap))
308                 this._breakpointsURLMap[breakpoint.url] = [];
309             this._breakpointsURLMap[breakpoint.url].unshift(breakpoint);
310
311             if (breakpoint.url in WebInspector.resourceURLMap) {
312                 var resource = WebInspector.resourceURLMap[breakpoint.url];
313                 sourceFrame = this._sourceFrameForScriptOrResource(resource);
314             }
315         }
316
317         if (breakpoint.sourceID && !sourceFrame) {
318             var object = this._sourceIDMap[breakpoint.sourceID]
319             sourceFrame = this._sourceFrameForScriptOrResource(object);
320         }
321
322         if (sourceFrame)
323             sourceFrame.addBreakpoint(breakpoint);
324     },
325
326     removeBreakpoint: function(breakpoint)
327     {
328         this.sidebarPanes.breakpoints.removeBreakpoint(breakpoint);
329
330         var sourceFrame;
331         if (breakpoint.url && breakpoint.url in this._breakpointsURLMap) {
332             var breakpoints = this._breakpointsURLMap[breakpoint.url];
333             breakpoints.remove(breakpoint);
334             if (!breakpoints.length)
335                 delete this._breakpointsURLMap[breakpoint.url];
336
337             if (breakpoint.url in WebInspector.resourceURLMap) {
338                 var resource = WebInspector.resourceURLMap[breakpoint.url];
339                 sourceFrame = this._sourceFrameForScriptOrResource(resource);
340             }
341         }
342
343         if (breakpoint.sourceID && !sourceFrame) {
344             var object = this._sourceIDMap[breakpoint.sourceID]
345             sourceFrame = this._sourceFrameForScriptOrResource(object);
346         }
347
348         if (sourceFrame)
349             sourceFrame.removeBreakpoint(breakpoint);
350     },
351
352     evaluateInSelectedCallFrame: function(code, updateInterface, callback)
353     {
354         var selectedCallFrame = this.sidebarPanes.callstack.selectedCallFrame;
355         if (!this._paused || !selectedCallFrame)
356             return;
357
358         if (typeof updateInterface === "undefined")
359             updateInterface = true;
360
361         var self = this;
362         function updatingCallbackWrapper(result)
363         {
364             callback(result);
365             if (updateInterface)
366                 self.sidebarPanes.scopechain.update(selectedCallFrame);
367         }        
368         this.doEvalInCallFrame(selectedCallFrame, code, updatingCallbackWrapper);
369     },
370
371     doEvalInCallFrame: function(callFrame, code, callback)
372     {
373         function delayedEvaluation()
374         {
375             try {
376                 callback(callFrame.evaluate(code));
377             } catch (e) {
378                 callback(e, true);
379             }
380         }
381         setTimeout(delayedEvaluation, 0);
382     },
383
384     variablesInScopeForSelectedCallFrame: function()
385     {
386         var selectedCallFrame = this.sidebarPanes.callstack.selectedCallFrame;
387         if (!this._paused || !selectedCallFrame)
388             return {};
389
390         var result = {};
391         var scopeChain = selectedCallFrame.scopeChain;
392         for (var i = 0; i < scopeChain.length; ++i) {
393             var scopeObject = scopeChain[i];
394             for (var property in scopeObject)
395                 result[property] = true;
396         }
397
398         return result;
399     },
400
401     debuggerPaused: function()
402     {
403         this._paused = true;
404         this._waitingToPause = false;
405         this._stepping = false;
406
407         this._updateDebuggerButtons();
408
409         var callStackPane = this.sidebarPanes.callstack;
410         var currentFrame = InspectorController.currentCallFrame();
411         callStackPane.update(currentFrame, this._sourceIDMap);
412         callStackPane.selectedCallFrame = currentFrame;
413
414         WebInspector.currentPanel = this;
415         window.focus();
416     },
417
418     debuggerResumed: function()
419     {
420         this._paused = false;
421         this._waitingToPause = false;
422         this._stepping = false;
423
424         this._clearInterface();
425     },
426
427     attachDebuggerWhenShown: function()
428     {
429         if (this.element.parentElement) {
430             InspectorController.enableDebuggerFromFrontend(false);
431         } else {
432             this._attachDebuggerWhenShown = true;
433         }
434     },
435
436     debuggerWasEnabled: function()
437     {
438         this.reset();
439     },
440
441     debuggerWasDisabled: function()
442     {
443         this.reset();
444     },
445
446     reset: function()
447     {
448         this.visibleView = null;
449
450         delete this.currentQuery;
451         this.searchCanceled();
452
453         if (!InspectorController.debuggerEnabled()) {
454             this._paused = false;
455             this._waitingToPause = false;
456             this._stepping = false;
457         }
458
459         this._clearInterface();
460
461         this._backForwardList = [];
462         this._currentBackForwardIndex = -1;
463         this._updateBackAndForwardButtons();
464
465         this._scriptsForURLsInFilesSelect = {};
466         this.filesSelectElement.removeChildren();
467         this.functionsSelectElement.removeChildren();
468         this.viewsContainerElement.removeChildren();
469
470         if (this._sourceIDMap) {
471             for (var sourceID in this._sourceIDMap) {
472                 var object = this._sourceIDMap[sourceID];
473                 if (object instanceof WebInspector.Resource)
474                     object.removeAllScripts();
475             }
476         }
477
478         this._sourceIDMap = {};
479     },
480
481     get visibleView()
482     {
483         return this._visibleView;
484     },
485
486     set visibleView(x)
487     {
488         if (this._visibleView === x)
489             return;
490
491         if (this._visibleView)
492             this._visibleView.hide();
493
494         this._visibleView = x;
495
496         if (x)
497             x.show(this.viewsContainerElement);
498     },
499
500     canShowResource: function(resource)
501     {
502         return resource && resource.scripts.length && InspectorController.debuggerEnabled();
503     },
504
505     showScript: function(script, line)
506     {
507         this._showScriptOrResource(script, line, true);
508     },
509
510     showResource: function(resource, line)
511     {
512         this._showScriptOrResource(resource, line, true);
513     },
514
515     showView: function(view)
516     {
517         if (!view)
518             return;
519         this._showScriptOrResource((view.resource || view.script));
520     },
521
522     handleKeyEvent: function(event)
523     {
524         var shortcut = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
525         var handler = this._shortcuts[shortcut];
526         if (handler) {
527             handler(event);
528             event.preventDefault();
529             event.handled = true;
530         } else {
531             this.sidebarPanes.callstack.handleKeyEvent(event);
532         }
533     },
534
535     scriptViewForScript: function(script)
536     {
537         if (!script)
538             return null;
539         if (!script._scriptView)
540             script._scriptView = new WebInspector.ScriptView(script);
541         return script._scriptView;
542     },
543
544     sourceFrameForScript: function(script)
545     {
546         var view = this.scriptViewForScript(script);
547         if (!view)
548             return null;
549
550         // Setting up the source frame requires that we be attached.
551         if (!this.element.parentNode)
552             this.attach();
553
554         view.setupSourceFrameIfNeeded();
555         return view.sourceFrame;
556     },
557
558     _sourceViewForScriptOrResource: function(scriptOrResource)
559     {
560         if (scriptOrResource instanceof WebInspector.Resource) {
561             if (!WebInspector.panels.resources)
562                 return null;
563             return WebInspector.panels.resources.resourceViewForResource(scriptOrResource);
564         }
565         if (scriptOrResource instanceof WebInspector.Script)
566             return this.scriptViewForScript(scriptOrResource);
567     },
568
569     _sourceFrameForScriptOrResource: function(scriptOrResource)
570     {
571         if (scriptOrResource instanceof WebInspector.Resource) {
572             if (!WebInspector.panels.resources)
573                 return null;
574             return WebInspector.panels.resources.sourceFrameForResource(scriptOrResource);
575         }
576         if (scriptOrResource instanceof WebInspector.Script)
577             return this.sourceFrameForScript(scriptOrResource);
578     },
579
580     _showScriptOrResource: function(scriptOrResource, line, shouldHighlightLine, fromBackForwardAction)
581     {
582         if (!scriptOrResource)
583             return;
584
585         var view;
586         if (scriptOrResource instanceof WebInspector.Resource) {
587             if (!WebInspector.panels.resources)
588                 return null;
589             view = WebInspector.panels.resources.resourceViewForResource(scriptOrResource);
590             view.headersVisible = false;
591
592             if (scriptOrResource.url in this._breakpointsURLMap) {
593                 var sourceFrame = this._sourceFrameForScriptOrResource(scriptOrResource);
594                 if (sourceFrame && !sourceFrame.breakpoints.length) {
595                     var breakpoints = this._breakpointsURLMap[scriptOrResource.url];
596                     var breakpointsLength = breakpoints.length;
597                     for (var i = 0; i < breakpointsLength; ++i)
598                         sourceFrame.addBreakpoint(breakpoints[i]);
599                 }
600             }
601         } else if (scriptOrResource instanceof WebInspector.Script)
602             view = this.scriptViewForScript(scriptOrResource);
603
604         if (!view)
605             return;
606
607         if (!fromBackForwardAction) {
608             var oldIndex = this._currentBackForwardIndex;
609             if (oldIndex >= 0)
610                 this._backForwardList.splice(oldIndex + 1, this._backForwardList.length - oldIndex);
611
612             // Check for a previous entry of the same object in _backForwardList.
613             // If one is found, remove it and update _currentBackForwardIndex to match.
614             var previousEntryIndex = this._backForwardList.indexOf(scriptOrResource);
615             if (previousEntryIndex !== -1) {
616                 this._backForwardList.splice(previousEntryIndex, 1);
617                 --this._currentBackForwardIndex;
618             }
619
620             this._backForwardList.push(scriptOrResource);
621             ++this._currentBackForwardIndex;
622
623             this._updateBackAndForwardButtons();
624         }
625
626         this.visibleView = view;
627
628         if (line) {
629             if (view.revealLine)
630                 view.revealLine(line);
631             if (view.highlightLine && shouldHighlightLine)
632                 view.highlightLine(line);
633         }
634
635         var option;
636         if (scriptOrResource instanceof WebInspector.Script) {
637             option = scriptOrResource.filesSelectOption;
638             console.assert(option);
639         } else {
640             var url = scriptOrResource.url;
641             var script = this._scriptsForURLsInFilesSelect[url];
642             if (script)
643                option = script.filesSelectOption;
644         }
645
646         if (option)
647             this.filesSelectElement.selectedIndex = option.index;
648     },
649
650     _addScriptToFilesMenu: function(script)
651     {
652         if (script.resource && this._scriptsForURLsInFilesSelect[script.sourceURL])
653             return;
654
655         this._scriptsForURLsInFilesSelect[script.sourceURL] = script;
656
657         var select = this.filesSelectElement;
658
659         var option = document.createElement("option");
660         option.representedObject = (script.resource || script);
661         option.text = (script.sourceURL ? WebInspector.displayNameForURL(script.sourceURL) : WebInspector.UIString("(program)"));
662
663         function optionCompare(a, b)
664         {
665             var aTitle = a.text.toLowerCase();
666             var bTitle = b.text.toLowerCase();
667             if (aTitle < bTitle)
668                 return -1;
669             else if (aTitle > bTitle)
670                 return 1;
671
672             var aSourceID = a.representedObject.sourceID;
673             var bSourceID = b.representedObject.sourceID;
674             if (aSourceID < bSourceID)
675                 return -1;
676             else if (aSourceID > bSourceID)
677                 return 1;
678             return 0;
679         }
680
681         var insertionIndex = insertionIndexForObjectInListSortedByFunction(option, select.childNodes, optionCompare);
682         if (insertionIndex < 0)
683             select.appendChild(option);
684         else
685             select.insertBefore(option, select.childNodes.item(insertionIndex));
686
687         script.filesSelectOption = option;
688
689         // Call _showScriptOrResource if the option we just appended ended up being selected.
690         // This will happen for the first item added to the menu.
691         if (select.options[select.selectedIndex] === option)
692             this._showScriptOrResource(option.representedObject);
693     },
694
695     _clearCurrentExecutionLine: function()
696     {
697         if (this._executionSourceFrame)
698             this._executionSourceFrame.executionLine = 0;
699         delete this._executionSourceFrame;
700     },
701
702     _callFrameSelected: function()
703     {
704         this._clearCurrentExecutionLine();
705
706         var callStackPane = this.sidebarPanes.callstack;
707         var currentFrame = callStackPane.selectedCallFrame;
708         if (!currentFrame)
709             return;
710
711         this.sidebarPanes.scopechain.update(currentFrame);
712
713         var scriptOrResource = this._sourceIDMap[currentFrame.sourceID];
714         this._showScriptOrResource(scriptOrResource, currentFrame.line);
715
716         this._executionSourceFrame = this._sourceFrameForScriptOrResource(scriptOrResource);
717         if (this._executionSourceFrame)
718             this._executionSourceFrame.executionLine = currentFrame.line;
719     },
720
721     _changeVisibleFile: function(event)
722     {
723         var select = this.filesSelectElement;
724         this._showScriptOrResource(select.options[select.selectedIndex].representedObject);
725     },
726
727     _startSidebarResizeDrag: function(event)
728     {
729         WebInspector.elementDragStart(this.sidebarElement, this._sidebarResizeDrag.bind(this), this._endSidebarResizeDrag.bind(this), event, "col-resize");
730
731         if (event.target === this.sidebarResizeWidgetElement)
732             this._dragOffset = (event.target.offsetWidth - (event.pageX - event.target.totalOffsetLeft));
733         else
734             this._dragOffset = 0;
735     },
736
737     _endSidebarResizeDrag: function(event)
738     {
739         WebInspector.elementDragEnd(event);
740
741         delete this._dragOffset;
742     },
743
744     _sidebarResizeDrag: function(event)
745     {
746         var x = event.pageX + this._dragOffset;
747         var newWidth = Number.constrain(window.innerWidth - x, Preferences.minScriptsSidebarWidth, window.innerWidth * 0.66);
748
749         this.sidebarElement.style.width = newWidth + "px";
750         this.sidebarButtonsElement.style.width = newWidth + "px";
751         this.viewsContainerElement.style.right = newWidth + "px";
752         this.sidebarResizeWidgetElement.style.right = newWidth + "px";
753         this.sidebarResizeElement.style.right = (newWidth - 3) + "px";
754
755         event.preventDefault();
756     },
757
758     _updatePauseOnExceptionsButton: function()
759     {
760         if (InspectorController.pauseOnExceptions()) {
761             this.pauseOnExceptionButton.title = WebInspector.UIString("Don't pause on exceptions.");
762             this.pauseOnExceptionButton.addStyleClass("toggled-on");
763         } else {
764             this.pauseOnExceptionButton.title = WebInspector.UIString("Pause on exceptions.");
765             this.pauseOnExceptionButton.removeStyleClass("toggled-on");
766         }
767     },
768
769     _updateDebuggerButtons: function()
770     {
771         if (InspectorController.debuggerEnabled()) {
772             this.enableToggleButton.title = WebInspector.UIString("Debugging enabled. Click to disable.");
773             this.enableToggleButton.addStyleClass("toggled-on");
774             this.pauseOnExceptionButton.removeStyleClass("hidden");
775             this.panelEnablerView.visible = false;
776         } else {
777             this.enableToggleButton.title = WebInspector.UIString("Debugging disabled. Click to enable.");
778             this.enableToggleButton.removeStyleClass("toggled-on");
779             this.pauseOnExceptionButton.addStyleClass("hidden");
780             this.panelEnablerView.visible = true;
781         }
782
783         this._updatePauseOnExceptionsButton();
784
785         if (this._paused) {
786             this.pauseButton.addStyleClass("paused");
787
788             this.pauseButton.disabled = false;
789             this.stepOverButton.disabled = false;
790             this.stepIntoButton.disabled = false;
791             this.stepOutButton.disabled = false;
792
793             this.debuggerStatusElement.textContent = WebInspector.UIString("Paused");
794         } else {
795             this.pauseButton.removeStyleClass("paused");
796
797             this.pauseButton.disabled = this._waitingToPause;
798             this.stepOverButton.disabled = true;
799             this.stepIntoButton.disabled = true;
800             this.stepOutButton.disabled = true;
801
802             if (this._waitingToPause)
803                 this.debuggerStatusElement.textContent = WebInspector.UIString("Pausing");
804             else if (this._stepping)
805                 this.debuggerStatusElement.textContent = WebInspector.UIString("Stepping");
806             else
807                 this.debuggerStatusElement.textContent = "";
808         }
809     },
810
811     _updateBackAndForwardButtons: function()
812     {
813         this.backButton.disabled = this._currentBackForwardIndex <= 0;
814         this.forwardButton.disabled = this._currentBackForwardIndex >= (this._backForwardList.length - 1);
815     },
816
817     _clearInterface: function()
818     {
819         this.sidebarPanes.callstack.update(null);
820         this.sidebarPanes.scopechain.update(null);
821
822         this._clearCurrentExecutionLine();
823         this._updateDebuggerButtons();
824     },
825
826     _goBack: function()
827     {
828         if (this._currentBackForwardIndex <= 0) {
829             console.error("Can't go back from index " + this._currentBackForwardIndex);
830             return;
831         }
832
833         this._showScriptOrResource(this._backForwardList[--this._currentBackForwardIndex], null, false, true);
834         this._updateBackAndForwardButtons();
835     },
836
837     _goForward: function()
838     {
839         if (this._currentBackForwardIndex >= this._backForwardList.length - 1) {
840             console.error("Can't go forward from index " + this._currentBackForwardIndex);
841             return;
842         }
843
844         this._showScriptOrResource(this._backForwardList[++this._currentBackForwardIndex], null, false, true);
845         this._updateBackAndForwardButtons();
846     },
847
848     _enableDebugging: function()
849     {
850         if (InspectorController.debuggerEnabled())
851             return;
852         this._toggleDebugging(this.panelEnablerView.alwaysEnabled);
853     },
854
855     _toggleDebugging: function(optionalAlways)
856     {
857         this._paused = false;
858         this._waitingToPause = false;
859         this._stepping = false;
860
861         if (InspectorController.debuggerEnabled())
862             InspectorController.disableDebugger(true);
863         else
864             InspectorController.enableDebuggerFromFrontend(!!optionalAlways);
865     },
866
867     _togglePauseOnExceptions: function()
868     {
869         InspectorController.setPauseOnExceptions(!InspectorController.pauseOnExceptions());
870         this._updatePauseOnExceptionsButton();
871     },
872
873     _togglePause: function()
874     {
875         if (this._paused) {
876             this._paused = false;
877             this._waitingToPause = false;
878             InspectorController.resumeDebugger();
879         } else {
880             this._stepping = false;
881             this._waitingToPause = true;
882             InspectorController.pauseInDebugger();
883         }
884
885         this._clearInterface();
886     },
887
888     _stepOverClicked: function()
889     {
890         this._paused = false;
891         this._stepping = true;
892
893         this._clearInterface();
894
895         InspectorController.stepOverStatementInDebugger();
896     },
897
898     _stepIntoClicked: function()
899     {
900         this._paused = false;
901         this._stepping = true;
902
903         this._clearInterface();
904
905         InspectorController.stepIntoStatementInDebugger();
906     },
907
908     _stepOutClicked: function()
909     {
910         this._paused = false;
911         this._stepping = true;
912
913         this._clearInterface();
914
915         InspectorController.stepOutOfFunctionInDebugger();
916     }
917 }
918
919 WebInspector.ScriptsPanel.prototype.__proto__ = WebInspector.Panel.prototype;