Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / util / Offset.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.util.Offset
17  * @ignore
18  */
19 Ext.define('Ext.util.Offset', {
20
21     /* Begin Definitions */
22
23     statics: {
24         fromObject: function(obj) {
25             return new this(obj.x, obj.y);
26         }
27     },
28
29     /* End Definitions */
30
31     constructor: function(x, y) {
32         this.x = (x != null && !isNaN(x)) ? x : 0;
33         this.y = (y != null && !isNaN(y)) ? y : 0;
34
35         return this;
36     },
37
38     copy: function() {
39         return new Ext.util.Offset(this.x, this.y);
40     },
41
42     copyFrom: function(p) {
43         this.x = p.x;
44         this.y = p.y;
45     },
46
47     toString: function() {
48         return "Offset[" + this.x + "," + this.y + "]";
49     },
50
51     equals: function(offset) {
52         //<debug>
53         if(!(offset instanceof this.statics())) {
54             Ext.Error.raise('Offset must be an instance of Ext.util.Offset');
55         }
56         //</debug>
57
58         return (this.x == offset.x && this.y == offset.y);
59     },
60
61     round: function(to) {
62         if (!isNaN(to)) {
63             var factor = Math.pow(10, to);
64             this.x = Math.round(this.x * factor) / factor;
65             this.y = Math.round(this.y * factor) / factor;
66         } else {
67             this.x = Math.round(this.x);
68             this.y = Math.round(this.y);
69         }
70     },
71
72     isZero: function() {
73         return this.x == 0 && this.y == 0;
74     }
75 });
76