YUI Library Examples: Layout Manager (beta): Layout inside a resizable Panel

Layout Manager (beta): Layout inside a resizable Panel

This example shows how to build a layout inside of a resizable Panel Control.

Create the Panel

First we must create the panel instance, like this:

1<div id="demo"></div> 
view plain | print | ?
1var panel = new YAHOO.widget.Panel('demo', { 
2    draggable: true
3    close: false
4    width: '500px'
5    xy: [100, 100] 
6}); 
7panel.render(); 
view plain | print | ?

Now let's give it some content. Note that we are adding a DIV to the body with an id of layout. This will be the element we anchor the layout to.

1var panel = new YAHOO.widget.Panel('demo', { 
2    draggable: true
3    close: false
4    width: '500px'
5    xy: [100, 100] 
6}); 
7panel.setHeader('Test Panel'); 
8panel.setBody('<div id="layout"></div>'); 
9panel.render(); 
view plain | print | ?

Adding the Layout instance

Now we need to listen for the beforeRender event to render our Layout.

Inside of the beforeRender event, we will wait for the element layout to appear in the document, then we will setup our Layout instance.

The layout instance we are creating will have top, left, bottom and center units configured below:

1panel.setBody('<div id="layout"></div>'); 
2panel.beforeRenderEvent.subscribe(function() { 
3    Event.onAvailable('layout'function() { 
4        layout = new YAHOO.widget.Layout('layout', { 
5            height: 400, 
6            units: [ 
7                { position: 'top', height: 25, resize: false, body: 'Top', gutter: '2' }, 
8                { position: 'left', width: 150, resize: true, body: 'Left', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 }, 
9                { position: 'bottom', height: 25, body: 'Bottom', gutter: '2' }, 
10                { position: 'center', body: 'Center Unit', gutter: '0 2 0 0' } 
11            ] 
12        }); 
13 
14        layout.render(); 
15    }); 
16}); 
17panel.render(); 
view plain | print | ?

Now we have a layout inside of a Panel.

Make the Panel resizable

After we have rendered our panel, we can attach the Resize Utility to it like this:

1panel.render(); 
2resize = new YAHOO.util.Resize('demo', { 
3    handles: ['br'], 
4    autoRatio: true
5    status: true
6    minWidth: 380, 
7    minHeight: 400 
8}); 
view plain | print | ?

Now give the resize handle a little CSS to make it look nicer.

1#demo .yui-resize-handle-br { 
2    height11px
3    width11px
4    background-position-20px -60px
5    background-colortransparent
6} 
view plain | print | ?

This will place a handle at the bottom right corner of the panel. This will only resize the outside portion of the panel, but we want the inside to resize properly.

Now we need to listen for the resize event on the Resize instance and do a little math.

1resize.on('resize'function(args) { 
2    var panelHeight = args.height; 
3    var headerHeight = this.header.offsetHeight; // Content + Padding + Border 
4    var bodyHeight = (panelHeight - headerHeight); 
5    var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING; 
6 
7    YAHOO.util.Dom.setStyle(this.body, 'height', bodyContentHeight + 'px'); 
8 
9    if (IE_SYNC) { 
10 
11        // Keep the underlay and iframe size in sync. 
12 
13        // You could also set the width property, to achieve the  
14        // same results, if you wanted to keep the panel's internal 
15        // width property in sync with the DOM width.  
16 
17        this.sizeUnderlay(); 
18 
19        // Syncing the iframe can be expensive. Disable iframe if you 
20        // don't need it. 
21 
22        this.syncIframe(); 
23    } 
24}, panel, true); 
view plain | print | ?

Now we have the Panel resizing the way we want, but the layout is not resizing to match. Inside the resize event from the Resize instance we need to add this at the bottom:

1layout.set('height', bodyContentHeight); 
2layout.set('width', (args.width - PANEL_BODY_PADDING)); 
3layout.resize(); 
view plain | print | ?

Now we have a resizable panel with a fixed layout inside.

Full Example Source

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event, 
4        layout = null
5        resize = null
6 
7    Event.onDOMReady(function() { 
8        // Setup constants 
9 
10        // QUIRKS FLAG, FOR BOX MODEL 
11        var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat"); 
12 
13        // UNDERLAY/IFRAME SYNC REQUIRED 
14        var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS)); 
15 
16        // PADDING USED FOR BODY ELEMENT (Hardcoded for example) 
17        var PANEL_BODY_PADDING = (10*2) // 10px top/bottom padding applied to Panel body element. The top/bottom border width is 0 
18     
19        var panel = new YAHOO.widget.Panel('demo', { 
20            draggable: true
21            close: false
22            underlay: 'none'
23            width: '500px'
24            xy: [100, 100] 
25        }); 
26        panel.setHeader('Test Panel'); 
27        panel.setBody('<div id="layout"></div>'); 
28        panel.beforeRenderEvent.subscribe(function() { 
29            Event.onAvailable('layout'function() { 
30                layout = new YAHOO.widget.Layout('layout', { 
31                    height: 400, 
32                    width: 480, 
33                    units: [ 
34                        { position: 'top', height: 25, resize: false, body: 'Top', gutter: '2' }, 
35                        { position: 'left', width: 150, resize: true, body: 'Left', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 }, 
36                        { position: 'bottom', height: 25, body: 'Bottom', gutter: '2' }, 
37                        { position: 'center', body: 'Center Unit', gutter: '0 2 0 0' } 
38                    ] 
39                }); 
40 
41                layout.render(); 
42            }); 
43        }); 
44        panel.render(); 
45        resize = new YAHOO.util.Resize('demo', { 
46            handles: ['br'], 
47            autoRatio: true
48            status: true
49            minWidth: 380, 
50            minHeight: 400 
51        }); 
52        resize.on('resize'function(args) { 
53            var panelHeight = args.height; 
54            var headerHeight = this.header.offsetHeight; // Content + Padding + Border 
55            var bodyHeight = (panelHeight - headerHeight); 
56            var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING; 
57 
58            YAHOO.util.Dom.setStyle(this.body, 'height', bodyContentHeight + 'px'); 
59 
60            if (IE_SYNC) { 
61 
62                // Keep the underlay and iframe size in sync. 
63 
64                // You could also set the width property, to achieve the  
65                // same results, if you wanted to keep the panel's internal 
66                // width property in sync with the DOM width.  
67 
68                this.sizeUnderlay(); 
69 
70                // Syncing the iframe can be expensive. Disable iframe if you 
71                // don't need it. 
72 
73                this.syncIframe(); 
74            } 
75 
76            layout.set('height', bodyContentHeight); 
77            layout.set('width', (args.width - PANEL_BODY_PADDING)); 
78            layout.resize(); 
79             
80        }, panel, true); 
81    }); 
82})(); 
view plain | print | ?

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings