Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Region.html
1 <!DOCTYPE html>
2 <html>
3 <head>
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; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-util-Region'>/**
19 </span> * @class Ext.util.Region
20  * @extends Object
21  *
22  * Represents a rectangular region and provides a number of utility methods
23  * to compare regions.
24  */
25
26 Ext.define('Ext.util.Region', {
27
28     /* Begin Definitions */
29
30     requires: ['Ext.util.Offset'],
31
32     statics: {
33 <span id='Ext-util-Region-method-getRegion'>        /**
34 </span>         * @static
35          * @param {Mixed} el A string, DomElement or Ext.core.Element representing an element
36          * on the page.
37          * @returns {Ext.util.Region} region
38          * Retrieves an Ext.util.Region for a particular element.
39          */
40         getRegion: function(el) {
41             return Ext.fly(el).getPageBox(true);
42         },
43
44 <span id='Ext-util-Region-method-from'>        /**
45 </span>         * @static
46          * @param {Object} o An object with top, right, bottom, left properties
47          * @return {Ext.util.Region} region The region constructed based on the passed object
48          */
49         from: function(o) {
50             return new this(o.top, o.right, o.bottom, o.left);
51         }
52     },
53
54     /* End Definitions */
55
56 <span id='Ext-util-Region-method-constructor'>    /**
57 </span>     * @constructor
58      * @param {Number} top Top
59      * @param {Number} right Right
60      * @param {Number} bottom Bottom
61      * @param {Number} left Left
62      */
63     constructor : function(t, r, b, l) {
64         var me = this;
65         me.y = me.top = me[1] = t;
66         me.right = r;
67         me.bottom = b;
68         me.x = me.left = me[0] = l;
69     },
70
71 <span id='Ext-util-Region-method-contains'>    /**
72 </span>     * Checks if this region completely contains the region that is passed in.
73      * @param {Ext.util.Region} region
74      */
75     contains : function(region) {
76         var me = this;
77         return (region.x &gt;= me.x &amp;&amp;
78                 region.right &lt;= me.right &amp;&amp;
79                 region.y &gt;= me.y &amp;&amp;
80                 region.bottom &lt;= me.bottom);
81
82     },
83
84 <span id='Ext-util-Region-method-intersect'>    /**
85 </span>     * Checks if this region intersects the region passed in.
86      * @param {Ext.util.Region} region
87      * @return {Ext.util.Region/Boolean} Returns the intersected region or false if there is no intersection.
88      */
89     intersect : function(region) {
90         var me = this,
91             t = Math.max(me.y, region.y),
92             r = Math.min(me.right, region.right),
93             b = Math.min(me.bottom, region.bottom),
94             l = Math.max(me.x, region.x);
95
96         if (b &gt; t &amp;&amp; r &gt; l) {
97             return new this.self(t, r, b, l);
98         }
99         else {
100             return false;
101         }
102     },
103
104 <span id='Ext-util-Region-method-union'>    /**
105 </span>     * Returns the smallest region that contains the current AND targetRegion.
106      * @param {Ext.util.Region} region
107      */
108     union : function(region) {
109         var me = this,
110             t = Math.min(me.y, region.y),
111             r = Math.max(me.right, region.right),
112             b = Math.max(me.bottom, region.bottom),
113             l = Math.min(me.x, region.x);
114
115         return new this.self(t, r, b, l);
116     },
117
118 <span id='Ext-util-Region-method-constrainTo'>    /**
119 </span>     * Modifies the current region to be constrained to the targetRegion.
120      * @param {Ext.util.Region} targetRegion
121      */
122     constrainTo : function(r) {
123         var me = this,
124             constrain = Ext.Number.constrain;
125         me.top = me.y = constrain(me.top, r.y, r.bottom);
126         me.bottom = constrain(me.bottom, r.y, r.bottom);
127         me.left = me.x = constrain(me.left, r.x, r.right);
128         me.right = constrain(me.right, r.x, r.right);
129         return me;
130     },
131
132 <span id='Ext-util-Region-method-adjust'>    /**
133 </span>     * Modifies the current region to be adjusted by offsets.
134      * @param {Number} top top offset
135      * @param {Number} right right offset
136      * @param {Number} bottom bottom offset
137      * @param {Number} left left offset
138      */
139     adjust : function(t, r, b, l) {
140         var me = this;
141         me.top = me.y += t;
142         me.left = me.x += l;
143         me.right += r;
144         me.bottom += b;
145         return me;
146     },
147
148 <span id='Ext-util-Region-method-getOutOfBoundOffset'>    /**
149 </span>     * Get the offset amount of a point outside the region
150      * @param {String} axis optional
151      * @param {Ext.util.Point} p the point
152      * @return {Ext.util.Offset}
153      */
154     getOutOfBoundOffset: function(axis, p) {
155         if (!Ext.isObject(axis)) {
156             if (axis == 'x') {
157                 return this.getOutOfBoundOffsetX(p);
158             } else {
159                 return this.getOutOfBoundOffsetY(p);
160             }
161         } else {
162             p = axis;
163             var d = Ext.create('Ext.util.Offset');
164             d.x = this.getOutOfBoundOffsetX(p.x);
165             d.y = this.getOutOfBoundOffsetY(p.y);
166             return d;
167         }
168
169     },
170
171 <span id='Ext-util-Region-method-getOutOfBoundOffsetX'>    /**
172 </span>     * Get the offset amount on the x-axis
173      * @param {Number} p the offset
174      * @return {Number}
175      */
176     getOutOfBoundOffsetX: function(p) {
177         if (p &lt;= this.x) {
178             return this.x - p;
179         } else if (p &gt;= this.right) {
180             return this.right - p;
181         }
182
183         return 0;
184     },
185
186 <span id='Ext-util-Region-method-getOutOfBoundOffsetY'>    /**
187 </span>     * Get the offset amount on the y-axis
188      * @param {Number} p the offset
189      * @return {Number}
190      */
191     getOutOfBoundOffsetY: function(p) {
192         if (p &lt;= this.y) {
193             return this.y - p;
194         } else if (p &gt;= this.bottom) {
195             return this.bottom - p;
196         }
197
198         return 0;
199     },
200
201 <span id='Ext-util-Region-method-isOutOfBound'>    /**
202 </span>     * Check whether the point / offset is out of bound
203      * @param {String} axis optional
204      * @param {Ext.util.Point/Number} p the point / offset
205      * @return {Boolean}
206      */
207     isOutOfBound: function(axis, p) {
208         if (!Ext.isObject(axis)) {
209             if (axis == 'x') {
210                 return this.isOutOfBoundX(p);
211             } else {
212                 return this.isOutOfBoundY(p);
213             }
214         } else {
215             p = axis;
216             return (this.isOutOfBoundX(p.x) || this.isOutOfBoundY(p.y));
217         }
218     },
219
220 <span id='Ext-util-Region-method-isOutOfBoundX'>    /**
221 </span>     * Check whether the offset is out of bound in the x-axis
222      * @param {Number} p the offset
223      * @return {Boolean}
224      */
225     isOutOfBoundX: function(p) {
226         return (p &lt; this.x || p &gt; this.right);
227     },
228
229 <span id='Ext-util-Region-method-isOutOfBoundY'>    /**
230 </span>     * Check whether the offset is out of bound in the y-axis
231      * @param {Number} p the offset
232      * @return {Boolean}
233      */
234     isOutOfBoundY: function(p) {
235         return (p &lt; this.y || p &gt; this.bottom);
236     },
237
238     /*
239      * Restrict a point within the region by a certain factor.
240      * @param {String} axis Optional
241      * @param {Ext.util.Point/Ext.util.Offset/Object} p
242      * @param {Number} factor
243      * @return {Ext.util.Point/Ext.util.Offset/Object/Number}
244      */
245     restrict: function(axis, p, factor) {
246         if (Ext.isObject(axis)) {
247             var newP;
248
249             factor = p;
250             p = axis;
251
252             if (p.copy) {
253                 newP = p.copy();
254             }
255             else {
256                 newP = {
257                     x: p.x,
258                     y: p.y
259                 };
260             }
261
262             newP.x = this.restrictX(p.x, factor);
263             newP.y = this.restrictY(p.y, factor);
264             return newP;
265         } else {
266             if (axis == 'x') {
267                 return this.restrictX(p, factor);
268             } else {
269                 return this.restrictY(p, factor);
270             }
271         }
272     },
273
274     /*
275      * Restrict an offset within the region by a certain factor, on the x-axis
276      * @param {Number} p
277      * @param {Number} factor The factor, optional, defaults to 1
278      * @return
279      */
280     restrictX : function(p, factor) {
281         if (!factor) {
282             factor = 1;
283         }
284
285         if (p &lt;= this.x) {
286             p -= (p - this.x) * factor;
287         }
288         else if (p &gt;= this.right) {
289             p -= (p - this.right) * factor;
290         }
291         return p;
292     },
293
294     /*
295      * Restrict an offset within the region by a certain factor, on the y-axis
296      * @param {Number} p
297      * @param {Number} factor The factor, optional, defaults to 1
298      */
299     restrictY : function(p, factor) {
300         if (!factor) {
301             factor = 1;
302         }
303
304         if (p &lt;= this.y) {
305             p -= (p - this.y) * factor;
306         }
307         else if (p &gt;= this.bottom) {
308             p -= (p - this.bottom) * factor;
309         }
310         return p;
311     },
312
313     /*
314      * Get the width / height of this region
315      * @return {Object} an object with width and height properties
316      */
317     getSize: function() {
318         return {
319             width: this.right - this.x,
320             height: this.bottom - this.y
321         };
322     },
323
324 <span id='Ext-util-Region-method-copy'>    /**
325 </span>     * Copy a new instance
326      * @return {Ext.util.Region}
327      */
328     copy: function() {
329         return new this.self(this.y, this.right, this.bottom, this.x);
330     },
331
332 <span id='Ext-util-Region-method-copyFrom'>    /**
333 </span>     * Copy the values of another Region to this Region
334      * @param {Region} The region to copy from.
335      * @return {Ext.util.Point} this This point
336      */
337     copyFrom: function(p) {
338         var me = this;
339         me.top = me.y = me[1] = p.y;
340         me.right = p.right;
341         me.bottom = p.bottom;
342         me.left = me.x = me[0] = p.x;
343
344         return this;
345     },
346
347 <span id='Ext-util-Region-method-toString'>    /**
348 </span>     * Dump this to an eye-friendly string, great for debugging
349      * @return {String}
350      */
351     toString: function() {
352         return &quot;Region[&quot; + this.top + &quot;,&quot; + this.right + &quot;,&quot; + this.bottom + &quot;,&quot; + this.left + &quot;]&quot;;
353     },
354
355
356 <span id='Ext-util-Region-method-translateBy'>    /**
357 </span>     * Translate this region by the given offset amount
358      * @param {Ext.util.Offset/Object} offset Object containing the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; properties.
359      * Or the x value is using the two argument form.
360      * @param {Number} The y value unless using an Offset object.
361      * @return {Ext.util.Region} this This Region
362      */
363     translateBy: function(x, y) {
364         if (arguments.length == 1) {
365             y = x.y;
366             x = x.x;
367         }
368         var me = this;
369         me.top = me.y += y;
370         me.right += x;
371         me.bottom += y;
372         me.left = me.x += x;
373
374         return me;
375     },
376
377 <span id='Ext-util-Region-method-round'>    /**
378 </span>     * Round all the properties of this region
379      * @return {Ext.util.Region} this This Region
380      */
381     round: function() {
382         var me = this;
383         me.top = me.y = Math.round(me.y);
384         me.right = Math.round(me.right);
385         me.bottom = Math.round(me.bottom);
386         me.left = me.x = Math.round(me.x);
387
388         return me;
389     },
390
391 <span id='Ext-util-Region-method-equals'>    /**
392 </span>     * Check whether this region is equivalent to the given region
393      * @param {Ext.util.Region} region The region to compare with
394      * @return {Boolean}
395      */
396     equals: function(region) {
397         return (this.top == region.top &amp;&amp; this.right == region.right &amp;&amp; this.bottom == region.bottom &amp;&amp; this.left == region.left);
398     }
399 });
400 </pre>
401 </body>
402 </html>