# LayoutRegionLite.js

    /**
     * @extends YAHOO.ext.util.Observable
     * This class represents a lightweight region in a layout manager.
     */
    YAHOO.ext.LayoutRegionLite = function(mgr, config, pos){
        this.mgr = mgr;
        this.position  = pos;
        this.events = {
            /**
             * @event beforeremove
             * Fires before a panel is removed (or closed). To cancel the removal set "e.cancel = true" on the event argument.
             * @param {YAHOO.ext.LayoutRegion} this
             * @param {YAHOO.ext.ContentPanel} panel The panel
             * @param {Object} e The cancel event object
             */
            'beforeremove' : new YAHOO.util.CustomEvent('beforeremove'),
            /**
             * @event invalidated
             * Fires when the layout for this region is changed.
             * @param {YAHOO.ext.LayoutRegion} this
             */
            'invalidated' : new YAHOO.util.CustomEvent('invalidated'),
            /**
             * @event visibilitychange
             * Fires when this region is shown or hidden
             * @param {YAHOO.ext.LayoutRegion} this
             * @param {Boolean} visibility true or false
             */
            'visibilitychange' : new YAHOO.util.CustomEvent('visibilitychange'),
            /**
             * @event paneladded
             * Fires when a panel is added.
             * @param {YAHOO.ext.LayoutRegion} this
             * @param {YAHOO.ext.ContentPanel} panel The panel
             */
            'paneladded' : new YAHOO.util.CustomEvent('paneladded'),
            /**
             * @event panelremoved
             * Fires when a panel is removed.
             * @param {YAHOO.ext.LayoutRegion} this
             * @param {YAHOO.ext.ContentPanel} panel The panel
             */
            'panelremoved' : new YAHOO.util.CustomEvent('panelremoved'),
            /**
             * @event collapsed
             * Fires when this region is collapsed.
             * @param {YAHOO.ext.LayoutRegion} this
             */
            'collapsed' : new YAHOO.util.CustomEvent('collapsed'),
            /**
             * @event expanded
             * Fires when this region is expanded.
             * @param {YAHOO.ext.LayoutRegion} this
             */
            'expanded' : new YAHOO.util.CustomEvent('expanded'),
            /**
             * @event panelactivated
             * Fires when a panel is activated.
             * @param {YAHOO.ext.LayoutRegion} this
             * @param {YAHOO.ext.ContentPanel} panel The activated panel
             */
            'panelactivated' : new YAHOO.util.CustomEvent('panelactivated'),
            /**
             * @event resized
             * Fires when the user resizes this region.
             * @param {YAHOO.ext.LayoutRegion} this
             * @param {Number} newSize The new size (width for east/west, height for north/south)
             */
            'resized' : new YAHOO.util.CustomEvent('resized')
        };
        /** A collection of panels in this region. @type YAHOO.ext.util.MixedCollection */
        this.panels = new YAHOO.ext.util.MixedCollection();
        this.panels.getKey = this.getPanelId.createDelegate(this);
        this.box = null;
        this.activePanel = null;

        this.applyConfig(config);
    };

    YAHOO.extendX(YAHOO.ext.LayoutRegionLite, YAHOO.ext.util.Observable, {
        getPanelId : function(p){
            return p.getId();
        },

        applyConfig : function(config){
            this.margins = config.margins || this.margins || {top: 0, left: 0, right:0, bottom: 0};
            this.config = config;
        },

        /**
         * Resizes the region to the specified size. For vertical regions (west, east) this adjusts
         * the width, for horizontal (north, south) the height.
         * @param {Number} newSize The new width or height
         */
        resizeTo : function(newSize){
            switch(this.position){
                case 'east':
                case 'west':
                    this.el.setWidth(newSize);
                    this.fireEvent('resized', this, newSize);
                break;
                case 'north':
                case 'south':
                    this.el.setHeight(newSize);
                    this.fireEvent('resized', this, newSize);
                break;
            }
        },

        getBox : function(){
            var b = this.activePanel.getBox(false, true);
            return b;
        },

        getMargins : function(){
            return this.collapsed ? this.cmargins : this.margins;
        },

        updateBox : function(box){
            this.box = box;
            this.activePanel.getEl().dom.style.left = box.x + 'px';
            this.activePanel.getEl().dom.style.top = box.y + 'px';
            this.activePanel.getEl().setSize(box.width, box.height);
        },

        /**
         * Returns the container element for this region.
         * @return {YAHOO.ext.Element}
         */
        getEl : function(){
            return this.activePanel;
        },

        /**
         * Returns true if this region is currently visible.
         * @return {Boolean}
         */
        isVisible : function(){
            return this.visible;
        },

        setActivePanel : function(panel){
            panel = this.getPanel(panel);
            if(this.activePanel && this.activePanel != panel){
                this.activePanel.setActiveState(false);
                this.activePanel.getEl().setStyle({left:-10000,right:-10000});
            }
            this.activePanel = panel;
            panel.setActiveState(true);
            if(this.panelSize){
                panel.setSize(this.panelSize.width, this.panelSize.height);
            }
            this.fireEvent('panelactivated', this, panel);
            this.fireEvent('invalidated');
        },

        /**
         * Show the specified panel.
         * @param {Number/String/ContentPanel} panelId The panels index, id or the panel itself
         * @return {YAHOO.ext.ContentPanel} The shown panel or null
         */
        showPanel : function(panel){
            if(panel = this.getPanel(panel)){
                this.setActivePanel(panel);
            }
            return panel;
        },

        /**
         * Get the active panel for this region.
         * @return {YAHOO.ext.ContentPanel} The active panel or null
         */
        getActivePanel : function(){
            return this.activePanel;
        },

        /**
         * Add the passed ContentPanel(s)
         * @param {ContentPanel...} panel The ContentPanel(s) to add (you can pass more than one)
         * @return {YAHOO.ext.ContentPanel} The panel added (if only one was added)
         */
        add : function(panel){
            if(arguments.length > 1){
                for(var i = 0, len = arguments.length; i < len; i++) {
                	this.add(arguments[i]);
                }
                return null;
            }
            if(this.hasPanel(panel)){
                this.showPanel(panel);
                return panel;
            }
            panel.setRegion(this);
            this.panels.add(panel);
            panel.getEl().setStyle('position', 'absolute');
            this.setActivePanel(panel);
            this.fireEvent('paneladded', this, panel);
            return panel;
        },

        /**
         * Returns true if the panel is in this region.
         * @param {Number/String/ContentPanel} panel The panels index, id or the panel itself
         * @return {Boolean}
         */
        hasPanel : function(panel){
            if(typeof panel == 'object'){ // must be panel obj
                panel = panel.getId();
            }
            return this.getPanel(panel) ? true : false;
        },

        /**
         * Removes the specified panel. If preservePanel is not true (either here or in the config), the panel is destroyed.
         * @param {Number/String/ContentPanel} panel The panels index, id or the panel itself
         * @param {Boolean} preservePanel Overrides the config preservePanel option
         * @return {YAHOO.ext.ContentPanel} The panel that was removed
         */
        remove : function(panel, preservePanel){
            panel = this.getPanel(panel);
            if(!panel){
                return null;
            }
            var e = {};
            this.fireEvent('beforeremove', this, panel, e);
            if(e.cancel === true){
                return null;
            }
            var panelId = panel.getId();
            this.panels.removeKey(panelId);
            return panel;
        },

        /**
         * Returns the panel specified or null if it's not in this region.
         * @param {Number/String/ContentPanel} panel The panels index, id or the panel itself
         * @return {YAHOO.ext.ContentPanel}
         */
        getPanel : function(id){
            if(typeof id == 'object'){ // must be panel obj
                return id;
            }
            return this.panels.get(id);
        },

        /**
         * Returns this regions position (north/south/east/west/center).
         * @return {String}
         */
        getPosition: function(){
            return this.position;
        }
    });

yui-ext - Copyright © 2006 Jack Slocum. | Yahoo! UI - Copyright © 2006 Yahoo! Inc.
All rights reserved.