2 * @class Ext.util.Region
5 * Represents a rectangular region and provides a number of utility methods
9 Ext.define('Ext.util.Region', {
11 /* Begin Definitions */
13 requires: ['Ext.util.Offset'],
18 * @param {Mixed} el A string, DomElement or Ext.core.Element representing an element
20 * @returns {Ext.util.Region} region
21 * Retrieves an Ext.util.Region for a particular element.
23 getRegion: function(el) {
24 return Ext.fly(el).getPageBox(true);
29 * @param {Object} o An object with top, right, bottom, left properties
30 * @return {Ext.util.Region} region The region constructed based on the passed object
33 return new this(o.top, o.right, o.bottom, o.left);
41 * @param {Number} top Top
42 * @param {Number} right Right
43 * @param {Number} bottom Bottom
44 * @param {Number} left Left
46 constructor : function(t, r, b, l) {
48 me.y = me.top = me[1] = t;
51 me.x = me.left = me[0] = l;
55 * Checks if this region completely contains the region that is passed in.
56 * @param {Ext.util.Region} region
58 contains : function(region) {
60 return (region.x >= me.x &&
61 region.right <= me.right &&
63 region.bottom <= me.bottom);
68 * Checks if this region intersects the region passed in.
69 * @param {Ext.util.Region} region
70 * @return {Ext.util.Region/Boolean} Returns the intersected region or false if there is no intersection.
72 intersect : function(region) {
74 t = Math.max(me.y, region.y),
75 r = Math.min(me.right, region.right),
76 b = Math.min(me.bottom, region.bottom),
77 l = Math.max(me.x, region.x);
80 return new this.self(t, r, b, l);
88 * Returns the smallest region that contains the current AND targetRegion.
89 * @param {Ext.util.Region} region
91 union : function(region) {
93 t = Math.min(me.y, region.y),
94 r = Math.max(me.right, region.right),
95 b = Math.max(me.bottom, region.bottom),
96 l = Math.min(me.x, region.x);
98 return new this.self(t, r, b, l);
102 * Modifies the current region to be constrained to the targetRegion.
103 * @param {Ext.util.Region} targetRegion
105 constrainTo : function(r) {
107 constrain = Ext.Number.constrain;
108 me.top = me.y = constrain(me.top, r.y, r.bottom);
109 me.bottom = constrain(me.bottom, r.y, r.bottom);
110 me.left = me.x = constrain(me.left, r.x, r.right);
111 me.right = constrain(me.right, r.x, r.right);
116 * Modifies the current region to be adjusted by offsets.
117 * @param {Number} top top offset
118 * @param {Number} right right offset
119 * @param {Number} bottom bottom offset
120 * @param {Number} left left offset
122 adjust : function(t, r, b, l) {
132 * Get the offset amount of a point outside the region
133 * @param {String} axis optional
134 * @param {Ext.util.Point} p the point
135 * @return {Ext.util.Offset}
137 getOutOfBoundOffset: function(axis, p) {
138 if (!Ext.isObject(axis)) {
140 return this.getOutOfBoundOffsetX(p);
142 return this.getOutOfBoundOffsetY(p);
146 var d = Ext.create('Ext.util.Offset');
147 d.x = this.getOutOfBoundOffsetX(p.x);
148 d.y = this.getOutOfBoundOffsetY(p.y);
155 * Get the offset amount on the x-axis
156 * @param {Number} p the offset
159 getOutOfBoundOffsetX: function(p) {
162 } else if (p >= this.right) {
163 return this.right - p;
170 * Get the offset amount on the y-axis
171 * @param {Number} p the offset
174 getOutOfBoundOffsetY: function(p) {
177 } else if (p >= this.bottom) {
178 return this.bottom - p;
185 * Check whether the point / offset is out of bound
186 * @param {String} axis optional
187 * @param {Ext.util.Point/Number} p the point / offset
190 isOutOfBound: function(axis, p) {
191 if (!Ext.isObject(axis)) {
193 return this.isOutOfBoundX(p);
195 return this.isOutOfBoundY(p);
199 return (this.isOutOfBoundX(p.x) || this.isOutOfBoundY(p.y));
204 * Check whether the offset is out of bound in the x-axis
205 * @param {Number} p the offset
208 isOutOfBoundX: function(p) {
209 return (p < this.x || p > this.right);
213 * Check whether the offset is out of bound in the y-axis
214 * @param {Number} p the offset
217 isOutOfBoundY: function(p) {
218 return (p < this.y || p > this.bottom);
222 * Restrict a point within the region by a certain factor.
223 * @param {String} axis Optional
224 * @param {Ext.util.Point/Ext.util.Offset/Object} p
225 * @param {Number} factor
226 * @return {Ext.util.Point/Ext.util.Offset/Object/Number}
228 restrict: function(axis, p, factor) {
229 if (Ext.isObject(axis)) {
245 newP.x = this.restrictX(p.x, factor);
246 newP.y = this.restrictY(p.y, factor);
250 return this.restrictX(p, factor);
252 return this.restrictY(p, factor);
258 * Restrict an offset within the region by a certain factor, on the x-axis
260 * @param {Number} factor The factor, optional, defaults to 1
263 restrictX : function(p, factor) {
269 p -= (p - this.x) * factor;
271 else if (p >= this.right) {
272 p -= (p - this.right) * factor;
278 * Restrict an offset within the region by a certain factor, on the y-axis
280 * @param {Number} factor The factor, optional, defaults to 1
282 restrictY : function(p, factor) {
288 p -= (p - this.y) * factor;
290 else if (p >= this.bottom) {
291 p -= (p - this.bottom) * factor;
297 * Get the width / height of this region
298 * @return {Object} an object with width and height properties
300 getSize: function() {
302 width: this.right - this.x,
303 height: this.bottom - this.y
308 * Copy a new instance
309 * @return {Ext.util.Region}
312 return new this.self(this.y, this.right, this.bottom, this.x);
316 * Copy the values of another Region to this Region
317 * @param {Region} The region to copy from.
318 * @return {Ext.util.Point} this This point
320 copyFrom: function(p) {
322 me.top = me.y = me[1] = p.y;
324 me.bottom = p.bottom;
325 me.left = me.x = me[0] = p.x;
331 * Dump this to an eye-friendly string, great for debugging
334 toString: function() {
335 return "Region[" + this.top + "," + this.right + "," + this.bottom + "," + this.left + "]";
340 * Translate this region by the given offset amount
341 * @param {Ext.util.Offset/Object} offset Object containing the <code>x</code> and <code>y</code> properties.
342 * Or the x value is using the two argument form.
343 * @param {Number} The y value unless using an Offset object.
344 * @return {Ext.util.Region} this This Region
346 translateBy: function(x, y) {
347 if (arguments.length == 1) {
361 * Round all the properties of this region
362 * @return {Ext.util.Region} this This Region
366 me.top = me.y = Math.round(me.y);
367 me.right = Math.round(me.right);
368 me.bottom = Math.round(me.bottom);
369 me.left = me.x = Math.round(me.x);
375 * Check whether this region is equivalent to the given region
376 * @param {Ext.util.Region} region The region to compare with
379 equals: function(region) {
380 return (this.top == region.top && this.right == region.right && this.bottom == region.bottom && this.left == region.left);