Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / widgets / layout / AnchorLayout.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.layout.AnchorLayout
9  * @extends Ext.layout.ContainerLayout
10  * <p>This is a layout that enables anchoring of contained elements relative to the container's dimensions.
11  * If the container is resized, all anchored items are automatically rerendered according to their
12  * <b><tt>{@link #anchor}</tt></b> rules.</p>
13  * <p>This class is intended to be extended or created via the layout:'anchor' {@link Ext.Container#layout}
14  * config, and should generally not need to be created directly via the new keyword.</p>
15  * <p>AnchorLayout does not have any direct config options (other than inherited ones). By default,
16  * AnchorLayout will calculate anchor measurements based on the size of the container itself. However, the
17  * container using the AnchorLayout can supply an anchoring-specific config property of <b>anchorSize</b>.
18  * If anchorSize is specifed, the layout will use it as a virtual container for the purposes of calculating
19  * anchor measurements based on it instead, allowing the container to be sized independently of the anchoring
20  * logic if necessary.  For example:</p>
21  * <pre><code>
22 var viewport = new Ext.Viewport({
23     layout:'anchor',
24     anchorSize: {width:800, height:600},
25     items:[{
26         title:'Item 1',
27         html:'Content 1',
28         width:800,
29         anchor:'right 20%'
30     },{
31         title:'Item 2',
32         html:'Content 2',
33         width:300,
34         anchor:'50% 30%'
35     },{
36         title:'Item 3',
37         html:'Content 3',
38         width:600,
39         anchor:'-100 50%'
40     }]
41 });
42  * </code></pre>
43  */
44 Ext.layout.AnchorLayout = Ext.extend(Ext.layout.ContainerLayout, {
45     /**
46      * @cfg {String} anchor
47      * <p>This configuation option is to be applied to <b>child <tt>items</tt></b> of a container managed by
48      * this layout (ie. configured with <tt>layout:'anchor'</tt>).</p><br/>
49      *
50      * <p>This value is what tells the layout how an item should be anchored to the container. <tt>items</tt>
51      * added to an AnchorLayout accept an anchoring-specific config property of <b>anchor</b> which is a string
52      * containing two values: the horizontal anchor value and the vertical anchor value (for example, '100% 50%').
53      * The following types of anchor values are supported:<div class="mdetail-params"><ul>
54      *
55      * <li><b>Percentage</b> : Any value between 1 and 100, expressed as a percentage.<div class="sub-desc">
56      * The first anchor is the percentage width that the item should take up within the container, and the
57      * second is the percentage height.  For example:<pre><code>
58 // two values specified
59 anchor: '100% 50%' // render item complete width of the container and
60                    // 1/2 height of the container
61 // one value specified
62 anchor: '100%'     // the width value; the height will default to auto
63      * </code></pre></div></li>
64      *
65      * <li><b>Offsets</b> : Any positive or negative integer value.<div class="sub-desc">
66      * This is a raw adjustment where the first anchor is the offset from the right edge of the container,
67      * and the second is the offset from the bottom edge. For example:<pre><code>
68 // two values specified
69 anchor: '-50 -100' // render item the complete width of the container
70                    // minus 50 pixels and
71                    // the complete height minus 100 pixels.
72 // one value specified
73 anchor: '-50'      // anchor value is assumed to be the right offset value
74                    // bottom offset will default to 0
75      * </code></pre></div></li>
76      *
77      * <li><b>Sides</b> : Valid values are <tt>'right'</tt> (or <tt>'r'</tt>) and <tt>'bottom'</tt>
78      * (or <tt>'b'</tt>).<div class="sub-desc">
79      * Either the container must have a fixed size or an anchorSize config value defined at render time in
80      * order for these to have any effect.</div></li>
81      *
82      * <li><b>Mixed</b> : <div class="sub-desc">
83      * Anchor values can also be mixed as needed.  For example, to render the width offset from the container
84      * right edge by 50 pixels and 75% of the container's height use:
85      * <pre><code>
86 anchor: '-50 75%'
87      * </code></pre></div></li>
88      *
89      *
90      * </ul></div>
91      */
92
93     // private
94     monitorResize:true,
95     type: 'anchor',
96
97     getLayoutTargetSize : function() {
98         var target = this.container.getLayoutTarget();
99         if (!target) {
100             return {};
101         }
102         // Style Sized (scrollbars not included)
103         return target.getStyleSize();
104     },
105
106     // private
107     onLayout : function(ct, target){
108         Ext.layout.AnchorLayout.superclass.onLayout.call(this, ct, target);
109         var size = this.getLayoutTargetSize();
110
111         var w = size.width, h = size.height;
112
113         if(w < 20 && h < 20){
114             return;
115         }
116
117         // find the container anchoring size
118         var aw, ah;
119         if(ct.anchorSize){
120             if(typeof ct.anchorSize == 'number'){
121                 aw = ct.anchorSize;
122             }else{
123                 aw = ct.anchorSize.width;
124                 ah = ct.anchorSize.height;
125             }
126         }else{
127             aw = ct.initialConfig.width;
128             ah = ct.initialConfig.height;
129         }
130
131         var cs = this.getRenderedItems(ct), len = cs.length, i, c, a, cw, ch, el, vs;
132         for(i = 0; i < len; i++){
133             c = cs[i];
134             el = c.getPositionEl();
135             if(c.anchor){
136                 a = c.anchorSpec;
137                 if(!a){ // cache all anchor values
138                     vs = c.anchor.split(' ');
139                     c.anchorSpec = a = {
140                         right: this.parseAnchor(vs[0], c.initialConfig.width, aw),
141                         bottom: this.parseAnchor(vs[1], c.initialConfig.height, ah)
142                     };
143                 }
144                 cw = a.right ? this.adjustWidthAnchor(a.right(w) - el.getMargins('lr'), c) : undefined;
145                 ch = a.bottom ? this.adjustHeightAnchor(a.bottom(h) - el.getMargins('tb'), c) : undefined;
146
147                 if(cw || ch){
148                     c.setSize(cw || undefined, ch || undefined);
149                 }
150             }
151         }
152     },
153
154     // private
155     parseAnchor : function(a, start, cstart){
156         if(a && a != 'none'){
157             var last;
158             if(/^(r|right|b|bottom)$/i.test(a)){   // standard anchor
159                 var diff = cstart - start;
160                 return function(v){
161                     if(v !== last){
162                         last = v;
163                         return v - diff;
164                     }
165                 }
166             }else if(a.indexOf('%') != -1){
167                 var ratio = parseFloat(a.replace('%', ''))*.01;   // percentage
168                 return function(v){
169                     if(v !== last){
170                         last = v;
171                         return Math.floor(v*ratio);
172                     }
173                 }
174             }else{
175                 a = parseInt(a, 10);
176                 if(!isNaN(a)){                            // simple offset adjustment
177                     return function(v){
178                         if(v !== last){
179                             last = v;
180                             return v + a;
181                         }
182                     }
183                 }
184             }
185         }
186         return false;
187     },
188
189     // private
190     adjustWidthAnchor : function(value, comp){
191         return value;
192     },
193
194     // private
195     adjustHeightAnchor : function(value, comp){
196         return value;
197     }
198
199     /**
200      * @property activeItem
201      * @hide
202      */
203 });
204 Ext.Container.LAYOUTS['anchor'] = Ext.layout.AnchorLayout;