@mat3ra/ade 2025.10.2-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +17 -0
- package/LICENSE.md +15 -0
- package/README.md +106 -0
- package/dist/js/ApplicationRegistry.d.ts +57 -0
- package/dist/js/ApplicationRegistry.js +171 -0
- package/dist/js/application.d.ts +8 -0
- package/dist/js/application.js +9 -0
- package/dist/js/applicationMixin.d.ts +23 -0
- package/dist/js/applicationMixin.js +48 -0
- package/dist/js/context/ContextProvider.d.ts +83 -0
- package/dist/js/context/ContextProvider.js +139 -0
- package/dist/js/context/ContextProviderRegistryContainer.d.ts +24 -0
- package/dist/js/context/ContextProviderRegistryContainer.js +30 -0
- package/dist/js/context/JSONSchemaFormDataProvider.d.ts +25 -0
- package/dist/js/context/JSONSchemaFormDataProvider.js +46 -0
- package/dist/js/executable.d.ts +8 -0
- package/dist/js/executable.js +12 -0
- package/dist/js/executableMixin.d.ts +22 -0
- package/dist/js/executableMixin.js +28 -0
- package/dist/js/flavor.d.ts +9 -0
- package/dist/js/flavor.js +12 -0
- package/dist/js/flavorMixin.d.ts +21 -0
- package/dist/js/flavorMixin.js +51 -0
- package/dist/js/index.d.ts +5 -0
- package/dist/js/index.js +14 -0
- package/dist/js/template.d.ts +8 -0
- package/dist/js/template.js +10 -0
- package/dist/js/templateMixin.d.ts +39 -0
- package/dist/js/templateMixin.js +162 -0
- package/package.json +88 -0
- package/src/js/ApplicationRegistry.ts +250 -0
- package/src/js/application.ts +18 -0
- package/src/js/applicationMixin.ts +86 -0
- package/src/js/context/ContextProvider.ts +189 -0
- package/src/js/context/ContextProviderRegistryContainer.ts +41 -0
- package/src/js/context/JSONSchemaFormDataProvider.ts +57 -0
- package/src/js/executable.ts +14 -0
- package/src/js/executableMixin.ts +49 -0
- package/src/js/flavor.ts +19 -0
- package/src/js/flavorMixin.ts +77 -0
- package/src/js/index.ts +6 -0
- package/src/js/template.ts +17 -0
- package/src/js/templateMixin.ts +242 -0
- package/tsconfig.json +3 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type { InMemoryEntity } from "@mat3ra/code/dist/js/entity";
|
|
2
|
+
import type { NamedInMemoryEntity } from "@mat3ra/code/dist/js/entity/mixins/NamedEntityMixin";
|
|
3
|
+
import { Utils } from "@mat3ra/utils";
|
|
4
|
+
import type { Constructor } from "@mat3ra/code/dist/js/utils/types";
|
|
5
|
+
import JSONSchemasInterface from "@mat3ra/esse/dist/js/esse/JSONSchemasInterface";
|
|
6
|
+
import type { AnyObject } from "@mat3ra/esse/dist/js/esse/types";
|
|
7
|
+
import type { TemplateSchema } from "@mat3ra/esse/dist/js/types";
|
|
8
|
+
import nunjucks from "nunjucks";
|
|
9
|
+
|
|
10
|
+
import ContextProvider, {
|
|
11
|
+
type ContextProviderConfig,
|
|
12
|
+
type ContextProviderName,
|
|
13
|
+
} from "./context/ContextProvider";
|
|
14
|
+
import ContextProviderRegistryContainer from "./context/ContextProviderRegistryContainer";
|
|
15
|
+
|
|
16
|
+
export type TemplateBase = InMemoryEntity & NamedInMemoryEntity;
|
|
17
|
+
|
|
18
|
+
export type TemplateMixin = {
|
|
19
|
+
isManuallyChanged: boolean;
|
|
20
|
+
content: string;
|
|
21
|
+
rendered: string | undefined;
|
|
22
|
+
applicationName: string | undefined;
|
|
23
|
+
executableName: string | undefined;
|
|
24
|
+
contextProviders: ContextProvider[];
|
|
25
|
+
addContextProvider: (provider: ContextProvider) => void;
|
|
26
|
+
removeContextProvider: (provider: ContextProvider) => void;
|
|
27
|
+
render: (externalContext?: Record<string, unknown>) => void;
|
|
28
|
+
getRenderedJSON: (context?: Record<string, unknown>) => AnyObject;
|
|
29
|
+
_cleanRenderingContext: (object: Record<string, unknown>) => Record<string, unknown>;
|
|
30
|
+
getDataFromProvidersForRenderingContext: (
|
|
31
|
+
context?: Record<string, unknown>,
|
|
32
|
+
) => Record<string, unknown>;
|
|
33
|
+
setContent: (text: string) => void;
|
|
34
|
+
setRendered: (text: string) => void;
|
|
35
|
+
getContextProvidersAsClassInstances: (
|
|
36
|
+
providerContext?: Record<string, unknown>,
|
|
37
|
+
) => ContextProvider[];
|
|
38
|
+
getDataFromProvidersForPersistentContext: (
|
|
39
|
+
providerContext?: Record<string, unknown>,
|
|
40
|
+
) => Record<string, unknown>;
|
|
41
|
+
getRenderingContext: (externalContext?: Record<string, unknown>) => Record<string, unknown>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function templateMixin(item: TemplateBase) {
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
const properties: TemplateMixin & TemplateBase = {
|
|
47
|
+
get isManuallyChanged() {
|
|
48
|
+
return this.prop("isManuallyChanged", false);
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
get content() {
|
|
52
|
+
return this.prop("content", "");
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
setContent(text: string) {
|
|
56
|
+
return this.setProp("content", text);
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
get rendered() {
|
|
60
|
+
return this.prop("rendered") || this.content;
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
setRendered(text: string) {
|
|
64
|
+
return this.setProp("rendered", text);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
get applicationName() {
|
|
68
|
+
return this.prop<string>("applicationName");
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
get executableName() {
|
|
72
|
+
return this.prop<string>("executableName");
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
get contextProviders() {
|
|
76
|
+
return this.prop("contextProviders", []);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
addContextProvider(provider: ContextProvider) {
|
|
80
|
+
this.setProp("contextProviders", [...this.contextProviders, provider]);
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
removeContextProvider(provider: ContextProvider) {
|
|
84
|
+
const contextProviders = this.contextProviders.filter((p) => {
|
|
85
|
+
return p.name !== provider.name && p.domain !== provider.domain;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.setProp("contextProviders", contextProviders);
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
render(externalContext?: Record<string, unknown>) {
|
|
92
|
+
const renderingContext = this.getRenderingContext(externalContext);
|
|
93
|
+
if (!this.isManuallyChanged) {
|
|
94
|
+
try {
|
|
95
|
+
const template = nunjucks.compile(this.content);
|
|
96
|
+
|
|
97
|
+
// deepClone to pass JSON data without classes
|
|
98
|
+
const rendered = template.render(
|
|
99
|
+
this._cleanRenderingContext(renderingContext),
|
|
100
|
+
) as string;
|
|
101
|
+
|
|
102
|
+
this.setRendered(this.isManuallyChanged ? rendered : rendered || this.content);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
console.log(`Template is not compiled: ${e}`);
|
|
105
|
+
console.log({
|
|
106
|
+
content: this.content,
|
|
107
|
+
_cleanRenderingContext: this._cleanRenderingContext(renderingContext),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
getRenderedJSON(context?: Record<string, unknown>) {
|
|
114
|
+
this.render(context);
|
|
115
|
+
return this.toJSON();
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
// Remove "bulky" items and JSON stringify before passing it to rendering engine (eg. jinja) to compile.
|
|
119
|
+
// This way the context should still be passed in full to contextProviders, but not to final text template.
|
|
120
|
+
// eslint-disable-next-line class-methods-use-this
|
|
121
|
+
_cleanRenderingContext(object: Record<string, unknown>) {
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
123
|
+
const { job, ...clone } = object;
|
|
124
|
+
return Utils.clone.deepClone(clone);
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
/*
|
|
128
|
+
* @summary Initializes context provider class instances. `providerContext` is used to pass the data about any
|
|
129
|
+
* previously stored values. That is if data was previously saved in database, the context provider
|
|
130
|
+
* shall receive it on initialization through providerContext and prioritize this value over the default.
|
|
131
|
+
*/
|
|
132
|
+
getContextProvidersAsClassInstances(providerContext?: Record<string, unknown>) {
|
|
133
|
+
return this.contextProviders.map((p) => {
|
|
134
|
+
const providerInstance = (
|
|
135
|
+
this.constructor as unknown as TemplateStaticMixin
|
|
136
|
+
).contextProviderRegistry?.findProviderInstanceByName(p.name);
|
|
137
|
+
|
|
138
|
+
if (!providerInstance) {
|
|
139
|
+
throw new Error(`Provider ${p.name} not found`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const clsInstance = new providerInstance.constructor({
|
|
143
|
+
...providerInstance.config,
|
|
144
|
+
context: providerContext,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return clsInstance;
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
* @summary Extracts the the data from all context providers for further use during render.
|
|
153
|
+
*/
|
|
154
|
+
getDataFromProvidersForRenderingContext(providerContext?: Record<string, unknown>) {
|
|
155
|
+
const result: AnyObject = {};
|
|
156
|
+
this.getContextProvidersAsClassInstances(providerContext).forEach((contextProvider) => {
|
|
157
|
+
const context = contextProvider.yieldDataForRendering();
|
|
158
|
+
Object.keys(context).forEach((key) => {
|
|
159
|
+
// merge context keys if they are objects otherwise override them.
|
|
160
|
+
result[key] =
|
|
161
|
+
result[key] !== null && typeof result[key] === "object"
|
|
162
|
+
? // @ts-ignore
|
|
163
|
+
{ ...result[key], ...context[key] }
|
|
164
|
+
: context[key];
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
return result;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
* @summary Extracts the the data from all context providers for further save in persistent context.
|
|
172
|
+
*/
|
|
173
|
+
// TODO: optimize logic to prevent re-initializing the context provider classes again below, reuse above function
|
|
174
|
+
getDataFromProvidersForPersistentContext(providerContext?: Record<string, unknown>) {
|
|
175
|
+
const result = {};
|
|
176
|
+
this.getContextProvidersAsClassInstances(providerContext).forEach((contextProvider) => {
|
|
177
|
+
// only save in the persistent context the data from providers that were edited (or able to be edited)
|
|
178
|
+
Object.assign(result, contextProvider.isEdited ? contextProvider.yieldData() : {});
|
|
179
|
+
});
|
|
180
|
+
return result;
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
* @summary Combines rendering context (in order of preference):
|
|
185
|
+
* - context from templates initialized with external context
|
|
186
|
+
* - "external" context and
|
|
187
|
+
*/
|
|
188
|
+
getRenderingContext(externalContext?: Record<string, unknown>) {
|
|
189
|
+
return {
|
|
190
|
+
...externalContext,
|
|
191
|
+
...this.getDataFromProvidersForRenderingContext(externalContext),
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
|
|
197
|
+
|
|
198
|
+
return properties;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export type ContextProviderConfigMapEntry = {
|
|
202
|
+
providerCls: typeof ContextProvider;
|
|
203
|
+
config: ContextProviderConfig;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export type ContextProviderConfigMap = Partial<
|
|
207
|
+
Record<ContextProviderName, ContextProviderConfigMapEntry>
|
|
208
|
+
>;
|
|
209
|
+
|
|
210
|
+
export type TemplateStaticMixin = {
|
|
211
|
+
contextProviderRegistry: ContextProviderRegistryContainer | null;
|
|
212
|
+
setContextProvidersConfig: (classConfigMap: ContextProviderConfigMap) => void;
|
|
213
|
+
jsonSchema: TemplateSchema;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export function templateStaticMixin(item: Constructor<TemplateBase & TemplateMixin>) {
|
|
217
|
+
// @ts-ignore
|
|
218
|
+
const properties: TemplateStaticMixin & Constructor<TemplateBase & TemplateMixin> = {
|
|
219
|
+
contextProviderRegistry: null,
|
|
220
|
+
|
|
221
|
+
get jsonSchema() {
|
|
222
|
+
return JSONSchemasInterface.getSchemaById("software/template") as TemplateSchema;
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
setContextProvidersConfig(classConfigMap: ContextProviderConfigMap) {
|
|
226
|
+
const contextProviderRegistry = new ContextProviderRegistryContainer();
|
|
227
|
+
|
|
228
|
+
Object.entries(classConfigMap).forEach(([name, { providerCls, config }]) => {
|
|
229
|
+
contextProviderRegistry.addProvider({
|
|
230
|
+
instance: providerCls.getConstructorConfig(config),
|
|
231
|
+
name,
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
this.contextProviderRegistry = contextProviderRegistry;
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
|
|
240
|
+
|
|
241
|
+
return properties;
|
|
242
|
+
}
|
package/tsconfig.json
ADDED