-
Notifications
You must be signed in to change notification settings - Fork 68
/
line-helper.js
73 lines (64 loc) · 2.43 KB
/
line-helper.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
// This JS will help draw the animated likens between the Elements
function LineHelper() {
var normalLineConfigs = {
dash: { len: 10, gap: 25, animation: { duration: 200 } },
endPlug: "arrow2",
hide:true
};
var highlightLineConfigs = {
dash: { len: 500, gap: 900, animation: { duration: 2000 } },
endPlug: "arrow2",
hide:true
};
this.allLines = [];
this.drawLine = function (startElement, EndElement) {
//draw line from start element to element
console.log(checkOrGetElement(startElement));
var line = new LeaderLine(
checkOrGetElement(startElement),
LeaderLine.pointAnchor({ element: checkOrGetElement(EndElement), x: -5, y: '50%' }),
normalLineConfigs
);
var LineObject = {};
LineObject.from = startElement;
LineObject.to = EndElement;
LineObject.line = line;
this.allLines.push(LineObject);
line.show("fade");
}
this.highlightLine = function (startElement, EndElement, message) {
//find the line
var line = this.allLines.find(line => line.from === startElement && line.to === EndElement);
//remove the line
line.line.remove();
this.allLines.splice(this.allLines.indexOf(line), 1);
highlightLineConfigs.middleLabel = LeaderLine.pathLabel({ text: message || `New post received` });
var highlightLine = new LeaderLine(
checkOrGetElement(startElement),
LeaderLine.pointAnchor({ element: checkOrGetElement(EndElement), x: -5, y: '50%' }),
highlightLineConfigs,
);
highlightLine.show("draw");
//create a new for a 1500ms
setTimeout(() => {
highlightLine.remove();
this.drawLine(startElement, EndElement);
}, 5000)
//create the normal line again
}
function checkOrGetElement(element) {
//returning element if it is a HTML element already
if (element instanceof Element || element instanceof HTMLDocument) {
return element;
} else if (typeof element === 'string') {
if (document.querySelector(element) !== null) {
return document.querySelector(element);
} else {
throw Error("Invalid element selector");
}
} else {
throw Error("Invalid element selector");
}
}
return this;
}