ModuleSystem.js 4.7 KB

12345
  1. var _extends=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a};import React from'react';import Logger from'./Logger';import{Resolver}from'../modules/ModuleUtilities';import Systems from'./Systems';class ModuleSystem{constructor(a){this.systemID=a,this.clean(),this.availableModules=Systems.getModules()}clean(){this.modules={},this.noresolve={},this.resolved={},this.metamodules={}}ghostModule(a,b,c){if(this.resolved[a])return{id:a,remove:()=>this.remove(a),render:()=>this.render(a)};let d={};for(var e in c)d[e]={value:c[e]};let f={ctor:b,id:a,inputs:d,readOnly:!0};return this.loadModule(f),this.refresh(!0),{id:f.id,remove:()=>this.remove(f.id),render:()=>this.render(f.id)}}loadModule(a,b=!1){a.inputs=a.inputs||{},a.outputs=a.outputs||{},a.children=a.children||[];let{ctor:c,id:d,inputs:e,outputs:f,children:g,readOnly:h}=a;if(void 0===this.availableModules[c])return void Logger.warning('Module '+c+' is not bundled! You either forgot to add it to ModuleSystem or it doesn\'t exist!');let j=[];for(var k in this.availableModules[c].Inputs){let a=this.availableModules[c].Inputs[k].defaultValue;e[k]&&(a=e[k].value||a),e[k]={name:k,value:a,pid:d,arrType:this.availableModules[c].Inputs[k].arrType,type:this.availableModules[c].Inputs[k].type},j[k]=this.LinkManager.addInputRef(e[k])}let l=[];for(var k in this.availableModules[c].Outputs)f[k]={name:k,pid:d,arrType:this.availableModules[c].Outputs[k].arrType,type:this.availableModules[c].Outputs[k].type},l[k]=this.LinkManager.addOutputRef(f[k]);this.__addToScene(_extends({},a,{children:[...g],inrefs:j,outrefs:l,key:''+d+d}))}connect(a,b){this.LinkManager.createLink(a,b)}rename(a,b){for(var c in this.metamodules){// only first child
  2. let d=this.metamodules[c].children.indexOf(a);-1<d&&(this.metamodules[c].children[d]=b)}for(var c in this.metamodules[a].inrefs)this.metamodules[a].inputs[c].pid=b,this.metamodules[a].inrefs[c].pid=b;for(var c in this.metamodules[a].outrefs)this.metamodules[a].outputs[c].pid=b,this.metamodules[a].outrefs[c].pid=b;this.LinkManager.rename(a,b),this.metamodules[b]=this.metamodules[a],this.metamodules[b].id=b,delete this.metamodules[a],this._shuffleKeys(this.resolved[a]),delete this.resolved[a],this.noresolve=_extends({},this.metamodules),this.modules={}}reorder(a,b){let c=this.resolved[a].parent,d=c.children.indexOf(a),e=d+b;if(-1<d&&-1<e&&e<c.children.length){let b=c.children[e];c.children[e]=a,c.children[d]=b,this._shuffleKeys(this.resolved[a]),this._shuffleKeys(this.resolved[b]),this.noresolve=_extends({},this.metamodules),this.modules={}}}change(a,b,c){let d=c;this.resolved[a].inputs[b].value=d,this.resolved[a].inrefs[b].value=d}addChild(a,b){//this._shuffleKeys(this.resolved[id]);
  3. this.resolved[a].children.push(b),this.modules={},this.noresolve=_extends({},this.metamodules)}remove(a){this.modules={};let b=this.resolved[a].parent;//this._shuffleKeys(this.resolved[id]);
  4. if(delete this.metamodules[a],b){let c=b.children.indexOf(a);-1<c&&b.children.splice(c,1)}this.noresolve=_extends({},this.metamodules),this.resolved={},this.LinkManager.breakLinks(a)}_shuffleAllKeys(){this.modules={},this.noresolve=_extends({},this.metamodules)}_shuffleKeys(a){let b=0;do a.key=a.id+Math.random()%231231,a=a.parent,b++;while(a&&2>b);// i < 2 for only change parent
  5. }_shuffleKey(a){a.key=a.id+Math.random()%231231}__Factory(a){let b=[];for(var c=0;c<a.children.length;c++)b.push(this.modules[a.children[c]]),this.resolved[a.children[c]].parent=a;let d=this.availableModules[a.ctor],e=a;return e.System=this,e.systemID=this.systemID,e.decorate=function(a='core',b){Systems.decorate(a,b)},e.__adminmode='normal',React.createElement(d,e,b)}__childrenReady(a){for(var b=0;b<a.length;b++){let c=a[b];if(!this.modules[c])return!1}return!0}__resolveItem(a){if(this.LinkManager.isReady(a)&&this.__childrenReady(a.children)){this.resolved[a.id]=a;let b=this.__Factory(a);a.reactElement=b,delete this.modules[a.id],this.modules[a.id]=b,delete this.noresolve[a.id],this.dirty=!0}}__resolve(){for(var a in this.noresolve)this.__resolveItem(this.noresolve[a])}__addToScene(a){this.noresolve[a.id]=a,this.metamodules[a.id]=a}__refresh(a=!1,b=!1){a&&this._shuffleAllKeys();do this.dirty=!1,this.__resolve();while(this.dirty);this._onUpdate&&!b&&this._onUpdate()}refresh(a,b){this.__refresh(a,b)}onUpdate(a){this._onUpdate=a}render(a){let b=this.modules[a||this.root];return b||(b=this.modules[this.root]),b||'No Root'}getModules(){return Object.keys(this.resolved).map(a=>this.resolved[a])}has(a){return this.modules[a]}export(){return Object.keys(this.resolved).map(a=>{let b=this.resolved[a];return{ctor:b.ctor,children:b.children,id:b.id,inputs:b.inputs,outputs:b.outputs,key:b.key,readOnly:b.readOnly}})}}export default ModuleSystem;