tizen beta release
[profile/ivi/webkit-efl.git] / debian / tmp / usr / share / ewebkit-0 / webinspector / 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  */
33 WebInspector.Drawer = function()
34 {
35     this.element = document.getElementById("drawer");
36     this._savedHeight = 200; // Default.
37     this._mainElement = document.getElementById("main");
38     this._toolbarElement = document.getElementById("toolbar");
39     this._mainStatusBar = document.getElementById("main-status-bar");
40     this._mainStatusBar.addEventListener("mousedown", this._startStatusBarDragging.bind(this), true);
41     this._counters = document.getElementById("counters");
42
43     this._drawerContentsElement = document.createElement("div");
44     this._drawerContentsElement.id = "drawer-contents";
45     this._drawerContentsElement.className = "drawer-contents";
46     this.element.appendChild(this._drawerContentsElement);
47     
48     this._drawerStatusBar = document.createElement("div");
49     this._drawerStatusBar.id = "drawer-status-bar";
50     this._drawerStatusBar.className = "status-bar";
51     this.element.appendChild(this._drawerStatusBar);
52
53     this._viewStatusBar = document.createElement("div");
54     this._drawerStatusBar.appendChild(this._viewStatusBar);
55 }
56
57 WebInspector.Drawer.AnimationType = {
58         Immediately: 0,
59         Normal: 1,
60         Slow: 2
61 }
62
63 WebInspector.Drawer.prototype = {
64     get visible()
65     {
66         return !!this._view;
67     },
68
69     _constrainHeight: function(height)
70     {
71         return Number.constrain(height, Preferences.minConsoleHeight, window.innerHeight - this._mainElement.totalOffsetTop() - Preferences.minConsoleHeight);
72     },
73
74     show: function(view, animationType)
75     {
76         this.immediatelyFinishAnimation();
77         if (this._view && this._view.counterElement)
78             this._view.counterElement.parentNode.removeChild(this._view.counterElement);
79
80         var drawerWasVisible = this.visible;
81
82         if (this._view) {
83             this._view.detach();
84             this._drawerContentsElement.removeChildren();
85         }
86
87         this._view = view;
88
89         var statusBarItems = this._view.statusBarItems || [];
90         this._viewStatusBar.removeChildren();
91         for (var i = 0; i < statusBarItems.length; ++i)
92             this._viewStatusBar.appendChild(statusBarItems[i]);
93
94         if (this._view.counterElement)
95             this._counters.insertBefore(this._view.counterElement, this._counters.firstChild);
96
97         document.body.addStyleClass("drawer-visible");
98         this._view.markAsRoot();
99         this._view.show(this._drawerContentsElement);
100
101         if (drawerWasVisible)
102             return;
103         
104         var anchoredItems = document.getElementById("anchored-status-bar-items");
105         var height = this._constrainHeight(this._savedHeight || this.element.offsetHeight);
106         var animations = [
107             {element: this.element, end: {height: height}},
108             {element: this._mainElement, end: {bottom: height}},
109             {element: this._mainStatusBar, start: {"padding-left": anchoredItems.offsetWidth - 1}, end: {"padding-left": 0}},
110             {element: this._viewStatusBar, start: {opacity: 0}, end: {opacity: 1}}
111         ];
112
113         this._drawerStatusBar.insertBefore(anchoredItems, this._drawerStatusBar.firstChild);
114
115         if (this._currentPanelCounters) {
116             var oldRight = this._drawerStatusBar.clientWidth - (this._counters.offsetLeft + this._currentPanelCounters.offsetWidth);
117             var newRight = WebInspector.Panel.counterRightMargin;
118             var rightPadding = (oldRight - newRight);
119             animations.push({element: this._currentPanelCounters, start: {"padding-right": rightPadding}, end: {"padding-right": 0}});
120             this._currentPanelCounters.parentNode.removeChild(this._currentPanelCounters);
121             this._mainStatusBar.appendChild(this._currentPanelCounters);
122         }
123         
124         function animationFinished()
125         {
126             WebInspector.inspectorView.currentPanel().statusBarResized();
127             if (this._view && this._view.afterShow)
128                 this._view.afterShow();
129             delete this._currentAnimation;
130             if (this._currentPanelCounters)
131                 this._currentPanelCounters.removeAttribute("style");
132         }
133
134         this._currentAnimation = WebInspector.animateStyle(animations, this._animationDuration(animationType), animationFinished.bind(this));
135         if (animationType === WebInspector.Drawer.AnimationType.Immediately)
136             this._currentAnimation.forceComplete();
137     },
138
139     hide: function(animationType)
140     {
141         this.immediatelyFinishAnimation();
142         if (!this.visible)
143             return;
144
145         this._savedHeight = this.element.offsetHeight;
146
147         if (this.element === WebInspector.currentFocusElement() || this.element.isAncestor(WebInspector.currentFocusElement()))
148             WebInspector.setCurrentFocusElement(WebInspector.previousFocusElement());
149
150         var anchoredItems = document.getElementById("anchored-status-bar-items");
151
152         // Temporarily set properties and classes to mimic the post-animation values so panels
153         // like Elements in their updateStatusBarItems call will size things to fit the final location.
154         this._mainStatusBar.style.setProperty("padding-left", (anchoredItems.offsetWidth - 1) + "px");
155         document.body.removeStyleClass("drawer-visible");
156         WebInspector.inspectorView.currentPanel().statusBarResized();
157         document.body.addStyleClass("drawer-visible");
158
159         var animations = [
160             {element: this._mainElement, end: {bottom: 0}},
161             {element: this._mainStatusBar, start: {"padding-left": 0}, end: {"padding-left": anchoredItems.offsetWidth - 1}},
162             {element: this._viewStatusBar, start: {opacity: 1}, end: {opacity: 0}}
163         ];
164
165         if (this._currentPanelCounters) {
166             var newRight = this._drawerStatusBar.clientWidth - this._counters.offsetLeft;
167             var oldRight = this._mainStatusBar.clientWidth - (this._currentPanelCounters.offsetLeft + this._currentPanelCounters.offsetWidth);
168             var rightPadding = (newRight - oldRight);
169             animations.push({element: this._currentPanelCounters, start: {"padding-right": 0}, end: {"padding-right": rightPadding}});
170         }
171
172         function animationFinished()
173         {
174             WebInspector.inspectorView.currentPanel().doResize();
175             this._mainStatusBar.insertBefore(anchoredItems, this._mainStatusBar.firstChild);
176             this._mainStatusBar.style.removeProperty("padding-left");
177
178             if (this._view.counterElement)
179                 this._view.counterElement.parentNode.removeChild(this._view.counterElement);
180
181             if (this._currentPanelCounters) {
182                 this._currentPanelCounters.setAttribute("style", null);
183                 this._currentPanelCounters.parentNode.removeChild(this._currentPanelCounters);
184                 this._counters.insertBefore(this._currentPanelCounters, this._counters.firstChild);
185             }
186
187             this._view.detach();
188             delete this._view;
189             this._drawerContentsElement.removeChildren();
190             document.body.removeStyleClass("drawer-visible");
191             delete this._currentAnimation;
192         }
193
194         this._currentAnimation = WebInspector.animateStyle(animations, this._animationDuration(animationType), animationFinished.bind(this));
195         if (animationType === WebInspector.Drawer.AnimationType.Immediately)
196             this._currentAnimation.forceComplete();
197     },
198
199     resize: function()
200     {
201         if (!this.visible)
202             return;
203
204         this._view.storeScrollPositions();
205         var height = this._constrainHeight(parseInt(this.element.style.height, 10));
206         this._mainElement.style.bottom = height + "px";
207         this.element.style.height = height + "px";
208         this._view.doResize();
209     },
210
211     immediatelyFinishAnimation: function()
212     {
213         if (this._currentAnimation)
214             this._currentAnimation.forceComplete();
215     },
216
217     set currentPanelCounters(x)
218     {
219         if (!x) {
220             if (this._currentPanelCounters)
221                 this._currentPanelCounters.parentElement.removeChild(this._currentPanelCounters);
222             delete this._currentPanelCounters;
223             return;
224         }
225
226         this._currentPanelCounters = x;
227         if (this.visible)
228             this._mainStatusBar.appendChild(x);
229         else
230             this._counters.insertBefore(x, this._counters.firstChild);
231     },
232
233     _animationDuration: function(animationType)
234     {
235         switch (animationType) {
236         case WebInspector.Drawer.AnimationType.Slow:
237             return 2000;
238         case WebInspector.Drawer.AnimationType.Normal:
239             return 250;
240         default:
241             return 0;
242         }        
243     },
244
245     _startStatusBarDragging: function(event)
246     {
247         if (!this.visible || event.target !== this._mainStatusBar)
248             return;
249
250         this._view.storeScrollPositions();
251         WebInspector.elementDragStart(this._mainStatusBar, this._statusBarDragging.bind(this), this._endStatusBarDragging.bind(this), event, "row-resize");
252
253         this._statusBarDragOffset = event.pageY - this.element.totalOffsetTop();
254
255         event.stopPropagation();
256     },
257
258     _statusBarDragging: function(event)
259     {
260         var height = window.innerHeight - event.pageY + this._statusBarDragOffset;
261         height = Number.constrain(height, Preferences.minConsoleHeight, window.innerHeight - this._mainElement.totalOffsetTop() - Preferences.minConsoleHeight);
262
263         this._mainElement.style.bottom = height + "px";
264         this.element.style.height = height + "px";
265         if (WebInspector.inspectorView.currentPanel())
266             WebInspector.inspectorView.currentPanel().doResize();
267         this._view.doResize();
268
269         event.preventDefault();
270         event.stopPropagation();
271     },
272
273     _endStatusBarDragging: function(event)
274     {
275         WebInspector.elementDragEnd(event);
276
277         this._savedHeight = this.element.offsetHeight;
278         delete this._statusBarDragOffset;
279
280         event.stopPropagation();
281     }
282 }
283
284 /**
285  * @type {WebInspector.Drawer}
286  */
287 WebInspector.drawer = null;