Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / graph / Position.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 */
19 /**
20 * Created by onvelocity on 8/25/15.
21 */
22
23 export default class Position {
24
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;
32 }
33 if (!isNaN(parseFloat(pos.height))) {
34 // ensure bottom is set
35 this.height = pos.height;
36 }
37 }
38
39 static defaults() {
40 return {
41 top: 0,
42 left: 0,
43 bottom: 0,
44 right: 0,
45 width: 0,
46 height: 0
47 };
48 }
49
50 get position() {
51 return Object.assign({}, this._position);
52 }
53
54 get top() {
55 return this._position.top;
56 }
57
58 set top(value) {
59 this._position.top = parseFloat(value) || 0;
60 return this;
61 }
62
63 get left() {
64 return this._position.left;
65 }
66
67 set left(value) {
68 this._position.left = parseFloat(value) || 0;
69 return this;
70 }
71
72 get bottom() {
73 return this._position.bottom;
74 }
75
76 set bottom(value) {
77 this._position.bottom = parseFloat(value) || 0;
78 return this;
79 }
80
81 get right() {
82 return this._position.right;
83 }
84
85 set right(value) {
86 this._position.right = parseFloat(value) || 0;
87 return this;
88 }
89
90 get width() {
91 return Math.abs(this.right - this.left);
92 }
93
94 set width(width) {
95 const value = parseFloat(width);
96 if (!isNaN(value)) {
97 this.right = value + this.left;
98 this._position.width = this.width;
99 }
100 }
101
102 get height() {
103 return Math.abs(this.bottom - this.top);
104 }
105
106 set height(height) {
107 const value = parseFloat(height);
108 if (!isNaN(value)) {
109 this.bottom = value + this.top;
110 this._position.height = this.height;
111 }
112 }
113
114 toString() {
115 return JSON.stringify(this._position);
116 }
117
118 value() {
119 return {
120 top: this.top,
121 left: this.left,
122 right: this.right,
123 bottom: this.bottom,
124 width: this.width,
125 height: this.height
126 };
127 }
128
129 centerPoint() {
130 return {
131 x: this.left + (this.width / 2),
132 y: this.top + (this.height / 2)
133 };
134 }
135
136 move(left, top) {
137 this.moveTop(top);
138 this.moveLeft(left);
139 }
140
141 moveLeft(left) {
142 const width = this.width;
143 const value = parseFloat(left);
144 if (!isNaN(value)) {
145 this._position.left = value;
146 this._position.right = value + width;
147 }
148 }
149
150 moveRight(right) {
151 const width = this.width;
152 const value = parseFloat(right);
153 if (!isNaN(value)) {
154 this._position.left = value - width;
155 this._position.right = value;
156 }
157 }
158
159 moveTop(top) {
160 const height = this.height;
161 const value = parseFloat(top);
162 if (!isNaN(value)) {
163 this._position.top = value;
164 this._position.bottom = value + height;
165 }
166 }
167
168 moveBottom(bottom) {
169 const height = this.height;
170 const value = parseFloat(bottom);
171 if (!isNaN(value)) {
172 this._position.top = value - height;
173 this._position.bottom = value;
174 }
175 }
176
177 }