initial import
[roojs1] / Roo / lib / Region.js
1 /*
2  * Portions of this file are based on pieces of Yahoo User Interface Library
3  * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
4  * YUI licensed under the BSD License:
5  * http://developer.yahoo.net/yui/license.txt
6  * <script type="text/javascript">
7  *
8  */
9
10 Roo.lib.Region = function(t, r, b, l) {
11     this.top = t;
12     this[1] = t;
13     this.right = r;
14     this.bottom = b;
15     this.left = l;
16     this[0] = l;
17 };
18
19
20 Roo.lib.Region.prototype = {
21     contains : function(region) {
22         return ( region.left >= this.left &&
23                  region.right <= this.right &&
24                  region.top >= this.top &&
25                  region.bottom <= this.bottom    );
26
27     },
28
29     getArea : function() {
30         return ( (this.bottom - this.top) * (this.right - this.left) );
31     },
32
33     intersect : function(region) {
34         var t = Math.max(this.top, region.top);
35         var r = Math.min(this.right, region.right);
36         var b = Math.min(this.bottom, region.bottom);
37         var l = Math.max(this.left, region.left);
38
39         if (b >= t && r >= l) {
40             return new Roo.lib.Region(t, r, b, l);
41         } else {
42             return null;
43         }
44     },
45     union : function(region) {
46         var t = Math.min(this.top, region.top);
47         var r = Math.max(this.right, region.right);
48         var b = Math.max(this.bottom, region.bottom);
49         var l = Math.min(this.left, region.left);
50
51         return new Roo.lib.Region(t, r, b, l);
52     },
53
54     adjust : function(t, l, b, r) {
55         this.top += t;
56         this.left += l;
57         this.right += r;
58         this.bottom += b;
59         return this;
60     }
61 };
62
63 Roo.lib.Region.getRegion = function(el) {
64     var p = Roo.lib.Dom.getXY(el);
65
66     var t = p[1];
67     var r = p[0] + el.offsetWidth;
68     var b = p[1] + el.offsetHeight;
69     var l = p[0];
70
71     return new Roo.lib.Region(t, r, b, l);
72 };