ModuleSystem.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _Logger = _interopRequireDefault(require("./Logger"));
  8. var _ModuleUtilities = require("../modules/ModuleUtilities");
  9. var _Systems = _interopRequireDefault(require("./Systems"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
  12. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
  13. function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
  14. 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; } }
  15. 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; }
  16. 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; }
  17. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  18. 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); } }
  19. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  20. var ModuleSystem =
  21. /*#__PURE__*/
  22. function () {
  23. function ModuleSystem(systemID) {
  24. _classCallCheck(this, ModuleSystem);
  25. this.systemID = systemID;
  26. this.clean();
  27. this.availableModules = _Systems.default.getModules();
  28. }
  29. _createClass(ModuleSystem, [{
  30. key: "clean",
  31. value: function clean() {
  32. this.modules = {};
  33. this.noresolve = {};
  34. this.resolved = {};
  35. this.metamodules = {};
  36. }
  37. }, {
  38. key: "ghostModule",
  39. value: function ghostModule(id, ctor, inputs) {
  40. var _this = this;
  41. if (this.resolved[id]) {
  42. return {
  43. id: id,
  44. remove: function remove() {
  45. return _this.remove(id);
  46. },
  47. render: function render() {
  48. return _this.render(id);
  49. }
  50. };
  51. }
  52. var ins = {};
  53. for (var i in inputs) {
  54. ins[i] = {
  55. value: inputs[i]
  56. };
  57. }
  58. var mod = {
  59. ctor: ctor.name,
  60. category: ctor.Category,
  61. namespace: ctor.Namespace,
  62. id: id,
  63. inputs: ins,
  64. readOnly: true
  65. };
  66. this.loadModule(mod);
  67. this.refresh(true);
  68. return {
  69. id: mod.id,
  70. remove: function remove() {
  71. return _this.remove(mod.id);
  72. },
  73. render: function render() {
  74. return _this.render(mod.id);
  75. }
  76. };
  77. }
  78. }, {
  79. key: "loadModule",
  80. value: function loadModule(mod) {
  81. var rootMod = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  82. mod.inputs = mod.inputs || {};
  83. mod.outputs = mod.outputs || {};
  84. mod.children = mod.children || [];
  85. var ctor = mod.ctor,
  86. category = mod.category,
  87. namespace = mod.namespace,
  88. id = mod.id,
  89. inputs = mod.inputs,
  90. outputs = mod.outputs,
  91. children = mod.children,
  92. readOnly = mod.readOnly;
  93. if (_Systems.default.getModule(ctor, category, namespace) === undefined) {
  94. _Logger.default.warning("Module " + ctor + "+" + category + "+" + namespace + " is not bundled! You either forgot to add it to ModuleSystem or it doesn't exist!");
  95. return;
  96. }
  97. var inrefs = [];
  98. for (var i in _Systems.default.getModule(ctor, category, namespace).Inputs) {
  99. var v = _Systems.default.getModule(ctor, category, namespace).Inputs[i].defaultValue;
  100. if (inputs[i]) v = inputs[i].value || v;
  101. inputs[i] = {
  102. name: i,
  103. value: v,
  104. pid: id,
  105. arrType: _Systems.default.getModule(ctor, category, namespace).Inputs[i].arrType,
  106. type: _Systems.default.getModule(ctor, category, namespace).Inputs[i].type
  107. };
  108. inrefs[i] = this.LinkManager.addInputRef(inputs[i]);
  109. }
  110. var outrefs = [];
  111. for (var i in _Systems.default.getModule(ctor, category, namespace).Outputs) {
  112. outputs[i] = {
  113. name: i,
  114. pid: id,
  115. arrType: _Systems.default.getModule(ctor, category, namespace).Outputs[i].arrType,
  116. type: _Systems.default.getModule(ctor, category, namespace).Outputs[i].type
  117. };
  118. outrefs[i] = this.LinkManager.addOutputRef(outputs[i]);
  119. }
  120. this.__addToScene(_objectSpread({}, mod, {
  121. children: _toConsumableArray(children),
  122. inrefs: inrefs,
  123. outrefs: outrefs,
  124. key: '' + id + id
  125. }));
  126. }
  127. }, {
  128. key: "connect",
  129. value: function connect(input, output) {
  130. this.LinkManager.createLink(input, output);
  131. }
  132. }, {
  133. key: "rename",
  134. value: function rename(oldid, newid) {
  135. for (var i in this.metamodules) {
  136. // only first child
  137. var inx = this.metamodules[i].children.indexOf(oldid);
  138. if (inx > -1) {
  139. this.metamodules[i].children[inx] = newid;
  140. }
  141. }
  142. for (var i in this.metamodules[oldid].inrefs) {
  143. this.metamodules[oldid].inputs[i].pid = newid;
  144. this.metamodules[oldid].inrefs[i].pid = newid;
  145. }
  146. for (var i in this.metamodules[oldid].outrefs) {
  147. this.metamodules[oldid].outputs[i].pid = newid;
  148. this.metamodules[oldid].outrefs[i].pid = newid;
  149. }
  150. this.LinkManager.rename(oldid, newid);
  151. this.metamodules[newid] = this.metamodules[oldid];
  152. this.metamodules[newid].id = newid;
  153. delete this.metamodules[oldid];
  154. this._shuffleKeys(this.resolved[oldid]);
  155. delete this.resolved[oldid];
  156. this.noresolve = _objectSpread({}, this.metamodules);
  157. this.modules = {};
  158. }
  159. }, {
  160. key: "reorder",
  161. value: function reorder(modid, times) {
  162. var parent = this.resolved[modid].parent;
  163. var childIndx = parent.children.indexOf(modid);
  164. var swapIndx = childIndx + times;
  165. if (childIndx > -1 && swapIndx > -1 && swapIndx < parent.children.length) {
  166. var temp = parent.children[swapIndx];
  167. parent.children[swapIndx] = modid;
  168. parent.children[childIndx] = temp;
  169. this._shuffleKeys(this.resolved[modid]);
  170. this._shuffleKeys(this.resolved[temp]);
  171. this.noresolve = _objectSpread({}, this.metamodules);
  172. this.modules = {};
  173. }
  174. }
  175. }, {
  176. key: "change",
  177. value: function change(id, name, value) {
  178. var v = value;
  179. this.resolved[id].inputs[name].value = v;
  180. this.resolved[id].inrefs[name].value = v; // re-render
  181. //this._shuffleKey(this.metamodules[id]);
  182. //this._shuffleKeys(this.resolved[id]);
  183. //this.noresolve = {...this.metamodules};
  184. //this.modules = {};
  185. }
  186. }, {
  187. key: "addChild",
  188. value: function addChild(id, childid) {
  189. this.resolved[id].children.push(childid); //this._shuffleKeys(this.resolved[id]);
  190. this.modules = {};
  191. this.noresolve = _objectSpread({}, this.metamodules);
  192. }
  193. }, {
  194. key: "remove",
  195. value: function remove(id) {
  196. this.modules = {};
  197. var parent = this.resolved[id].parent; //this._shuffleKeys(this.resolved[id]);
  198. delete this.metamodules[id];
  199. if (parent) {
  200. var indx = parent.children.indexOf(id);
  201. if (indx > -1) parent.children.splice(indx, 1);
  202. }
  203. this.noresolve = _objectSpread({}, this.metamodules);
  204. this.resolved = {};
  205. this.LinkManager.breakLinks(id);
  206. }
  207. }, {
  208. key: "_shuffleAllKeys",
  209. value: function _shuffleAllKeys(temp) {
  210. this.modules = {};
  211. this.noresolve = _objectSpread({}, this.metamodules); // for(var i in this.metamodules) {
  212. // this.metamodules[i].key = this.metamodules[i].id + Math.random() %231231;
  213. // }
  214. }
  215. }, {
  216. key: "_shuffleKeys",
  217. value: function _shuffleKeys(temp) {
  218. var i = 0;
  219. do {
  220. temp.key = temp.id + Math.random() % 231231;
  221. temp = temp.parent;
  222. i++;
  223. } while (temp && i < 2); // i < 2 for only change parent
  224. }
  225. }, {
  226. key: "_shuffleKey",
  227. value: function _shuffleKey(temp) {
  228. temp.key = temp.id + Math.random() % 231231;
  229. }
  230. }, {
  231. key: "__Factory",
  232. value: function __Factory(item) {
  233. var children = [];
  234. for (var i = 0; i < item.children.length; i++) {
  235. children.push(this.modules[item.children[i]]);
  236. this.resolved[item.children[i]].parent = item;
  237. }
  238. var ctor = _Systems.default.getModule(item.ctor, item.category, item.namespace);
  239. var props = item;
  240. props.System = this; // I have systemID but this is parametric for Module/Service
  241. props.systemID = this.systemID;
  242. props.__adminmode = 'normal';
  243. console.log("FACTORY", ctor, item);
  244. return _react.default.createElement(ctor, props, children);
  245. }
  246. }, {
  247. key: "__childrenReady",
  248. value: function __childrenReady(children) {
  249. for (var i = 0; i < children.length; i++) {
  250. var id = children[i];
  251. if (!this.modules[id]) {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. }, {
  258. key: "__resolveItem",
  259. value: function __resolveItem(item) {
  260. if (this.LinkManager.isReady(item) && this.__childrenReady(item.children)) {
  261. this.resolved[item.id] = item;
  262. var mod = this.__Factory(item);
  263. item.reactElement = mod;
  264. delete this.modules[item.id];
  265. this.modules[item.id] = mod;
  266. delete this.noresolve[item.id];
  267. this.dirty = true;
  268. }
  269. }
  270. }, {
  271. key: "__resolve",
  272. value: function __resolve() {
  273. for (var i in this.noresolve) {
  274. this.__resolveItem(this.noresolve[i]);
  275. }
  276. }
  277. }, {
  278. key: "__addToScene",
  279. value: function __addToScene(item) {
  280. this.noresolve[item.id] = item;
  281. this.metamodules[item.id] = item;
  282. }
  283. }, {
  284. key: "__refresh",
  285. value: function __refresh() {
  286. var hard = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  287. var suppresedCallbacks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  288. if (hard) this._shuffleAllKeys();
  289. do {
  290. this.dirty = false;
  291. this.__resolve();
  292. } while (this.dirty);
  293. if (this._onUpdate && !suppresedCallbacks) this._onUpdate();
  294. }
  295. }, {
  296. key: "refresh",
  297. value: function refresh(h, s) {
  298. this.__refresh(h, s);
  299. }
  300. }, {
  301. key: "onUpdate",
  302. value: function onUpdate(fn) {
  303. this._onUpdate = fn;
  304. }
  305. }, {
  306. key: "render",
  307. value: function render(id) {
  308. var mod = this.modules[id || this.root];
  309. if (!mod) {
  310. mod = this.modules[this.root];
  311. }
  312. return mod || 'No Root';
  313. }
  314. }, {
  315. key: "getModules",
  316. value: function getModules() {
  317. var _this2 = this;
  318. return Object.keys(this.resolved).map(function (key) {
  319. return _this2.resolved[key];
  320. });
  321. }
  322. }, {
  323. key: "has",
  324. value: function has(id) {
  325. return this.modules[id];
  326. }
  327. }, {
  328. key: "export",
  329. value: function _export() {
  330. var _this3 = this;
  331. return Object.keys(this.resolved).map(function (key) {
  332. var item = _this3.resolved[key];
  333. return {
  334. ctor: item.ctor,
  335. category: item.category,
  336. namespace: item.namespace,
  337. children: item.children,
  338. id: item.id,
  339. inputs: item.inputs,
  340. outputs: item.outputs,
  341. key: item.key,
  342. readOnly: item.readOnly
  343. };
  344. });
  345. }
  346. }]);
  347. return ModuleSystem;
  348. }();
  349. var _default = ModuleSystem;
  350. exports.default = _default;