3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.layout.container.Anchor
17 * @extends Ext.layout.container.Container
19 * This is a layout that enables anchoring of contained elements relative to the container's dimensions.
20 * If the container is resized, all anchored items are automatically rerendered according to their
21 * `{@link #anchor}` rules.
23 * This class is intended to be extended or created via the {@link Ext.container.AbstractContainer#layout layout}: 'anchor'
24 * config, and should generally not need to be created directly via the new keyword.
26 * AnchorLayout does not have any direct config options (other than inherited ones). By default,
27 * AnchorLayout will calculate anchor measurements based on the size of the container itself. However, the
28 * container using the AnchorLayout can supply an anchoring-specific config property of `anchorSize`.
30 * If anchorSize is specifed, the layout will use it as a virtual container for the purposes of calculating
31 * anchor measurements based on it instead, allowing the container to be sized independently of the anchoring
35 * Ext.create('Ext.Panel', {
38 * title: "AnchorLayout Panel",
40 * renderTo: Ext.getBody(),
44 * title: '75% Width and 20% Height',
49 * title: 'Offset -300 Width & -200 Height',
54 * title: 'Mixed Offset and Percent',
60 Ext.define('Ext.layout.container.Anchor', {
62 /* Begin Definitions */
64 alias: 'layout.anchor',
65 extend: 'Ext.layout.container.Container',
66 alternateClassName: 'Ext.layout.AnchorLayout',
71 * @cfg {String} anchor
73 * This configuation option is to be applied to **child `items`** of a container managed by
74 * this layout (ie. configured with `layout:'anchor'`).
76 * This value is what tells the layout how an item should be anchored to the container. `items`
77 * added to an AnchorLayout accept an anchoring-specific config property of **anchor** which is a string
78 * containing two values: the horizontal anchor value and the vertical anchor value (for example, '100% 50%').
79 * The following types of anchor values are supported:
81 * - **Percentage** : Any value between 1 and 100, expressed as a percentage.
83 * The first anchor is the percentage width that the item should take up within the container, and the
84 * second is the percentage height. For example:
86 * // two values specified
87 * anchor: '100% 50%' // render item complete width of the container and
88 * // 1/2 height of the container
89 * // one value specified
90 * anchor: '100%' // the width value; the height will default to auto
92 * - **Offsets** : Any positive or negative integer value.
94 * This is a raw adjustment where the first anchor is the offset from the right edge of the container,
95 * and the second is the offset from the bottom edge. For example:
97 * // two values specified
98 * anchor: '-50 -100' // render item the complete width of the container
99 * // minus 50 pixels and
100 * // the complete height minus 100 pixels.
101 * // one value specified
102 * anchor: '-50' // anchor value is assumed to be the right offset value
103 * // bottom offset will default to 0
105 * - **Sides** : Valid values are `right` (or `r`) and `bottom` (or `b`).
107 * Either the container must have a fixed size or an anchorSize config value defined at render time in
108 * order for these to have any effect.
112 * Anchor values can also be mixed as needed. For example, to render the width offset from the container
113 * right edge by 50 pixels and 75% of the container's height use:
120 * @cfg {String} defaultAnchor
121 * Default anchor for all child <b>container</b> items applied if no anchor or specific width is set on the child item. Defaults to '100%'.
123 defaultAnchor: '100%',
125 parseAnchorRE: /^(r|right|b|bottom)$/i,
128 onLayout: function() {
129 this.callParent(arguments);
132 size = me.getLayoutTargetSize(),
134 target = me.getTarget(),
135 ownerWidth = size.width,
136 ownerHeight = size.height,
137 overflow = target.getStyle('overflow'),
138 components = me.getVisibleItems(owner),
139 len = components.length,
141 box, newTargetSize, component, anchorSpec, calcWidth, calcHeight,
144 if (ownerWidth < 20 && ownerHeight < 20) {
148 // Anchor layout uses natural HTML flow to arrange the child items.
149 // To ensure that all browsers (I'm looking at you IE!) add the bottom margin of the last child to the
150 // containing element height, we create a zero-sized element with style clear:both to force a "new line"
152 me.clearEl = target.createChild({
153 cls: Ext.baseCSSPrefix + 'clear',
158 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
159 if (!Ext.supports.RightMargin) {
160 cleaner = Ext.Element.getRightMarginFixCleaner(target);
161 target.addCls(Ext.baseCSSPrefix + 'inline-children');
164 for (i = 0; i < len; i++) {
165 component = components[i];
168 anchorSpec = component.anchorSpec;
170 if (anchorSpec.right) {
171 calcWidth = me.adjustWidthAnchor(anchorSpec.right(ownerWidth) - el.getMargin('lr'), component);
173 calcWidth = undefined;
175 if (anchorSpec.bottom) {
176 calcHeight = me.adjustHeightAnchor(anchorSpec.bottom(ownerHeight) - el.getMargin('tb'), component);
178 calcHeight = undefined;
182 component: component,
184 width: calcWidth || undefined,
185 height: calcHeight || undefined
189 component: component,
195 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
196 if (!Ext.supports.RightMargin) {
197 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) {
258 configureItem: function(item) {
267 if (!item.anchor && item.items && !Ext.isNumber(item.width) && !(Ext.isIE6 && Ext.isStrict)) {
268 item.anchor = anchor = me.defaultAnchor;
271 // find the container anchoring size
272 if (owner.anchorSize) {
273 if (typeof owner.anchorSize == 'number') {
274 anchorWidth = owner.anchorSize;
277 anchorWidth = owner.anchorSize.width;
278 anchorHeight = owner.anchorSize.height;
282 anchorWidth = owner.initialConfig.width;
283 anchorHeight = owner.initialConfig.height;
287 // cache all anchor values
288 anchorsArray = anchor.split(' ');
289 item.anchorSpec = anchorSpec = {
290 right: me.parseAnchor(anchorsArray[0], item.initialConfig.width, anchorWidth),
291 bottom: me.parseAnchor(anchorsArray[1], item.initialConfig.height, anchorHeight)
294 if (anchorSpec.right) {
295 item.layoutManagedWidth = 1;
297 item.layoutManagedWidth = 2;
300 if (anchorSpec.bottom) {
301 item.layoutManagedHeight = 1;
303 item.layoutManagedHeight = 2;
306 item.layoutManagedWidth = 2;
307 item.layoutManagedHeight = 2;
309 this.callParent(arguments);