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
124 * default anchor for all child container items applied if no anchor or specific width is set on the child item. Defaults to '100%'.
127 defaultAnchor: '100%',
129 parseAnchorRE: /^(r|right|b|bottom)$/i,
132 onLayout: function() {
133 this.callParent(arguments);
136 size = me.getLayoutTargetSize(),
138 target = me.getTarget(),
139 ownerWidth = size.width,
140 ownerHeight = size.height,
141 overflow = target.getStyle('overflow'),
142 components = me.getVisibleItems(owner),
143 len = components.length,
145 box, newTargetSize, anchorWidth, anchorHeight, component, anchorSpec, calcWidth, calcHeight,
146 anchorsArray, anchor, i, el, cleaner;
148 if (ownerWidth < 20 && ownerHeight < 20) {
152 // Anchor layout uses natural HTML flow to arrange the child items.
153 // To ensure that all browsers (I'm looking at you IE!) add the bottom margin of the last child to the
154 // containing element height, we create a zero-sized element with style clear:both to force a "new line"
156 me.clearEl = target.createChild({
157 cls: Ext.baseCSSPrefix + 'clear',
162 // find the container anchoring size
163 if (owner.anchorSize) {
164 if (typeof owner.anchorSize == 'number') {
165 anchorWidth = owner.anchorSize;
168 anchorWidth = owner.anchorSize.width;
169 anchorHeight = owner.anchorSize.height;
173 anchorWidth = owner.initialConfig.width;
174 anchorHeight = owner.initialConfig.height;
177 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
178 if (!Ext.supports.RightMargin) {
179 cleaner = Ext.core.Element.getRightMarginFixCleaner(target);
180 target.addCls(Ext.baseCSSPrefix + 'inline-children');
183 for (i = 0; i < len; i++) {
184 component = components[i];
186 anchor = component.anchor;
188 if (!component.anchor && component.items && !Ext.isNumber(component.width) && !(Ext.isIE6 && Ext.isStrict)) {
189 component.anchor = anchor = me.defaultAnchor;
193 anchorSpec = component.anchorSpec;
194 // cache all anchor values
196 anchorsArray = anchor.split(' ');
197 component.anchorSpec = anchorSpec = {
198 right: me.parseAnchor(anchorsArray[0], component.initialConfig.width, anchorWidth),
199 bottom: me.parseAnchor(anchorsArray[1], component.initialConfig.height, anchorHeight)
202 calcWidth = anchorSpec.right ? me.adjustWidthAnchor(anchorSpec.right(ownerWidth) - el.getMargin('lr'), component) : undefined;
203 calcHeight = anchorSpec.bottom ? me.adjustHeightAnchor(anchorSpec.bottom(ownerHeight) - el.getMargin('tb'), component) : undefined;
206 component: component,
208 width: calcWidth || undefined,
209 height: calcHeight || undefined
213 component: component,
219 // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
220 if (!Ext.supports.RightMargin) {
221 target.removeCls(Ext.baseCSSPrefix + 'inline-children');
225 for (i = 0; i < len; i++) {
227 me.setItemSize(box.component, box.width, box.height);
230 if (overflow && overflow != 'hidden' && !me.adjustmentPass) {
231 newTargetSize = me.getLayoutTargetSize();
232 if (newTargetSize.width != size.width || newTargetSize.height != size.height) {
233 me.adjustmentPass = true;
238 delete me.adjustmentPass;
242 parseAnchor: function(a, start, cstart) {
243 if (a && a != 'none') {
246 if (this.parseAnchorRE.test(a)) {
247 var diff = cstart - start;
253 else if (a.indexOf('%') != -1) {
254 ratio = parseFloat(a.replace('%', '')) * 0.01;
256 return Math.floor(v * ratio);
259 // simple offset adjustment
273 adjustWidthAnchor: function(value, comp) {
278 adjustHeightAnchor: function(value, comp) {