import * as React from "react" ; import { Mapping } from "./RootComponentRegistry" ; import WrapperFactory from "./component/WrapperFactory" ; import { ComponentConfig } from "./component/WrapperFactory" ; export default class ComponentRegistry { public mappings : Mapping [ ] ; private mapping : any ; constructor ( mapping ? ) ; constructor ( mapping ? ) ; constructor ( mapping ? ) { this . mappings = [ ] ; this . mapping = mapping ; } public register ( componentClass ) { let componentClassName = ( componentClass as any ) [ "name" ] ; let resourceType = this . mapToResourceType ( componentClassName ) ; this . mappings . push ( new Mapping ( resourceType , componentClass , null ) ) ; } public registerVanilla ( config ) { let componentClassName = ( config . component as any ) [ "name" ] ; let resourceType = this . mapToResourceType ( componentClassName ) ; let wrapperClass = WrapperFactory . createWrapper ( config , resourceType ) ; this . mappings . push ( new Mapping ( resourceType , wrapperClass , config . component ) ) ; } private mapToResourceType ( componentClassName ) { let resourceType = null ; if ( typeof this . mapping === "function" ) { resourceType = this . mapping ( componentClassName ) ; } else if ( typeof this . mapping === "string" ) { resourceType = this . mapping + "/" + this . mapClassToResourceType ( componentClassName ) ; } else { resourceType = this . mapClassToResourceType ( componentClassName ) ; } return resourceType ; } private mapClassToResourceType ( componentClassName ) { let parts = componentClassName . match ( /([A-Z][a-z0-9]*)/ ) ; if ( parts ) { let resourceType = parts [ 0 ] . toLocaleLowerCase ( ) ; let rest = componentClassName . substring ( parts [ 0 ] . length ) ; if ( rest . length > 0 ) { resourceType += "-" + this . mapClassToResourceType ( rest ) ; } return resourceType ; } } }