Roo/SplitLayoutRegion.js
[roojs1] / Roo / SplitLayoutRegion.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12
13
14 /**
15  * @class Roo.SplitLayoutRegion
16  * @extends Roo.LayoutRegion
17  * Adds a splitbar and other (private) useful functionality to a {@link Roo.LayoutRegion}.
18  */
19 Roo.SplitLayoutRegion = function(mgr, config, pos, cursor){
20     this.cursor = cursor;
21     Roo.SplitLayoutRegion.superclass.constructor.call(this, mgr, config, pos);
22 };
23
24 Roo.extend(Roo.SplitLayoutRegion, Roo.LayoutRegion, {
25     splitTip : "Drag to resize.",
26     collapsibleSplitTip : "Drag to resize. Double click to hide.",
27     useSplitTips : false,
28
29     applyConfig : function(config){
30         Roo.SplitLayoutRegion.superclass.applyConfig.call(this, config);
31         if(config.split){
32             if(!this.split){
33                 var splitEl = Roo.DomHelper.append(this.mgr.el.dom, 
34                         {tag: "div", id: this.el.id + "-split", cls: "x-layout-split x-layout-split-"+this.position, html: "&#160;"});
35                 /** The SplitBar for this region 
36                 * @type Roo.SplitBar */
37                 this.split = new Roo.SplitBar(splitEl, this.el, this.orientation);
38                 this.split.on("moved", this.onSplitMove, this);
39                 this.split.useShim = config.useShim === true;
40                 this.split.getMaximumSize = this[this.position == 'north' || this.position == 'south' ? 'getVMaxSize' : 'getHMaxSize'].createDelegate(this);
41                 if(this.useSplitTips){
42                     this.split.el.dom.title = config.collapsible ? this.collapsibleSplitTip : this.splitTip;
43                 }
44                 if(config.collapsible){
45                     this.split.el.on("dblclick", this.collapse,  this);
46                 }
47             }
48             if(typeof config.minSize != "undefined"){
49                 this.split.minSize = config.minSize;
50             }
51             if(typeof config.maxSize != "undefined"){
52                 this.split.maxSize = config.maxSize;
53             }
54             if(config.hideWhenEmpty || config.hidden || config.collapsed){
55                 this.hideSplitter();
56             }
57         }
58     },
59
60     getHMaxSize : function(){
61          var cmax = this.config.maxSize || 10000;
62          var center = this.mgr.getRegion("center");
63          return Math.min(cmax, (this.el.getWidth()+center.getEl().getWidth())-center.getMinWidth());
64     },
65
66     getVMaxSize : function(){
67          var cmax = this.config.maxSize || 10000;
68          var center = this.mgr.getRegion("center");
69          return Math.min(cmax, (this.el.getHeight()+center.getEl().getHeight())-center.getMinHeight());
70     },
71
72     onSplitMove : function(split, newSize){
73         this.fireEvent("resized", this, newSize);
74     },
75     
76     /** 
77      * Returns the {@link Roo.SplitBar} for this region.
78      * @return {Roo.SplitBar}
79      */
80     getSplitBar : function(){
81         return this.split;
82     },
83     
84     hide : function(){
85         this.hideSplitter();
86         Roo.SplitLayoutRegion.superclass.hide.call(this);
87     },
88
89     hideSplitter : function(){
90         if(this.split){
91             this.split.el.setLocation(-2000,-2000);
92             this.split.el.hide();
93         }
94     },
95
96     show : function(){
97         if(this.split){
98             this.split.el.show();
99         }
100         Roo.SplitLayoutRegion.superclass.show.call(this);
101     },
102     
103     beforeSlide: function(){
104         if(Roo.isGecko){// firefox overflow auto bug workaround
105             this.bodyEl.clip();
106             if(this.tabs) {
107                 this.tabs.bodyEl.clip();
108             }
109             if(this.activePanel){
110                 this.activePanel.getEl().clip();
111                 
112                 if(this.activePanel.beforeSlide){
113                     this.activePanel.beforeSlide();
114                 }
115             }
116         }
117     },
118     
119     afterSlide : function(){
120         if(Roo.isGecko){// firefox overflow auto bug workaround
121             this.bodyEl.unclip();
122             if(this.tabs) this.tabs.bodyEl.unclip();
123             if(this.activePanel){
124                 this.activePanel.getEl().unclip();
125                 if(this.activePanel.afterSlide){
126                     this.activePanel.afterSlide();
127                 }
128             }
129         }
130     },
131
132     initAutoHide : function(){
133         if(this.autoHide !== false){
134             if(!this.autoHideHd){
135                 var st = new Roo.util.DelayedTask(this.slideIn, this);
136                 this.autoHideHd = {
137                     "mouseout": function(e){
138                         if(!e.within(this.el, true)){
139                             st.delay(500);
140                         }
141                     },
142                     "mouseover" : function(e){
143                         st.cancel();
144                     },
145                     scope : this
146                 };
147             }
148             this.el.on(this.autoHideHd);
149         }
150     },
151
152     clearAutoHide : function(){
153         if(this.autoHide !== false){
154             this.el.un("mouseout", this.autoHideHd.mouseout);
155             this.el.un("mouseover", this.autoHideHd.mouseover);
156         }
157     },
158
159     clearMonitor : function(){
160         Roo.get(document).un("click", this.slideInIf, this);
161     },
162
163     // these names are backwards but not changed for compat
164     slideOut : function(){
165         if(this.isSlid || this.el.hasActiveFx()){
166             return;
167         }
168         this.isSlid = true;
169         if(this.collapseBtn){
170             this.collapseBtn.hide();
171         }
172         this.closeBtnState = this.closeBtn.getStyle('display');
173         this.closeBtn.hide();
174         if(this.stickBtn){
175             this.stickBtn.show();
176         }
177         this.el.show();
178         this.el.alignTo(this.collapsedEl, this.getCollapseAnchor());
179         this.beforeSlide();
180         this.el.setStyle("z-index", 10001);
181         this.el.slideIn(this.getSlideAnchor(), {
182             callback: function(){
183                 this.afterSlide();
184                 this.initAutoHide();
185                 Roo.get(document).on("click", this.slideInIf, this);
186                 this.fireEvent("slideshow", this);
187             },
188             scope: this,
189             block: true
190         });
191     },
192
193     afterSlideIn : function(){
194         this.clearAutoHide();
195         this.isSlid = false;
196         this.clearMonitor();
197         this.el.setStyle("z-index", "");
198         if(this.collapseBtn){
199             this.collapseBtn.show();
200         }
201         this.closeBtn.setStyle('display', this.closeBtnState);
202         if(this.stickBtn){
203             this.stickBtn.hide();
204         }
205         this.fireEvent("slidehide", this);
206     },
207
208     slideIn : function(cb){
209         if(!this.isSlid || this.el.hasActiveFx()){
210             Roo.callback(cb);
211             return;
212         }
213         this.isSlid = false;
214         this.beforeSlide();
215         this.el.slideOut(this.getSlideAnchor(), {
216             callback: function(){
217                 this.el.setLeftTop(-10000, -10000);
218                 this.afterSlide();
219                 this.afterSlideIn();
220                 Roo.callback(cb);
221             },
222             scope: this,
223             block: true
224         });
225     },
226     
227     slideInIf : function(e){
228         if(!e.within(this.el)){
229             this.slideIn();
230         }
231     },
232
233     animateCollapse : function(){
234         this.beforeSlide();
235         this.el.setStyle("z-index", 20000);
236         var anchor = this.getSlideAnchor();
237         this.el.slideOut(anchor, {
238             callback : function(){
239                 this.el.setStyle("z-index", "");
240                 this.collapsedEl.slideIn(anchor, {duration:.3});
241                 this.afterSlide();
242                 this.el.setLocation(-10000,-10000);
243                 this.el.hide();
244                 this.fireEvent("collapsed", this);
245             },
246             scope: this,
247             block: true
248         });
249     },
250
251     animateExpand : function(){
252         this.beforeSlide();
253         this.el.alignTo(this.collapsedEl, this.getCollapseAnchor(), this.getExpandAdj());
254         this.el.setStyle("z-index", 20000);
255         this.collapsedEl.hide({
256             duration:.1
257         });
258         this.el.slideIn(this.getSlideAnchor(), {
259             callback : function(){
260                 this.el.setStyle("z-index", "");
261                 this.afterSlide();
262                 if(this.split){
263                     this.split.el.show();
264                 }
265                 this.fireEvent("invalidated", this);
266                 this.fireEvent("expanded", this);
267             },
268             scope: this,
269             block: true
270         });
271     },
272
273     anchors : {
274         "west" : "left",
275         "east" : "right",
276         "north" : "top",
277         "south" : "bottom"
278     },
279
280     sanchors : {
281         "west" : "l",
282         "east" : "r",
283         "north" : "t",
284         "south" : "b"
285     },
286
287     canchors : {
288         "west" : "tl-tr",
289         "east" : "tr-tl",
290         "north" : "tl-bl",
291         "south" : "bl-tl"
292     },
293
294     getAnchor : function(){
295         return this.anchors[this.position];
296     },
297
298     getCollapseAnchor : function(){
299         return this.canchors[this.position];
300     },
301
302     getSlideAnchor : function(){
303         return this.sanchors[this.position];
304     },
305
306     getAlignAdj : function(){
307         var cm = this.cmargins;
308         switch(this.position){
309             case "west":
310                 return [0, 0];
311             break;
312             case "east":
313                 return [0, 0];
314             break;
315             case "north":
316                 return [0, 0];
317             break;
318             case "south":
319                 return [0, 0];
320             break;
321         }
322     },
323
324     getExpandAdj : function(){
325         var c = this.collapsedEl, cm = this.cmargins;
326         switch(this.position){
327             case "west":
328                 return [-(cm.right+c.getWidth()+cm.left), 0];
329             break;
330             case "east":
331                 return [cm.right+c.getWidth()+cm.left, 0];
332             break;
333             case "north":
334                 return [0, -(cm.top+cm.bottom+c.getHeight())];
335             break;
336             case "south":
337                 return [0, cm.top+cm.bottom+c.getHeight()];
338             break;
339         }
340     }
341 });