4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * This class represents a rectangular region in X,Y space, and performs geometric
20 * transformations or tests upon the region.
22 * This class may be used to compare the document regions occupied by elements.
24 Ext.define('Ext.util.Region', {
26 /* Begin Definitions */
28 requires: ['Ext.util.Offset'],
31 <span id='Ext-util-Region-static-method-getRegion'> /**
33 * Retrieves an Ext.util.Region for a particular element.
34 * @param {String/HTMLElement/Ext.Element} el An element ID, htmlElement or Ext.Element representing an element in the document.
35 * @returns {Ext.util.Region} region
37 getRegion: function(el) {
38 return Ext.fly(el).getPageBox(true);
41 <span id='Ext-util-Region-static-method-from'> /**
43 * Creates a Region from a "box" Object which contains four numeric properties `top`, `right`, `bottom` and `left`.
44 * @param {Object} o An object with `top`, `right`, `bottom` and `left` 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);
54 <span id='Ext-util-Region-method-constructor'> /**
55 </span> * 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;
69 <span id='Ext-util-Region-method-contains'> /**
70 </span> * Checks if this region completely contains the region that is passed in.
71 * @param {Ext.util.Region} region
74 contains : function(region) {
76 return (region.x >= me.x &&
77 region.right <= me.right &&
78 region.y >= me.y &&
79 region.bottom <= me.bottom);
83 <span id='Ext-util-Region-method-intersect'> /**
84 </span> * Checks if this region intersects the region passed in.
85 * @param {Ext.util.Region} region
86 * @return {Ext.util.Region/Boolean} Returns the intersected region or false if there is no intersection.
88 intersect : function(region) {
90 t = Math.max(me.y, region.y),
91 r = Math.min(me.right, region.right),
92 b = Math.min(me.bottom, region.bottom),
93 l = Math.max(me.x, region.x);
95 if (b > t && r > l) {
96 return new this.self(t, r, b, l);
103 <span id='Ext-util-Region-method-union'> /**
104 </span> * Returns the smallest region that contains the current AND targetRegion.
105 * @param {Ext.util.Region} region
106 * @return {Ext.util.Region} a new 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
121 * @return {Ext.util.Region} this
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
139 * @return {Ext.util.Region} this
141 adjust : function(t, r, b, l) {
150 <span id='Ext-util-Region-method-getOutOfBoundOffset'> /**
151 </span> * Get the offset amount of a point outside the region
152 * @param {String} [axis]
153 * @param {Ext.util.Point} [p] the point
154 * @return {Ext.util.Offset}
156 getOutOfBoundOffset: function(axis, p) {
157 if (!Ext.isObject(axis)) {
159 return this.getOutOfBoundOffsetX(p);
161 return this.getOutOfBoundOffsetY(p);
165 var d = Ext.create('Ext.util.Offset');
166 d.x = this.getOutOfBoundOffsetX(p.x);
167 d.y = this.getOutOfBoundOffsetY(p.y);
173 <span id='Ext-util-Region-method-getOutOfBoundOffsetX'> /**
174 </span> * Get the offset amount on the x-axis
175 * @param {Number} p the offset
178 getOutOfBoundOffsetX: function(p) {
179 if (p <= this.x) {
181 } else if (p >= this.right) {
182 return this.right - p;
188 <span id='Ext-util-Region-method-getOutOfBoundOffsetY'> /**
189 </span> * Get the offset amount on the y-axis
190 * @param {Number} p the offset
193 getOutOfBoundOffsetY: function(p) {
194 if (p <= this.y) {
196 } else if (p >= this.bottom) {
197 return this.bottom - p;
203 <span id='Ext-util-Region-method-isOutOfBound'> /**
204 </span> * Check whether the point / offset is out of bound
205 * @param {String} [axis]
206 * @param {Ext.util.Point/Number} [p] the point / offset
209 isOutOfBound: function(axis, p) {
210 if (!Ext.isObject(axis)) {
212 return this.isOutOfBoundX(p);
214 return this.isOutOfBoundY(p);
218 return (this.isOutOfBoundX(p.x) || this.isOutOfBoundY(p.y));
222 <span id='Ext-util-Region-method-isOutOfBoundX'> /**
223 </span> * Check whether the offset is out of bound in the x-axis
224 * @param {Number} p the offset
227 isOutOfBoundX: function(p) {
228 return (p < this.x || p > this.right);
231 <span id='Ext-util-Region-method-isOutOfBoundY'> /**
232 </span> * Check whether the offset is out of bound in the y-axis
233 * @param {Number} p the offset
236 isOutOfBoundY: function(p) {
237 return (p < this.y || p > this.bottom);
240 <span id='Ext-util-Region-method-restrict'> /**
241 </span> * Restrict a point within the region by a certain factor.
242 * @param {String} [axis]
243 * @param {Ext.util.Point/Ext.util.Offset/Object} [p]
244 * @param {Number} [factor]
245 * @return {Ext.util.Point/Ext.util.Offset/Object/Number}
248 restrict: function(axis, p, factor) {
249 if (Ext.isObject(axis)) {
265 newP.x = this.restrictX(p.x, factor);
266 newP.y = this.restrictY(p.y, factor);
270 return this.restrictX(p, factor);
272 return this.restrictY(p, factor);
277 <span id='Ext-util-Region-method-restrictX'> /**
278 </span> * Restrict an offset within the region by a certain factor, on the x-axis
280 * @param {Number} [factor=1] The factor.
284 restrictX : function(p, factor) {
289 if (p <= this.x) {
290 p -= (p - this.x) * factor;
292 else if (p >= this.right) {
293 p -= (p - this.right) * factor;
298 <span id='Ext-util-Region-method-restrictY'> /**
299 </span> * Restrict an offset within the region by a certain factor, on the y-axis
301 * @param {Number} [factor] The factor, defaults to 1
305 restrictY : function(p, factor) {
310 if (p <= this.y) {
311 p -= (p - this.y) * factor;
313 else if (p >= this.bottom) {
314 p -= (p - this.bottom) * factor;
319 <span id='Ext-util-Region-method-getSize'> /**
320 </span> * Get the width / height of this region
321 * @return {Object} an object with width and height properties
324 getSize: function() {
326 width: this.right - this.x,
327 height: this.bottom - this.y
331 <span id='Ext-util-Region-method-copy'> /**
332 </span> * Create a copy of this Region.
333 * @return {Ext.util.Region}
336 return new this.self(this.y, this.right, this.bottom, this.x);
339 <span id='Ext-util-Region-method-copyFrom'> /**
340 </span> * Copy the values of another Region to this Region
341 * @param {Ext.util.Region} p The region to copy from.
342 * @return {Ext.util.Region} This Region
344 copyFrom: function(p) {
346 me.top = me.y = me[1] = p.y;
348 me.bottom = p.bottom;
349 me.left = me.x = me[0] = p.x;
355 * Dump this to an eye-friendly string, great for debugging
358 toString: function() {
359 return "Region[" + this.top + "," + this.right + "," + this.bottom + "," + this.left + "]";
362 <span id='Ext-util-Region-method-translateBy'> /**
363 </span> * Translate this region by the given offset amount
364 * @param {Ext.util.Offset/Object} x Object containing the `x` and `y` properties.
365 * Or the x value is using the two argument form.
366 * @param {Number} y The y value unless using an Offset object.
367 * @return {Ext.util.Region} this This Region
369 translateBy: function(x, y) {
370 if (arguments.length == 1) {
383 <span id='Ext-util-Region-method-round'> /**
384 </span> * Round all the properties of this region
385 * @return {Ext.util.Region} this This Region
389 me.top = me.y = Math.round(me.y);
390 me.right = Math.round(me.right);
391 me.bottom = Math.round(me.bottom);
392 me.left = me.x = Math.round(me.x);
397 <span id='Ext-util-Region-method-equals'> /**
398 </span> * Check whether this region is equivalent to the given region
399 * @param {Ext.util.Region} region The region to compare with
402 equals: function(region) {
403 return (this.top == region.top && this.right == region.right && this.bottom == region.bottom && this.left == region.left);