Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / widgets / layout / AnchorLayout.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
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
96     type : 'anchor',
97
98     /**
99      * @cfg {String} defaultAnchor
100      *
101      * default anchor for all child container items applied if no anchor or specific width is set on the child item.  Defaults to '100%'.
102      *
103      */
104     defaultAnchor : '100%',
105
106     parseAnchorRE : /^(r|right|b|bottom)$/i,
107
108     getLayoutTargetSize : function() {
109         var target = this.container.getLayoutTarget();
110         if (!target) {
111             return {};
112         }
113         // Style Sized (scrollbars not included)
114         return target.getStyleSize();
115     },
116
117     // private
118     onLayout : function(ct, target){
119         Ext.layout.AnchorLayout.superclass.onLayout.call(this, ct, target);
120         var size = this.getLayoutTargetSize();
121
122         var w = size.width, h = size.height;
123
124         if(w < 20 && h < 20){
125             return;
126         }
127
128         // find the container anchoring size
129         var aw, ah;
130         if(ct.anchorSize){
131             if(typeof ct.anchorSize == 'number'){
132                 aw = ct.anchorSize;
133             }else{
134                 aw = ct.anchorSize.width;
135                 ah = ct.anchorSize.height;
136             }
137         }else{
138             aw = ct.initialConfig.width;
139             ah = ct.initialConfig.height;
140         }
141
142         var cs = this.getRenderedItems(ct), len = cs.length, i, c, a, cw, ch, el, vs, boxes = [];
143         for(i = 0; i < len; i++){
144             c = cs[i];
145             el = c.getPositionEl();
146
147             // If a child container item has no anchor and no specific width, set the child to the default anchor size
148             if (!c.anchor && c.items && !Ext.isNumber(c.width) && !(Ext.isIE6 && Ext.isStrict)){
149                 c.anchor = this.defaultAnchor;
150             }
151
152             if(c.anchor){
153                 a = c.anchorSpec;
154                 if(!a){ // cache all anchor values
155                     vs = c.anchor.split(' ');
156                     c.anchorSpec = a = {
157                         right: this.parseAnchor(vs[0], c.initialConfig.width, aw),
158                         bottom: this.parseAnchor(vs[1], c.initialConfig.height, ah)
159                     };
160                 }
161                 cw = a.right ? this.adjustWidthAnchor(a.right(w) - el.getMargins('lr'), c) : undefined;
162                 ch = a.bottom ? this.adjustHeightAnchor(a.bottom(h) - el.getMargins('tb'), c) : undefined;
163
164                 if(cw || ch){
165                     boxes.push({
166                         comp: c,
167                         width: cw || undefined,
168                         height: ch || undefined
169                     });
170                 }
171             }
172         }
173         for (i = 0, len = boxes.length; i < len; i++) {
174             c = boxes[i];
175             c.comp.setSize(c.width, c.height);
176         }
177     },
178
179     // private
180     parseAnchor : function(a, start, cstart){
181         if(a && a != 'none'){
182             var last;
183             // standard anchor
184             if(this.parseAnchorRE.test(a)){
185                 var diff = cstart - start;
186                 return function(v){
187                     if(v !== last){
188                         last = v;
189                         return v - diff;
190                     }
191                 }
192             // percentage
193             }else if(a.indexOf('%') != -1){
194                 var ratio = parseFloat(a.replace('%', ''))*.01;
195                 return function(v){
196                     if(v !== last){
197                         last = v;
198                         return Math.floor(v*ratio);
199                     }
200                 }
201             // simple offset adjustment
202             }else{
203                 a = parseInt(a, 10);
204                 if(!isNaN(a)){
205                     return function(v){
206                         if(v !== last){
207                             last = v;
208                             return v + a;
209                         }
210                     }
211                 }
212             }
213         }
214         return false;
215     },
216
217     // private
218     adjustWidthAnchor : function(value, comp){
219         return value;
220     },
221
222     // private
223     adjustHeightAnchor : function(value, comp){
224         return value;
225     }
226
227     /**
228      * @property activeItem
229      * @hide
230      */
231 });
232 Ext.Container.LAYOUTS['anchor'] = Ext.layout.AnchorLayout;