"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _Logger = _interopRequireDefault(require("./Logger")); var _ModuleUtilities = require("../modules/ModuleUtilities"); var _Systems = _interopRequireDefault(require("./Systems")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var ModuleSystem = /*#__PURE__*/ function () { function ModuleSystem(systemID) { _classCallCheck(this, ModuleSystem); this.systemID = systemID; this.clean(); this.availableModules = _Systems.default.getModules(); } _createClass(ModuleSystem, [{ key: "clean", value: function clean() { this.modules = {}; this.noresolve = {}; this.resolved = {}; this.metamodules = {}; } }, { key: "ghostModule", value: function ghostModule(id, ctor, inputs) { var _this = this; if (this.resolved[id]) { return { id: id, remove: function remove() { return _this.remove(id); }, render: function render() { return _this.render(id); } }; } var ins = {}; for (var i in inputs) { ins[i] = { value: inputs[i] }; } var mod = { ctor: ctor.name, category: ctor.Category, namespace: ctor.Namespace, id: id, inputs: ins, readOnly: true }; this.loadModule(mod); this.refresh(true); return { id: mod.id, remove: function remove() { return _this.remove(mod.id); }, render: function render() { return _this.render(mod.id); } }; } }, { key: "loadModule", value: function loadModule(mod) { var rootMod = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; mod.inputs = mod.inputs || {}; mod.outputs = mod.outputs || {}; mod.children = mod.children || []; var ctor = mod.ctor, category = mod.category, namespace = mod.namespace, id = mod.id, inputs = mod.inputs, outputs = mod.outputs, children = mod.children, readOnly = mod.readOnly; if (_Systems.default.getModule(ctor, category, namespace) === undefined) { _Logger.default.warning("Module " + ctor + "+" + category + "+" + namespace + " is not bundled! You either forgot to add it to ModuleSystem or it doesn't exist!"); return; } var inrefs = []; for (var i in _Systems.default.getModule(ctor, category, namespace).Inputs) { var v = _Systems.default.getModule(ctor, category, namespace).Inputs[i].defaultValue; if (inputs[i]) v = inputs[i].value || v; inputs[i] = { name: i, value: v, pid: id, arrType: _Systems.default.getModule(ctor, category, namespace).Inputs[i].arrType, type: _Systems.default.getModule(ctor, category, namespace).Inputs[i].type }; inrefs[i] = this.LinkManager.addInputRef(inputs[i]); } var outrefs = []; for (var i in _Systems.default.getModule(ctor, category, namespace).Outputs) { outputs[i] = { name: i, pid: id, arrType: _Systems.default.getModule(ctor, category, namespace).Outputs[i].arrType, type: _Systems.default.getModule(ctor, category, namespace).Outputs[i].type }; outrefs[i] = this.LinkManager.addOutputRef(outputs[i]); } this.__addToScene(_objectSpread({}, mod, { children: _toConsumableArray(children), inrefs: inrefs, outrefs: outrefs, key: '' + id + id })); } }, { key: "connect", value: function connect(input, output) { this.LinkManager.createLink(input, output); } }, { key: "rename", value: function rename(oldid, newid) { for (var i in this.metamodules) { // only first child var inx = this.metamodules[i].children.indexOf(oldid); if (inx > -1) { this.metamodules[i].children[inx] = newid; } } for (var i in this.metamodules[oldid].inrefs) { this.metamodules[oldid].inputs[i].pid = newid; this.metamodules[oldid].inrefs[i].pid = newid; } for (var i in this.metamodules[oldid].outrefs) { this.metamodules[oldid].outputs[i].pid = newid; this.metamodules[oldid].outrefs[i].pid = newid; } this.LinkManager.rename(oldid, newid); this.metamodules[newid] = this.metamodules[oldid]; this.metamodules[newid].id = newid; delete this.metamodules[oldid]; this._shuffleKeys(this.resolved[oldid]); delete this.resolved[oldid]; this.noresolve = _objectSpread({}, this.metamodules); this.modules = {}; } }, { key: "reorder", value: function reorder(modid, times) { var parent = this.resolved[modid].parent; var childIndx = parent.children.indexOf(modid); var swapIndx = childIndx + times; if (childIndx > -1 && swapIndx > -1 && swapIndx < parent.children.length) { var temp = parent.children[swapIndx]; parent.children[swapIndx] = modid; parent.children[childIndx] = temp; this._shuffleKeys(this.resolved[modid]); this._shuffleKeys(this.resolved[temp]); this.noresolve = _objectSpread({}, this.metamodules); this.modules = {}; } } }, { key: "change", value: function change(id, name, value) { var v = value; this.resolved[id].inputs[name].value = v; this.resolved[id].inrefs[name].value = v; // re-render //this._shuffleKey(this.metamodules[id]); //this._shuffleKeys(this.resolved[id]); //this.noresolve = {...this.metamodules}; //this.modules = {}; } }, { key: "addChild", value: function addChild(id, childid) { this.resolved[id].children.push(childid); //this._shuffleKeys(this.resolved[id]); this.modules = {}; this.noresolve = _objectSpread({}, this.metamodules); } }, { key: "remove", value: function remove(id) { this.modules = {}; var parent = this.resolved[id].parent; //this._shuffleKeys(this.resolved[id]); delete this.metamodules[id]; if (parent) { var indx = parent.children.indexOf(id); if (indx > -1) parent.children.splice(indx, 1); } this.noresolve = _objectSpread({}, this.metamodules); this.resolved = {}; this.LinkManager.breakLinks(id); } }, { key: "_shuffleAllKeys", value: function _shuffleAllKeys(temp) { this.modules = {}; this.noresolve = _objectSpread({}, this.metamodules); // for(var i in this.metamodules) { // this.metamodules[i].key = this.metamodules[i].id + Math.random() %231231; // } } }, { key: "_shuffleKeys", value: function _shuffleKeys(temp) { var i = 0; do { temp.key = temp.id + Math.random() % 231231; temp = temp.parent; i++; } while (temp && i < 2); // i < 2 for only change parent } }, { key: "_shuffleKey", value: function _shuffleKey(temp) { temp.key = temp.id + Math.random() % 231231; } }, { key: "__Factory", value: function __Factory(item) { var children = []; for (var i = 0; i < item.children.length; i++) { children.push(this.modules[item.children[i]]); this.resolved[item.children[i]].parent = item; } var ctor = _Systems.default.getModule(item.ctor, item.category, item.namespace); var props = item; props.System = this; // I have systemID but this is parametric for Module/Service props.systemID = this.systemID; props.__adminmode = 'normal'; console.log("FACTORY", ctor, item); return _react.default.createElement(ctor, props, children); } }, { key: "__childrenReady", value: function __childrenReady(children) { for (var i = 0; i < children.length; i++) { var id = children[i]; if (!this.modules[id]) { return false; } } return true; } }, { key: "__resolveItem", value: function __resolveItem(item) { if (this.LinkManager.isReady(item) && this.__childrenReady(item.children)) { this.resolved[item.id] = item; var mod = this.__Factory(item); item.reactElement = mod; delete this.modules[item.id]; this.modules[item.id] = mod; delete this.noresolve[item.id]; this.dirty = true; } } }, { key: "__resolve", value: function __resolve() { for (var i in this.noresolve) { this.__resolveItem(this.noresolve[i]); } } }, { key: "__addToScene", value: function __addToScene(item) { this.noresolve[item.id] = item; this.metamodules[item.id] = item; } }, { key: "__refresh", value: function __refresh() { var hard = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var suppresedCallbacks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (hard) this._shuffleAllKeys(); do { this.dirty = false; this.__resolve(); } while (this.dirty); if (this._onUpdate && !suppresedCallbacks) this._onUpdate(); } }, { key: "refresh", value: function refresh(h, s) { this.__refresh(h, s); } }, { key: "onUpdate", value: function onUpdate(fn) { this._onUpdate = fn; } }, { key: "render", value: function render(id) { var mod = this.modules[id || this.root]; if (!mod) { mod = this.modules[this.root]; } return mod || 'No Root'; } }, { key: "getModules", value: function getModules() { var _this2 = this; return Object.keys(this.resolved).map(function (key) { return _this2.resolved[key]; }); } }, { key: "has", value: function has(id) { return this.modules[id]; } }, { key: "export", value: function _export() { var _this3 = this; return Object.keys(this.resolved).map(function (key) { var item = _this3.resolved[key]; return { ctor: item.ctor, category: item.category, namespace: item.namespace, children: item.children, id: item.id, inputs: item.inputs, outputs: item.outputs, key: item.key, readOnly: item.readOnly }; }); } }]); return ModuleSystem; }(); var _default = ModuleSystem; exports.default = _default;