4 * Copyright 2016 RIFT.IO Inc
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * Created by onvelocity on 8/25/15.
23 export default class Position
{
25 constructor(pos
= {}) {
26 this._position
= Object
.assign({}, Position
.defaults(), pos
);
27 // if consumer specified width or height in the
28 // constructor then set right and bottom....
29 if (!isNaN(parseFloat(pos
.width
))) {
30 // ensure right is set
31 this.width
= pos
.width
;
33 if (!isNaN(parseFloat(pos
.height
))) {
34 // ensure bottom is set
35 this.height
= pos
.height
;
51 return Object
.assign({}, this._position
);
55 return this._position
.top
;
59 this._position
.top
= parseFloat(value
) || 0;
64 return this._position
.left
;
68 this._position
.left
= parseFloat(value
) || 0;
73 return this._position
.bottom
;
77 this._position
.bottom
= parseFloat(value
) || 0;
82 return this._position
.right
;
86 this._position
.right
= parseFloat(value
) || 0;
91 return Math
.abs(this.right
- this.left
);
95 const value
= parseFloat(width
);
97 this.right
= value
+ this.left
;
98 this._position
.width
= this.width
;
103 return Math
.abs(this.bottom
- this.top
);
107 const value
= parseFloat(height
);
109 this.bottom
= value
+ this.top
;
110 this._position
.height
= this.height
;
115 return JSON
.stringify(this._position
);
131 x
: this.left
+ (this.width
/ 2),
132 y
: this.top
+ (this.height
/ 2)
142 const width
= this.width
;
143 const value
= parseFloat(left
);
145 this._position
.left
= value
;
146 this._position
.right
= value
+ width
;
151 const width
= this.width
;
152 const value
= parseFloat(right
);
154 this._position
.left
= value
- width
;
155 this._position
.right
= value
;
160 const height
= this.height
;
161 const value
= parseFloat(top
);
163 this._position
.top
= value
;
164 this._position
.bottom
= value
+ height
;
169 const height
= this.height
;
170 const value
= parseFloat(bottom
);
172 this._position
.top
= value
- height
;
173 this._position
.bottom
= value
;