Yahoo! Developer Network Home - Help

YUI Library Examples: Layout Manager (beta): Simple Application

Layout Manager (beta): Simple Application

This example shows an simple application built using the Layout Mananger, Calendar Control, Rich Text Editor and Connection Manager.

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    close: false
3    underlay: false
4    width: '700px'
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    close: false
3    underlay: false
4    width: '700px'
5    xy: [100, 100] 
6}); 
7panel.setHeader('Simple Application'); 
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: 30, header: 'Date Editor', gutter: '2' }, 
8                { position: 'left', width: 205, body: '', gutter: '0 5 0 2' }, 
9                { position: 'bottom', height: 25, id: 'status', body: 'Status', gutter: '2' }, 
10                { position: 'center', body: 'Select a date..', gutter: '0 2 0 0' } 
11            ] 
12        }); 
13        layout.render(); 
14    }); 
15}); 
16panel.render(); 
view plain | print | ?

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    proxy: true
7    minWidth: 700, 
8    minHeight: 400 
9}); 
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 h = args.height; 
3    var hh = this.header.clientHeight; 
4    var padding = ((10 * 2) + 2); //Sam's skin applied a 10px padding and a 1 px border to the element.. 
5    var bh = (h - hh - padding); 
6    Dom.setStyle(this.body, 'height', bh + 'px'); 
7}, 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', bh); 
2layout.set('width', (args.width - padding));     
3layout.resize(); 
view plain | print | ?

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

Adding the calendar

Now we listen for the layout's render event and setup our Calendar.

Inside the render event we will call the layout method getUnitByPosition('left') to get the left unit's instance. Then we will create an empty DIV and append it to the body element of the left unit.

Then we will pass that element as the root node for the Calendar Control and render it.

1layout.on('render'function() { 
2    var l = layout.getUnitByPosition('left'); 
3    var el = document.createElement('div'); 
4    l.body.appendChild(el); 
5    cal = new YAHOO.widget.Calendar(el); 
6    cal.render(); 
7}, layout, true); 
view plain | print | ?

Adding the Rich Text Editor

Using the same technique as above, we will add a Rich Text Editor to the center unit.

Using the layout's getUnitByPosition('center') method, we will set the body of the unit to the textarea that we will convert into an Simple Editor instance. We will also set a custom toolbar to limit the number of buttons.

1layout.on('render'function() { 
2    var c = layout.getUnitByPosition('center'); 
3    c.set('body''<textarea id="editor"></textarea>'); 
4    editor = new YAHOO.widget.SimpleEditor('editor', { 
5        height: '305px'
6        width: c.get('width') + 'px'
7        dompath: false
8        animate: false
9        toolbar: { 
10            grouplabels: false
11            buttons: [ 
12                { group: 'textstyle', label: 'Font Style'
13                    buttons: [ 
14                    { type: 'select', label: 'Arial', value: 'fontname', disabled: true
15                        menu: [ 
16                            { text: 'Arial', checked: true }, 
17                            { text: 'Arial Black' }, 
18                            { text: 'Comic Sans MS' }, 
19                            { text: 'Courier New' }, 
20                            { text: 'Lucida Console' }, 
21                            { text: 'Tahoma' }, 
22                            { text: 'Times New Roman' }, 
23                            { text: 'Trebuchet MS' }, 
24                            { text: 'Verdana' } 
25                        ] 
26                    }, 
27                    { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }, 
28                    { type: 'separator' }, 
29                    { type: 'push', label: 'Bold', value: 'bold' }, 
30                    { type: 'push', label: 'Italic', value: 'italic' }, 
31                    { type: 'push', label: 'Underline', value: 'underline' }, 
32                    { type: 'separator' }, 
33                    { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true }, 
34                    { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true } 
35                    ] 
36                } 
37            ] 
38        } 
39    }); 
40    editor.on('editorContentLoaded'function() { 
41        var d = new Date(); 
42        var today = d.getMonth() + 1 + '/' + d.getDate() + '/' + d.getFullYear(); 
43        dateSelected = [today]; 
44        layout.getUnitByPosition('top').set('header''Editing Date: ' + today); 
45        cal.cfg.setProperty('selected', today); 
46        cal.render(); 
47    }); 
48    editor.render(); 
view plain | print | ?

Make the Editor resize with the Layout and Panel

Now we need to add some code to the resize event to resize the Editor instance when the Layout and Panel are resized.

1//Editor Resize 
2var th = (editor.toolbar.get('element').clientHeight + 2); //It has a 1px border.. 
3var eH = (h - th); 
4editor.set('width', args.width + 'px'); 
5editor.set('height', eH + 'px'); 
6editor.show(); 
view plain | print | ?

Making them talk to each other

Now we need to connect the Calendar selectEvent to the Layout Manager.

1cal = new YAHOO.widget.Calendar(el); 
2cal.selectEvent.subscribe(function(ev, args) { 
3    var d = args[0][0]; 
4    layout.getUnitByPosition('top').set('header''Editing Date: ' + d[1] + '/' + d[2] + '/' + d[0]); 
5}); 
6cal.render(); 
view plain | print | ?

Now the top unit's header will be updated with the selected date of the Calendar and the Editor will be enabled.

From here, we will set up some pseudo code for saving and storing the data entered on each date selection.

Basically the code below does the following:

  • When a date is selected, set the var dateSelected to the date.
  • Then select that date
  • If the object editorData contains a key matching this date, update the Editor's content with the value.
  • If the var dateSelected was already set, do this:
    • Update the body of the bottom unit with an animated image and status message.
    • Save the Editor HTML to a var html
    • Add a key to editorData for the date and assign its value to the html from the Editor.
    • Fire the Connection Manager request to save the data (this example doesn't do that).
    • Clear the Editor's content.

1cal = new YAHOO.widget.Calendar(el); 
2cal.selectEvent.subscribe(function(ev, args) { 
3    if (dateSelected) { 
4        //Connection Manager 
5        layout.getUnitByPosition('bottom').set('body''<img src="assets/progress.gif"> Sending Data...'); 
6        var html = editor.getEditorHTML(); 
7        var url = 'assets/post4.php?html=' + html; 
8        editorData[dateSelected] = html; 
9        conn = YAHOO.util.Connect.asyncRequest('POST', url, { 
10            success: function() { 
11                layout.getUnitByPosition('bottom').set('body''Data Saved..'); 
12            }, 
13            failure: function() { 
14            } 
15        }); 
16        editor.setEditorHTML(' '); 
17    } 
18    var d = args[0][0]; 
19    dateSelected = d[1] + '/' + d[2] + '/' + d[0]; 
20    layout.getUnitByPosition('top').set('header''Editing Date: ' + d[1] + '/' + d[2] + '/' + d[0]); 
21    if (dateSelected && editorData[dateSelected]) { 
22        editor.setEditorHTML(editorData[dateSelected]); 
23    } 
24 
25    var dates = [dateSelected]; 
26    for (var i in editorData) { 
27        dates[dates.length] = i; 
28    } 
29    cal.cfg.setProperty('selected', dates.join(',')); 
30    cal.render(); 
31}); 
32cal.render(); 
view plain | print | ?

Full Example Source

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event, 
4        layout = null
5        cal = null
6        editor = null
7        conn = null 
8        dateSelected = null
9        editorData = {}; 
10 
11    var panel = new YAHOO.widget.Panel('demo', { 
12        width: '700px'
13        underlay: false
14        close: false
15        xy: [100, 100] 
16    }); 
17    panel.setHeader('Editor'); 
18    panel.setBody('<div id="layout"></div>'); 
19    panel.renderEvent.subscribe(function() { 
20        Event.onAvailable('layout'function() { 
21            layout = new YAHOO.widget.Layout('layout', { 
22                height: 400, 
23                units: [ 
24                    { position: 'top', height: 25, header: 'Date Editor', gutter: '2' }, 
25                    { position: 'left', width: 205, body: '', gutter: '0 5 0 2' }, 
26                    { position: 'bottom', height: 25, id: 'status', body: 'Status', gutter: '2' }, 
27                    { position: 'center', body: 'Select a date..', gutter: '0 2 0 0' } 
28                ] 
29            }); 
30             
31            layout.on('render'function() { 
32                var c = layout.getUnitByPosition('center'); 
33                c.set('body''<textarea id="caleditor"></textarea>'); 
34                editor = new YAHOO.widget.SimpleEditor('caleditor', { 
35                    height: '305px'
36                    width: c.get('width') + 'px'
37                    dompath: false
38                    animate: false
39                    focusAtStart: true
40                    toolbar: { 
41                        grouplabels: false
42                        buttons: [ 
43                            { group: 'textstyle', label: 'Font Style'
44                                buttons: [ 
45                                    { type: 'select', label: 'Arial', value: 'fontname', disabled: true
46                                        menu: [ 
47                                            { text: 'Arial', checked: true }, 
48                                            { text: 'Arial Black' }, 
49                                            { text: 'Comic Sans MS' }, 
50                                            { text: 'Courier New' }, 
51                                            { text: 'Lucida Console' }, 
52                                            { text: 'Tahoma' }, 
53                                            { text: 'Times New Roman' }, 
54                                            { text: 'Trebuchet MS' }, 
55                                            { text: 'Verdana' } 
56                                        ] 
57                                    }, 
58                                    { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }, 
59                                    { type: 'separator' }, 
60                                    { type: 'push', label: 'Bold', value: 'bold' }, 
61                                    { type: 'push', label: 'Italic', value: 'italic' }, 
62                                    { type: 'push', label: 'Underline', value: 'underline' }, 
63                                    { type: 'separator' }, 
64                                    { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true }, 
65                                    { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true } 
66                                ] 
67                            } 
68                        ] 
69                    } 
70                }); 
71                editor.on('editorContentLoaded'function() { 
72                    var d = new Date(); 
73                    var today = d.getMonth() + 1 + '/' + d.getDate() + '/' + d.getFullYear(); 
74                    dateSelected = [today]; 
75                    layout.getUnitByPosition('top').set('header''Editing Date: ' + today); 
76                    cal.cfg.setProperty('selected', today); 
77                    cal.render(); 
78                }); 
79                editor.render(); 
80                var l = layout.getUnitByPosition('left'); 
81                var el = document.createElement('div'); 
82                l.body.appendChild(el); 
83                cal = new YAHOO.widget.Calendar(el); 
84                cal.selectEvent.subscribe(function(ev, args) { 
85                    if (dateSelected) { 
86                        //Connection Manager 
87                        layout.getUnitByPosition('bottom').set('body''<img src="assets/progress.gif"> Sending Data...'); 
88                        var html = editor.getEditorHTML(); 
89                        var url = 'assets/post4.php?html=' + html; 
90                        editorData[dateSelected] = html; 
91                        conn = YAHOO.util.Connect.asyncRequest('POST', url, { 
92                            success: function() { 
93                                layout.getUnitByPosition('bottom').set('body''Data Saved..'); 
94                            }, 
95                            failure: function() { 
96                            } 
97                        }); 
98                        editor.setEditorHTML(' '); 
99                    } 
100                    var d = args[0][0]; 
101                    dateSelected = d[1] + '/' + d[2] + '/' + d[0]; 
102                    layout.getUnitByPosition('top').set('header''Editing Date: ' + d[1] + '/' + d[2] + '/' + d[0]); 
103                    if (dateSelected && editorData[dateSelected]) { 
104                        editor.setEditorHTML(editorData[dateSelected]); 
105                    } 
106 
107                    var dates = [dateSelected]; 
108                    for (var i in editorData) { 
109                        dates[dates.length] = i; 
110                    } 
111                    cal.cfg.setProperty('selected', dates.join(',')); 
112                    cal.render(); 
113                }); 
114                cal.render(); 
115            }); 
116            layout.render(); 
117        }); 
118    }); 
119    panel.render(document.body); 
120    resize = new YAHOO.util.Resize('demo', { 
121        handles: ['br'], 
122        autoRatio: true
123        status: true
124        proxy: true
125        minWidth: 700, 
126        minHeight: 400 
127    }); 
128    resize.on('startResize'function() { 
129        editor.set('disbaled'true); 
130        editor.hide(); 
131    }); 
132    resize.on('resize'function(args) { 
133        var h = args.height; 
134        var hh = this.header.clientHeight; 
135        var padding = ((10 * 2) + 2); //Sam's skin applied a 10px padding and a 1 px border to the element.. 
136        var bh = (h - hh - padding); 
137        Dom.setStyle(this.body, 'height', bh + 'px'); 
138        layout.set('height', bh); 
139        layout.set('width', (args.width - padding)); 
140        layout.resize(); 
141 
142        //Editor Resize 
143        var th = (editor.toolbar.get('element').clientHeight + 2); //It has a 1px border.. 
144        var eH = (h - th); 
145        editor.set('width', args.width + 'px'); 
146        editor.set('height', eH + 'px'); 
147        editor.show(); 
148    }, panel, true); 
149     
150})(); 
view plain | print | ?

Copyright © 2008 Yahoo! Inc. All rights reserved.

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