2 * @class Ext.layout.container.Anchor
3 * @extends Ext.layout.container.Container
4 * <p>This is a layout that enables anchoring of contained elements relative to the container's dimensions.
5 * If the container is resized, all anchored items are automatically rerendered according to their
6 * <b><tt>{@link #anchor}</tt></b> rules.</p>
7 * <p>This class is intended to be extended or created via the layout: 'anchor' {@link Ext.layout.container.AbstractContainer#layout}
8 * config, and should generally not need to be created directly via the new keyword.</p>
9 * <p>AnchorLayout does not have any direct config options (other than inherited ones). By default,
10 * AnchorLayout will calculate anchor measurements based on the size of the container itself. However, the
11 * container using the AnchorLayout can supply an anchoring-specific config property of <b>anchorSize</b>.
12 * If anchorSize is specifed, the layout will use it as a virtual container for the purposes of calculating
13 * anchor measurements based on it instead, allowing the container to be sized independently of the anchoring
15 * {@img Ext.layout.container.Anchor/Ext.layout.container.Anchor.png Ext.layout.container.Anchor container layout}
17 Ext.create('Ext.Panel', {
20 title: "AnchorLayout Panel",
22 renderTo: Ext.getBody(),
25 title: '75% Width and 20% Height',
29 title: 'Offset -300 Width & -200 Height',
33 title: 'Mixed Offset and Percent',
39 Ext.define('Ext.layout.container.Anchor', {
41 /* Begin Definitions */
43 alias: 'layout.anchor',
44 extend: 'Ext.layout.container.Container',
45 alternateClassName: 'Ext.layout.AnchorLayout',
50 * @cfg {String} anchor
51 * <p>This configuation option is to be applied to <b>child <tt>items</tt></b> of a container managed by
52 * this layout (ie. configured with <tt>layout:'anchor'</tt>).</p><br/>
54 * <p>This value is what tells the layout how an item should be anchored to the container. <tt>items</tt>
55 * added to an AnchorLayout accept an anchoring-specific config property of <b>anchor</b> which is a string
56 * containing two values: the horizontal anchor value and the vertical anchor value (for example, '100% 50%').
57 * The following types of anchor values are supported:<div class="mdetail-params"><ul>
59 * <li><b>Percentage</b> : Any value between 1 and 100, expressed as a percentage.<div class="sub-desc">
60 * The first anchor is the percentage width that the item should take up within the container, and the
61 * second is the percentage height. For example:<pre><code>
62 // two values specified
63 anchor: '100% 50%' // render item complete width of the container and
64 // 1/2 height of the container
65 // one value specified
66 anchor: '100%' // the width value; the height will default to auto
67 * </code></pre></div></li>
69 * <li><b>Offsets</b> : Any positive or negative integer value.<div class="sub-desc">
70 * This is a raw adjustment where the first anchor is the offset from the right edge of the container,
71 * and the second is the offset from the bottom edge. For example:<pre><code>
72 // two values specified
73 anchor: '-50 -100' // render item the complete width of the container
74 // minus 50 pixels and
75 // the complete height minus 100 pixels.
76 // one value specified
77 anchor: '-50' // anchor value is assumed to be the right offset value
78 // bottom offset will default to 0
79 * </code></pre></div></li>
81 * <li><b>Sides</b> : Valid values are <tt>'right'</tt> (or <tt>'r'</tt>) and <tt>'bottom'</tt>
82 * (or <tt>'b'</tt>).<div class="sub-desc">
83 * Either the container must have a fixed size or an anchorSize config value defined at render time in
84 * order for these to have any effect.</div></li>
86 * <li><b>Mixed</b> : <div class="sub-desc">
87 * Anchor values can also be mixed as needed. For example, to render the width offset from the container
88 * right edge by 50 pixels and 75% of the container's height use:
91 * </code></pre></div></li>
100 * @cfg {String} defaultAnchor
102 * default anchor for all child container items applied if no anchor or specific width is set on the child item. Defaults to '100%'.
105 defaultAnchor: '100%',
107 parseAnchorRE: /^(r|right|b|bottom)$/i,
110 onLayout: function() {
111 this.callParent(arguments);
114 size = me.getLayoutTargetSize(),
116 target = me.getTarget(),
117 ownerWidth = size.width,
118 ownerHeight = size.height,
119 overflow = target.getStyle('overflow'),
120 components = me.getVisibleItems(owner),
121 len = components.length,
123 box, newTargetSize, anchorWidth, anchorHeight, component, anchorSpec, calcWidth, calcHeight,
124 anchorsArray, anchor, i, el;
126 if (ownerWidth < 20 && ownerHeight < 20) {
130 // Anchor layout uses natural HTML flow to arrange the child items.
131 // To ensure that all browsers (I'm looking at you IE!) add the bottom margin of the last child to the
132 // containing element height, we create a zero-sized element with style clear:both to force a "new line"
134 me.clearEl = target.createChild({
135 cls: Ext.baseCSSPrefix + 'clear',
140 // find the container anchoring size
141 if (owner.anchorSize) {
142 if (typeof owner.anchorSize == 'number') {
143 anchorWidth = owner.anchorSize;
146 anchorWidth = owner.anchorSize.width;
147 anchorHeight = owner.anchorSize.height;
151 anchorWidth = owner.initialConfig.width;
152 anchorHeight = owner.initialConfig.height;
155 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
156 if (!Ext.supports.RightMargin) {
157 target.addCls(Ext.baseCSSPrefix + 'inline-children');
160 for (i = 0; i < len; i++) {
161 component = components[i];
163 anchor = component.anchor;
165 if (!component.anchor && component.items && !Ext.isNumber(component.width) && !(Ext.isIE6 && Ext.isStrict)) {
166 component.anchor = anchor = me.defaultAnchor;
170 anchorSpec = component.anchorSpec;
171 // cache all anchor values
173 anchorsArray = anchor.split(' ');
174 component.anchorSpec = anchorSpec = {
175 right: me.parseAnchor(anchorsArray[0], component.initialConfig.width, anchorWidth),
176 bottom: me.parseAnchor(anchorsArray[1], component.initialConfig.height, anchorHeight)
179 calcWidth = anchorSpec.right ? me.adjustWidthAnchor(anchorSpec.right(ownerWidth) - el.getMargin('lr'), component) : undefined;
180 calcHeight = anchorSpec.bottom ? me.adjustHeightAnchor(anchorSpec.bottom(ownerHeight) - el.getMargin('tb'), component) : undefined;
183 component: component,
185 width: calcWidth || undefined,
186 height: calcHeight || undefined
190 component: component,
196 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
197 if (!Ext.supports.RightMargin) {
198 target.removeCls(Ext.baseCSSPrefix + 'inline-children');
201 for (i = 0; i < len; i++) {
203 me.setItemSize(box.component, box.width, box.height);
206 if (overflow && overflow != 'hidden' && !me.adjustmentPass) {
207 newTargetSize = me.getLayoutTargetSize();
208 if (newTargetSize.width != size.width || newTargetSize.height != size.height) {
209 me.adjustmentPass = true;
214 delete me.adjustmentPass;
218 parseAnchor: function(a, start, cstart) {
219 if (a && a != 'none') {
222 if (this.parseAnchorRE.test(a)) {
223 var diff = cstart - start;
229 else if (a.indexOf('%') != -1) {
230 ratio = parseFloat(a.replace('%', '')) * 0.01;
232 return Math.floor(v * ratio);
235 // simple offset adjustment
249 adjustWidthAnchor: function(value, comp) {
254 adjustHeightAnchor: function(value, comp) {