initial import
[roojs1] / Roo / LayoutRegion.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  * @class Roo.LayoutRegion
14  * @extends Roo.BasicLayoutRegion
15  * This class represents a region in a layout manager.
16  * @cfg {Boolean} collapsible False to disable collapsing (defaults to true)
17  * @cfg {Boolean} collapsed True to set the initial display to collapsed (defaults to false)
18  * @cfg {Boolean} floatable False to disable floating (defaults to true)
19  * @cfg {Object} margins Margins for the element (defaults to {top: 0, left: 0, right:0, bottom: 0})
20  * @cfg {Object} cmargins Margins for the element when collapsed (defaults to: north/south {top: 2, left: 0, right:0, bottom: 2} or east/west {top: 0, left: 2, right:2, bottom: 0})
21  * @cfg {String} tabPosition "top" or "bottom" (defaults to "bottom")
22  * @cfg {String} collapsedTitle Optional string message to display in the collapsed block of a north or south region
23  * @cfg {Boolean} alwaysShowTabs True to always display tabs even when there is only 1 panel (defaults to false)
24  * @cfg {Boolean} autoScroll True to enable overflow scrolling (defaults to false)
25  * @cfg {Boolean} titlebar True to display a title bar (defaults to true)
26  * @cfg {String} title The title for the region (overrides panel titles)
27  * @cfg {Boolean} animate True to animate expand/collapse (defaults to false)
28  * @cfg {Boolean} autoHide False to disable auto hiding when the mouse leaves the "floated" region (defaults to true)
29  * @cfg {Boolean} preservePanels True to preserve removed panels so they can be readded later (defaults to false)
30  * @cfg {Boolean} closeOnTab True to place the close icon on the tabs instead of the region titlebar (defaults to false)
31  * @cfg {Boolean} hideTabs True to hide the tab strip (defaults to false)
32  * @cfg {Boolean} resizeTabs True to enable automatic tab resizing. This will resize the tabs so they are all the same size and fit within
33  * the space available, similar to FireFox 1.5 tabs (defaults to false)
34  * @cfg {Number} minTabWidth The minimum tab width (defaults to 40)
35  * @cfg {Number} preferredTabWidth The preferred tab width (defaults to 150)
36  * @cfg {Boolean} showPin True to show a pin button
37 * @cfg {Boolean} hidden True to start the region hidden (defaults to false)
38 * @cfg {Boolean} hideWhenEmpty True to hide the region when it has no panels
39 * @cfg {Boolean} disableTabTips True to disable tab tooltips
40 * @cfg {Number} width  For East/West panels
41 * @cfg {Number} height For North/South panels
42 * @cfg {Boolean} split To show the splitter
43  */
44 Roo.LayoutRegion = function(mgr, config, pos){
45     Roo.LayoutRegion.superclass.constructor.call(this, mgr, config, pos, true);
46     var dh = Roo.DomHelper;
47     /** This region's container element 
48     * @type Roo.Element */
49     this.el = dh.append(mgr.el.dom, {tag: "div", cls: "x-layout-panel x-layout-panel-" + this.position}, true);
50     /** This region's title element 
51     * @type Roo.Element */
52
53     this.titleEl = dh.append(this.el.dom, {tag: "div", unselectable: "on", cls: "x-unselectable x-layout-panel-hd x-layout-title-"+this.position, children:[
54         {tag: "span", cls: "x-unselectable x-layout-panel-hd-text", unselectable: "on", html: "&#160;"},
55         {tag: "div", cls: "x-unselectable x-layout-panel-hd-tools", unselectable: "on"}
56     ]}, true);
57     this.titleEl.enableDisplayMode();
58     /** This region's title text element 
59     * @type HTMLElement */
60     this.titleTextEl = this.titleEl.dom.firstChild;
61     this.tools = Roo.get(this.titleEl.dom.childNodes[1], true);
62     this.closeBtn = this.createTool(this.tools.dom, "x-layout-close");
63     this.closeBtn.enableDisplayMode();
64     this.closeBtn.on("click", this.closeClicked, this);
65     this.closeBtn.hide();
66
67     this.createBody(config);
68     this.visible = true;
69     this.collapsed = false;
70
71     if(config.hideWhenEmpty){
72         this.hide();
73         this.on("paneladded", this.validateVisibility, this);
74         this.on("panelremoved", this.validateVisibility, this);
75     }
76     this.applyConfig(config);
77 };
78
79 Roo.extend(Roo.LayoutRegion, Roo.BasicLayoutRegion, {
80
81     createBody : function(){
82         /** This region's body element 
83         * @type Roo.Element */
84         this.bodyEl = this.el.createChild({tag: "div", cls: "x-layout-panel-body"});
85     },
86
87     applyConfig : function(c){
88         if(c.collapsible && this.position != "center" && !this.collapsedEl){
89             var dh = Roo.DomHelper;
90             if(c.titlebar !== false){
91                 this.collapseBtn = this.createTool(this.tools.dom, "x-layout-collapse-"+this.position);
92                 this.collapseBtn.on("click", this.collapse, this);
93                 this.collapseBtn.enableDisplayMode();
94
95                 if(c.showPin === true || this.showPin){
96                     this.stickBtn = this.createTool(this.tools.dom, "x-layout-stick");
97                     this.stickBtn.enableDisplayMode();
98                     this.stickBtn.on("click", this.expand, this);
99                     this.stickBtn.hide();
100                 }
101             }
102             /** This region's collapsed element
103             * @type Roo.Element */
104             this.collapsedEl = dh.append(this.mgr.el.dom, {cls: "x-layout-collapsed x-layout-collapsed-"+this.position, children:[
105                 {cls: "x-layout-collapsed-tools", children:[{cls: "x-layout-ctools-inner"}]}
106             ]}, true);
107             if(c.floatable !== false){
108                this.collapsedEl.addClassOnOver("x-layout-collapsed-over");
109                this.collapsedEl.on("click", this.collapseClick, this);
110             }
111
112             if(c.collapsedTitle && (this.position == "north" || this.position== "south")) {
113                 this.collapsedTitleTextEl = dh.append(this.collapsedEl.dom, {tag: "div", cls: "x-unselectable x-layout-panel-hd-text",
114                    id: "message", unselectable: "on", style:{"float":"left"}});
115                this.collapsedTitleTextEl.innerHTML = c.collapsedTitle;
116              }
117             this.expandBtn = this.createTool(this.collapsedEl.dom.firstChild.firstChild, "x-layout-expand-"+this.position);
118             this.expandBtn.on("click", this.expand, this);
119         }
120         if(this.collapseBtn){
121             this.collapseBtn.setVisible(c.collapsible == true);
122         }
123         this.cmargins = c.cmargins || this.cmargins ||
124                          (this.position == "west" || this.position == "east" ?
125                              {top: 0, left: 2, right:2, bottom: 0} :
126                              {top: 2, left: 0, right:0, bottom: 2});
127         this.margins = c.margins || this.margins || {top: 0, left: 0, right:0, bottom: 0};
128         this.bottomTabs = c.tabPosition != "top";
129         this.autoScroll = c.autoScroll || false;
130         if(this.autoScroll){
131             this.bodyEl.setStyle("overflow", "auto");
132         }else{
133             this.bodyEl.setStyle("overflow", "hidden");
134         }
135         //if(c.titlebar !== false){
136             if((!c.titlebar && !c.title) || c.titlebar === false){
137                 this.titleEl.hide();
138             }else{
139                 this.titleEl.show();
140                 if(c.title){
141                     this.titleTextEl.innerHTML = c.title;
142                 }
143             }
144         //}
145         this.duration = c.duration || .30;
146         this.slideDuration = c.slideDuration || .45;
147         this.config = c;
148         if(c.collapsed){
149             this.collapse(true);
150         }
151         if(c.hidden){
152             this.hide();
153         }
154     },
155     /**
156      * Returns true if this region is currently visible.
157      * @return {Boolean}
158      */
159     isVisible : function(){
160         return this.visible;
161     },
162
163     /**
164      * Updates the title for collapsed north/south regions (used with {@link #collapsedTitle} config option)
165      * @param {String} title (optional) The title text (accepts HTML markup, defaults to the numeric character reference for a non-breaking space, "&amp;#160;")
166      */
167     setCollapsedTitle : function(title){
168         title = title || "&#160;";
169         if(this.collapsedTitleTextEl){
170             this.collapsedTitleTextEl.innerHTML = title;
171         }
172     },
173
174     getBox : function(){
175         var b;
176         if(!this.collapsed){
177             b = this.el.getBox(false, true);
178         }else{
179             b = this.collapsedEl.getBox(false, true);
180         }
181         return b;
182     },
183
184     getMargins : function(){
185         return this.collapsed ? this.cmargins : this.margins;
186     },
187
188     highlight : function(){
189         this.el.addClass("x-layout-panel-dragover");
190     },
191
192     unhighlight : function(){
193         this.el.removeClass("x-layout-panel-dragover");
194     },
195
196     updateBox : function(box){
197         this.box = box;
198         if(!this.collapsed){
199             this.el.dom.style.left = box.x + "px";
200             this.el.dom.style.top = box.y + "px";
201             this.updateBody(box.width, box.height);
202         }else{
203             this.collapsedEl.dom.style.left = box.x + "px";
204             this.collapsedEl.dom.style.top = box.y + "px";
205             this.collapsedEl.setSize(box.width, box.height);
206         }
207         if(this.tabs){
208             this.tabs.autoSizeTabs();
209         }
210     },
211
212     updateBody : function(w, h){
213         if(w !== null){
214             this.el.setWidth(w);
215             w -= this.el.getBorderWidth("rl");
216             if(this.config.adjustments){
217                 w += this.config.adjustments[0];
218             }
219         }
220         if(h !== null){
221             this.el.setHeight(h);
222             h = this.titleEl && this.titleEl.isDisplayed() ? h - (this.titleEl.getHeight()||0) : h;
223             h -= this.el.getBorderWidth("tb");
224             if(this.config.adjustments){
225                 h += this.config.adjustments[1];
226             }
227             this.bodyEl.setHeight(h);
228             if(this.tabs){
229                 h = this.tabs.syncHeight(h);
230             }
231         }
232         if(this.panelSize){
233             w = w !== null ? w : this.panelSize.width;
234             h = h !== null ? h : this.panelSize.height;
235         }
236         if(this.activePanel){
237             var el = this.activePanel.getEl();
238             w = w !== null ? w : el.getWidth();
239             h = h !== null ? h : el.getHeight();
240             this.panelSize = {width: w, height: h};
241             this.activePanel.setSize(w, h);
242         }
243         if(Roo.isIE && this.tabs){
244             this.tabs.el.repaint();
245         }
246     },
247
248     /**
249      * Returns the container element for this region.
250      * @return {Roo.Element}
251      */
252     getEl : function(){
253         return this.el;
254     },
255
256     /**
257      * Hides this region.
258      */
259     hide : function(){
260         if(!this.collapsed){
261             this.el.dom.style.left = "-2000px";
262             this.el.hide();
263         }else{
264             this.collapsedEl.dom.style.left = "-2000px";
265             this.collapsedEl.hide();
266         }
267         this.visible = false;
268         this.fireEvent("visibilitychange", this, false);
269     },
270
271     /**
272      * Shows this region if it was previously hidden.
273      */
274     show : function(){
275         if(!this.collapsed){
276             this.el.show();
277         }else{
278             this.collapsedEl.show();
279         }
280         this.visible = true;
281         this.fireEvent("visibilitychange", this, true);
282     },
283
284     closeClicked : function(){
285         if(this.activePanel){
286             this.remove(this.activePanel);
287         }
288     },
289
290     collapseClick : function(e){
291         if(this.isSlid){
292            e.stopPropagation();
293            this.slideIn();
294         }else{
295            e.stopPropagation();
296            this.slideOut();
297         }
298     },
299
300     /**
301      * Collapses this region.
302      * @param {Boolean} skipAnim (optional) true to collapse the element without animation (if animate is true)
303      */
304     collapse : function(skipAnim){
305         if(this.collapsed) return;
306         this.collapsed = true;
307         if(this.split){
308             this.split.el.hide();
309         }
310         if(this.config.animate && skipAnim !== true){
311             this.fireEvent("invalidated", this);
312             this.animateCollapse();
313         }else{
314             this.el.setLocation(-20000,-20000);
315             this.el.hide();
316             this.collapsedEl.show();
317             this.fireEvent("collapsed", this);
318             this.fireEvent("invalidated", this);
319         }
320     },
321
322     animateCollapse : function(){
323         // overridden
324     },
325
326     /**
327      * Expands this region if it was previously collapsed.
328      * @param {Roo.EventObject} e The event that triggered the expand (or null if calling manually)
329      * @param {Boolean} skipAnim (optional) true to expand the element without animation (if animate is true)
330      */
331     expand : function(e, skipAnim){
332         if(e) e.stopPropagation();
333         if(!this.collapsed || this.el.hasActiveFx()) return;
334         if(this.isSlid){
335             this.afterSlideIn();
336             skipAnim = true;
337         }
338         this.collapsed = false;
339         if(this.config.animate && skipAnim !== true){
340             this.animateExpand();
341         }else{
342             this.el.show();
343             if(this.split){
344                 this.split.el.show();
345             }
346             this.collapsedEl.setLocation(-2000,-2000);
347             this.collapsedEl.hide();
348             this.fireEvent("invalidated", this);
349             this.fireEvent("expanded", this);
350         }
351     },
352
353     animateExpand : function(){
354         // overridden
355     },
356
357     initTabs : function(){
358         this.bodyEl.setStyle("overflow", "hidden");
359         var ts = new Roo.TabPanel(this.bodyEl.dom, {
360             tabPosition: this.bottomTabs ? 'bottom' : 'top',
361             disableTooltips: this.config.disableTabTips
362         });
363         if(this.config.hideTabs){
364             ts.stripWrap.setDisplayed(false);
365         }
366         this.tabs = ts;
367         ts.resizeTabs = this.config.resizeTabs === true;
368         ts.minTabWidth = this.config.minTabWidth || 40;
369         ts.maxTabWidth = this.config.maxTabWidth || 250;
370         ts.preferredTabWidth = this.config.preferredTabWidth || 150;
371         ts.monitorResize = false;
372         ts.bodyEl.setStyle("overflow", this.config.autoScroll ? "auto" : "hidden");
373         ts.bodyEl.addClass('x-layout-tabs-body');
374         this.panels.each(this.initPanelAsTab, this);
375     },
376
377     initPanelAsTab : function(panel){
378         var ti = this.tabs.addTab(panel.getEl().id, panel.getTitle(), null,
379                     this.config.closeOnTab && panel.isClosable());
380         if(panel.tabTip !== undefined){
381             ti.setTooltip(panel.tabTip);
382         }
383         ti.on("activate", function(){
384               this.setActivePanel(panel);
385         }, this);
386         if(this.config.closeOnTab){
387             ti.on("beforeclose", function(t, e){
388                 e.cancel = true;
389                 this.remove(panel);
390             }, this);
391         }
392         return ti;
393     },
394
395     updatePanelTitle : function(panel, title){
396         if(this.activePanel == panel){
397             this.updateTitle(title);
398         }
399         if(this.tabs){
400             var ti = this.tabs.getTab(panel.getEl().id);
401             ti.setText(title);
402             if(panel.tabTip !== undefined){
403                 ti.setTooltip(panel.tabTip);
404             }
405         }
406     },
407
408     updateTitle : function(title){
409         if(this.titleTextEl && !this.config.title){
410             this.titleTextEl.innerHTML = (typeof title != "undefined" && title.length > 0 ? title : "&#160;");
411         }
412     },
413
414     setActivePanel : function(panel){
415         panel = this.getPanel(panel);
416         if(this.activePanel && this.activePanel != panel){
417             this.activePanel.setActiveState(false);
418         }
419         this.activePanel = panel;
420         panel.setActiveState(true);
421         if(this.panelSize){
422             panel.setSize(this.panelSize.width, this.panelSize.height);
423         }
424         if(this.closeBtn){
425             this.closeBtn.setVisible(!this.config.closeOnTab && !this.isSlid && panel.isClosable());
426         }
427         this.updateTitle(panel.getTitle());
428         if(this.tabs){
429             this.fireEvent("invalidated", this);
430         }
431         this.fireEvent("panelactivated", this, panel);
432     },
433
434     /**
435      * Shows the specified panel.
436      * @param {Number/String/ContentPanel} panelId The panel's index, id or the panel itself
437      * @return {Roo.ContentPanel} The shown panel, or null if a panel could not be found from panelId
438      */
439     showPanel : function(panel){
440         if(panel = this.getPanel(panel)){
441             if(this.tabs){
442                 var tab = this.tabs.getTab(panel.getEl().id);
443                 if(tab.isHidden()){
444                     this.tabs.unhideTab(tab.id);
445                 }
446                 tab.activate();
447             }else{
448                 this.setActivePanel(panel);
449             }
450         }
451         return panel;
452     },
453
454     /**
455      * Get the active panel for this region.
456      * @return {Roo.ContentPanel} The active panel or null
457      */
458     getActivePanel : function(){
459         return this.activePanel;
460     },
461
462     validateVisibility : function(){
463         if(this.panels.getCount() < 1){
464             this.updateTitle("&#160;");
465             this.closeBtn.hide();
466             this.hide();
467         }else{
468             if(!this.isVisible()){
469                 this.show();
470             }
471         }
472     },
473
474     /**
475      * Adds the passed ContentPanel(s) to this region.
476      * @param {ContentPanel...} panel The ContentPanel(s) to add (you can pass more than one)
477      * @return {Roo.ContentPanel} The panel added (if only one was added; null otherwise)
478      */
479     add : function(panel){
480         if(arguments.length > 1){
481             for(var i = 0, len = arguments.length; i < len; i++) {
482                 this.add(arguments[i]);
483             }
484             return null;
485         }
486         if(this.hasPanel(panel)){
487             this.showPanel(panel);
488             return panel;
489         }
490         panel.setRegion(this);
491         this.panels.add(panel);
492         if(this.panels.getCount() == 1 && !this.config.alwaysShowTabs){
493             this.bodyEl.dom.appendChild(panel.getEl().dom);
494             if(panel.background !== true){
495                 this.setActivePanel(panel);
496             }
497             this.fireEvent("paneladded", this, panel);
498             return panel;
499         }
500         if(!this.tabs){
501             this.initTabs();
502         }else{
503             this.initPanelAsTab(panel);
504         }
505         if(panel.background !== true){
506             this.tabs.activate(panel.getEl().id);
507         }
508         this.fireEvent("paneladded", this, panel);
509         return panel;
510     },
511
512     /**
513      * Hides the tab for the specified panel.
514      * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
515      */
516     hidePanel : function(panel){
517         if(this.tabs && (panel = this.getPanel(panel))){
518             this.tabs.hideTab(panel.getEl().id);
519         }
520     },
521
522     /**
523      * Unhides the tab for a previously hidden panel.
524      * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
525      */
526     unhidePanel : function(panel){
527         if(this.tabs && (panel = this.getPanel(panel))){
528             this.tabs.unhideTab(panel.getEl().id);
529         }
530     },
531
532     clearPanels : function(){
533         while(this.panels.getCount() > 0){
534              this.remove(this.panels.first());
535         }
536     },
537
538     /**
539      * Removes the specified panel. If preservePanel is not true (either here or in the config), the panel is destroyed.
540      * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
541      * @param {Boolean} preservePanel Overrides the config preservePanel option
542      * @return {Roo.ContentPanel} The panel that was removed
543      */
544     remove : function(panel, preservePanel){
545         panel = this.getPanel(panel);
546         if(!panel){
547             return null;
548         }
549         var e = {};
550         this.fireEvent("beforeremove", this, panel, e);
551         if(e.cancel === true){
552             return null;
553         }
554         preservePanel = (typeof preservePanel != "undefined" ? preservePanel : (this.config.preservePanels === true || panel.preserve === true));
555         var panelId = panel.getId();
556         this.panels.removeKey(panelId);
557         if(preservePanel){
558             document.body.appendChild(panel.getEl().dom);
559         }
560         if(this.tabs){
561             this.tabs.removeTab(panel.getEl().id);
562         }else if (!preservePanel){
563             this.bodyEl.dom.removeChild(panel.getEl().dom);
564         }
565         if(this.panels.getCount() == 1 && this.tabs && !this.config.alwaysShowTabs){
566             var p = this.panels.first();
567             var tempEl = document.createElement("div"); // temp holder to keep IE from deleting the node
568             tempEl.appendChild(p.getEl().dom);
569             this.bodyEl.update("");
570             this.bodyEl.dom.appendChild(p.getEl().dom);
571             tempEl = null;
572             this.updateTitle(p.getTitle());
573             this.tabs = null;
574             this.bodyEl.setStyle("overflow", this.config.autoScroll ? "auto" : "hidden");
575             this.setActivePanel(p);
576         }
577         panel.setRegion(null);
578         if(this.activePanel == panel){
579             this.activePanel = null;
580         }
581         if(this.config.autoDestroy !== false && preservePanel !== true){
582             try{panel.destroy();}catch(e){}
583         }
584         this.fireEvent("panelremoved", this, panel);
585         return panel;
586     },
587
588     /**
589      * Returns the TabPanel component used by this region
590      * @return {Roo.TabPanel}
591      */
592     getTabs : function(){
593         return this.tabs;
594     },
595
596     createTool : function(parentEl, className){
597         var btn = Roo.DomHelper.append(parentEl, {tag: "div", cls: "x-layout-tools-button",
598             children: [{tag: "div", cls: "x-layout-tools-button-inner " + className, html: "&#160;"}]}, true);
599         btn.addClassOnOver("x-layout-tools-button-over");
600         return btn;
601     }
602 });