1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import React from 'react';
- import Module from '../Module';
- import { Resolver } from '../Types';
- export default class ModuleSystem {
- constructor() {
- this.constructors = {};
- this.refs = {};
- }
- isAcceptedModule(ctor) {
- return (ctor.prototype instanceof Module ||
- ctor.prototype instanceof React.Component ||
- typeof ctor === "function");
- }
- loadModule(name, ctor, namespace = "default") {
- // Load a Module constructor
- if(!this.isAcceptedModule(ctor)){
- throw new Error("This is not a module: ", ctor);
- }
- if(!this.constructors[namespace])
- this.constructors[namespace] = {};
-
- if(this.constructors[namespace][name]) {
- log("Duplicate module: ", ctor);
- return true;
- }
- this.constructors[namespace][name] = ctor;
- }
- load(Dictionary, Namespace = "default") {
- for(var i in Dictionary) {
- let ctor = Dictionary[i];
- if(!this.isAcceptedModule(ctor) && typeof ctor === "object"){
- console.log("Loading new namespace : " , i);
- this.load(Dictionary[i], i);
- } else if (this.isAcceptedModule(ctor)) {
- this.loadModule(i, Dictionary[i], Namespace);
- }
- }
- console.log(this.constructors);
- }
- list() {
- return this.constructors;
- }
- get(modName, namespace = "default") {
- return this.constructors[namespace][modName];
- }
- fromViewNode(vn) {
- return this.get(vn.value, vn.namespace);
- }
- createRef(id) {
- !this.refs[id] && (this.refs[id] = React.createRef());
- return this.refs[id];
- }
- getRef(id) {
- return this.refs[id];
- }
- createElement(name, props, children, namespace = "default", ref) {
- let ctor = this.get(name, namespace);
- return this.createElementCtor(ctor, props, children, ref);
- }
- createElementCtor(ctor, props, children, ref) {
- let validatedProps = this.validateProps(ctor, props);
- if (ref) {
- validatedProps = {
- ...validatedProps,
- ref: this.refs[ref]
- }
- }
- return React.createElement(ctor, validatedProps, children);
- }
- validateProps(ctor, props = {}) {
- let res = {};
- if(ctor && ctor.Inputs) {
- res = Resolver(ctor.Inputs, props);
- }
- return {
- ...props,
- ...res
- }
- }
- }
|