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/**
27 * @constructor
28 * @extends {WebInspector.SidebarPane}
29 */
30WebInspector.CallStackSidebarPane = function()
31{
32    WebInspector.SidebarPane.call(this, WebInspector.UIString("Call Stack"));
33    this._model = WebInspector.debuggerModel;
34
35    this.bodyElement.addEventListener("keydown", this._keyDown.bind(this), true);
36    this.bodyElement.tabIndex = 0;
37}
38
39WebInspector.CallStackSidebarPane.prototype = {
40    update: function(callFrames)
41    {
42        this.bodyElement.removeChildren();
43        delete this._statusMessageElement;
44        this.placards = [];
45
46        if (!callFrames) {
47            var infoElement = document.createElement("div");
48            infoElement.className = "info";
49            infoElement.textContent = WebInspector.UIString("Not Paused");
50            this.bodyElement.appendChild(infoElement);
51            return;
52        }
53
54        for (var i = 0; i < callFrames.length; ++i) {
55            var callFrame = callFrames[i];
56            var placard = new WebInspector.CallStackSidebarPane.Placard(callFrame, this);
57            placard.element.addEventListener("click", this._placardSelected.bind(this, placard), false);
58            this.placards.push(placard);
59            this.bodyElement.appendChild(placard.element);
60        }
61    },
62
63    setSelectedCallFrame: function(x)
64    {
65        for (var i = 0; i < this.placards.length; ++i) {
66            var placard = this.placards[i];
67            placard.selected = (placard._callFrame === x);
68        }
69    },
70
71    /**
72     * @param {Event=} event
73     * @return {boolean}
74     */
75    _selectNextCallFrameOnStack: function(event)
76    {
77        var index = this._selectedCallFrameIndex();
78        if (index == -1)
79            return true;
80        this._selectedPlacardByIndex(index + 1);
81        return true;
82    },
83
84    /**
85     * @param {Event=} event
86     * @return {boolean}
87     */
88    _selectPreviousCallFrameOnStack: function(event)
89    {
90        var index = this._selectedCallFrameIndex();
91        if (index == -1)
92            return true;
93        this._selectedPlacardByIndex(index - 1);
94        return true;
95    },
96
97    /**
98     * @param {number} index
99     */
100    _selectedPlacardByIndex: function(index)
101    {
102        if (index < 0 || index >= this.placards.length)
103            return;
104        this._placardSelected(this.placards[index])
105    },
106
107    /**
108     * @return {number}
109     */
110    _selectedCallFrameIndex: function()
111    {
112        if (!this._model.selectedCallFrame())
113            return -1;
114        for (var i = 0; i < this.placards.length; ++i) {
115            var placard = this.placards[i];
116            if (placard._callFrame === this._model.selectedCallFrame())
117                return i;
118        }
119        return -1;
120    },
121
122    _placardSelected: function(placard)
123    {
124        this._model.setSelectedCallFrame(placard._callFrame);
125    },
126
127    _copyStackTrace: function()
128    {
129        var text = "";
130        for (var i = 0; i < this.placards.length; ++i)
131            text += this.placards[i].title + " (" + this.placards[i].subtitle + ")\n";
132        InspectorFrontendHost.copyText(text);
133    },
134
135    /**
136     * @param {function(!Array.<!WebInspector.KeyboardShortcut.Descriptor>, function(Event=):boolean)} registerShortcutDelegate
137     */
138    registerShortcuts: function(registerShortcutDelegate)
139    {
140        registerShortcutDelegate(WebInspector.ScriptsPanelDescriptor.ShortcutKeys.NextCallFrame, this._selectNextCallFrameOnStack.bind(this));
141        registerShortcutDelegate(WebInspector.ScriptsPanelDescriptor.ShortcutKeys.PrevCallFrame, this._selectPreviousCallFrameOnStack.bind(this));
142    },
143
144    setStatus: function(status)
145    {
146        if (!this._statusMessageElement) {
147            this._statusMessageElement = document.createElement("div");
148            this._statusMessageElement.className = "info";
149            this.bodyElement.appendChild(this._statusMessageElement);
150        }
151        if (typeof status === "string")
152            this._statusMessageElement.textContent = status;
153        else {
154            this._statusMessageElement.removeChildren();
155            this._statusMessageElement.appendChild(status);
156        }
157    },
158
159    _keyDown: function(event)
160    {
161        if (event.altKey || event.shiftKey || event.metaKey || event.ctrlKey)
162            return;
163
164        if (event.keyIdentifier === "Up") {
165            this._selectPreviousCallFrameOnStack();
166            event.consume();
167        } else if (event.keyIdentifier === "Down") {
168            this._selectNextCallFrameOnStack();
169            event.consume();
170        }
171    },
172
173    __proto__: WebInspector.SidebarPane.prototype
174}
175
176/**
177 * @constructor
178 * @extends {WebInspector.Placard}
179 * @param {WebInspector.DebuggerModel.CallFrame} callFrame
180 * @param {WebInspector.CallStackSidebarPane} pane
181 */
182WebInspector.CallStackSidebarPane.Placard = function(callFrame, pane)
183{
184    WebInspector.Placard.call(this, callFrame.functionName || WebInspector.UIString("(anonymous function)"), "");
185    callFrame.createLiveLocation(this._update.bind(this));
186    this.element.addEventListener("contextmenu", this._placardContextMenu.bind(this), true);
187    this._callFrame = callFrame;
188    this._pane = pane;
189}
190
191WebInspector.CallStackSidebarPane.Placard.prototype = {
192    _update: function(uiLocation)
193    {
194        this.subtitle = WebInspector.formatLinkText(uiLocation.uiSourceCode.originURL(), uiLocation.lineNumber).centerEllipsizedToLength(100);
195    },
196
197    _placardContextMenu: function(event)
198    {
199        var contextMenu = new WebInspector.ContextMenu(event);
200
201        if (WebInspector.debuggerModel.canSetScriptSource()) {
202            contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Restart frame" : "Restart Frame"), this._restartFrame.bind(this));
203            contextMenu.appendSeparator();
204        }
205        contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Copy stack trace" : "Copy Stack Trace"), this._pane._copyStackTrace.bind(this._pane));
206
207        contextMenu.show();
208    },
209
210    _restartFrame: function()
211    {
212        this._callFrame.restart(undefined);
213    },
214
215    __proto__: WebInspector.Placard.prototype
216}
217