UNPKG

@projectstorm/react-diagrams-core

Version:
103 lines 3.14 kB
import _forEach from 'lodash/forEach'; import _map from 'lodash/map'; import _values from 'lodash/values'; import { Point, Rectangle } from '@projectstorm/geometry'; import { BasePositionModel } from '@projectstorm/react-canvas-core'; export class NodeModel extends BasePositionModel { constructor(options) { super(options); this.ports = {}; this.width = 0; this.height = 0; } getBoundingBox() { return Rectangle.fromPointAndSize(this.getPosition(), this.width, this.height); } setPosition(x, y) { const old = this.position; if (x instanceof Point) { super.setPosition(x); } else { super.setPosition(x, y); } //also update the port co-ordinates (for make glorious speed) _forEach(this.ports, (port) => { port.setPosition(port.getX() + this.position.x - old.x, port.getY() + this.position.y - old.y); }); } deserialize(event) { super.deserialize(event); //deserialize ports _forEach(event.data.ports, (port) => { let portOb = event.engine.getFactoryForPort(port.type).generateModel({}); portOb.deserialize(Object.assign(Object.assign({}, event), { data: port })); // the links need these event.registerModel(portOb); this.addPort(portOb); }); } serialize() { return Object.assign(Object.assign({}, super.serialize()), { ports: _map(this.ports, (port) => { return port.serialize(); }) }); } doClone(lookupTable = {}, clone) { // also clone the ports clone.ports = {}; _forEach(this.ports, (port) => { clone.addPort(port.clone(lookupTable)); }); } remove() { super.remove(); _forEach(this.ports, (port) => { _forEach(port.getLinks(), (link) => { link.remove(); }); }); } getPortFromID(id) { for (var i in this.ports) { if (this.ports[i].getID() === id) { return this.ports[i]; } } return null; } getLink(id) { for (let portID in this.ports) { const links = this.ports[portID].getLinks(); if (links[id]) { return links[id]; } } } getPort(name) { return this.ports[name]; } getPorts() { return this.ports; } removePort(port) { // clear the port from the links for (let link of _values(port.getLinks())) { link.clearPort(port); } //clear the parent node reference if (this.ports[port.getName()]) { this.ports[port.getName()].setParent(null); delete this.ports[port.getName()]; } } addPort(port) { port.setParent(this); this.ports[port.getName()] = port; return port; } updateDimensions({ width, height }) { this.width = width; this.height = height; } } //# sourceMappingURL=NodeModel.js.map