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-util-Region'>/**
19 </span> * @class Ext.util.Region
22 * <p>This class represents a rectangular region in X,Y space, and performs geometric
23 * transformations or tests upon the region.</p>
24 * <p>This class may be used to compare the document regions occupied by elements.</p>
27 Ext.define('Ext.util.Region', {
29 /* Begin Definitions */
31 requires: ['Ext.util.Offset'],
34 <span id='Ext-util-Region-method-getRegion'> /**
36 * Retrieves an Ext.util.Region for a particular element.
37 * @param {Mixed} el An element ID, htmlElement or Ext.core.Element representing an element in the document.
38 * @returns {Ext.util.Region} region
40 getRegion: function(el) {
41 return Ext.fly(el).getPageBox(true);
44 <span id='Ext-util-Region-method-from'> /**
46 * 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>.
47 * @param {Object} o An object with <code>top</code>, <code>right</code>, <code>bottom</code> and <code>left</code> properties.
48 * @return {Ext.util.Region} region The Region constructed based on the passed object
51 return new this(o.top, o.right, o.bottom, o.left);
57 <span id='Ext-util-Region-method-constructor'> /**
58 </span> * Creates a region from the bounding sides.
59 * @param {Number} top Top The topmost pixel of the Region.
60 * @param {Number} right Right The rightmost pixel of the Region.
61 * @param {Number} bottom Bottom The bottom pixel of the Region.
62 * @param {Number} left Left The leftmost pixel of the Region.
64 constructor : function(t, r, b, l) {
66 me.y = me.top = me[1] = t;
69 me.x = me.left = me[0] = l;
72 <span id='Ext-util-Region-method-contains'> /**
73 </span> * Checks if this region completely contains the region that is passed in.
74 * @param {Ext.util.Region} region
76 contains : function(region) {
78 return (region.x >= me.x &&
79 region.right <= me.right &&
80 region.y >= me.y &&
81 region.bottom <= me.bottom);
85 <span id='Ext-util-Region-method-intersect'> /**
86 </span> * Checks if this region intersects the region passed in.
87 * @param {Ext.util.Region} region
88 * @return {Ext.util.Region/Boolean} Returns the intersected region or false if there is no intersection.
90 intersect : function(region) {
92 t = Math.max(me.y, region.y),
93 r = Math.min(me.right, region.right),
94 b = Math.min(me.bottom, region.bottom),
95 l = Math.max(me.x, region.x);
97 if (b > t && r > l) {
98 return new this.self(t, r, b, l);
105 <span id='Ext-util-Region-method-union'> /**
106 </span> * Returns the smallest region that contains the current AND targetRegion.
107 * @param {Ext.util.Region} region
109 union : function(region) {
111 t = Math.min(me.y, region.y),
112 r = Math.max(me.right, region.right),
113 b = Math.max(me.bottom, region.bottom),
114 l = Math.min(me.x, region.x);
116 return new this.self(t, r, b, l);
119 <span id='Ext-util-Region-method-constrainTo'> /**
120 </span> * Modifies the current region to be constrained to the targetRegion.
121 * @param {Ext.util.Region} targetRegion
123 constrainTo : function(r) {
125 constrain = Ext.Number.constrain;
126 me.top = me.y = constrain(me.top, r.y, r.bottom);
127 me.bottom = constrain(me.bottom, r.y, r.bottom);
128 me.left = me.x = constrain(me.left, r.x, r.right);
129 me.right = constrain(me.right, r.x, r.right);
133 <span id='Ext-util-Region-method-adjust'> /**
134 </span> * Modifies the current region to be adjusted by offsets.
135 * @param {Number} top top offset
136 * @param {Number} right right offset
137 * @param {Number} bottom bottom offset
138 * @param {Number} left left offset
140 adjust : function(t, r, b, l) {
149 <span id='Ext-util-Region-method-getOutOfBoundOffset'> /**
150 </span> * Get the offset amount of a point outside the region
151 * @param {String} axis optional
152 * @param {Ext.util.Point} p the point
153 * @return {Ext.util.Offset}
155 getOutOfBoundOffset: function(axis, p) {
156 if (!Ext.isObject(axis)) {
158 return this.getOutOfBoundOffsetX(p);
160 return this.getOutOfBoundOffsetY(p);
164 var d = Ext.create('Ext.util.Offset');
165 d.x = this.getOutOfBoundOffsetX(p.x);
166 d.y = this.getOutOfBoundOffsetY(p.y);
172 <span id='Ext-util-Region-method-getOutOfBoundOffsetX'> /**
173 </span> * Get the offset amount on the x-axis
174 * @param {Number} p the offset
177 getOutOfBoundOffsetX: function(p) {
178 if (p <= this.x) {
180 } else if (p >= this.right) {
181 return this.right - p;
187 <span id='Ext-util-Region-method-getOutOfBoundOffsetY'> /**
188 </span> * Get the offset amount on the y-axis
189 * @param {Number} p the offset
192 getOutOfBoundOffsetY: function(p) {
193 if (p <= this.y) {
195 } else if (p >= this.bottom) {
196 return this.bottom - p;
202 <span id='Ext-util-Region-method-isOutOfBound'> /**
203 </span> * Check whether the point / offset is out of bound
204 * @param {String} axis optional
205 * @param {Ext.util.Point/Number} p the point / offset
208 isOutOfBound: function(axis, p) {
209 if (!Ext.isObject(axis)) {
211 return this.isOutOfBoundX(p);
213 return this.isOutOfBoundY(p);
217 return (this.isOutOfBoundX(p.x) || this.isOutOfBoundY(p.y));
221 <span id='Ext-util-Region-method-isOutOfBoundX'> /**
222 </span> * Check whether the offset is out of bound in the x-axis
223 * @param {Number} p the offset
226 isOutOfBoundX: function(p) {
227 return (p < this.x || p > this.right);
230 <span id='Ext-util-Region-method-isOutOfBoundY'> /**
231 </span> * Check whether the offset is out of bound in the y-axis
232 * @param {Number} p the offset
235 isOutOfBoundY: function(p) {
236 return (p < this.y || p > this.bottom);
240 * Restrict a point within the region by a certain factor.
241 * @param {String} axis Optional
242 * @param {Ext.util.Point/Ext.util.Offset/Object} p
243 * @param {Number} factor
244 * @return {Ext.util.Point/Ext.util.Offset/Object/Number}
246 restrict: function(axis, p, factor) {
247 if (Ext.isObject(axis)) {
263 newP.x = this.restrictX(p.x, factor);
264 newP.y = this.restrictY(p.y, factor);
268 return this.restrictX(p, factor);
270 return this.restrictY(p, factor);
276 * Restrict an offset within the region by a certain factor, on the x-axis
278 * @param {Number} factor The factor, optional, defaults to 1
281 restrictX : function(p, factor) {
286 if (p <= this.x) {
287 p -= (p - this.x) * factor;
289 else if (p >= this.right) {
290 p -= (p - this.right) * factor;
296 * Restrict an offset within the region by a certain factor, on the y-axis
298 * @param {Number} factor The factor, optional, defaults to 1
300 restrictY : function(p, factor) {
305 if (p <= this.y) {
306 p -= (p - this.y) * factor;
308 else if (p >= this.bottom) {
309 p -= (p - this.bottom) * factor;
315 * Get the width / height of this region
316 * @return {Object} an object with width and height properties
318 getSize: function() {
320 width: this.right - this.x,
321 height: this.bottom - this.y
325 <span id='Ext-util-Region-method-copy'> /**
326 </span> * Create a copy of this Region.
327 * @return {Ext.util.Region}
330 return new this.self(this.y, this.right, this.bottom, this.x);
333 <span id='Ext-util-Region-method-copyFrom'> /**
334 </span> * Copy the values of another Region to this Region
335 * @param {Region} The region to copy from.
336 * @return {Ext.util.Region} This Region
338 copyFrom: function(p) {
340 me.top = me.y = me[1] = p.y;
342 me.bottom = p.bottom;
343 me.left = me.x = me[0] = p.x;
349 * Dump this to an eye-friendly string, great for debugging
352 toString: function() {
353 return "Region[" + this.top + "," + this.right + "," + this.bottom + "," + this.left + "]";
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 <code>x</code> and <code>y</code> 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
363 translateBy: function(x, y) {
364 if (arguments.length == 1) {
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
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);
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
396 equals: function(region) {
397 return (this.top == region.top && this.right == region.right && this.bottom == region.bottom && this.left == region.left);