Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / sources / ScopeChainSidebarPane.js
blobacaaa19b7e5520d10689b2fc5fa8b88327fb6160
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2011 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
27 /**
28  * @constructor
29  * @extends {WebInspector.SidebarPane}
30  */
31 WebInspector.ScopeChainSidebarPane = function()
33     WebInspector.SidebarPane.call(this, WebInspector.UIString("Scope"));
34     this._sections = [];
35     /** @type {!Set.<?string>} */
36     this._expandedSections = new Set();
37     /** @type {!Set.<string>} */
38     this._expandedProperties = new Set();
41 WebInspector.ScopeChainSidebarPane._pathSymbol = Symbol("path");
43 WebInspector.ScopeChainSidebarPane.prototype = {
44     /**
45      * @param {?WebInspector.DebuggerModel.CallFrame} callFrame
46      */
47     update: function(callFrame)
48     {
49         this.element.removeChildren();
51         if (!callFrame) {
52             var infoElement = createElement("div");
53             infoElement.className = "info";
54             infoElement.textContent = WebInspector.UIString("Not Paused");
55             this.element.appendChild(infoElement);
56             return;
57         }
59         for (var i = 0; i < this._sections.length; ++i) {
60             var section = this._sections[i];
61             if (!section.title)
62                 continue;
63             if (section.expanded)
64                 this._expandedSections.add(section.title);
65             else
66                 this._expandedSections.delete(section.title);
67         }
69         this._sections = [];
71         var foundLocalScope = false;
72         var scopeChain = callFrame.scopeChain();
73         for (var i = 0; i < scopeChain.length; ++i) {
74             var scope = scopeChain[i];
75             var title = null;
76             var emptyPlaceholder = null;
77             var extraProperties = [];
79             switch (scope.type()) {
80             case DebuggerAgent.ScopeType.Local:
81                 foundLocalScope = true;
82                 title = WebInspector.UIString("Local");
83                 emptyPlaceholder = WebInspector.UIString("No Variables");
84                 var thisObject = callFrame.thisObject();
85                 if (thisObject)
86                     extraProperties.push(new WebInspector.RemoteObjectProperty("this", thisObject));
87                 if (i == 0) {
88                     var details = callFrame.debuggerModel.debuggerPausedDetails();
89                     if (!callFrame.isAsync()) {
90                         var exception = details.exception();
91                         if (exception)
92                             extraProperties.push(new WebInspector.RemoteObjectProperty("<exception>", exception));
93                     }
94                     var returnValue = callFrame.returnValue();
95                     if (returnValue)
96                         extraProperties.push(new WebInspector.RemoteObjectProperty("<return>", returnValue));
97                 }
98                 break;
99             case DebuggerAgent.ScopeType.Closure:
100                 title = WebInspector.UIString("Closure");
101                 emptyPlaceholder = WebInspector.UIString("No Variables");
102                 break;
103             case DebuggerAgent.ScopeType.Catch:
104                 title = WebInspector.UIString("Catch");
105                 break;
106             case DebuggerAgent.ScopeType.Block:
107                 title = WebInspector.UIString("Block");
108                 break;
109             case DebuggerAgent.ScopeType.Script:
110                 title = WebInspector.UIString("Script");
111                 break;
112             case DebuggerAgent.ScopeType.With:
113                 title = WebInspector.UIString("With Block");
114                 break;
115             case DebuggerAgent.ScopeType.Global:
116                 title = WebInspector.UIString("Global");
117                 break;
118             }
120             var subtitle = scope.description();
121             if (!title || title === subtitle)
122                 subtitle = undefined;
124             var titleElement = createElementWithClass("div");
125             titleElement.createChild("div", "scope-chain-sidebar-pane-section-subtitle").textContent = subtitle;
126             titleElement.createChild("div", "scope-chain-sidebar-pane-section-title").textContent = title;
128             var section = new WebInspector.ObjectPropertiesSection(scope.object(), titleElement, emptyPlaceholder, true, extraProperties);
129             section[WebInspector.ScopeChainSidebarPane._pathSymbol] = title + ":" + (subtitle ? subtitle + ":" : "");
130             section.addEventListener(TreeOutline.Events.ElementAttached, this._elementAttached, this);
131             section.addEventListener(TreeOutline.Events.ElementExpanded, this._elementExpanded, this);
132             section.addEventListener(TreeOutline.Events.ElementCollapsed, this._elementCollapsed, this);
134             if (scope.type() === DebuggerAgent.ScopeType.Global)
135                 section.objectTreeElement().collapse();
136             else if (!foundLocalScope || scope.type() === DebuggerAgent.ScopeType.Local || this._expandedSections.has(title))
137                 section.objectTreeElement().expand();
139             section.element.classList.add("scope-chain-sidebar-pane-section");
140             this._sections.push(section);
141             this.element.appendChild(section.element);
142         }
143     },
145     /**
146      * @param {!WebInspector.Event} event
147      */
148     _elementAttached: function(event)
149     {
150         var element = /** @type {!WebInspector.ObjectPropertyTreeElement} */ (event.data);
151         if (element.isExpandable() && this._expandedProperties.has(this._propertyPath(element)))
152             element.expand();
153     },
155     /**
156      * @param {!WebInspector.Event} event
157      */
158     _elementExpanded: function(event)
159     {
160         var element = /** @type {!WebInspector.ObjectPropertyTreeElement} */ (event.data);
161         this._expandedProperties.add(this._propertyPath(element));
162     },
164     /**
165      * @param {!WebInspector.Event} event
166      */
167     _elementCollapsed: function(event)
168     {
169         var element = /** @type {!WebInspector.ObjectPropertyTreeElement} */ (event.data);
170         this._expandedProperties.delete(this._propertyPath(element));
171     },
173     /**
174      * @param {!WebInspector.ObjectPropertyTreeElement} treeElement
175      * @return {string}
176      */
177     _propertyPath: function(treeElement)
178     {
179         return treeElement.treeOutline[WebInspector.ScopeChainSidebarPane._pathSymbol] + WebInspector.ObjectPropertyTreeElement.prototype.propertyPath.call(treeElement);
180     },
182     __proto__: WebInspector.SidebarPane.prototype