Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / Drawer.js
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
3  * Copyright (C) 2009 Joseph Pecoraro
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  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /**
31  * @constructor
32  * @extends {WebInspector.View}
33  * @param {!WebInspector.SplitView} splitView
34  */
35 WebInspector.Drawer = function(splitView)
36 {
37     WebInspector.View.call(this);
38     this.element.id = "drawer-contents";
39
40     this._splitView = splitView;
41     splitView.hideDefaultResizer();
42     this.show(splitView.sidebarElement());
43     splitView.hideSidebar();
44
45     this._toggleDrawerButton = new WebInspector.StatusBarButton(WebInspector.UIString("Show drawer."), "console-status-bar-item");
46     this._toggleDrawerButton.addEventListener("click", this.toggle, this);
47
48     this._tabbedPane = new WebInspector.TabbedPane();
49     this._tabbedPane.closeableTabs = false;
50     this._tabbedPane.setRetainTabOrder(true, WebInspector.moduleManager.orderComparator(WebInspector.Drawer.ViewFactory, "name", "order"));
51
52     this._tabbedPane.addEventListener(WebInspector.TabbedPane.EventTypes.TabSelected, this._tabSelected, this);
53     splitView.installResizer(this._tabbedPane.headerElement());
54     this._showDrawerOnLoadSetting = WebInspector.settings.createSetting("WebInspector.Drawer.showOnLoad", false);
55     this._lastSelectedViewSetting = WebInspector.settings.createSetting("WebInspector.Drawer.lastSelectedView", "console");
56     this._initializeViewFactories();
57 }
58
59 WebInspector.Drawer.prototype = {
60     _initializeViewFactories: function()
61     {
62         this._viewFactories = {};
63         var extensions = WebInspector.moduleManager.extensions(WebInspector.Drawer.ViewFactory);
64
65         for (var i = 0; i < extensions.length; ++i) {
66             var descriptor = extensions[i].descriptor();
67             var id = descriptor["name"];
68             var title = WebInspector.UIString(descriptor["title"]);
69             var settingName = descriptor["setting"];
70             var setting = settingName ? /** @type {!WebInspector.Setting|undefined} */ (WebInspector.settings[settingName]) : null;
71
72             this._viewFactories[id] = extensions[i];
73
74             if (setting) {
75                 setting.addChangeListener(this._toggleSettingBasedView.bind(this, id, title, setting));
76                 if (setting.get())
77                     this._tabbedPane.appendTab(id, title, new WebInspector.View());
78             } else {
79                 this._tabbedPane.appendTab(id, title, new WebInspector.View());
80             }
81         }
82         this._tabbedPane.show(this.element);
83     },
84
85     /**
86      * @param {string} id
87      * @param {string} title
88      * @param {!WebInspector.Setting} setting
89      */
90     _toggleSettingBasedView: function(id, title, setting)
91     {
92         this._tabbedPane.closeTab(id);
93         if (setting.get())
94             this._tabbedPane.appendTab(id, title, new WebInspector.View());
95     },
96
97     /**
98      * @return {!Element}
99      */
100     toggleButtonElement: function()
101     {
102         return this._toggleDrawerButton.element;
103     },
104
105     /**
106      * @param {string} tabId
107      * @param {string} title
108      * @param {!WebInspector.View} view
109      */
110     _addView: function(tabId, title, view)
111     {
112         if (!this._tabbedPane.hasTab(tabId)) {
113             this._tabbedPane.appendTab(tabId, title, view,  undefined, false);
114         } else {
115             this._tabbedPane.changeTabTitle(tabId, title);
116             this._tabbedPane.changeTabView(tabId, view);
117         }
118     },
119
120     /**
121      * @param {string} id
122      */
123     closeView: function(id)
124     {
125         this._tabbedPane.closeTab(id);
126     },
127
128     /**
129      * @param {string} id
130      * @param {boolean=} immediate
131      */
132     showView: function(id, immediate)
133     {
134         if (!this._toggleDrawerButton.enabled())
135             return;
136         if (!this._tabbedPane.hasTab(id)) {
137             // Hidden tab.
138             this._innerShow(immediate);
139             return;
140         }
141         var viewFactory = this._viewFactory(id);
142         if (viewFactory)
143             this._tabbedPane.changeTabView(id, viewFactory.createView());
144         this._innerShow(immediate);
145         this._tabbedPane.selectTab(id, true);
146         // In case this id is already selected, anyways persist it as the last saved value.
147         this._lastSelectedViewSetting.set(id);
148     },
149
150     /**
151      * @param {string} id
152      * @param {string} title
153      * @param {!WebInspector.View} view
154      */
155     showCloseableView: function(id, title, view)
156     {
157         if (!this._toggleDrawerButton.enabled())
158             return;
159         if (!this._tabbedPane.hasTab(id)) {
160             this._tabbedPane.appendTab(id, title, view, undefined, false, true);
161         } else {
162             this._tabbedPane.changeTabView(id, view);
163             this._tabbedPane.changeTabTitle(id, title);
164         }
165         this._innerShow();
166         this._tabbedPane.selectTab(id, true);
167     },
168
169     showDrawer: function()
170     {
171         this.showView(this._lastSelectedViewSetting.get());
172     },
173
174     showOnLoadIfNecessary: function()
175     {
176         if (this._showDrawerOnLoadSetting.get())
177             this.showView(this._lastSelectedViewSetting.get(), true);
178     },
179
180     /**
181      * @param {boolean=} immediate
182      */
183     _innerShow: function(immediate)
184     {
185         if (this._toggleDrawerButton.toggled)
186             return;
187
188         this._showDrawerOnLoadSetting.set(true);
189         this._toggleDrawerButton.toggled = true;
190         this._toggleDrawerButton.title = WebInspector.UIString("Hide drawer.");
191
192         this._splitView.showBoth(!immediate);
193
194         if (this._visibleView())
195             this._visibleView().focus();
196     },
197
198     closeDrawer: function()
199     {
200         if (!this._toggleDrawerButton.toggled)
201             return;
202         this._showDrawerOnLoadSetting.set(false);
203         this._toggleDrawerButton.toggled = false;
204         this._toggleDrawerButton.title = WebInspector.UIString("Show console.");
205
206         WebInspector.restoreFocusFromElement(this.element);
207         this._splitView.hideSidebar(true);
208     },
209
210     /**
211      * @return {!WebInspector.View} view
212      */
213     _visibleView: function()
214     {
215         return this._tabbedPane.visibleView;
216     },
217
218     /**
219      * @param {!WebInspector.Event} event
220      */
221     _tabSelected: function(event)
222     {
223         var tabId = this._tabbedPane.selectedTabId;
224         if (event.data["isUserGesture"] && !this._tabbedPane.isTabCloseable(tabId))
225             this._lastSelectedViewSetting.set(tabId);
226         var viewFactory = this._viewFactory(tabId);
227         if (viewFactory)
228             this._tabbedPane.changeTabView(tabId, viewFactory.createView());
229     },
230
231     toggle: function()
232     {
233         if (this._toggleDrawerButton.toggled)
234             this.closeDrawer();
235         else
236             this.showDrawer();
237     },
238
239     /**
240      * @return {boolean}
241      */
242     visible: function()
243     {
244         return this._toggleDrawerButton.toggled;
245     },
246
247     /**
248      * @return {string}
249      */
250     selectedViewId: function()
251     {
252         return this._tabbedPane.selectedTabId;
253     },
254
255     /**
256      * @return {?WebInspector.Drawer.ViewFactory}
257      */
258     _viewFactory: function(id)
259     {
260         return this._viewFactories[id] ? /** @type {!WebInspector.Drawer.ViewFactory} */ (this._viewFactories[id].instance()) : null;
261     },
262
263     __proto__: WebInspector.View.prototype
264 }
265
266 /**
267  * @interface
268  */
269 WebInspector.Drawer.ViewFactory = function()
270 {
271 }
272
273 WebInspector.Drawer.ViewFactory.prototype = {
274     /**
275      * @return {!WebInspector.View}
276      */
277     createView: function() {}
278 }
279
280 /**
281  * @constructor
282  * @implements {WebInspector.Drawer.ViewFactory}
283  * @param {function(new:T)} constructor
284  * @template T
285  */
286 WebInspector.Drawer.SingletonViewFactory = function(constructor)
287 {
288     this._constructor = constructor;
289 }
290
291 WebInspector.Drawer.SingletonViewFactory.prototype = {
292     /**
293      * @return {!WebInspector.View}
294      */
295     createView: function()
296     {
297         if (!this._instance)
298             this._instance = /** @type {!WebInspector.View} */(new this._constructor());
299         return this._instance;
300     }
301 }