Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / util / Offset.js
1 /**
2  * @class Ext.util.Offset
3  * @ignore
4  */
5 Ext.define('Ext.util.Offset', {
6
7     /* Begin Definitions */
8
9     statics: {
10         fromObject: function(obj) {
11             return new this(obj.x, obj.y);
12         }
13     },
14
15     /* End Definitions */
16
17     constructor: function(x, y) {
18         this.x = (x != null && !isNaN(x)) ? x : 0;
19         this.y = (y != null && !isNaN(y)) ? y : 0;
20
21         return this;
22     },
23
24     copy: function() {
25         return new Ext.util.Offset(this.x, this.y);
26     },
27
28     copyFrom: function(p) {
29         this.x = p.x;
30         this.y = p.y;
31     },
32
33     toString: function() {
34         return "Offset[" + this.x + "," + this.y + "]";
35     },
36
37     equals: function(offset) {
38         //<debug>
39         if(!(offset instanceof this.statics())) {
40             Ext.Error.raise('Offset must be an instance of Ext.util.Offset');
41         }
42         //</debug>
43
44         return (this.x == offset.x && this.y == offset.y);
45     },
46
47     round: function(to) {
48         if (!isNaN(to)) {
49             var factor = Math.pow(10, to);
50             this.x = Math.round(this.x * factor) / factor;
51             this.y = Math.round(this.y * factor) / factor;
52         } else {
53             this.x = Math.round(this.x);
54             this.y = Math.round(this.y);
55         }
56     },
57
58     isZero: function() {
59         return this.x == 0 && this.y == 0;
60     }
61 });