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.util.Region
19 * <p>This class represents a rectangular region in X,Y space, and performs geometric
20 * transformations or tests upon the region.</p>
21 * <p>This class may be used to compare the document regions occupied by elements.</p>
24 Ext.define('Ext.util.Region', {
26 /* Begin Definitions */
28 requires: ['Ext.util.Offset'],
33 * Retrieves an Ext.util.Region for a particular element.
34 * @param {Mixed} el An element ID, htmlElement or Ext.core.Element representing an element in the document.
35 * @returns {Ext.util.Region} region
37 getRegion: function(el) {
38 return Ext.fly(el).getPageBox(true);
43 * Creates a Region from a "box" Object which contains four numeric properties <code>top</code>, <code>right</code>, <code>bottom</code> and <code>left</code>.
44 * @param {Object} o An object with <code>top</code>, <code>right</code>, <code>bottom</code> and <code>left</code> properties.
45 * @return {Ext.util.Region} region The Region constructed based on the passed object
48 return new this(o.top, o.right, o.bottom, o.left);
55 * Creates a region from the bounding sides.
56 * @param {Number} top Top The topmost pixel of the Region.
57 * @param {Number} right Right The rightmost pixel of the Region.
58 * @param {Number} bottom Bottom The bottom pixel of the Region.
59 * @param {Number} left Left The leftmost pixel of the Region.
61 constructor : function(t, r, b, l) {
63 me.y = me.top = me[1] = t;
66 me.x = me.left = me[0] = l;
70 * Checks if this region completely contains the region that is passed in.
71 * @param {Ext.util.Region} region
73 contains : function(region) {
75 return (region.x >= me.x &&
76 region.right <= me.right &&
78 region.bottom <= me.bottom);
83 * Checks if this region intersects the region passed in.
84 * @param {Ext.util.Region} region
85 * @return {Ext.util.Region/Boolean} Returns the intersected region or false if there is no intersection.
87 intersect : function(region) {
89 t = Math.max(me.y, region.y),
90 r = Math.min(me.right, region.right),
91 b = Math.min(me.bottom, region.bottom),
92 l = Math.max(me.x, region.x);
95 return new this.self(t, r, b, l);
103 * Returns the smallest region that contains the current AND targetRegion.
104 * @param {Ext.util.Region} region
106 union : function(region) {
108 t = Math.min(me.y, region.y),
109 r = Math.max(me.right, region.right),
110 b = Math.max(me.bottom, region.bottom),
111 l = Math.min(me.x, region.x);
113 return new this.self(t, r, b, l);
117 * Modifies the current region to be constrained to the targetRegion.
118 * @param {Ext.util.Region} targetRegion
120 constrainTo : function(r) {
122 constrain = Ext.Number.constrain;
123 me.top = me.y = constrain(me.top, r.y, r.bottom);
124 me.bottom = constrain(me.bottom, r.y, r.bottom);
125 me.left = me.x = constrain(me.left, r.x, r.right);
126 me.right = constrain(me.right, r.x, r.right);
131 * Modifies the current region to be adjusted by offsets.
132 * @param {Number} top top offset
133 * @param {Number} right right offset
134 * @param {Number} bottom bottom offset
135 * @param {Number} left left offset
137 adjust : function(t, r, b, l) {
147 * Get the offset amount of a point outside the region
148 * @param {String} axis optional
149 * @param {Ext.util.Point} p the point
150 * @return {Ext.util.Offset}
152 getOutOfBoundOffset: function(axis, p) {
153 if (!Ext.isObject(axis)) {
155 return this.getOutOfBoundOffsetX(p);
157 return this.getOutOfBoundOffsetY(p);
161 var d = Ext.create('Ext.util.Offset');
162 d.x = this.getOutOfBoundOffsetX(p.x);
163 d.y = this.getOutOfBoundOffsetY(p.y);
170 * Get the offset amount on the x-axis
171 * @param {Number} p the offset
174 getOutOfBoundOffsetX: function(p) {
177 } else if (p >= this.right) {
178 return this.right - p;
185 * Get the offset amount on the y-axis
186 * @param {Number} p the offset
189 getOutOfBoundOffsetY: function(p) {
192 } else if (p >= this.bottom) {
193 return this.bottom - p;
200 * Check whether the point / offset is out of bound
201 * @param {String} axis optional
202 * @param {Ext.util.Point/Number} p the point / offset
205 isOutOfBound: function(axis, p) {
206 if (!Ext.isObject(axis)) {
208 return this.isOutOfBoundX(p);
210 return this.isOutOfBoundY(p);
214 return (this.isOutOfBoundX(p.x) || this.isOutOfBoundY(p.y));
219 * Check whether the offset is out of bound in the x-axis
220 * @param {Number} p the offset
223 isOutOfBoundX: function(p) {
224 return (p < this.x || p > this.right);
228 * Check whether the offset is out of bound in the y-axis
229 * @param {Number} p the offset
232 isOutOfBoundY: function(p) {
233 return (p < this.y || p > this.bottom);
237 * Restrict a point within the region by a certain factor.
238 * @param {String} axis Optional
239 * @param {Ext.util.Point/Ext.util.Offset/Object} p
240 * @param {Number} factor
241 * @return {Ext.util.Point/Ext.util.Offset/Object/Number}
243 restrict: function(axis, p, factor) {
244 if (Ext.isObject(axis)) {
260 newP.x = this.restrictX(p.x, factor);
261 newP.y = this.restrictY(p.y, factor);
265 return this.restrictX(p, factor);
267 return this.restrictY(p, factor);
273 * Restrict an offset within the region by a certain factor, on the x-axis
275 * @param {Number} factor The factor, optional, defaults to 1
278 restrictX : function(p, factor) {
284 p -= (p - this.x) * factor;
286 else if (p >= this.right) {
287 p -= (p - this.right) * factor;
293 * Restrict an offset within the region by a certain factor, on the y-axis
295 * @param {Number} factor The factor, optional, defaults to 1
297 restrictY : function(p, factor) {
303 p -= (p - this.y) * factor;
305 else if (p >= this.bottom) {
306 p -= (p - this.bottom) * factor;
312 * Get the width / height of this region
313 * @return {Object} an object with width and height properties
315 getSize: function() {
317 width: this.right - this.x,
318 height: this.bottom - this.y
323 * Create a copy of this Region.
324 * @return {Ext.util.Region}
327 return new this.self(this.y, this.right, this.bottom, this.x);
331 * Copy the values of another Region to this Region
332 * @param {Region} The region to copy from.
333 * @return {Ext.util.Region} This Region
335 copyFrom: function(p) {
337 me.top = me.y = me[1] = p.y;
339 me.bottom = p.bottom;
340 me.left = me.x = me[0] = p.x;
346 * Dump this to an eye-friendly string, great for debugging
349 toString: function() {
350 return "Region[" + this.top + "," + this.right + "," + this.bottom + "," + this.left + "]";
354 * Translate this region by the given offset amount
355 * @param {Ext.util.Offset/Object} offset Object containing the <code>x</code> and <code>y</code> properties.
356 * Or the x value is using the two argument form.
357 * @param {Number} The y value unless using an Offset object.
358 * @return {Ext.util.Region} this This Region
360 translateBy: function(x, y) {
361 if (arguments.length == 1) {
375 * Round all the properties of this region
376 * @return {Ext.util.Region} this This Region
380 me.top = me.y = Math.round(me.y);
381 me.right = Math.round(me.right);
382 me.bottom = Math.round(me.bottom);
383 me.left = me.x = Math.round(me.x);
389 * Check whether this region is equivalent to the given region
390 * @param {Ext.util.Region} region The region to compare with
393 equals: function(region) {
394 return (this.top == region.top && this.right == region.right && this.bottom == region.bottom && this.left == region.left);