Reviewed by Adam.
[WebKit-https.git] / WebCore / page / inspector / Panel.js
1 /*
2  * Copyright (C) 2007 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  *
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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 WebInspector.Panel = function(views)
30 {
31     this._visible = false;
32
33     this.element = document.createElement("div");
34     this.element.className = "panel";
35
36     this.views = {};
37     this.viewButtons = [];
38
39     if (views) {
40         var selectViewFunction = function(event)
41         {
42             var clickedView = event.currentTarget.view;
43             clickedView.panel.currentView = clickedView;
44         };
45
46         for (var i = 0; i < views.length; ++i) {
47             var view = views[i];
48             view.panel = this;
49
50             view.buttonElement = document.createElement("button");
51             view.buttonElement.setAttribute("title", view.title);
52             view.buttonElement.addEventListener("click", selectViewFunction, false);
53             view.buttonElement.appendChild(document.createElement("img"));
54             view.buttonElement.view = view;
55
56             view.contentElement = document.createElement("div");
57             view.contentElement.className = "content " + view.title.toLowerCase();
58
59             this.views[view.title.toLowerCase()] = view;
60             this.viewButtons.push(view.buttonElement);
61             this.element.appendChild(view.contentElement);
62         }
63     }
64 }
65
66 WebInspector.Panel.prototype = {
67     show: function()
68     {
69         this._visible = true;
70         if (!this.element.parentNode)
71             this.attach();
72         this.element.addStyleClass("selected");
73         this.updateToolbar();
74         if (this.currentView && this.currentView.show)
75             this.currentView.show();
76     },
77
78     hide: function()
79     {
80         if (this.currentView && this.currentView.hide)
81             this.currentView.hide();
82         document.getElementById("toolbarButtons").removeChildren();
83         this.element.removeStyleClass("selected");
84         this._visible = false;
85     },
86
87     updateToolbar: function()
88     {
89         var buttonContainer = document.getElementById("toolbarButtons");
90         buttonContainer.removeChildren();
91
92         var buttons = this.viewButtons;
93         if (buttons.length < 2)
94             return;
95
96         for (var i = 0; i < buttons.length; ++i) {
97             var button = buttons[i];
98
99             if (i === 0)
100                 button.addStyleClass("first");
101             else if (i === (buttons.length - 1))
102                 button.addStyleClass("last");
103
104             if (i) {
105                 var divider = document.createElement("img");
106                 divider.className = "split-button-divider";
107                 buttonContainer.appendChild(divider);
108             }
109
110             button.addStyleClass("split-button");
111             button.addStyleClass("view-button-" + button.title.toLowerCase());
112
113             buttonContainer.appendChild(button);
114         }
115     },
116
117     attach: function()
118     {
119         document.getElementById("main").appendChild(this.element);
120     },
121
122     detach: function()
123     {
124         if (WebInspector.currentPanel === this)
125             WebInspector.currentPanel = null;
126         if (this.element && this.element.parentNode)
127             this.element.parentNode.removeChild(this.element);
128     },
129
130     get currentView()
131     {
132         return this._currentView;
133     },
134
135     set currentView(x)
136     {
137         if (typeof x === "string" || x instanceof String)
138             x = this.views[x];
139
140         if (this._currentView === x)
141             return;
142
143         if (this._currentView) {
144             this._currentView.buttonElement.removeStyleClass("selected");
145             this._currentView.contentElement.removeStyleClass("selected");
146             if (this._currentView.hide)
147                 this._currentView.hide();
148         }
149
150         this._currentView = x;
151
152         if (x) {
153             x.buttonElement.addStyleClass("selected");
154             x.contentElement.addStyleClass("selected");
155             if (x.show)
156                 x.show();
157             if (x.panel !== this)
158                 InspectorController.log("Set currentView to a view " + x.title + " whose panel is not this panel");
159         }
160     },
161
162     get visible()
163     {
164         return this._visible;
165     },
166
167     set visible(x)
168     {
169         if (this._visible === x)
170             return;
171
172         if (x)
173             this.show();
174         else
175             this.hide();
176     }
177 }