commit extjs-2.2.1
[extjs.git] / source / widgets / layout / AnchorLayout.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.layout.AnchorLayout\r
11  * @extends Ext.layout.ContainerLayout\r
12  * <p>This is a layout that enables anchoring of contained elements relative to the container's dimensions.  If\r
13  * the container is resized, all anchored items are automatically rerendered according to their anchor rules.\r
14  * This class is intended to be extended or created via the layout:'anchor' {@link Ext.Container#layout} config,\r
15  * and should generally not need to be created directly via the new keyword.</p>\r
16  * <p>AnchorLayout does not have any direct config options (other than inherited ones).  However, the container\r
17  * using the AnchorLayout can supply an anchoring-specific config property of <b>anchorSize</b>.  By default,\r
18  * AnchorLayout will calculate anchor measurements based on the size of the container itself.  However, if\r
19  * anchorSize is specifed, the layout will use it as a virtual container for the purposes of calculating anchor\r
20  * measurements based on it instead, allowing the container to be sized independently of the anchoring logic if necessary.</p>\r
21  * <p>The items added to an AnchorLayout can also supply an anchoring-specific config property of <b>anchor</b> which\r
22  * is a string containing two values: the horizontal anchor value and the vertical anchor value (for example, '100% 50%').\r
23  * This value is what tells the layout how the item should be anchored to the container.  The following types of\r
24  * anchor values are supported:\r
25  * <ul>\r
26  * <li><b>Percentage</b>: Any value between 1 and 100, expressed as a percentage.  The first anchor is the percentage\r
27  * width that the item should take up within the container, and the second is the percentage height.  Example: '100% 50%'\r
28  * would render an item the complete width of the container and 1/2 its height.  If only one anchor value is supplied\r
29  * it is assumed to be the width value and the height will default to auto.</li>\r
30  * <li><b>Offsets</b>: Any positive or negative integer value.  The first anchor is the offset from the right edge of\r
31  * the container, and the second is the offset from the bottom edge.  Example: '-50 -100' would render an item the\r
32  * complete width of the container minus 50 pixels and the complete height minus 100 pixels.  If only one anchor value\r
33  * is supplied it is assumed to be the right offset value and the bottom offset will default to 0.</li>\r
34  * <li><b>Sides</b>: Valid values are 'right' (or 'r') and 'bottom' (or 'b').  Either the container must have a fixed\r
35  * size or an anchorSize config value defined at render time in order for these to have any effect.</li>\r
36  * </ul>\r
37  * <p>Anchor values can also be mixed as needed.  For example, '-50 75%' would render the width offset from the\r
38  * container right edge by 50 pixels and 75% of the container's height.</p>\r
39  */\r
40 Ext.layout.AnchorLayout = Ext.extend(Ext.layout.ContainerLayout, {\r
41     // private\r
42     monitorResize:true,\r
43 \r
44     // private\r
45     getAnchorViewSize : function(ct, target){\r
46         return target.dom == document.body ?\r
47                    target.getViewSize() : target.getStyleSize();\r
48     },\r
49 \r
50     // private\r
51     onLayout : function(ct, target){\r
52         Ext.layout.AnchorLayout.superclass.onLayout.call(this, ct, target);\r
53 \r
54         var size = this.getAnchorViewSize(ct, target);\r
55 \r
56         var w = size.width, h = size.height;\r
57 \r
58         if(w < 20 || h < 20){\r
59             return;\r
60         }\r
61 \r
62         // find the container anchoring size\r
63         var aw, ah;\r
64         if(ct.anchorSize){\r
65             if(typeof ct.anchorSize == 'number'){\r
66                 aw = ct.anchorSize;\r
67             }else{\r
68                 aw = ct.anchorSize.width;\r
69                 ah = ct.anchorSize.height;\r
70             }\r
71         }else{\r
72             aw = ct.initialConfig.width;\r
73             ah = ct.initialConfig.height;\r
74         }\r
75 \r
76         var cs = ct.items.items, len = cs.length, i, c, a, cw, ch;\r
77         for(i = 0; i < len; i++){\r
78             c = cs[i];\r
79             if(c.anchor){\r
80                 a = c.anchorSpec;\r
81                 if(!a){ // cache all anchor values\r
82                     var vs = c.anchor.split(' ');\r
83                     c.anchorSpec = a = {\r
84                         right: this.parseAnchor(vs[0], c.initialConfig.width, aw),\r
85                         bottom: this.parseAnchor(vs[1], c.initialConfig.height, ah)\r
86                     };\r
87                 }\r
88                 cw = a.right ? this.adjustWidthAnchor(a.right(w), c) : undefined;\r
89                 ch = a.bottom ? this.adjustHeightAnchor(a.bottom(h), c) : undefined;\r
90 \r
91                 if(cw || ch){\r
92                     c.setSize(cw || undefined, ch || undefined);\r
93                 }\r
94             }\r
95         }\r
96     },\r
97 \r
98     // private\r
99     parseAnchor : function(a, start, cstart){\r
100         if(a && a != 'none'){\r
101             var last;\r
102             if(/^(r|right|b|bottom)$/i.test(a)){   // standard anchor\r
103                 var diff = cstart - start;\r
104                 return function(v){\r
105                     if(v !== last){\r
106                         last = v;\r
107                         return v - diff;\r
108                     }\r
109                 }\r
110             }else if(a.indexOf('%') != -1){\r
111                 var ratio = parseFloat(a.replace('%', ''))*.01;   // percentage\r
112                 return function(v){\r
113                     if(v !== last){\r
114                         last = v;\r
115                         return Math.floor(v*ratio);\r
116                     }\r
117                 }\r
118             }else{\r
119                 a = parseInt(a, 10);\r
120                 if(!isNaN(a)){                            // simple offset adjustment\r
121                     return function(v){\r
122                         if(v !== last){\r
123                             last = v;\r
124                             return v + a;\r
125                         }\r
126                     }\r
127                 }\r
128             }\r
129         }\r
130         return false;\r
131     },\r
132 \r
133     // private\r
134     adjustWidthAnchor : function(value, comp){\r
135         return value;\r
136     },\r
137 \r
138     // private\r
139     adjustHeightAnchor : function(value, comp){\r
140         return value;\r
141     }\r
142     \r
143     /**\r
144      * @property activeItem\r
145      * @hide\r
146      */\r
147 });\r
148 Ext.Container.LAYOUTS['anchor'] = Ext.layout.AnchorLayout;