-
Notifications
You must be signed in to change notification settings - Fork 1
/
addPortInteraction.js
98 lines (83 loc) · 2.43 KB
/
addPortInteraction.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
import { createListener, buildPipeID } from "./utils.js";
export function addPortInteraction(workspace, state) {
const listen = createListener(workspace);
function getToolDetails(e) {
let id = parentToolElement(e).dataset.toolid;
return [id, state.toolchain.tools[id]];
}
function parentToolElement(e) {
return e.target.closest(".tool");
}
function addPipe(start, end) {
if (start.toolID == end.toolID) {
console.log("Can't connect a tool to itself!");
return;
}
let pipeID = buildPipeID(
start.toolID,
start.portID,
end.toolID,
end.portID
);
let startTool = state.toolchain.tools[start.toolID];
let endTool = state.toolchain.tools[end.toolID];
state.toolchain.pipes[pipeID] = { start, end };
endTool.inports[end.portID].value = startTool.outports[start.portID].value;
if ("inportsUpdated" in endTool.lifecycle) {
// run the inports updated method when a pipe is connected
endTool.lifecycle.inportsUpdated();
}
}
function handleInport(e) {
const [toolID, toolInfo] = getToolDetails(e);
const portID = e.target.dataset.portid;
if ("loose" in state.toolchain.pipes) {
let start = state.toolchain.pipes["loose"].start;
if (!start) {
delete state.toolchain.pipes["loose"];
return;
}
let end = {
toolID: toolID,
portID: portID,
};
addPipe(start, end);
delete state.toolchain.pipes["loose"];
} else {
state.toolchain.pipes["loose"] = {
end: {
toolID: toolID,
portID: portID,
},
};
}
}
function handleOutport(e) {
let [toolID, toolInfo] = getToolDetails(e);
const portID = e.target.dataset.portid;
if ("loose" in state.toolchain.pipes) {
let end = state.toolchain.pipes["loose"].end;
if (!end) {
delete state.toolchain.pipes["loose"];
return;
}
let start = {
toolID: toolID,
portID: portID,
};
addPipe(start, end);
delete state.toolchain.pipes["loose"];
} else {
state.toolchain.pipes["loose"] = {
start: {
toolID: toolID,
portID: portID,
},
};
}
}
listen("pointerup", ".inports .port", handleInport);
listen("pointerdown", ".inports .port", handleInport);
listen("pointerup", ".outports .port", handleOutport);
listen("pointerdown", ".outports .port", handleOutport);
}