mirror of
https://github.com/fluxscape/fluxscape.git
synced 2026-01-12 23:32:55 +01:00
Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com> Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com> Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com> Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com> Co-Authored-By: Johan <4934465+joolsus@users.noreply.github.com> Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com> Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
This commit is contained in:
389
packages/noodl-runtime/src/models/componentmodel.js
Normal file
389
packages/noodl-runtime/src/models/componentmodel.js
Normal file
@@ -0,0 +1,389 @@
|
||||
"use strict";
|
||||
|
||||
var NodeModel = require('./nodemodel');
|
||||
var EventSender = require('../eventsender');
|
||||
|
||||
function ComponentModel(name) {
|
||||
EventSender.call(this);
|
||||
|
||||
this.name = name;
|
||||
this.nodes = [];
|
||||
this.connections = [];
|
||||
this.roots = [];
|
||||
this.inputPorts = {};
|
||||
this.outputPorts = {};
|
||||
this.metadata = {};
|
||||
}
|
||||
|
||||
ComponentModel.prototype = Object.create(EventSender.prototype);
|
||||
|
||||
ComponentModel.prototype.addNode = async function(node) {
|
||||
node.component = this;
|
||||
this.nodes[node.id] = node;
|
||||
await this.emit("nodeAdded", node);
|
||||
};
|
||||
|
||||
ComponentModel.prototype.hasNodeWithId = function(id) {
|
||||
return this.getNodeWithId(id) !== undefined;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getNodeWithId = function(id) {
|
||||
return this.nodes[id];
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getAllNodes = function() {
|
||||
return Object.values(this.nodes);
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getNodesWithType = function(type) {
|
||||
var nodes = [];
|
||||
var self = this;
|
||||
Object.keys(this.nodes).forEach(function(id) {
|
||||
var node = self.nodes[id];
|
||||
if(node.type === type) {
|
||||
nodes.push(node);
|
||||
}
|
||||
});
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.addConnection = function(connection) {
|
||||
this.connections.push(connection);
|
||||
this.emit("connectionAdded", connection);
|
||||
|
||||
//emit an event on the target node model
|
||||
//used by numbered inputs
|
||||
if(connection.targetId) {
|
||||
const node = this.getNodeWithId(connection.targetId);
|
||||
if(node) {
|
||||
node.emit("inputConnectionAdded", connection);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.removeConnection = function(connection) {
|
||||
const index = this.connections.findIndex(con => {
|
||||
return con.sourceId === connection.sourceId &&
|
||||
con.sourcePort === connection.sourcePort &&
|
||||
con.targetId === connection.targetId &&
|
||||
con.targetPort === connection.targetPort;
|
||||
});
|
||||
|
||||
if(index === -1) {
|
||||
console.log("Connection doesn't exist", connection);
|
||||
return;
|
||||
}
|
||||
|
||||
this.connections.splice(index, 1);
|
||||
this.emit("connectionRemoved", connection);
|
||||
|
||||
//emit an event on the target node model
|
||||
//used by numbered inputs
|
||||
if(connection.targetId) {
|
||||
const node = this.getNodeWithId(connection.targetId);
|
||||
if(node) {
|
||||
node.emit("inputConnectionRemoved", connection);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getConnectionsFromPort = function(nodeId, sourcePortName) {
|
||||
return this.connections.filter(function(connection) {
|
||||
return connection.sourceId === nodeId && connection.sourcePort === sourcePortName;
|
||||
})
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getConnectionsToPort = function(nodeId, targetPortName) {
|
||||
return this.connections.filter(function(connection) {
|
||||
return connection.targetId === nodeId && connection.targetPort === targetPortName;
|
||||
})
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getConnectionsFrom = function(nodeId) {
|
||||
return this.connections.filter(function(connection) {
|
||||
return connection.sourceId === nodeId;
|
||||
})
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getConnectionsTo = function(nodeId) {
|
||||
return this.connections.filter(function(connection) {
|
||||
return connection.targetId === nodeId;
|
||||
})
|
||||
};
|
||||
|
||||
ComponentModel.prototype.addRootId = function(rootId) {
|
||||
if(this.roots.indexOf(rootId) !== -1) {
|
||||
return;
|
||||
}
|
||||
this.roots.push(rootId);
|
||||
this.emit("rootAdded", rootId);
|
||||
};
|
||||
|
||||
ComponentModel.prototype.removeRootId = function(rootId) {
|
||||
const index = this.roots.indexOf(rootId);
|
||||
if(index !== -1) {
|
||||
this.roots.splice(index, 1);
|
||||
this.emit("rootRemoved", rootId);
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getRoots = function() {
|
||||
return this.roots;
|
||||
}
|
||||
|
||||
ComponentModel.prototype.removeNodeWithId = async function(id) {
|
||||
const node = this.getNodeWithId(id);
|
||||
|
||||
if (!node) {
|
||||
console.warn("ERROR: Attempted to remove non-existing node with ID:", id);
|
||||
return false;
|
||||
}
|
||||
|
||||
//remove children first
|
||||
while(node.children.length > 0) {
|
||||
const child = node.children[0];
|
||||
const childRemoved = await this.removeNodeWithId(child.id);
|
||||
if (!childRemoved) {
|
||||
// Workaround for corrupt node trees, should never happen, a remove should always be successful
|
||||
node.children.shift();
|
||||
}
|
||||
}
|
||||
|
||||
const connections = this.getConnectionsTo(id).concat(this.getConnectionsFrom(id));
|
||||
|
||||
for(let i=0; i<connections.length; i++) {
|
||||
this.removeConnection(connections[i]);
|
||||
}
|
||||
|
||||
this.setNodeParent(node, null);
|
||||
|
||||
if(this.roots.indexOf(node.id) !== -1) {
|
||||
this.removeRootId(node.id);
|
||||
}
|
||||
|
||||
await this.emit("nodeRemoved", node);
|
||||
|
||||
node.removeAllListeners();
|
||||
delete this.nodes[id];
|
||||
|
||||
await this.emit("nodeWasRemoved", node);
|
||||
return true;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getAllConnections = function() {
|
||||
return this.connections;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getInputPorts = function() {
|
||||
return this.inputPorts;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getOutputPorts = function() {
|
||||
return this.outputPorts;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.addInputPort = function(port) {
|
||||
this.inputPorts[port.name] = port;
|
||||
this.emit("inputPortAdded", port);
|
||||
};
|
||||
|
||||
ComponentModel.prototype.addOutputPort = function(port) {
|
||||
this.outputPorts[port.name] = port;
|
||||
this.emit("outputPortAdded", port);
|
||||
};
|
||||
|
||||
ComponentModel.prototype.removeOutputPortWithName = function(portName) {
|
||||
if(this.outputPorts.hasOwnProperty(portName)) {
|
||||
var port = this.outputPorts[portName];
|
||||
delete this.outputPorts[portName];
|
||||
this.emit("outputPortRemoved", port);
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.removeInputPortWithName = function(portName) {
|
||||
if(this.inputPorts.hasOwnProperty(portName)) {
|
||||
var port = this.inputPorts[portName];
|
||||
delete this.inputPorts[portName];
|
||||
this.emit("inputPortRemoved", port);
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.updateInputPortTypes = function(ports) {
|
||||
var changed = false;
|
||||
for(var key in ports) {
|
||||
if(this.inputPorts[key] !== undefined) {
|
||||
this.inputPorts[key].type = ports[key].type;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
changed && this.emit("inputPortTypesUpdated");
|
||||
}
|
||||
|
||||
ComponentModel.prototype.updateOutputPortTypes = function(ports) {
|
||||
var changed = false;
|
||||
for(var key in ports) {
|
||||
if(this.outputPorts[key] !== undefined) {
|
||||
this.outputPorts[key].type = ports[key].type;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
changed && this.emit("outputPortTypesUpdated");
|
||||
}
|
||||
|
||||
ComponentModel.prototype.renameInputPortOnNodeWithId = function(id, oldName, newName) {
|
||||
|
||||
//remove connections
|
||||
var connections = this.getConnectionsToPort(id, oldName);
|
||||
connections.forEach(this.removeConnection.bind(this));
|
||||
|
||||
//get port before deleting it
|
||||
var nodeModel = this.getNodeWithId(id);
|
||||
var port = {...nodeModel.getInputPort(oldName)};
|
||||
|
||||
//remove old port
|
||||
if(port) {
|
||||
nodeModel.removeInputPortWithName(oldName);
|
||||
|
||||
//rename and add new port
|
||||
port.name = newName;
|
||||
nodeModel.addInputPort(port);
|
||||
}
|
||||
|
||||
//add new connection
|
||||
connections.forEach(function(connection) {
|
||||
connection.targetPort = newName;
|
||||
});
|
||||
|
||||
connections.forEach(this.addConnection.bind(this));
|
||||
};
|
||||
|
||||
ComponentModel.prototype.renameOutputPortOnNodeWithId = function(id, oldName, newName) {
|
||||
|
||||
//remove connections
|
||||
var connections = this.getConnectionsFromPort(id, oldName);
|
||||
connections.forEach(this.removeConnection.bind(this));
|
||||
|
||||
//get port before deleting it
|
||||
var nodeModel = this.getNodeWithId(id);
|
||||
var port = {...nodeModel.getOutputPort(oldName)};
|
||||
|
||||
//remove old port
|
||||
nodeModel.removeOutputPortWithName(oldName);
|
||||
|
||||
//rename and add new port
|
||||
port.name = newName;
|
||||
nodeModel.addOutputPort(port);
|
||||
|
||||
//add new connection
|
||||
connections.forEach(function(connection) {
|
||||
connection.sourcePort = newName;
|
||||
});
|
||||
|
||||
connections.forEach(this.addConnection.bind(this));
|
||||
};
|
||||
|
||||
ComponentModel.prototype.setNodeParent = function(childModel, newParentModel, index) {
|
||||
|
||||
if(this.roots.indexOf(childModel.id) !== -1) {
|
||||
this.removeRootId(childModel.id)
|
||||
}
|
||||
|
||||
if(childModel.parent) {
|
||||
this.emit("nodeParentWillBeRemoved", childModel);
|
||||
childModel.parent.removeChild(childModel);
|
||||
}
|
||||
childModel.emit("parentUpdated", newParentModel);
|
||||
if(newParentModel) {
|
||||
newParentModel.addChild(childModel, index);
|
||||
this.emit("nodeParentUpdated", childModel);
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.importEditorNodeData = async function(nodeData, parentId, childIndex) {
|
||||
var nodeModel = NodeModel.createFromExportData(nodeData);
|
||||
await this.addNode(nodeModel);
|
||||
|
||||
if(parentId) {
|
||||
this.setNodeParent(nodeModel, this.getNodeWithId(parentId), childIndex);
|
||||
}
|
||||
|
||||
if(nodeData.children) {
|
||||
for(let i=0; i<nodeData.children.length; i++) {
|
||||
const child = nodeData.children[i];
|
||||
await this.importEditorNodeData(child, nodeModel.id, i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.reset = async function() {
|
||||
while(this.roots.length) {
|
||||
await this.removeNodeWithId(this.roots[0]);
|
||||
}
|
||||
|
||||
for(const id of this.nodes) {
|
||||
//note: with an incomplete library there will be no roots
|
||||
//so some of the nodes will have children, which will be recursively removed by
|
||||
//removeNodeWithId(), so some IDs from the Object.keys(this.nodes) that runs this loop
|
||||
//will already have been removed, so check if they exist before removing
|
||||
if(this.hasNodeWithId(id)) {
|
||||
await this.removeNodeWithId(id);
|
||||
}
|
||||
}
|
||||
|
||||
if(this.nodes.length > 0) {
|
||||
throw new Error("Not all nodes were removed during a reset");
|
||||
}
|
||||
|
||||
if(this.connections.length > 0) {
|
||||
throw new Error("Not all connections were removed during a reset");
|
||||
}
|
||||
};
|
||||
|
||||
ComponentModel.prototype.rename = function(newName) {
|
||||
var oldName = this.name;
|
||||
this.name = newName;
|
||||
this.emit("renamed", {oldName: oldName, newName: newName});
|
||||
};
|
||||
|
||||
ComponentModel.prototype.setMetadata = function(key, data) {
|
||||
this.metadata[key] = data;
|
||||
};
|
||||
|
||||
ComponentModel.prototype.getMetadata = function(key) {
|
||||
if(!key) return this.metadata;
|
||||
return this.metadata[key];
|
||||
};
|
||||
|
||||
ComponentModel.createFromExportData = async function(componentData) {
|
||||
|
||||
var componentModel = new ComponentModel(componentData.name);
|
||||
|
||||
if(componentData.metadata) {
|
||||
for(const key in componentData.metadata) {
|
||||
componentModel.setMetadata(key, componentData.metadata[key]);
|
||||
}
|
||||
}
|
||||
|
||||
componentData.ports && componentData.ports.forEach(function(port) {
|
||||
if(port.plug === "input" || port.plug === "input/output") {
|
||||
componentModel.addInputPort(port);
|
||||
}
|
||||
if(port.plug === "output" || port.plug === "input/output") {
|
||||
componentModel.addOutputPort(port);
|
||||
}
|
||||
});
|
||||
|
||||
if(componentData.nodes) {
|
||||
for(const node of componentData.nodes) {
|
||||
await componentModel.importEditorNodeData(node);
|
||||
}
|
||||
}
|
||||
|
||||
componentData.connections && componentData.connections.forEach(connection => componentModel.addConnection(connection));
|
||||
componentData.roots && componentData.roots.forEach(root => componentModel.addRootId(root));
|
||||
|
||||
return componentModel;
|
||||
};
|
||||
|
||||
module.exports = ComponentModel;
|
||||
337
packages/noodl-runtime/src/models/graphmodel.js
Normal file
337
packages/noodl-runtime/src/models/graphmodel.js
Normal file
@@ -0,0 +1,337 @@
|
||||
'use strict';
|
||||
|
||||
var ComponentModel = require('./componentmodel');
|
||||
var EventSender = require('../eventsender');
|
||||
|
||||
function GraphModel() {
|
||||
EventSender.call(this);
|
||||
this.components = {};
|
||||
|
||||
this.settings = {};
|
||||
|
||||
this.metadata = {};
|
||||
}
|
||||
|
||||
GraphModel.prototype = Object.create(EventSender.prototype);
|
||||
|
||||
GraphModel.prototype.importComponentFromEditorData = async function (componentData) {
|
||||
var componentModel = await ComponentModel.createFromExportData(componentData);
|
||||
this.addComponent(componentModel);
|
||||
};
|
||||
|
||||
GraphModel.prototype.getBundleContainingComponent = function (name) {
|
||||
return this.componentToBundleMap.get(name);
|
||||
};
|
||||
|
||||
GraphModel.prototype.getBundlesContainingSheet = function (sheetName) {
|
||||
const bundles = new Set();
|
||||
for (const name of this.componentToBundleMap.keys()) {
|
||||
const isOnDefaultSheet = name.indexOf('/#') !== 0;
|
||||
|
||||
const isMatch =
|
||||
(isOnDefaultSheet && sheetName === 'Default') || (!isOnDefaultSheet && name.indexOf('/#' + sheetName) === 0);
|
||||
|
||||
if (isMatch) {
|
||||
bundles.add(this.componentToBundleMap.get(name));
|
||||
}
|
||||
}
|
||||
return Array.from(bundles);
|
||||
};
|
||||
|
||||
GraphModel.prototype.getBundleDependencies = function (bundleName) {
|
||||
const result = new Set();
|
||||
|
||||
const recurse = (name) => {
|
||||
const bundle = this.componentIndex[name];
|
||||
for (const dep of bundle.dependencies) {
|
||||
if (!result.has(dep)) {
|
||||
result.add(dep);
|
||||
recurse(dep);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
recurse(bundleName);
|
||||
|
||||
return Array.from(result);
|
||||
};
|
||||
|
||||
GraphModel.prototype.importEditorData = async function (exportData) {
|
||||
this.componentIndex = exportData.componentIndex;
|
||||
this.routerIndex = exportData.routerIndex;
|
||||
|
||||
this.componentToBundleMap = new Map();
|
||||
|
||||
for (const bundleName in exportData.componentIndex) {
|
||||
const bundle = exportData.componentIndex[bundleName];
|
||||
|
||||
for (const componentName of bundle.components) {
|
||||
this.componentToBundleMap.set(componentName, bundleName);
|
||||
}
|
||||
}
|
||||
|
||||
this.variants = exportData.variants || [];
|
||||
|
||||
exportData.settings && this.setSettings(exportData.settings);
|
||||
|
||||
exportData.metadata && this.setAllMetaData(exportData.metadata);
|
||||
|
||||
for (const component of exportData.components) {
|
||||
await this.importComponentFromEditorData(component);
|
||||
}
|
||||
|
||||
this.setRootComponentName(exportData.rootComponent);
|
||||
};
|
||||
|
||||
GraphModel.prototype.setRootComponentName = function (componentName) {
|
||||
this.rootComponent = componentName;
|
||||
this.emit('rootComponentNameUpdated', componentName);
|
||||
};
|
||||
|
||||
GraphModel.prototype.getNodesWithType = function (type) {
|
||||
var nodes = [];
|
||||
|
||||
var componentNames = Object.keys(this.components);
|
||||
for (var i = 0; i < componentNames.length; i++) {
|
||||
var component = this.components[componentNames[i]];
|
||||
nodes = nodes.concat(component.getNodesWithType(type));
|
||||
}
|
||||
return nodes;
|
||||
};
|
||||
|
||||
GraphModel.prototype.getComponentWithName = function (type) {
|
||||
return this.components[type];
|
||||
};
|
||||
|
||||
GraphModel.prototype.hasComponentWithName = function (type) {
|
||||
return this.components[type] ? true : false;
|
||||
};
|
||||
|
||||
GraphModel.prototype.getAllComponents = function () {
|
||||
return Object.keys(this.components).map((name) => {
|
||||
return this.components[name];
|
||||
});
|
||||
};
|
||||
|
||||
GraphModel.prototype.getAllNodes = function () {
|
||||
var nodes = [];
|
||||
|
||||
var componentNames = Object.keys(this.components);
|
||||
for (var i = 0; i < componentNames.length; i++) {
|
||||
var component = this.components[componentNames[i]];
|
||||
nodes = nodes.concat(component.getAllNodes());
|
||||
}
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
GraphModel.prototype.addComponent = function (component) {
|
||||
this.components[component.name] = component;
|
||||
|
||||
//nodes that are already added are missing component input/output ports if the component is registered after the nodes
|
||||
//now when we have the component info, add them to the node instance models
|
||||
this.getNodesWithType(component.name).forEach(this._addComponentPorts.bind(this));
|
||||
|
||||
//emit the "nodeAdded" event for every node already in the component
|
||||
component.getAllNodes().forEach(this._onNodeAdded.bind(this));
|
||||
|
||||
//emit the same event for future nodes that will be added
|
||||
component.on('nodeAdded', this._onNodeAdded.bind(this), this);
|
||||
|
||||
//and for nodes that are removed
|
||||
component.on('nodeRemoved', this._onNodeRemoved.bind(this), this);
|
||||
component.on('nodeWasRemoved', this._onNodeWasRemoved.bind(this), this);
|
||||
|
||||
this.emit('componentAdded', component);
|
||||
};
|
||||
|
||||
GraphModel.prototype.removeComponentWithName = async function (componentName) {
|
||||
if (this.components.hasOwnProperty(componentName) === false) {
|
||||
console.error('GraphModel: Component with name ' + componentName + ' not in graph');
|
||||
return;
|
||||
}
|
||||
|
||||
var component = this.components[componentName];
|
||||
await component.reset();
|
||||
|
||||
component.removeAllListeners();
|
||||
delete this.components[component.name];
|
||||
|
||||
this.emit('componentRemoved', component);
|
||||
};
|
||||
|
||||
GraphModel.prototype.renameComponent = function (componentName, newName) {
|
||||
if (this.components.hasOwnProperty(componentName) === false) {
|
||||
console.error('GraphModel: Component with name ' + componentName + ' not in graph');
|
||||
return;
|
||||
}
|
||||
|
||||
this.getNodesWithType(componentName).forEach(function (nodeModel) {
|
||||
nodeModel.type = newName;
|
||||
});
|
||||
|
||||
var component = this.components[componentName];
|
||||
component.rename(newName);
|
||||
|
||||
delete this.components[componentName];
|
||||
this.components[newName] = component;
|
||||
|
||||
this.emit('componentRenamed', component);
|
||||
};
|
||||
|
||||
GraphModel.prototype._addComponentPorts = function (node) {
|
||||
//check if this node is a known component and add port to the model
|
||||
if (this.components.hasOwnProperty(node.type)) {
|
||||
//a component was created, add component ports to model
|
||||
var component = this.components[node.type];
|
||||
|
||||
const inputPorts = component.getInputPorts();
|
||||
const outputPorts = component.getOutputPorts();
|
||||
|
||||
Object.keys(inputPorts).forEach((portName) => {
|
||||
node.addInputPort(inputPorts[portName]);
|
||||
});
|
||||
|
||||
Object.keys(outputPorts).forEach((portName) => {
|
||||
node.addOutputPort(outputPorts[portName]);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
GraphModel.prototype._onNodeAdded = function (node) {
|
||||
this._addComponentPorts(node);
|
||||
|
||||
this.emit('nodeAdded', node);
|
||||
this.emit('nodeAdded.' + node.type, node);
|
||||
};
|
||||
|
||||
GraphModel.prototype._onNodeRemoved = function (node) {
|
||||
this.emit('nodeRemoved', node);
|
||||
this.emit('nodeRemoved.' + node.type, node);
|
||||
};
|
||||
|
||||
GraphModel.prototype._onNodeWasRemoved = function (node) {
|
||||
this.emit('nodeWasRemoved', node);
|
||||
this.emit('nodeWasRemoved.' + node.type, node);
|
||||
};
|
||||
|
||||
GraphModel.prototype.reset = async function () {
|
||||
for (const componentName of Object.keys(this.components)) {
|
||||
await this.removeComponentWithName(componentName);
|
||||
}
|
||||
this.setSettings({});
|
||||
};
|
||||
|
||||
GraphModel.prototype.isEmpty = function () {
|
||||
return Object.keys(this.components).length === 0;
|
||||
};
|
||||
|
||||
GraphModel.prototype.setSettings = function (settings) {
|
||||
this.settings = settings;
|
||||
this.emit('projectSettingsChanged', settings);
|
||||
};
|
||||
|
||||
GraphModel.prototype.getSettings = function () {
|
||||
return this.settings;
|
||||
};
|
||||
|
||||
GraphModel.prototype.setAllMetaData = function (metadata) {
|
||||
for (const p in metadata) {
|
||||
this.setMetaData(p, metadata[p]);
|
||||
}
|
||||
};
|
||||
|
||||
GraphModel.prototype.setMetaData = function (key, data) {
|
||||
//metadata changes can trigger lots of ports to evaluate (e.g. when a database model has been changed)
|
||||
//check if the data actually has been updated before since the editor can send the same data multiple times
|
||||
if (this.metadata[key] && JSON.stringify(this.metadata[key]) === JSON.stringify(data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.metadata[key] = data;
|
||||
this.emit('metadataChanged', { key, data });
|
||||
this.emit('metadataChanged.' + key, data);
|
||||
};
|
||||
|
||||
GraphModel.prototype.getMetaData = function (key) {
|
||||
if (key) return this.metadata[key];
|
||||
return this.metadata;
|
||||
};
|
||||
|
||||
GraphModel.prototype.getVariants = function () {
|
||||
return this.variants || [];
|
||||
};
|
||||
|
||||
GraphModel.prototype.getVariant = function (typename, name) {
|
||||
return this.variants.find((v) => v.name === name && v.typename === typename);
|
||||
};
|
||||
|
||||
GraphModel.prototype.updateVariant = function (variant) {
|
||||
const i = this.variants.findIndex((v) => v.name === variant.name && v.typename === variant.typename);
|
||||
if (i !== -1) this.variants.splice(i, 1);
|
||||
this.variants.push(variant);
|
||||
|
||||
this.emit('variantUpdated', variant);
|
||||
};
|
||||
|
||||
GraphModel.prototype.updateVariantParameter = function (
|
||||
variantName,
|
||||
variantTypeName,
|
||||
parameterName,
|
||||
parameterValue,
|
||||
state
|
||||
) {
|
||||
const variant = this.getVariant(variantTypeName, variantName);
|
||||
if (!variant) {
|
||||
console.log("updateVariantParameter: can't find variant", variantName, variantTypeName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!state) {
|
||||
if (parameterValue === undefined) {
|
||||
delete variant.parameters[parameterName];
|
||||
} else {
|
||||
variant.parameters[parameterName] = parameterValue;
|
||||
}
|
||||
} else {
|
||||
if (!variant.stateParameters.hasOwnProperty(state)) {
|
||||
variant.stateParameters[state] = {};
|
||||
}
|
||||
|
||||
if (parameterValue === undefined) {
|
||||
delete variant.stateParameters[state][parameterName];
|
||||
} else {
|
||||
variant.stateParameters[state][parameterName] = parameterValue;
|
||||
}
|
||||
}
|
||||
|
||||
this.emit('variantUpdated', variant);
|
||||
};
|
||||
|
||||
GraphModel.prototype.updateVariantDefaultStateTransition = function (variantName, variantTypeName, transition, state) {
|
||||
const variant = this.getVariant(variantTypeName, variantName);
|
||||
if (!variant) return;
|
||||
|
||||
variant.defaultStateTransitions[state] = transition;
|
||||
this.emit('variantUpdated', variant);
|
||||
};
|
||||
|
||||
GraphModel.prototype.updateVariantStateTransition = function (args) {
|
||||
const { variantTypeName, variantName, state, parameterName, curve } = args;
|
||||
|
||||
const variant = this.getVariant(variantTypeName, variantName);
|
||||
if (!variant) return;
|
||||
|
||||
if (!variant.stateTransitions[state]) {
|
||||
variant.stateTransitions[state] = {};
|
||||
}
|
||||
|
||||
variant.stateTransitions[state][parameterName] = curve;
|
||||
};
|
||||
|
||||
GraphModel.prototype.deleteVariant = function (typename, name) {
|
||||
const i = this.variants.findIndex((v) => v.name === name && v.typename === typename);
|
||||
if (i !== -1) this.variants.splice(i, 1);
|
||||
};
|
||||
|
||||
module.exports = GraphModel;
|
||||
203
packages/noodl-runtime/src/models/nodemodel.js
Normal file
203
packages/noodl-runtime/src/models/nodemodel.js
Normal file
@@ -0,0 +1,203 @@
|
||||
"use strict";
|
||||
|
||||
var EventSender = require('../eventsender');
|
||||
|
||||
function NodeModel(id, type) {
|
||||
EventSender.call(this);
|
||||
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
|
||||
this.inputs = [];
|
||||
this.outputs = [];
|
||||
this.children = [];
|
||||
this.parameters = {};
|
||||
this.inputPorts = {};
|
||||
this.outputPorts = {};
|
||||
}
|
||||
|
||||
NodeModel.prototype = Object.create(EventSender.prototype);
|
||||
|
||||
NodeModel.prototype.setParameter = function(name, value, state) {
|
||||
if(state) {
|
||||
if(!this.stateParameters) this.stateParameters = {};
|
||||
if(!this.stateParameters[state]) this.stateParameters[state] = {};
|
||||
|
||||
if(value === undefined) {
|
||||
delete this.stateParameters[state][name];
|
||||
}
|
||||
else {
|
||||
this.stateParameters[state][name] = value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(value === undefined) {
|
||||
delete this.parameters[name];
|
||||
}
|
||||
else {
|
||||
this.parameters[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
this.emit("parameterUpdated", {name, value, state});
|
||||
};
|
||||
|
||||
NodeModel.prototype.setParameters = function(parameters) {
|
||||
Object.keys(parameters).forEach(name => {
|
||||
this.setParameter(name, parameters[name]);
|
||||
});
|
||||
};
|
||||
|
||||
NodeModel.prototype.setStateParameters = function(parameters) {
|
||||
this.stateParameters = parameters;
|
||||
};
|
||||
|
||||
NodeModel.prototype.setStateTransitions = function(stateTransitions) {
|
||||
this.stateTransitions = stateTransitions;
|
||||
};
|
||||
|
||||
NodeModel.prototype.setStateTransitionParamter = function(parameter, curve, state) {
|
||||
if(!this.stateTransitions) {
|
||||
this.stateTransitions = {};
|
||||
}
|
||||
|
||||
if(curve) {
|
||||
this.stateTransitions[state][parameter] = curve;
|
||||
}
|
||||
else {
|
||||
delete this.stateTransitions[state][parameter];
|
||||
}
|
||||
};
|
||||
|
||||
NodeModel.prototype.setDefaultStateTransition = function(stateTransition, state) {
|
||||
if(!this.defaultStateTransitions) {
|
||||
this.defaultStateTransitions = {};
|
||||
}
|
||||
this.defaultStateTransitions[state] = stateTransition;
|
||||
};
|
||||
|
||||
NodeModel.prototype.addInputPort = function(port) {
|
||||
this.inputPorts[port.name] = port;
|
||||
this.emit("inputPortAdded", port);
|
||||
};
|
||||
|
||||
NodeModel.prototype.getInputPort = function(portName) {
|
||||
return this.inputPorts[portName];
|
||||
};
|
||||
|
||||
NodeModel.prototype.getInputPorts = function() {
|
||||
return this.inputPorts;
|
||||
};
|
||||
|
||||
NodeModel.prototype.removeInputPortWithName = function(portName) {
|
||||
if(this.inputPorts.hasOwnProperty(portName)) {
|
||||
var port = this.inputPorts[portName];
|
||||
delete this.inputPorts[portName];
|
||||
this.emit("inputPortRemoved", port);
|
||||
}
|
||||
};
|
||||
|
||||
NodeModel.prototype.updateInputPortTypes = function(ports) {
|
||||
var changed = false;
|
||||
for(var key in ports) {
|
||||
if(this.inputPorts[key] !== undefined) {
|
||||
this.inputPorts[key].type = ports[key].type;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
changed && this.emit("inputPortTypesUpdated");
|
||||
}
|
||||
|
||||
NodeModel.prototype.addOutputPort = function(port) {
|
||||
this.outputPorts[port.name] = port;
|
||||
this.emit("outputPortAdded", port);
|
||||
};
|
||||
|
||||
NodeModel.prototype.getOutputPort = function(portName) {
|
||||
return this.outputPorts[portName];
|
||||
};
|
||||
|
||||
NodeModel.prototype.getOutputPorts = function() {
|
||||
return this.outputPorts;
|
||||
};
|
||||
|
||||
NodeModel.prototype.removeOutputPortWithName = function(portName) {
|
||||
if(this.outputPorts.hasOwnProperty(portName)) {
|
||||
var port = this.outputPorts[portName];
|
||||
delete this.outputPorts[portName];
|
||||
this.emit("outputPortRemoved", port);
|
||||
}
|
||||
};
|
||||
|
||||
NodeModel.prototype.updateOutputPortTypes = function(ports) {
|
||||
var changed = false;
|
||||
for(var key in ports) {
|
||||
if(this.outputPorts[key] !== undefined) {
|
||||
this.outputPorts[key].type = ports[key].type;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
changed && this.emit("outputPortTypesUpdated");
|
||||
}
|
||||
|
||||
NodeModel.prototype.addChild = function(child, index) {
|
||||
child.parent = this;
|
||||
if(index === undefined) {
|
||||
this.children.push(child);
|
||||
}
|
||||
else {
|
||||
this.children.splice(index, 0, child);
|
||||
}
|
||||
this.emit("childAdded", child);
|
||||
};
|
||||
|
||||
NodeModel.prototype.removeChild = function(child) {
|
||||
child.parent = undefined;
|
||||
var index = this.children.indexOf(child);
|
||||
this.children.splice(index, 1);
|
||||
this.emit("childRemoved", child);
|
||||
};
|
||||
|
||||
NodeModel.prototype.reset = function() {
|
||||
this.removeAllListeners();
|
||||
};
|
||||
|
||||
NodeModel.prototype.setVariant = function(variant) {
|
||||
this.variant = variant;
|
||||
this.emit("variantUpdated", variant);
|
||||
};
|
||||
|
||||
NodeModel.createFromExportData = function(nodeData) {
|
||||
var node = new NodeModel(nodeData.id, nodeData.type);
|
||||
nodeData.parameters && node.setParameters(nodeData.parameters);
|
||||
nodeData.stateParameters && node.setStateParameters(nodeData.stateParameters);
|
||||
nodeData.stateTransitions && node.setStateTransitions(nodeData.stateTransitions);
|
||||
|
||||
if(nodeData.defaultStateTransitions) {
|
||||
for(const state in nodeData.defaultStateTransitions) {
|
||||
node.setDefaultStateTransition(nodeData.defaultStateTransitions[state], state);
|
||||
}
|
||||
}
|
||||
|
||||
nodeData.ports && nodeData.ports.forEach(function(port) {
|
||||
|
||||
//some ports are incorrectly named outputs instead of output, patch it here so
|
||||
//the rest of the code doesn't need to care
|
||||
if(port.plug === "outputs") {
|
||||
port.plug = "output";
|
||||
}
|
||||
|
||||
if(port.plug === "input" || port.plug === "input/output") {
|
||||
node.addInputPort(port);
|
||||
}
|
||||
if(port.plug === "output" || port.plug === "input/output") {
|
||||
node.addOutputPort(port);
|
||||
}
|
||||
});
|
||||
|
||||
nodeData.variant && node.setVariant(nodeData.variant);
|
||||
|
||||
return node;
|
||||
};
|
||||
|
||||
module.exports = NodeModel;
|
||||
Reference in New Issue
Block a user