forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.ts
More file actions
130 lines (96 loc) · 3.58 KB
/
resource.ts
File metadata and controls
130 lines (96 loc) · 3.58 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { ISkeleton } from '@alilc/lowcode-editor-skeleton';
import { IPublicTypeEditorView, IPublicResourceData, IPublicResourceTypeConfig, IBaseModelResource, IPublicEnumPluginRegisterLevel } from '@alilc/lowcode-types';
import { Logger } from '@alilc/lowcode-utils';
import { BasicContext, IBasicContext } from './context/base-context';
import { ResourceType, IResourceType } from './resource-type';
import { IWorkspace } from './workspace';
const logger = new Logger({ level: 'warn', bizName: 'workspace:resource' });
export interface IBaseResource<T> extends IBaseModelResource<T> {
readonly resourceType: ResourceType;
skeleton: ISkeleton;
description?: string;
get editorViews(): IPublicTypeEditorView[];
get defaultViewName(): string | undefined;
getEditorView(name: string): IPublicTypeEditorView | undefined;
import(schema: any): Promise<any>;
save(value: any): Promise<any>;
url(): Promise<string | undefined>;
}
export type IResource = IBaseResource<IResource>;
export class Resource implements IResource {
private context: IBasicContext;
resourceTypeInstance: IPublicResourceTypeConfig;
editorViewMap: Map<string, IPublicTypeEditorView> = new Map<string, IPublicTypeEditorView>();
get name() {
return this.resourceType.name;
}
get viewName() {
return this.resourceData.viewName || (this.resourceData as any).viewType || this.defaultViewName;
}
get description() {
return this.resourceTypeInstance?.description;
}
get icon() {
return this.resourceData.icon || this.resourceTypeInstance?.icon;
}
get type() {
return this.resourceType.type;
}
get title(): string | undefined {
return this.resourceData.title || this.resourceTypeInstance.defaultTitle;
}
get id(): string | undefined {
return this.resourceData.id;
}
get options() {
return this.resourceData.options;
}
get category() {
return this.resourceData?.category;
}
get skeleton() {
return this.context.innerSkeleton;
}
children: IResource[];
get config() {
return this.resourceData.config;
}
constructor(readonly resourceData: IPublicResourceData, readonly resourceType: IResourceType, readonly workspace: IWorkspace) {
this.context = new BasicContext(workspace, `resource-${resourceData.resourceName || resourceType.name}`, IPublicEnumPluginRegisterLevel.Resource);
this.resourceTypeInstance = resourceType.resourceTypeModel(this.context.innerPlugins._getLowCodePluginContext({
pluginName: '',
}), this.options);
this.init();
if (this.resourceTypeInstance.editorViews) {
this.resourceTypeInstance.editorViews.forEach((d: any) => {
this.editorViewMap.set(d.viewName, d);
});
}
if (!resourceType) {
logger.error(`resourceType[${resourceType}] is unValid.`);
}
this.children = this.resourceData?.children?.map(d => new Resource(d, this.workspace.getResourceType(d.resourceName || this.resourceType.name), this.workspace)) || [];
}
async init() {
await this.resourceTypeInstance.init?.();
await this.context.innerPlugins.init();
}
async import(schema: any) {
return await this.resourceTypeInstance.import?.(schema);
}
async url() {
return await this.resourceTypeInstance.url?.();
}
async save(value: any) {
return await this.resourceTypeInstance.save?.(value);
}
get editorViews() {
return this.resourceTypeInstance.editorViews;
}
get defaultViewName() {
return this.resourceTypeInstance.defaultViewName || this.resourceTypeInstance.defaultViewType;
}
getEditorView(name: string) {
return this.editorViewMap.get(name);
}
}