• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26WebInspector.CallStackSidebarPane = function()
27{
28    WebInspector.SidebarPane.call(this, WebInspector.UIString("Call Stack"));
29
30    this._shortcuts = {};
31
32    var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.Period,
33                                                         WebInspector.KeyboardShortcut.Modifiers.Ctrl);
34    this._shortcuts[shortcut] = this._selectNextCallFrameOnStack.bind(this);
35
36    var shortcut = WebInspector.KeyboardShortcut.makeKey(WebInspector.KeyboardShortcut.KeyCodes.Comma,
37                                                         WebInspector.KeyboardShortcut.Modifiers.Ctrl);
38    this._shortcuts[shortcut] = this._selectPreviousCallFrameOnStack.bind(this);
39}
40
41WebInspector.CallStackSidebarPane.prototype = {
42    update: function(callFrames, sourceIDMap)
43    {
44        this.bodyElement.removeChildren();
45
46        this.placards = [];
47        delete this._selectedCallFrame;
48
49        if (!callFrames) {
50            var infoElement = document.createElement("div");
51            infoElement.className = "info";
52            infoElement.textContent = WebInspector.UIString("Not Paused");
53            this.bodyElement.appendChild(infoElement);
54            return;
55        }
56
57        var title;
58        var subtitle;
59        var scriptOrResource;
60
61        for (var i = 0; i < callFrames.length; ++i) {
62            var callFrame = callFrames[i];
63            switch (callFrame.type) {
64            case "function":
65                title = callFrame.functionName || WebInspector.UIString("(anonymous function)");
66                break;
67            case "program":
68                title = WebInspector.UIString("(program)");
69                break;
70            }
71
72            scriptOrResource = sourceIDMap[callFrame.sourceID];
73            subtitle = WebInspector.displayNameForURL(scriptOrResource.sourceURL || scriptOrResource.url);
74
75            if (callFrame.line > 0) {
76                if (subtitle)
77                    subtitle += ":" + callFrame.line;
78                else
79                    subtitle = WebInspector.UIString("line %d", callFrame.line);
80            }
81
82            var placard = new WebInspector.Placard(title, subtitle);
83            placard.callFrame = callFrame;
84
85            placard.element.addEventListener("click", this._placardSelected.bind(this), false);
86
87            this.placards.push(placard);
88            this.bodyElement.appendChild(placard.element);
89        }
90    },
91
92    get selectedCallFrame()
93    {
94        return this._selectedCallFrame;
95    },
96
97    set selectedCallFrame(x)
98    {
99        if (this._selectedCallFrame === x)
100            return;
101
102        this._selectedCallFrame = x;
103
104        for (var i = 0; i < this.placards.length; ++i) {
105            var placard = this.placards[i];
106            placard.selected = (placard.callFrame === this._selectedCallFrame);
107        }
108
109        this.dispatchEventToListeners("call frame selected");
110    },
111
112    handleShortcut: function(event)
113    {
114        var shortcut = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
115        var handler = this._shortcuts[shortcut];
116        if (handler) {
117            handler(event);
118            event.handled = true;
119        }
120    },
121
122    _selectNextCallFrameOnStack: function()
123    {
124        var index = this._selectedCallFrameIndex();
125        if (index == -1)
126            return;
127        this._selectedPlacardByIndex(index + 1);
128    },
129
130    _selectPreviousCallFrameOnStack: function()
131    {
132        var index = this._selectedCallFrameIndex();
133        if (index == -1)
134            return;
135        this._selectedPlacardByIndex(index - 1);
136    },
137
138    _selectedPlacardByIndex: function(index)
139    {
140        if (index < 0 || index >= this.placards.length)
141            return;
142        var placard = this.placards[index];
143        this.selectedCallFrame = placard.callFrame
144    },
145
146    _selectedCallFrameIndex: function()
147    {
148        if (!this._selectedCallFrame)
149            return -1;
150        for (var i = 0; i < this.placards.length; ++i) {
151            var placard = this.placards[i];
152            if (placard.callFrame === this._selectedCallFrame)
153                return i;
154        }
155        return -1;
156    },
157
158    _placardSelected: function(event)
159    {
160        var placardElement = event.target.enclosingNodeOrSelfWithClass("placard");
161        this.selectedCallFrame = placardElement.placard.callFrame;
162    }
163}
164
165WebInspector.CallStackSidebarPane.prototype.__proto__ = WebInspector.SidebarPane.prototype;
166