d49b04e33460029c691c05af9288083c5fab0f0f
[WebKit-https.git] / Source / WebCore / inspector / front-end / InspectorView.js
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.View}
34  */
35 WebInspector.InspectorView = function()
36 {
37     WebInspector.View.call(this);
38     this.markAsRoot();
39     this.element.id = "main-panels";
40     this.element.setAttribute("spellcheck", false);
41     this._history = [];
42     this._historyIterator = -1;
43     document.addEventListener("keydown", this._keyDown.bind(this), false);
44     document.addEventListener("keypress", this._keyPress.bind(this), false);
45     this._panelOrder = [];
46     this._panelDescriptors = {};
47
48     // Windows and Mac have two different definitions of '[' and ']', so accept both of each.
49     this._openBracketIdentifiers = ["U+005B", "U+00DB"].keySet();
50     this._closeBracketIdentifiers = ["U+005D", "U+00DD"].keySet();
51     this._footerElementContainer = this.element.createChild("div", "inspector-footer status-bar hidden");
52     this._panelsElement = this.element.createChild("div", "fill");
53 }
54
55 WebInspector.InspectorView.Events = {
56     PanelSelected: "PanelSelected"
57 }
58
59 WebInspector.InspectorView.prototype = {
60     /**
61      * @param {WebInspector.PanelDescriptor} panelDescriptor
62      */
63     addPanel: function(panelDescriptor)
64     {
65         this._panelOrder.push(panelDescriptor.name());
66         this._panelDescriptors[panelDescriptor.name()] = panelDescriptor;
67         WebInspector.toolbar.addPanel(panelDescriptor);
68     },
69
70     /**
71      * @param {string} panelName
72      * @return {?WebInspector.Panel}
73      */
74     panel: function(panelName)
75     {
76         var panelDescriptor = this._panelDescriptors[panelName];
77         if (!panelDescriptor && this._panelOrder.length)
78             panelDescriptor = this._panelDescriptors[this._panelOrder[0]];
79         return panelDescriptor ? panelDescriptor.panel() : null;
80     },
81
82     /**
83      * @param {string} panelName
84      * @return {?WebInspector.Panel}
85      */
86     showPanel: function(panelName)
87     {
88         var panel = this.panel(panelName);
89         if (panel)
90             this.setCurrentPanel(panel);
91         return panel;
92     },
93
94     currentPanel: function()
95     {
96         return this._currentPanel;
97     },
98
99     setCurrentPanel: function(x)
100     {
101         if (this._currentPanel === x)
102             return;
103
104         if (this._currentPanel)
105             this._currentPanel.detach();
106
107         this._currentPanel = x;
108
109         if (x) {
110             x.show();
111             this.dispatchEventToListeners(WebInspector.InspectorView.Events.PanelSelected);
112             // FIXME: remove search controller.
113             WebInspector.searchController.cancelSearch();
114         }
115         for (var panelName in WebInspector.panels) {
116             if (WebInspector.panels[panelName] === x) {
117                 WebInspector.settings.lastActivePanel.set(panelName);
118                 this._pushToHistory(panelName);
119                 WebInspector.userMetrics.panelShown(panelName);
120             }
121         }
122     },
123
124     _keyPress: function(event)
125     {
126         clearTimeout(this._keyDownTimer);
127         delete this._keyDownTimer;
128     },
129
130     _keyDown: function(event)
131     {
132         // BUG85312: On French AZERTY keyboards, AltGr-]/[ combinations (synonymous to Ctrl-Alt-]/[ on Windows) are used to enter ]/[,
133         // so for a ]/[-related keydown we delay the panel switch using a timer, to see if there is a keypress event following this one.
134         // If there is, we cancel the timer and do not consider this a panel switch.
135         if (!WebInspector.isWin() || (!this._openBracketIdentifiers.hasOwnProperty(event.keyIdentifier) && !this._closeBracketIdentifiers.hasOwnProperty(event.keyIdentifier))) {
136             this._keyDownInternal(event);
137             return;
138         }
139
140         this._keyDownTimer = setTimeout(this._keyDownInternal.bind(this, event), 0);
141     },
142
143     _keyDownInternal: function(event)
144     {
145         if (this._openBracketIdentifiers.hasOwnProperty(event.keyIdentifier)) {
146             var isRotateLeft = WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event) && !event.shiftKey && !event.altKey;
147             if (isRotateLeft) {
148                 var index = this._panelOrder.indexOf(this.currentPanel().name);
149                 index = (index === 0) ? this._panelOrder.length - 1 : index - 1;
150                 this.showPanel(this._panelOrder[index]);
151                 event.consume(true);
152                 return;
153             }
154
155             var isGoBack = WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event) && event.altKey;
156             if (isGoBack && this._canGoBackInHistory()) {
157                 this._goBackInHistory();
158                 event.consume(true);
159             }
160             return;
161         }
162
163         if (this._closeBracketIdentifiers.hasOwnProperty(event.keyIdentifier)) {
164             var isRotateRight = WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event) && !event.shiftKey && !event.altKey;
165             if (isRotateRight) {
166                 var index = this._panelOrder.indexOf(this.currentPanel().name);
167                 index = (index + 1) % this._panelOrder.length;
168                 this.showPanel(this._panelOrder[index]);
169                 event.consume(true);
170                 return;
171             }
172
173             var isGoForward = WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event) && event.altKey;
174             if (isGoForward && this._canGoForwardInHistory()) {
175                 this._goForwardInHistory();
176                 event.consume(true);
177             }
178             return;
179         }
180     },
181
182     _canGoBackInHistory: function()
183     {
184         return this._historyIterator > 0;
185     },
186
187     _goBackInHistory: function()
188     {
189         this._inHistory = true;
190         this.setCurrentPanel(WebInspector.panels[this._history[--this._historyIterator]]);
191         delete this._inHistory;
192     },
193
194     _canGoForwardInHistory: function()
195     {
196         return this._historyIterator < this._history.length - 1;
197     },
198
199     _goForwardInHistory: function()
200     {
201         this._inHistory = true;
202         this.setCurrentPanel(WebInspector.panels[this._history[++this._historyIterator]]);
203         delete this._inHistory;
204     },
205
206     _pushToHistory: function(panelName)
207     {
208         if (this._inHistory)
209             return;
210
211         this._history.splice(this._historyIterator + 1, this._history.length - this._historyIterator - 1);
212         if (!this._history.length || this._history[this._history.length - 1] !== panelName)
213             this._history.push(panelName);
214         this._historyIterator = this._history.length - 1;
215     },
216
217     panelsElement: function()
218     {
219         return this._panelsElement;
220     },
221
222     /**
223      * @param {Element?} element
224      */
225     setFooterElement: function(element)
226     {
227         if (element) {
228             this._footerElementContainer.removeStyleClass("hidden");
229             this._footerElementContainer.appendChild(element);
230             this._panelsElement.style.bottom = this._footerElementContainer.offsetHeight + "px";
231         } else {
232             this._footerElementContainer.addStyleClass("hidden");
233             this._footerElementContainer.removeChildren();
234             this._panelsElement.style.bottom = 0;
235         }
236         this.doResize();
237     }
238 }
239
240 WebInspector.InspectorView.prototype.__proto__ = WebInspector.View.prototype;
241
242 /**
243  * @type {WebInspector.InspectorView}
244  */
245 WebInspector.inspectorView = null;