|
11 | 11 | </style>
|
12 | 12 | <script type="module">
|
13 | 13 | import WiredPanels from '../WiredPanels.js';
|
14 |
| -const wiredPanels = new WiredPanels(document.body); |
15 |
| -wiredPanels.config.onactivation = function() { |
16 |
| - const panels = new Set(); |
17 |
| - for (const node of wiredPanels.selection) |
18 |
| - if (node.type !== 'wire') { |
19 |
| - const newLabel = prompt('Label', node.label.textContent); |
20 |
| - if (newLabel != undefined) node.label.textContent = newLabel; |
21 |
| - panels.add(node.type === 'panel' ? node : node.panel); |
22 |
| - } |
23 |
| - for (const node of panels) wiredPanels.updatePanelGeometry(node); |
24 |
| -}; |
25 |
| -wiredPanels.config.ondeletion = function() {}; |
26 |
| -wiredPanels.config.onwiredrag = function(socket) { |
27 |
| - return true; |
28 |
| -}; |
29 |
| -wiredPanels.config.onwireconnect = function(node, wire, nodesToAdd) { |
30 |
| - if (node.type === 'panel') { |
31 |
| - const rect = wiredPanels.boundingRectOfPanel(node), |
32 |
| - diffX = wire.dstSocket.primaryElement.x - (rect[0] + rect[1]) / 2, |
33 |
| - diffY = wire.dstSocket.primaryElement.y - (rect[2] + rect[3]) / 2; |
34 |
| - wire.dstSocket = wiredPanels.createSocket(); |
35 |
| - wire.dstSocket.panel = node; |
36 |
| - if (Math.abs(diffX) === Math.max(Math.abs(diffX), Math.abs(diffY))) |
37 |
| - wire.dstSocket.orientation = diffX < 0 ? 'left' : 'right'; |
38 |
| - else wire.dstSocket.orientation = diffY < 0 ? 'top' : 'bottom'; |
39 |
| - wire.dstSocket.label.textContent = 'added'; |
40 |
| - nodesToAdd.add(wire.dstSocket); |
41 |
| - } else if (node !== wire.srcSocket) wire.dstSocket = node; |
42 |
| - return []; |
43 |
| -}; |
44 |
| -wiredPanels.svg.ondblclick = function(event) { |
45 |
| - const mousePos = wiredPanels.mousePositionOfEvent(event), |
46 |
| - panel = wiredPanels.createPanel(), |
47 |
| - sockets = []; |
48 |
| - panel.x = mousePos[0]; |
49 |
| - panel.y = mousePos[1]; |
50 |
| - panel.label.textContent = 'panel'; |
51 |
| - for (let i = 0; i < 10; ++i) { |
52 |
| - const socket = wiredPanels.createSocket(); |
53 |
| - socket.panel = panel; |
54 |
| - socket.orientation = ['top', 'left', 'right', 'bottom'][i % 4]; |
55 |
| - socket.label.textContent = socket.orientation + ' ' + i; |
56 |
| - sockets.push(socket); |
| 14 | +const wiredPanels = new WiredPanels(document.body,{ |
| 15 | + ondeletion() {}, |
| 16 | + onwiredrag(socket) { return true;}, |
| 17 | + onactivation() { |
| 18 | + const panels = new Set(); |
| 19 | + for (const node of wiredPanels.selection) |
| 20 | + if (node.type !== 'wire') { |
| 21 | + const newLabel = prompt('Label', node.label.textContent); |
| 22 | + if (newLabel != undefined) node.label.textContent = newLabel; |
| 23 | + panels.add(node.type === 'panel' ? node : node.panel); |
| 24 | + } |
| 25 | + for (const node of panels) wiredPanels.updatePanelGeometry(node); |
| 26 | + }, |
| 27 | + onwireconnect(node, wire, nodesToAdd) { |
| 28 | + if (node.type === 'panel') { |
| 29 | + const rect = wiredPanels.boundingRectOfPanel(node), |
| 30 | + diffX = wire.dstSocket.primaryElement.x - (rect[0] + rect[1]) / 2, |
| 31 | + diffY = wire.dstSocket.primaryElement.y - (rect[2] + rect[3]) / 2; |
| 32 | + wire.dstSocket = wiredPanels.createSocket(); |
| 33 | + wire.dstSocket.panel = node; |
| 34 | + if (Math.abs(diffX) === Math.max(Math.abs(diffX), Math.abs(diffY))) |
| 35 | + wire.dstSocket.orientation = diffX < 0 ? 'left' : 'right'; |
| 36 | + else wire.dstSocket.orientation = diffY < 0 ? 'top' : 'bottom'; |
| 37 | + wire.dstSocket.label.textContent = 'added'; |
| 38 | + nodesToAdd.add(wire.dstSocket); |
| 39 | + } else if (node !== wire.srcSocket) wire.dstSocket = node; |
| 40 | + return []; |
57 | 41 | }
|
58 |
| - wiredPanels.changeGraphUndoable(new Set([panel, ...sockets]), []); |
| 42 | +}); |
| 43 | + |
| 44 | +wiredPanels.svg.ondblclick = function(event) { |
| 45 | + const mousePos = wiredPanels.mousePositionOfEvent(event), |
| 46 | + panel = wiredPanels.createPanel(), sockets = []; |
| 47 | + panel.x = mousePos[0]; |
| 48 | + panel.y = mousePos[1]; |
| 49 | + panel.label.textContent = 'panel'; |
| 50 | + for(let i = 0; i < 10; ++i) { |
| 51 | + const socket = wiredPanels.createSocket(); |
| 52 | + socket.panel = panel; |
| 53 | + socket.orientation = ['top', 'left', 'right', 'bottom'][i % 4]; |
| 54 | + socket.label.textContent = socket.orientation + ' ' + i; |
| 55 | + sockets.push(socket); |
| 56 | + } |
| 57 | + wiredPanels.changeGraphUndoable(new Set([panel, ...sockets]), []); |
59 | 58 | };
|
60 | 59 |
|
61 | 60 | const panel = wiredPanels.createPanel(),
|
|
0 commit comments