Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / graph / PathBuilder.js
1
2 /**
3 * Created by onvelocity on 2/10/16.
4 */
5
6 export default class PathBuilder {
7
8 constructor() {
9 this.path = [];
10 }
11
12 static archFlags(override) {
13 return Object.assign({xAxisRotation: 0, largeArcFlag: 0, sweepFlag: 0}, override);
14 }
15
16 M(x, y, info = {}) {
17 this.path.push({x: x, y: y, cmd: 'M', info: info});
18 return this;
19 }
20
21 m(dx, dy, info = {}) {
22 this.path.push({dx: dx, dy: dy, cmd: 'm', info: info});
23 return this;
24 }
25
26 L(x, y, info = {}) {
27 this.path.push({x: x, y: y, cmd: 'L', info: info});
28 return this;
29 }
30
31 l(dx, dy, info = {}) {
32 this.path.push({dx: dx, dy: dy, cmd: 'l', info: info});
33 return this;
34 }
35
36 H(x, info = {}) {
37 this.path.push({x: x, cmd: 'H', info: info});
38 return this;
39 }
40
41 h(dx, info = {}) {
42 this.path.push({dx: dx, cmd: 'h', info: info});
43 return this;
44 }
45
46 V(y, info = {}) {
47 this.path.push({y: y, cmd: 'V', info: info});
48 return this;
49 }
50
51 v(dy, info = {}) {
52 this.path.push({dy: dy, cmd: 'v', info: info});
53 return this;
54 }
55
56 C(x1, y1, x2, y2, x, y, info = {}) {
57 this.path.push({x1: x1, y1: y1, x2: x2, y2: y2, x: x, y: y, cmd: 'C', info: info});
58 return this;
59 }
60
61 c(dx1, dy1, dx2, dy2, dx, dy, info = {}) {
62 this.path.push({dx1: dx1, dy1: dy1, dx2: dx2, dy2: dy2, dx: dx, dy: dy, cmd: 'c', info: info});
63 return this;
64 }
65
66 Q(x1, y1, x, y, info = {}) {
67 this.path.push({x1: x1, y1: y1, x: x, y: y, cmd: 'Q', info: info});
68 return this;
69 }
70
71 q(x1, y1, x, y, info = {}) {
72 this.path.push({x1: x1, y1: y1, x: x, y: y, cmd: 'q', info: info});
73 return this;
74 }
75
76 T(x, y, info = {}) {
77 this.path.push({x: x, y: y, cmd: 'T', info: info});
78 return this;
79 }
80
81 A (rx, ry, x, y, info = {xAxisRotation: 0, largeArcFlag: 0, sweepFlag: 0}) {
82 info = PathBuilder.archFlags(info);
83 this.path.push({rx: rx, ry: ry, 'x-axis-rotation': info.xAxisRotation, 'large-arc-flag': info.largeArcFlag, 'sweep-flag': info.sweepFlag, x: x, y: y, cmd: 'A', info: info});
84 return this;
85 }
86
87 S(x2, y2, x, y, info = {}) {
88 this.path.push({x2: x2, y2: y2, x: x, y: y, cmd: 'S', info: info});
89 return this;
90 }
91
92 H(x, info = {}) {
93 this.path.push({x: x, info: info});
94 return this;
95 }
96
97 h(dx, info = {}) {
98 this.path.push({dx: dx, info: info});
99 return this;
100 }
101
102 V(y, info = {}) {
103 this.path.push({y: y, info: info});
104 return this;
105 }
106
107 v(dy, info = {}) {
108 this.path.push({dy: dy, info: info});
109 return this;
110 }
111
112 get Z() {
113 this.path.push({cmd: 'Z'});
114 return this;
115 }
116
117 last() {
118 return this.path[this.path.length - 1];
119 }
120
121 findPrevious(filter) {
122 return this.path.splice().reverse().reduce((r, d) => {
123 if (r) {
124 return r;
125 }
126 if (filter(d)) {
127 return d
128 }
129 }, null);
130 }
131
132 insertBeforeLast() {
133 if (this.path.length < 3) {
134 return this;
135 }
136 const last = this.path.pop();
137 this.path.splice(this.length - 2, 0, last);
138 return this;
139 }
140
141 concat(pathBuilder) {
142 this.path = this.path.filter(d => d.cmd !== 'Z').concat(pathBuilder);
143 return this;
144 }
145
146 toString() {
147 return this.path.reduce((r, d) => {
148 return r + ' ' + d.cmd + ' ' + Object.keys(d).filter(k => !(k == 'cmd' || k === 'info')).map(k => d[k]).reduce((r, val) => r + ' ' + val, '');
149 }, '').replace(/\s+/g, ' ').trim();
150 }
151
152 get length() {
153 return this.path.length;
154 }
155
156 static descriptorPath(borderRadius = 20, height = 55, width = 250) {
157 const path = new PathBuilder();
158 borderRadius = Math.max(0, borderRadius);
159 width = Math.max(borderRadius, width) - borderRadius;
160 height = Math.max(borderRadius, height) - (borderRadius);
161 const iconWidthHeight = 40;
162 return path.M(0, 0)
163 // top
164 .L(width, 0).C(width, 0, width + borderRadius, 0, width + borderRadius, borderRadius)
165 // right
166 .L(width + borderRadius, height).C(width + borderRadius, height, width + borderRadius, height + borderRadius, width, height + borderRadius)
167 // bottom
168 .L(0, height + borderRadius).C(0, height + borderRadius, 0 - borderRadius, height + borderRadius, 0 - borderRadius, height)
169 // left
170 .L(0 - borderRadius, 0 + borderRadius).C(0 - borderRadius, 0 + borderRadius, 0 - borderRadius, 0, 0, 0)
171 // icon separator
172 //.L(iconWidthHeight, 0).L(iconWidthHeight, iconWidthHeight + (borderRadius + height - iconWidthHeight))
173 // label separator
174 //.L(iconWidthHeight, iconWidthHeight).L(0 - borderRadius, iconWidthHeight);
175 }
176
177 }