tizen beta release
[framework/web/webkit-efl.git] / debian / libwebkit-engine / usr / share / ewebkit-0 / webinspector / Toolbar.js
1  /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc.  All rights reserved.
3  * Copyright (C) 2007 Matt Lilek (pewtermoose@gmail.com).
4  * Copyright (C) 2009 Joseph Pecoraro
5  * Copyright (C) 2011 Google Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1.  Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  * 2.  Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17  *     its contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /**
33  * @constructor
34  */
35 WebInspector.Toolbar = function()
36 {
37     this.element = document.getElementById("toolbar");
38     this.element.addEventListener("mousedown", this._toolbarDragStart.bind(this), true);
39
40     this._dropdownButton = document.getElementById("toolbar-dropdown-arrow");
41     this._dropdownButton.addEventListener("click", this._toggleDropdown.bind(this), false);
42
43     document.getElementById("close-button-left").addEventListener("click", this._onClose, true);
44     document.getElementById("close-button-right").addEventListener("click", this._onClose, true);
45 }
46
47 WebInspector.Toolbar.prototype = {
48     set attached(attached)
49     {
50         if (attached)
51             this.element.addStyleClass("toolbar-small");
52         else
53             this.element.removeStyleClass("toolbar-small");
54         this._updateDropdownButtonAndHideDropdown();
55     },
56
57     resize: function()
58     {
59         this._updateDropdownButtonAndHideDropdown();
60     },
61
62     addPanel: function(panel)
63     {
64         this.element.appendChild(panel.toolbarItem);
65         this.resize();
66     },
67
68     _toolbarDragStart: function(event)
69     {
70         if ((!WebInspector.attached && WebInspector.platformFlavor() !== WebInspector.PlatformFlavor.MacLeopard && WebInspector.platformFlavor() !== WebInspector.PlatformFlavor.MacSnowLeopard) || WebInspector.port() == "qt")
71             return;
72
73         var target = event.target;
74         if (target.hasStyleClass("toolbar-item") && target.hasStyleClass("toggleable"))
75             return;
76
77         if (target !== this.element && !target.hasStyleClass("toolbar-item"))
78             return;
79
80         this.element.lastScreenX = event.screenX;
81         this.element.lastScreenY = event.screenY;
82
83         WebInspector.elementDragStart(this.element, this._toolbarDrag.bind(this), this._toolbarDragEnd.bind(this), event, (WebInspector.attached ? "row-resize" : "default"));
84     },
85
86     _toolbarDragEnd: function(event)
87     {
88         WebInspector.elementDragEnd(event);
89
90         delete this.element.lastScreenX;
91         delete this.element.lastScreenY;
92     },
93
94     _toolbarDrag: function(event)
95     {
96         if (WebInspector.attached) {
97             var height = window.innerHeight - (event.screenY - this.element.lastScreenY);
98
99             InspectorFrontendHost.setAttachedWindowHeight(height);
100         } else {
101             var x = event.screenX - this.element.lastScreenX;
102             var y = event.screenY - this.element.lastScreenY;
103
104             // We cannot call window.moveBy here because it restricts the movement
105             // of the window at the edges.
106             InspectorFrontendHost.moveWindowBy(x, y);
107         }
108
109         this.element.lastScreenX = event.screenX;
110         this.element.lastScreenY = event.screenY;
111
112         event.preventDefault();
113     },
114
115     _onClose: function()
116     {
117         WebInspector.close();
118     },
119
120     _setDropdownVisible: function(visible)
121     {
122         if (!this._dropdown) {
123             if (!visible)
124                 return;
125             this._dropdown = new WebInspector.ToolbarDropdown();
126         }
127         if (visible)
128             this._dropdown.show();
129         else
130             this._dropdown.hide();
131     },
132
133     _toggleDropdown: function()
134     {
135         this._setDropdownVisible(!this._dropdown || !this._dropdown.visible);
136     },
137
138     _updateDropdownButtonAndHideDropdown: function()
139     {
140         this._setDropdownVisible(false);
141
142         var toolbar = document.getElementById("toolbar");
143         if (this.element.scrollHeight > this.element.clientHeight)
144             this._dropdownButton.removeStyleClass("hidden");
145         else
146             this._dropdownButton.addStyleClass("hidden");
147     }
148 }
149
150 WebInspector.Toolbar.createPanelToolbarItem = function(panel)
151 {
152     var toolbarItem = document.createElement("button");
153     toolbarItem.className = "toolbar-item toggleable";
154     toolbarItem.panel = panel;
155     toolbarItem.addStyleClass(panel._panelName);
156     function onToolbarItemClicked()
157     {
158         WebInspector.toolbar._updateDropdownButtonAndHideDropdown();
159         WebInspector.inspectorView.setCurrentPanel(panel);
160     }
161     toolbarItem.addEventListener("click", onToolbarItemClicked, false);
162
163     var iconElement = toolbarItem.createChild("div", "toolbar-icon");
164
165     if ("toolbarItemLabel" in panel)
166         toolbarItem.createChild("div", "toolbar-label").textContent = panel.toolbarItemLabel;
167
168     if (panel === WebInspector.inspectorView.currentPanel())
169         toolbarItem.addStyleClass("toggled-on");
170
171     return toolbarItem;
172 }
173
174 /**
175  * @constructor
176  */
177 WebInspector.ToolbarDropdown = function()
178 {
179     this._toolbar = document.getElementById("toolbar");
180     this._arrow = document.getElementById("toolbar-dropdown-arrow");
181     this.element = document.createElement("div");
182     this.element.id = "toolbar-dropdown";
183     this.element.className = "toolbar-small";
184     this._contentElement = this.element.createChild("div", "scrollable-content");
185     this._contentElement.tabIndex = 0;
186     this._contentElement.addEventListener("keydown", this._onKeyDown.bind(this), true);
187 }
188
189 WebInspector.ToolbarDropdown.prototype = {
190     show: function()
191     {
192         if (this.visible)
193             return;
194         var style = this.element.style;
195         this._populate();
196         var top = this._arrow.totalOffsetTop() + this._arrow.clientHeight;
197         this._arrow.addStyleClass("dropdown-visible");
198         this.element.style.top = top + "px";
199         this.element.style.left = this._arrow.totalOffsetLeft() + "px";
200         this._contentElement.style.maxHeight = window.innerHeight - top - 20 + "px";
201         this._toolbar.appendChild(this.element);
202     },
203
204     hide: function()
205     {
206         if (!this.visible)
207             return;
208         this._arrow.removeStyleClass("dropdown-visible");
209         this.element.parentNode.removeChild(this.element);
210         this._contentElement.removeChildren();
211     },
212
213     get visible()
214     {
215         return !!this.element.parentNode;
216     },
217
218     _populate: function()
219     {
220         var toolbarItems = this._toolbar.querySelectorAll(".toolbar-item.toggleable");
221
222         for (var i = 0; i < toolbarItems.length; ++i) {
223             if (toolbarItems[i].offsetTop > 0)
224                 this._contentElement.appendChild(WebInspector.Toolbar.createPanelToolbarItem(toolbarItems[i].panel));
225         }
226     },
227
228     _onKeyDown: function(event)
229     {
230         if (event.keyCode !== WebInspector.KeyboardShortcut.Keys.Esc.code)
231             return;
232         event.stopPropagation();
233         this.hide();
234     }
235 }
236
237 /**
238  * @type {?WebInspector.Toolbar}
239  */
240 WebInspector.toolbar = null;