Skip to content

Commit 8d497ad

Browse files
committed
feat: optional config arg in WiredPanels constructor
WiredPanels config can now be given to in the constructor ```js wp = new WiredPanels(parentElement,{ onwiredrag(socket) { return true;} .... }); ```
1 parent 0c44f84 commit 8d497ad

File tree

2 files changed

+47
-55
lines changed

2 files changed

+47
-55
lines changed

WiredPanels.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function deleteSelected(event) {
125125
}
126126

127127
export default class WiredPanels {
128-
constructor(parentElement) {
128+
constructor(parentElement,config={}) {
129129
const acceptClipboard = function(event) {
130130
if(!this.config.onacceptclipboard)
131131
return false;
@@ -336,7 +336,7 @@ export default class WiredPanels {
336336
this.tickCount = 0;
337337
this.actionStack = [];
338338
this.actionIndex = 0;
339-
this.config = {
339+
this.config = Object.assign({
340340
socketRadius: 5,
341341
verticalSocketsOutside: false,
342342
horizontalSocketsOutside: false,
@@ -348,15 +348,8 @@ export default class WiredPanels {
348348
springStiffness: 0.1,
349349
panelCollision: true,
350350
borderCollision: true,
351-
undoActionLimit: 0,
352-
ondeletion: undefined,
353-
onactivation: undefined,
354-
onwiredrag: undefined,
355-
onwireconnect: undefined,
356-
oncopy: undefined,
357-
onpaste: undefined,
358-
onacceptclipboard: undefined
359-
};
351+
undoActionLimit: 0
352+
},config);
360353
}
361354

362355
setSelected(nodes, selectionMode) {

tests/index.html

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,50 @@
1111
</style>
1212
<script type="module">
1313
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 [];
5741
}
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]), []);
5958
};
6059

6160
const panel = wiredPanels.createPanel(),

0 commit comments

Comments
 (0)