Repository navigation
Expand file tree
/
Copy pathComponentRegistry.tsx
More file actions
62 lines (51 loc) · 2.36 KB
/
Copy pathComponentRegistry.tsx
File metadata and controls
62 lines (51 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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?: (componentClassName: string) => string);
constructor(mapping?: string);
constructor(mapping?: any) {
this.mappings = [];
this.mapping = mapping;
}
public register(componentClass: React.ComponentClass): void {
/* tslint:disable:no-string-literal */
let componentClassName: string = (componentClass as any)["name"];
/* tsslint:enable:no-string-literal */
let resourceType: string = this.mapToResourceType(componentClassName);
this.mappings.push(new Mapping(resourceType, componentClass, null));
}
public registerVanilla(config: ComponentConfig): void {
/* tslint:disable:no-string-literal */
let componentClassName: string = (config.component as any)["name"];
/* tsslint:enable:no-string-literal */
let resourceType: string = this.mapToResourceType(componentClassName);
let wrapperClass: React.ComponentClass<any> = WrapperFactory.createWrapper(config, resourceType);
this.mappings.push(new Mapping(resourceType, wrapperClass, config.component));
}
private mapToResourceType(componentClassName: string): string {
let resourceType: string = 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: string): string {
let parts: string[] = componentClassName.match(/([A-Z][a-z0-9]*)/);
if (parts) {
let resourceType: string = parts[0].toLocaleLowerCase();
let rest: string = componentClassName.substring(parts[0].length);
if (rest.length > 0) {
resourceType += "-" + this.mapClassToResourceType(rest);
}
return resourceType;
}
}
}