3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
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>
22 var viewport = new Ext.Viewport({
24 anchorSize: {width:800, height:600},
44 Ext.layout.AnchorLayout = Ext.extend(Ext.layout.ContainerLayout, {
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/>
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>
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>
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>
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>
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:
87 * </code></pre></div></li>
99 * @cfg {String} defaultAnchor
101 * default anchor for all child container items applied if no anchor or specific width is set on the child item. Defaults to '100%'.
104 defaultAnchor : '100%',
106 parseAnchorRE : /^(r|right|b|bottom)$/i,
108 getLayoutTargetSize : function() {
109 var target = this.container.getLayoutTarget();
113 // Style Sized (scrollbars not included)
114 return target.getStyleSize();
118 onLayout : function(ct, target){
119 Ext.layout.AnchorLayout.superclass.onLayout.call(this, ct, target);
120 var size = this.getLayoutTargetSize();
122 var w = size.width, h = size.height;
124 if(w < 20 && h < 20){
128 // find the container anchoring size
131 if(typeof ct.anchorSize == 'number'){
134 aw = ct.anchorSize.width;
135 ah = ct.anchorSize.height;
138 aw = ct.initialConfig.width;
139 ah = ct.initialConfig.height;
142 var cs = this.getRenderedItems(ct), len = cs.length, i, c, a, cw, ch, el, vs, boxes = [];
143 for(i = 0; i < len; i++){
145 el = c.getPositionEl();
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;
154 if(!a){ // cache all anchor values
155 vs = c.anchor.split(' ');
157 right: this.parseAnchor(vs[0], c.initialConfig.width, aw),
158 bottom: this.parseAnchor(vs[1], c.initialConfig.height, ah)
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;
167 width: cw || undefined,
168 height: ch || undefined
173 for (i = 0, len = boxes.length; i < len; i++) {
175 c.comp.setSize(c.width, c.height);
180 parseAnchor : function(a, start, cstart){
181 if(a && a != 'none'){
184 if(this.parseAnchorRE.test(a)){
185 var diff = cstart - start;
193 }else if(a.indexOf('%') != -1){
194 var ratio = parseFloat(a.replace('%', ''))*.01;
198 return Math.floor(v*ratio);
201 // simple offset adjustment
218 adjustWidthAnchor : function(value, comp){
223 adjustHeightAnchor : function(value, comp){
228 * @property activeItem
232 Ext.Container.LAYOUTS['anchor'] = Ext.layout.AnchorLayout;