This repository was archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
208 lines (160 loc) · 4.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Connector element
*
* @module connection-line
*/
var extend = require('xtend/mutable');
var offset = require('mucss/offset');
/**
* @constructor
*
* @param {Object} properties Init options
*/
function Connector (properties) {
if (!(this instanceof Connector)) return new Connector(properties);
//read attributes of an element once
if (properties.element) {
for (var i = 0, l = properties.element.attributes.length; i < l; i++) {
var attribute = properties.element.attributes[i];
if (properties[attribute.name] === undefined) {
properties[attribute.name] = attribute.value;
}
}
}
//take over options
extend(this, properties);
this.ns = 'http://www.w3.org/2000/svg';
//ensure element
if (!this.element) {
this.element = document.createElement('connection-line');
}
//ensure element style
this.element.style.position = 'absolute';
this.element.style.minWidth = '20px';
this.element.style.minHeight = '20px';
//create path
this.svg = document.createElementNS(this.ns, 'svg');
this.path = document.createElementNS(this.ns, 'path');
this.path.style.stroke = this.lineColor;
this.path.style.fill = 'transparent';
this.path.style.strokeWidth = this.lineWidth;
this.svg.appendChild(this.path);
this.element.appendChild(this.svg);
//set proper connector attributes
this.update();
}
/**
* Preferred direction of connection.
* horizontal, vertical or diagonal.
* If undefined - it is picker automatically.
*/
Connector.prototype.orientation = 'horizontal';
/**
* Source/target element or coordinates
*/
Connector.prototype.from = [0, 0];
Connector.prototype.to = [100, 100];
/**
* Line style options
*/
Connector.prototype.lineEnd = '➤';
Connector.prototype.lineStart = '';
Connector.prototype.lineDash = '';
Connector.prototype.lineWidth = 1;
Connector.prototype.lineColor = 'black';
/**
* Curvature displays style of rendering
* 1 - smooth curve
* 0 - straight line
*/
Connector.prototype.curvature = 0.5;
/**
* Number of sections to split the curve.
* 0 - straight line.
* 1 - one-angle smooth transition.
* 2 - elbow-connector
* 3 - ...
*/
Connector.prototype.divisions = 1;
/**
* Number of parallel lines
*/
Connector.prototype.channels = 1;
/**
* Display swapped channels, e. g. {0: 1, 1: 0}
*/
Connector.prototype.swap = {};
/**
* Display title of a connector
*/
Connector.prototype.title = '';
/**
* Display status - a label on top of the connector
*/
Connector.prototype.modifier = '✔';
/**
* Update position of connection
*/
Connector.prototype.update = function () {
var self = this;
//get target offsets
var from = getCoords(this.from);
var to = getCoords(this.to);
//set size so to fit distance
var size = [Math.abs(to[0] - from[0]), Math.abs(to[1] - from[1])];
this.element.style.width = size[0] + 'px';
this.element.style.height = size[1] + 'px';
this.svg.setAttribute('width', size[0]);
this.svg.setAttribute('height', size[1]);
//detect h/v directions
var isTop = to[1] < from[1];
var isRight = to[0] > from[0];
//correct target offset
this.element.style.left = Math.min(to[0], from[0]) + 'px';
this.element.style.top = Math.min(to[1], from[1]) + 'px';
//form path
var c = [size[0]/2, size[1]/2];
var path = 'M ' + (isRight ? 0 : size[0]) + ' ' + (isTop ? size[1] : 0) + ' ' +
'C ' + c[0] + ' ' + (isTop ? size[1] : 0) + ' ' +
c[0] + ' ' + (isTop ? 0 : size[1]) + ' ' +
(isRight ? size[0] : 0) + ' ' + (isTop ? 0 : size[1]);
//set path coords
this.path.setAttribute('d', path);
//return absolute offset for a target
function getCoords (target) {
var coords;
if (typeof target === 'string') {
//`100, 200` - coords relative to offsetParent
if ((coords = target.split(/\s*,\s*/)).length === 2) {
coords[0] = parseInt(coords[0]);
coords[1] = parseInt(coords[1]);
}
//`.selector` - calc selected target coords relative to offset parent
else {
var target = document.querySelector(target);
if (!target) {
coords = [0, 0];
}
else {
var targetOffset = offset(target);
var parent = self.element.offsetParent || self.element.parentNode;
var parentOffset = offset(parent);
coords = [
targetOffset.left + targetOffset.width/2 - parentOffset.left,
targetOffset.top + targetOffset.height/2 - parentOffset.top
];
}
}
}
else if (target instanceof Array) {
coords = target;
}
return coords;
}
};
/**
* Render frame, e.g. animation, physics of movement etc
*/
Connector.prototype.render = function () {
};
module.exports = Connector;