Skip to content

Commit a216376

Browse files
committed
performance updates and readonly mode
1 parent b795c25 commit a216376

File tree

10 files changed

+4775
-17079
lines changed

10 files changed

+4775
-17079
lines changed

src/components/Graph/graph-view-edge.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { ContextMenu } from '../../pcui';
1+
import { ContextMenu } from '../../pcui-external';
22
import * as joint from 'jointjs';
33

44
import sourceMarkerDefaultImage from '../../assets/source-marker-default.png';
55
import sourceMarkerActiveImage from '../../assets/source-marker-active.png';
66
import sourceMarkerDeactiveImage from '../../assets/source-marker-deactive.png';
77

8-
joint.connectors.smoothInOut = function(sourcePoint, targetPoint, vertices, args) {
8+
joint.connectors.smoothInOut = function (sourcePoint, targetPoint, vertices, args) {
99
var p1 = sourcePoint.clone();
1010
p1.offset(30, 0);
1111

@@ -15,7 +15,7 @@ joint.connectors.smoothInOut = function(sourcePoint, targetPoint, vertices, args
1515
var path = new joint.g.Path(joint.g.Path.createSegment('M', sourcePoint));
1616
path.appendSegment(joint.g.Path.createSegment('C', p1, p2, targetPoint));
1717
return path;
18-
}
18+
};
1919

2020
class GraphViewEdge {
2121
constructor(graphView, paper, graph, graphSchema, edgeData, edgeSchema, onEdgeSelected) {
@@ -46,12 +46,23 @@ class GraphViewEdge {
4646
} else {
4747
link.target(targetNode.model);
4848
}
49-
this._graph.addCell(link);
50-
link.toBack();
5149

52-
this._paper.findViewByModel(link).on('cell:pointerdown', function () {
53-
onEdgeSelected(edgeData);
54-
});
50+
var onCellMountedToDom = () => {
51+
this._paper.findViewByModel(link).on('cell:pointerdown', function () {
52+
onEdgeSelected(edgeData);
53+
});
54+
if (edgeData && Number.isFinite(edgeData.inPort)) {
55+
this._graphView.updatePortStatesForEdge(link, true);
56+
}
57+
link.toBack();
58+
};
59+
if (this._graphView._batchingCells) {
60+
this._graphView._cells.push(link);
61+
this._graphView._cellMountedFunctions.push(onCellMountedToDom);
62+
} else {
63+
this._graph.addCell(link);
64+
onCellMountedToDom();
65+
}
5566

5667
this.model = link;
5768
}
@@ -95,6 +106,7 @@ class GraphViewEdge {
95106
}
96107

97108
addContextMenu(items) {
109+
if (this._graphView._config.readOnly) return;
98110
var edgeCell = this._paper.findViewByModel(this.model);
99111
if (!edgeCell) return;
100112
var contextMenu = document.createElement('div');

src/components/Graph/graph-view-node.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class GraphViewNode {
3535

3636
var labelName;
3737
if (nodeSchema.outPorts || nodeSchema.inPorts) {
38-
labelName = nodeData.attributes && nodeData.attributes.name ? `${nodeSchema.name} (${nodeData.attributes.name})` : nodeSchema.name;
38+
labelName = nodeData.attributes && nodeData.attributes.name ? `${nodeData.attributes.name} (${nodeSchema.name})` : nodeSchema.name;
3939
} else {
4040
labelName = nodeData.attributes && nodeData.attributes.name || nodeData.name;
4141
}
@@ -223,10 +223,9 @@ class GraphViewNode {
223223
}
224224
}));
225225
}
226-
this._graph.addCell(rect);
227226

227+
var containers = [];
228228
if (nodeSchema.attributes) {
229-
const nodeDiv = document.querySelector(`#nodediv_${rect.id}`);
230229
nodeSchema.attributes.forEach((attribute, i) => {
231230
const container = new Container({ class: 'graph-node-container' });
232231
const label = new Label({ text: attribute.name, class: 'graph-node-label' });
@@ -276,26 +275,42 @@ class GraphViewNode {
276275
input.inputs.forEach(i => i._sliderControl.dom.remove());
277276
break;
278277
}
278+
input.enabled = !this._graphView._config.readOnly;
279279
input.dom.setAttribute('id', `input_${attribute.name}`);
280280
container.dom.setAttribute('style', `margin-top: ${i === 0 ? 33 + portHeight : 5}px; margin-bottom: 5px;`);
281281
container.append(label);
282282
container.append(input);
283-
nodeDiv.appendChild(container.dom);
283+
containers.push(container);
284284
});
285285
}
286286

287-
this._paper.findViewByModel(rect).on('element:pointerdown', () => {
288-
if (this._hasLinked) {
289-
this._hasLinked = false;
290-
return;
291-
}
292-
onNodeSelected(this.nodeData);
293-
});
287+
var onCellMountedToDom = () => {
288+
var nodeDiv = document.querySelector(`#nodediv_${rect.id}`);
289+
containers.forEach(container => {
290+
nodeDiv.appendChild(container.dom);
291+
});
292+
this._paper.findViewByModel(rect).on('element:pointerdown', () => {
293+
if (this._hasLinked) {
294+
this._hasLinked = false;
295+
return;
296+
}
297+
onNodeSelected(this.nodeData);
298+
});
299+
};
300+
301+
if (this._graphView._batchingCells) {
302+
this._graphView._cells.push(rect);
303+
this._graphView._cellMountedFunctions.push(onCellMountedToDom);
304+
} else {
305+
this._graph.addCell(rect);
306+
onCellMountedToDom();
307+
}
294308

295309
this.model = rect;
296310
}
297311

298312
addContextMenu(items) {
313+
if (this._graphView._config.readOnly) return;
299314
var nodeView = this._paper.findViewByModel(this.model);
300315
var contextMenu = document.createElement('div');
301316
this._paper.el.appendChild(contextMenu);
@@ -307,20 +322,36 @@ class GraphViewNode {
307322
});
308323
}
309324

325+
326+
mapVectorToArray(v) {
327+
var arr = [];
328+
if (v.x) arr.push(v.x);
329+
if (v.y) arr.push(v.y);
330+
if (v.z) arr.push(v.z);
331+
if (v.w) arr.push(v.w);
332+
return arr;
333+
}
334+
310335
updateAttribute(attribute, value) {
311336
if (attribute === 'name') {
312337
var labelName;
313338
if (this.nodeSchema.outPorts || this.nodeSchema.inPorts) {
314-
labelName = `${this.nodeSchema.name} (${value})`;
339+
labelName = `${value} (${this.nodeSchema.name})`;
315340
} else {
316341
labelName = value;
317342
}
318343
this.model.attr('label/text', labelName);
319344
}
320345
const attributeElement = document.querySelector(`#nodediv_${this.model.id}`).querySelector(`#input_${attribute}`);
321346
if (attributeElement) {
322-
attributeElement.ui.value = value;
347+
attributeElement.ui.suspendEvents = true;
348+
if (Number.isFinite(value.x)) {
349+
attributeElement.ui.value = this.mapVectorToArray(value);
350+
} else {
351+
attributeElement.ui.value = value;
352+
}
323353
attributeElement.ui.error = false;
354+
attributeElement.ui.suspendEvents = false;
324355
}
325356
}
326357

src/components/Graph/graph-view.js

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ class GraphView extends JointGraph {
2929
this._graphSchema = graphSchema;
3030
this._graphData = graphData;
3131

32+
this._config = config;
33+
3234
this._nodes = {};
3335
this._edges = {};
3436

37+
this._cells = [];
38+
this._cellMountedFunctions = [];
39+
3540
joint.shapes.html = {};
3641
joint.shapes.html.Element = jointShapeElement();
3742
joint.shapes.html.ElementView = jointShapeElementView(this._paper);
3843

39-
this._graph.on('add', (cell) => this.updateNodePort(cell, true));
40-
this._graph.on('remove', (cell) => this.updateNodePort(cell, false));
41-
this._graph.on('change:target', (cell) => this.updateNodePort(cell, true));
44+
// this._graph.on('add', (cell) => this.updatePortStatesForEdge(cell, true));
45+
this._graph.on('remove', (cell) => this.updatePortStatesForEdge(cell, false));
46+
this._graph.on('change:target', (cell) => this.updatePortStatesForEdge(cell, true));
4247

4348
this._paper.on('cell:mousewheel', () => {
4449
parent.dom.dispatchEvent(new CustomEvent(GRAPH_ACTIONS.UPDATE_SCALE, { detail: { scale: this._paper.scale().sx } } ));
@@ -130,7 +135,27 @@ class GraphView extends JointGraph {
130135
});
131136
}
132137

133-
updateNodePort(cell, connected) {
138+
batchCells() {
139+
this._batchingCells = true;
140+
}
141+
142+
isBatchingCells() {
143+
return this._batchingCells;
144+
}
145+
146+
addCellMountedFunction(f) {
147+
this._cellMountedFunctions.push(f);
148+
}
149+
150+
applyBatchedCells() {
151+
this._batchingCells = false;
152+
this._graph.addCells(this._cells);
153+
this._cellMountedFunctions.forEach(f => f());
154+
this._cells = [];
155+
this._cellMountedFunctions = [];
156+
}
157+
158+
updatePortStatesForEdge(cell, connected) {
134159
var source = cell.get('source');
135160
var target = cell.get('target');
136161
if (source && source.port && target && target.port) {
@@ -160,15 +185,24 @@ class GraphView extends JointGraph {
160185
addCanvasContextMenu(items) {
161186
this._viewContextMenu = document.createElement('div');
162187
this._paper.el.appendChild(this._viewContextMenu);
163-
new ContextMenu({
188+
var contextMenu = new ContextMenu({
164189
dom: this._viewContextMenu,
165190
items: items
166191
});
192+
window.contextMenu = contextMenu;
193+
return contextMenu._contextMenuEvent;
167194
}
168195

169196
addNodeContextMenu(id, items) {
170-
var node = this.getNode(id);
171-
node.addContextMenu(items);
197+
var addNodeContextMenuFunction = () => {
198+
var node = this.getNode(id);
199+
node.addContextMenu(items);
200+
};
201+
if (this._batchingCells) {
202+
this._cellMountedFunctions.push(addNodeContextMenuFunction);
203+
} else {
204+
addNodeContextMenuFunction();
205+
}
172206
}
173207

174208
addEdgeContextMenu(id, items) {
@@ -219,8 +253,15 @@ class GraphView extends JointGraph {
219253
}
220254

221255
addNodeEvent(id, event, callback, attribute) {
222-
var node = this.getNode(id);
223-
node.addEvent(event, callback, attribute);
256+
var addNodeEventFunction = () => {
257+
var node = this.getNode(id);
258+
node.addEvent(event, callback, attribute);
259+
};
260+
if (this._batchingCells) {
261+
this._cellMountedFunctions.push(addNodeEventFunction);
262+
} else {
263+
addNodeEventFunction();
264+
}
224265
}
225266

226267
getEdge(id) {
@@ -275,7 +316,20 @@ class GraphView extends JointGraph {
275316
delete this._edges[id];
276317
}
277318

319+
disableInputEvents() {
320+
document.querySelectorAll('.graph-node-input').forEach(input => {
321+
input.classList.add('graph-node-input-no-pointer-events');
322+
});
323+
}
324+
325+
enableInputEvents() {
326+
document.querySelectorAll('.graph-node-input').forEach(input => {
327+
input.classList.remove('graph-node-input-no-pointer-events');
328+
});
329+
}
330+
278331
addUnconnectedEdge(nodeId, edgeType, edgeSchema, validateEdge, onEdgeConnected) {
332+
this.disableInputEvents();
279333
const link = GraphViewEdge.createLink(edgeSchema);
280334
link.source(this.getNode(nodeId).model);
281335
link.target(this.getNode(nodeId).model);
@@ -303,11 +357,13 @@ class GraphView extends JointGraph {
303357
this._graph.removeCells(link);
304358
document.removeEventListener('mousemove', mouseMoveEvent);
305359
this._paper.off('cell:pointerdown', cellPointerDownEvent);
360+
this.enableInputEvents();
306361
};
307362
var mouseDownEvent = () => {
308363
this._paper.off('cell:pointerdown', cellPointerDownEvent);
309364
document.removeEventListener('mousemove', mouseMoveEvent);
310365
this._graph.removeCells(link);
366+
this.enableInputEvents();
311367
};
312368

313369
document.addEventListener('mousemove', mouseMoveEvent);

0 commit comments

Comments
 (0)