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 * Represents a rectangular region and provides a number of utility methods
26 Ext.define('Ext.util.Region', {
28 /* Begin Definitions */
30 requires: ['Ext.util.Offset'],
33 <span id='Ext-util-Region-method-getRegion'> /**
35 * @param {Mixed} el A string, DomElement or Ext.core.Element representing an element
37 * @returns {Ext.util.Region} region
38 * Retrieves an Ext.util.Region for a particular element.
40 getRegion: function(el) {
41 return Ext.fly(el).getPageBox(true);
44 <span id='Ext-util-Region-method-from'> /**
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
50 return new this(o.top, o.right, o.bottom, o.left);
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
63 constructor : function(t, r, b, l) {
65 me.y = me.top = me[1] = t;
68 me.x = me.left = me[0] = l;
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
75 contains : function(region) {
77 return (region.x >= me.x &&
78 region.right <= me.right &&
79 region.y >= me.y &&
80 region.bottom <= me.bottom);
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.
89 intersect : function(region) {
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);
96 if (b > t && r > l) {
97 return new this.self(t, r, b, l);
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
108 union : function(region) {
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);
115 return new this.self(t, r, b, l);
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
122 constrainTo : function(r) {
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);
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
139 adjust : function(t, r, b, l) {
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}
154 getOutOfBoundOffset: function(axis, p) {
155 if (!Ext.isObject(axis)) {
157 return this.getOutOfBoundOffsetX(p);
159 return this.getOutOfBoundOffsetY(p);
163 var d = Ext.create('Ext.util.Offset');
164 d.x = this.getOutOfBoundOffsetX(p.x);
165 d.y = this.getOutOfBoundOffsetY(p.y);
171 <span id='Ext-util-Region-method-getOutOfBoundOffsetX'> /**
172 </span> * Get the offset amount on the x-axis
173 * @param {Number} p the offset
176 getOutOfBoundOffsetX: function(p) {
177 if (p <= this.x) {
179 } else if (p >= this.right) {
180 return this.right - p;
186 <span id='Ext-util-Region-method-getOutOfBoundOffsetY'> /**
187 </span> * Get the offset amount on the y-axis
188 * @param {Number} p the offset
191 getOutOfBoundOffsetY: function(p) {
192 if (p <= this.y) {
194 } else if (p >= this.bottom) {
195 return this.bottom - p;
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
207 isOutOfBound: function(axis, p) {
208 if (!Ext.isObject(axis)) {
210 return this.isOutOfBoundX(p);
212 return this.isOutOfBoundY(p);
216 return (this.isOutOfBoundX(p.x) || this.isOutOfBoundY(p.y));
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
225 isOutOfBoundX: function(p) {
226 return (p < this.x || p > this.right);
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
234 isOutOfBoundY: function(p) {
235 return (p < this.y || p > this.bottom);
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}
245 restrict: function(axis, p, factor) {
246 if (Ext.isObject(axis)) {
262 newP.x = this.restrictX(p.x, factor);
263 newP.y = this.restrictY(p.y, factor);
267 return this.restrictX(p, factor);
269 return this.restrictY(p, factor);
275 * Restrict an offset within the region by a certain factor, on the x-axis
277 * @param {Number} factor The factor, optional, defaults to 1
280 restrictX : function(p, factor) {
285 if (p <= this.x) {
286 p -= (p - this.x) * factor;
288 else if (p >= this.right) {
289 p -= (p - this.right) * factor;
295 * Restrict an offset within the region by a certain factor, on the y-axis
297 * @param {Number} factor The factor, optional, defaults to 1
299 restrictY : function(p, factor) {
304 if (p <= this.y) {
305 p -= (p - this.y) * factor;
307 else if (p >= this.bottom) {
308 p -= (p - this.bottom) * factor;
314 * Get the width / height of this region
315 * @return {Object} an object with width and height properties
317 getSize: function() {
319 width: this.right - this.x,
320 height: this.bottom - this.y
324 <span id='Ext-util-Region-method-copy'> /**
325 </span> * Copy a new instance
326 * @return {Ext.util.Region}
329 return new this.self(this.y, this.right, this.bottom, this.x);
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
337 copyFrom: function(p) {
339 me.top = me.y = me[1] = p.y;
341 me.bottom = p.bottom;
342 me.left = me.x = me[0] = p.x;
347 <span id='Ext-util-Region-method-toString'> /**
348 </span> * Dump this to an eye-friendly string, great for debugging
351 toString: function() {
352 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);