-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
executable file
·90 lines (84 loc) · 3.11 KB
/
utils.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
export function hasClass(el, className) {
if (el.classList)
return el.classList.contains(className)
else
return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))
}
export function addClass(el, className) {
if (el.classList)
el.classList.add(className)
else if (!hasClass(el, className))
el.className += " " + className
}
export function removeClass(el, className) {
if (el.classList)
el.classList.remove(className)
else if (hasClass(el, className)) {
var reg = new RegExp('(\\s|^)' + className + '(\\s|$)')
el.className=el.className.replace(reg, ' ')
}
}
export function calculateLine(link) {
var lx1, lx2, ly1, ly2;
var xIntersect = (link.source.x <= link.target.x && link.source.x + link.source.width >= link.target.x)
|| (link.target.x <= link.source.x && link.target.x + link.target.width >= link.source.x);
var yIntersect = link.source.y <= link.target.y && link.source.y + link.source.height >= link.target.y
|| (link.target.y <= link.source.y && link.target.y + link.target.height >= link.source.y);
if (xIntersect) {
lx1 = link.source.x + link.source.width / 2;
lx2 = link.target.x + link.target.width / 2;
if (link.source.y + link.source.height <= link.target.y) {
//source higher
ly1 = link.source.y + link.source.height;
ly2 = link.target.y;
}
else {
ly1 = link.source.y;
ly2 = link.target.y + link.target.height;
}
link.lineType = "LINE_TYPE_RECT_2BREAK";
link.lineStart = "LINE_START_VERTICAL";
}
else if (yIntersect) {
ly1 = link.source.y + link.source.height / 2;
ly2 = link.target.y + link.target.height / 2;
if (link.source.x + link.source.width <= link.target.x) {
lx1 = link.source.x + link.source.width;
lx2 = link.target.x;
}
else {
lx1 = link.source.x;
lx2 = link.target.x + link.target.width;
}
link.lineType = "LINE_TYPE_RECT_2BREAK";
link.lineStart = "LINE_START_HORIZONTAL";
}
else {
if (link.source.y + link.source.height <= link.target.y) {
//source higher
ly1 = link.source.y + link.source.height / 2;
ly2 = link.target.y;
if (link.source.x + link.source.width <= link.target.x) {
lx1 = link.source.x + link.source.width;
}
else {
lx1 = link.source.x;
}
lx2 = link.target.x + link.target.width / 2;
}
else {
ly1 = link.source.y + link.source.height / 2;
ly2 = link.target.y + link.target.height;
if (link.source.x + link.source.width <= link.target.x) {
lx1 = link.source.x + link.source.width;
}
else {
lx1 = link.source.x;
}
lx2 = link.target.x + link.target.width / 2;
}
link.lineType = "LINE_TYPE_RECT_1BREAK";
link.lineStart = "LINE_START_HORIZONTAL";
}
return { x1: lx1, y1: ly1, x2: lx2, y2: ly2};
}