4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-layout-container-Anchor'>/**
19 </span> * @class Ext.layout.container.Anchor
20 * @extends Ext.layout.container.Container
22 * This is a layout that enables anchoring of contained elements relative to the container's dimensions.
23 * If the container is resized, all anchored items are automatically rerendered according to their
24 * <b><tt>{@link #anchor}</tt></b> rules.
26 * This class is intended to be extended or created via the layout: 'anchor' {@link Ext.layout.container.AbstractContainer#layout}
27 * config, and should generally not need to be created directly via the new keyword.</p>
29 * AnchorLayout does not have any direct config options (other than inherited ones). By default,
30 * AnchorLayout will calculate anchor measurements based on the size of the container itself. However, the
31 * container using the AnchorLayout can supply an anchoring-specific config property of <b>anchorSize</b>.
32 * If anchorSize is specifed, the layout will use it as a virtual container for the purposes of calculating
33 * anchor measurements based on it instead, allowing the container to be sized independently of the anchoring
36 * {@img Ext.layout.container.Anchor/Ext.layout.container.Anchor.png Ext.layout.container.Anchor container layout}
39 * Ext.create('Ext.Panel', {
42 * title: "AnchorLayout Panel",
44 * renderTo: Ext.getBody(),
47 * title: '75% Width and 20% Height',
51 * title: 'Offset -300 Width & -200 Height',
55 * title: 'Mixed Offset and Percent',
61 Ext.define('Ext.layout.container.Anchor', {
63 /* Begin Definitions */
65 alias: 'layout.anchor',
66 extend: 'Ext.layout.container.Container',
67 alternateClassName: 'Ext.layout.AnchorLayout',
71 <span id='Ext-layout-container-Anchor-cfg-anchor'> /**
72 </span> * @cfg {String} anchor
73 * <p>This configuation option is to be applied to <b>child <tt>items</tt></b> of a container managed by
74 * this layout (ie. configured with <tt>layout:'anchor'</tt>).</p><br/>
76 * <p>This value is what tells the layout how an item should be anchored to the container. <tt>items</tt>
77 * added to an AnchorLayout accept an anchoring-specific config property of <b>anchor</b> 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:<div class="mdetail-params"><ul>
81 * <li><b>Percentage</b> : Any value between 1 and 100, expressed as a percentage.<div class="sub-desc">
82 * The first anchor is the percentage width that the item should take up within the container, and the
83 * second is the percentage height. For example:<pre><code>
84 // two values specified
85 anchor: '100% 50%' // render item complete width of the container and
86 // 1/2 height of the container
87 // one value specified
88 anchor: '100%' // the width value; the height will default to auto
89 * </code></pre></div></li>
91 * <li><b>Offsets</b> : Any positive or negative integer value.<div class="sub-desc">
92 * This is a raw adjustment where the first anchor is the offset from the right edge of the container,
93 * and the second is the offset from the bottom edge. For example:<pre><code>
94 // two values specified
95 anchor: '-50 -100' // render item the complete width of the container
96 // minus 50 pixels and
97 // the complete height minus 100 pixels.
98 // one value specified
99 anchor: '-50' // anchor value is assumed to be the right offset value
100 // bottom offset will default to 0
101 * </code></pre></div></li>
103 * <li><b>Sides</b> : Valid values are <tt>'right'</tt> (or <tt>'r'</tt>) and <tt>'bottom'</tt>
104 * (or <tt>'b'</tt>).<div class="sub-desc">
105 * Either the container must have a fixed size or an anchorSize config value defined at render time in
106 * order for these to have any effect.</div></li>
108 * <li><b>Mixed</b> : <div class="sub-desc">
109 * Anchor values can also be mixed as needed. For example, to render the width offset from the container
110 * right edge by 50 pixels and 75% of the container's height use:
111 * <pre><code>
113 * </code></pre></div></li>
116 * </ul></div>
121 <span id='Ext-layout-container-Anchor-cfg-defaultAnchor'> /**
122 </span> * @cfg {String} defaultAnchor
123 * 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%'.
125 defaultAnchor: '100%',
127 parseAnchorRE: /^(r|right|b|bottom)$/i,
130 onLayout: function() {
131 this.callParent(arguments);
134 size = me.getLayoutTargetSize(),
136 target = me.getTarget(),
137 ownerWidth = size.width,
138 ownerHeight = size.height,
139 overflow = target.getStyle('overflow'),
140 components = me.getVisibleItems(owner),
141 len = components.length,
143 box, newTargetSize, component, anchorSpec, calcWidth, calcHeight,
146 if (ownerWidth < 20 && ownerHeight < 20) {
150 // Anchor layout uses natural HTML flow to arrange the child items.
151 // To ensure that all browsers (I'm looking at you IE!) add the bottom margin of the last child to the
152 // containing element height, we create a zero-sized element with style clear:both to force a "new line"
154 me.clearEl = target.createChild({
155 cls: Ext.baseCSSPrefix + 'clear',
160 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
161 if (!Ext.supports.RightMargin) {
162 cleaner = Ext.core.Element.getRightMarginFixCleaner(target);
163 target.addCls(Ext.baseCSSPrefix + 'inline-children');
166 for (i = 0; i < len; i++) {
167 component = components[i];
170 anchorSpec = component.anchorSpec;
172 if (anchorSpec.right) {
173 calcWidth = me.adjustWidthAnchor(anchorSpec.right(ownerWidth) - el.getMargin('lr'), component);
175 calcWidth = undefined;
177 if (anchorSpec.bottom) {
178 calcHeight = me.adjustHeightAnchor(anchorSpec.bottom(ownerHeight) - el.getMargin('tb'), component);
180 calcHeight = undefined;
184 component: component,
186 width: calcWidth || undefined,
187 height: calcHeight || undefined
191 component: component,
197 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
198 if (!Ext.supports.RightMargin) {
199 target.removeCls(Ext.baseCSSPrefix + 'inline-children');
203 for (i = 0; i < len; i++) {
205 me.setItemSize(box.component, box.width, box.height);
208 if (overflow && overflow != 'hidden' && !me.adjustmentPass) {
209 newTargetSize = me.getLayoutTargetSize();
210 if (newTargetSize.width != size.width || newTargetSize.height != size.height) {
211 me.adjustmentPass = true;
216 delete me.adjustmentPass;
220 parseAnchor: function(a, start, cstart) {
221 if (a && a != 'none') {
224 if (this.parseAnchorRE.test(a)) {
225 var diff = cstart - start;
231 else if (a.indexOf('%') != -1) {
232 ratio = parseFloat(a.replace('%', '')) * 0.01;
234 return Math.floor(v * ratio);
237 // simple offset adjustment
251 adjustWidthAnchor: function(value, comp) {
256 adjustHeightAnchor: function(value, comp) {
260 configureItem: function(item) {
269 if (!item.anchor && item.items && !Ext.isNumber(item.width) && !(Ext.isIE6 && Ext.isStrict)) {
270 item.anchor = anchor = me.defaultAnchor;
273 // find the container anchoring size
274 if (owner.anchorSize) {
275 if (typeof owner.anchorSize == 'number') {
276 anchorWidth = owner.anchorSize;
279 anchorWidth = owner.anchorSize.width;
280 anchorHeight = owner.anchorSize.height;
284 anchorWidth = owner.initialConfig.width;
285 anchorHeight = owner.initialConfig.height;
289 // cache all anchor values
290 anchorsArray = anchor.split(' ');
291 item.anchorSpec = anchorSpec = {
292 right: me.parseAnchor(anchorsArray[0], item.initialConfig.width, anchorWidth),
293 bottom: me.parseAnchor(anchorsArray[1], item.initialConfig.height, anchorHeight)
296 if (anchorSpec.right) {
297 item.layoutManagedWidth = 1;
299 item.layoutManagedWidth = 2;
302 if (anchorSpec.bottom) {
303 item.layoutManagedHeight = 1;
305 item.layoutManagedHeight = 2;
308 item.layoutManagedWidth = 2;
309 item.layoutManagedHeight = 2;
311 this.callParent(arguments);