Skip to content

Commit 704687a

Browse files
committed
update: remove template rendering logic
1 parent bea3753 commit 704687a

30 files changed

+91
-1270
lines changed

dist/js/ApplicationRegistry.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { ApplicationSchemaBase } from "@mat3ra/esse/dist/js/types";
2-
import Application from "./application";
3-
import Executable from "./executable";
4-
import Flavor from "./flavor";
5-
import Template from "./template";
1+
import type { ApplicationSchemaBase, TemplateSchema } from "@mat3ra/esse/dist/js/types";
2+
import Application from "./Application";
3+
import Executable from "./Executable";
4+
import Flavor from "./Flavor";
5+
import Template from "./Template";
66
type ApplicationVersion = {
77
[build: string]: ApplicationSchemaBase;
88
};
@@ -51,7 +51,7 @@ export default class ApplicationRegistry {
5151
name: string;
5252
}): Flavor | undefined;
5353
static getInputAsTemplates(flavor: Flavor): Template[];
54-
static getInputAsRenderedTemplates(flavor: Flavor, context: Record<string, unknown>): import("@mat3ra/esse/dist/js/esse/types").AnyObject[];
54+
static getInput(flavor: Flavor): TemplateSchema[];
5555
static getAllFlavorsForApplication(appName: string, version?: string): Flavor[];
5656
}
5757
export {};

dist/js/ApplicationRegistry.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
const object_1 = require("@mat3ra/code/dist/js/utils/object");
77
const standata_1 = require("@mat3ra/standata");
8-
const application_1 = __importDefault(require("./application"));
9-
const executable_1 = __importDefault(require("./executable"));
10-
const flavor_1 = __importDefault(require("./flavor"));
11-
const template_1 = __importDefault(require("./template"));
8+
const Application_1 = __importDefault(require("./Application"));
9+
const Executable_1 = __importDefault(require("./Executable"));
10+
const Flavor_1 = __importDefault(require("./Flavor"));
11+
const Template_1 = __importDefault(require("./Template"));
1212
class ApplicationRegistry {
1313
static createApplication({ name, version = null, build = null }) {
1414
const staticConfig = ApplicationRegistry.getApplicationConfig({ name, version, build });
15-
return new application_1.default({
15+
return new Application_1.default({
1616
...staticConfig,
1717
name,
1818
...(version && { version }),
@@ -115,7 +115,7 @@ class ApplicationRegistry {
115115
return (!supportedApplicationVersions ||
116116
(version && supportedApplicationVersions.includes(version)));
117117
})
118-
.map((key) => new executable_1.default({ ...tree[key], name: key }));
118+
.map((key) => new Executable_1.default({ ...tree[key], name: key }));
119119
}
120120
static getExecutableByName(appName, execName) {
121121
const appTree = new standata_1.ApplicationStandata().getAppTreeForApplication(appName);
@@ -125,7 +125,7 @@ class ApplicationRegistry {
125125
const config = execName
126126
? appTree[execName]
127127
: (0, object_1.getOneMatchFromObject)(appTree, "isDefault", true);
128-
return new executable_1.default(config);
128+
return new Executable_1.default(config);
129129
}
130130
// TODO: remove this method and use getApplicationExecutableByName directly
131131
static getExecutableByConfig(appName, config) {
@@ -134,7 +134,7 @@ class ApplicationRegistry {
134134
static getExecutableFlavors(executable) {
135135
const flavorsTree = executable.prop("flavors", {});
136136
return Object.keys(flavorsTree).map((key) => {
137-
return new flavor_1.default({
137+
return new Flavor_1.default({
138138
...flavorsTree[key],
139139
name: key,
140140
});
@@ -146,24 +146,26 @@ class ApplicationRegistry {
146146
static getFlavorByConfig(executable, config) {
147147
return this.getFlavorByName(executable, config === null || config === void 0 ? void 0 : config.name);
148148
}
149-
// flavors
150149
static getInputAsTemplates(flavor) {
151-
const appName = flavor.prop("applicationName", "");
152-
const execName = flavor.prop("executableName", "");
150+
return this.getInput(flavor).map((template) => new Template_1.default(template));
151+
}
152+
static getInput(flavor) {
153+
const appName = flavor.applicationName || "";
154+
const execName = flavor.executableName || "";
153155
return flavor.input.map((input) => {
154156
const inputName = input.templateName || input.name;
155157
const filtered = new standata_1.ApplicationStandata().getTemplatesByName(appName, execName, inputName);
156158
if (filtered.length !== 1) {
157159
console.log(`found ${filtered.length} templates for app=${appName} exec=${execName} name=${inputName} expected 1`);
158160
}
159-
return new template_1.default({ ...filtered[0], name: input.name });
160-
});
161-
}
162-
static getInputAsRenderedTemplates(flavor, context) {
163-
return this.getInputAsTemplates(flavor).map((template) => {
164-
return template.getRenderedJSON(context);
161+
return { ...filtered[0], name: input.name || "" };
165162
});
166163
}
164+
// static getInputAsRenderedTemplates(flavor: Flavor, context: ContextProviderConfig) {
165+
// return this.getInputAsTemplates(flavor).map((template) => {
166+
// return template.setContext(context).render().toJSON();
167+
// });
168+
// }
167169
static getAllFlavorsForApplication(appName, version) {
168170
const allExecutables = this.getExecutables({ name: appName, version });
169171
return allExecutables.flatMap((executable) => this.getExecutableFlavors(executable));

dist/js/applicationMixin.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Defaultable } from "@mat3ra/code/dist/js/entity/mixins/Defaultable
33
import type { NamedEntity } from "@mat3ra/code/dist/js/entity/mixins/NamedEntityMixin";
44
import type { Constructor } from "@mat3ra/code/dist/js/utils/types";
55
import type { ApplicationSchemaBase } from "@mat3ra/esse/dist/js/types";
6-
import Executable from "./executable";
6+
import Executable from "./Executable";
77
import { type ApplicationSchemaMixin } from "./generated/ApplicationSchemaMixin";
88
type Base = InMemoryEntity & NamedEntity & Defaultable;
99
export type BaseConstructor = Constructor<Base> & {

dist/js/generated/TemplateSchemaMixin.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,12 @@ function templateSchemaMixin(item) {
2828
set contextProviders(value) {
2929
this.setProp("contextProviders", value);
3030
},
31-
get isManuallyChanged() {
32-
return this.prop("isManuallyChanged");
33-
},
34-
set isManuallyChanged(value) {
35-
this.setProp("isManuallyChanged", value);
36-
},
37-
get name() {
38-
return this.requiredProp("name");
39-
},
40-
set name(value) {
41-
this.setProp("name", value);
42-
},
4331
get content() {
4432
return this.requiredProp("content");
4533
},
4634
set content(value) {
4735
this.setProp("content", value);
4836
},
49-
get rendered() {
50-
return this.prop("rendered");
51-
},
52-
set rendered(value) {
53-
this.setProp("rendered", value);
54-
},
5537
};
5638
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
5739
}

dist/js/index.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import Application from "./application";
1+
import Application from "./Application";
22
import { applicationMixin } from "./applicationMixin";
33
import ApplicationRegistry from "./ApplicationRegistry";
4-
import ContextProvider from "./context/ContextProvider";
5-
import Executable from "./executable";
4+
import Executable from "./Executable";
65
import { executableMixin } from "./executableMixin";
7-
import Flavor from "./flavor";
6+
import Flavor from "./Flavor";
87
import { flavorMixin } from "./flavorMixin";
9-
import Template from "./template";
8+
import Template from "./Template";
109
import { templateMixin } from "./templateMixin";
1110
declare const allApplications: string[];
12-
export { Application, Executable, Flavor, Template, ApplicationRegistry, ContextProvider, executableMixin, flavorMixin, applicationMixin, templateMixin, allApplications, };
11+
export { Application, Executable, Flavor, Template, ApplicationRegistry, executableMixin, flavorMixin, applicationMixin, templateMixin, allApplications, };
1312
export type * from "./types";

dist/js/index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
exports.allApplications = exports.templateMixin = exports.applicationMixin = exports.flavorMixin = exports.executableMixin = exports.ContextProvider = exports.ApplicationRegistry = exports.Template = exports.Flavor = exports.Executable = exports.Application = void 0;
7-
const application_1 = __importDefault(require("./application"));
8-
exports.Application = application_1.default;
6+
exports.allApplications = exports.templateMixin = exports.applicationMixin = exports.flavorMixin = exports.executableMixin = exports.ApplicationRegistry = exports.Template = exports.Flavor = exports.Executable = exports.Application = void 0;
7+
const Application_1 = __importDefault(require("./Application"));
8+
exports.Application = Application_1.default;
99
const applicationMixin_1 = require("./applicationMixin");
1010
Object.defineProperty(exports, "applicationMixin", { enumerable: true, get: function () { return applicationMixin_1.applicationMixin; } });
1111
const ApplicationRegistry_1 = __importDefault(require("./ApplicationRegistry"));
1212
exports.ApplicationRegistry = ApplicationRegistry_1.default;
13-
const ContextProvider_1 = __importDefault(require("./context/ContextProvider"));
14-
exports.ContextProvider = ContextProvider_1.default;
15-
const executable_1 = __importDefault(require("./executable"));
16-
exports.Executable = executable_1.default;
13+
const Executable_1 = __importDefault(require("./Executable"));
14+
exports.Executable = Executable_1.default;
1715
const executableMixin_1 = require("./executableMixin");
1816
Object.defineProperty(exports, "executableMixin", { enumerable: true, get: function () { return executableMixin_1.executableMixin; } });
19-
const flavor_1 = __importDefault(require("./flavor"));
20-
exports.Flavor = flavor_1.default;
17+
const Flavor_1 = __importDefault(require("./Flavor"));
18+
exports.Flavor = Flavor_1.default;
2119
const flavorMixin_1 = require("./flavorMixin");
2220
Object.defineProperty(exports, "flavorMixin", { enumerable: true, get: function () { return flavorMixin_1.flavorMixin; } });
23-
const template_1 = __importDefault(require("./template"));
24-
exports.Template = template_1.default;
21+
const Template_1 = __importDefault(require("./Template"));
22+
exports.Template = Template_1.default;
2523
const templateMixin_1 = require("./templateMixin");
2624
Object.defineProperty(exports, "templateMixin", { enumerable: true, get: function () { return templateMixin_1.templateMixin; } });
2725
const allApplications = ApplicationRegistry_1.default.getUniqueAvailableApplicationNames();

dist/js/templateMixin.d.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
11
import type { InMemoryEntity } from "@mat3ra/code/dist/js/entity";
22
import type { Constructor } from "@mat3ra/code/dist/js/utils/types";
3-
import type { AnyObject } from "@mat3ra/esse/dist/js/esse/types";
4-
import type { ContextProviderNameEnum, ContextProviderSchema, TemplateSchema } from "@mat3ra/esse/dist/js/types";
5-
import ContextProvider from "./context/ContextProvider";
6-
import ContextProviderRegistryContainer from "./context/ContextProviderRegistryContainer";
3+
import type { TemplateSchema } from "@mat3ra/esse/dist/js/types";
74
import { type TemplateSchemaMixin } from "./generated/TemplateSchemaMixin";
85
export type TemplateBase = InMemoryEntity;
9-
export type TemplateMixin = TemplateSchemaMixin & {
10-
render: (externalContext?: Record<string, unknown>) => void;
11-
getRenderedJSON: (context?: Record<string, unknown>) => AnyObject;
12-
_cleanRenderingContext: (object: Record<string, unknown>) => Record<string, unknown>;
13-
getDataFromProvidersForRenderingContext: (context?: Record<string, unknown>) => Record<string, unknown>;
14-
setContent: (text: string) => void;
15-
setRendered: (text: string) => void;
16-
getContextProvidersAsClassInstances: (providerContext?: Record<string, unknown>) => ContextProvider[];
17-
getDataFromProvidersForPersistentContext: (providerContext?: Record<string, unknown>) => Record<string, unknown>;
18-
getRenderingContext: (externalContext?: Record<string, unknown>) => Record<string, unknown>;
19-
};
20-
export type ContextProviderConfigMapEntry = {
21-
providerCls: typeof ContextProvider;
22-
config: ContextProviderSchema;
23-
};
24-
export type ContextProviderConfigMap = Partial<Record<ContextProviderNameEnum, ContextProviderConfigMapEntry>>;
6+
export type TemplateMixin = TemplateSchemaMixin;
257
export type TemplateStaticMixin = {
26-
contextProviderRegistry: ContextProviderRegistryContainer | null;
27-
setContextProvidersConfig: (classConfigMap: ContextProviderConfigMap) => void;
288
jsonSchema: TemplateSchema;
299
};
3010
export declare function templateStaticMixin(item: Constructor<TemplateBase>): void;

dist/js/templateMixin.js

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -6,146 +6,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
66
exports.templateStaticMixin = templateStaticMixin;
77
exports.templateMixin = templateMixin;
88
const JSONSchemasInterface_1 = __importDefault(require("@mat3ra/esse/dist/js/esse/JSONSchemasInterface"));
9-
const utils_1 = require("@mat3ra/utils");
10-
const nunjucks_1 = __importDefault(require("nunjucks"));
11-
const ContextProviderRegistryContainer_1 = __importDefault(require("./context/ContextProviderRegistryContainer"));
129
const TemplateSchemaMixin_1 = require("./generated/TemplateSchemaMixin");
13-
function templatePropertiesMixin(item) {
14-
// @ts-expect-error
15-
const properties = {
16-
setContent(text) {
17-
this.content = text;
18-
if (!this.rendered) {
19-
this.rendered = text;
20-
}
21-
},
22-
setRendered(text) {
23-
this.rendered = text;
24-
},
25-
// addContextProvider(provider: ContextProvider) {
26-
// this.setProp("contextProviders", [...this.contextProviders, provider]);
27-
// },
28-
// removeContextProvider(provider: ContextProvider) {
29-
// const contextProviders = this.contextProviders.filter((p) => {
30-
// return p.name !== provider.name && p.domain !== provider.domain;
31-
// });
32-
// this.setProp("contextProviders", contextProviders);
33-
// },
34-
render(externalContext) {
35-
const renderingContext = this.getRenderingContext(externalContext);
36-
if (!this.isManuallyChanged) {
37-
try {
38-
const template = nunjucks_1.default.compile(this.content);
39-
// deepClone to pass JSON data without classes
40-
const rendered = template.render(this._cleanRenderingContext(renderingContext));
41-
this.setRendered(this.isManuallyChanged ? rendered : rendered || this.content);
42-
}
43-
catch (e) {
44-
console.log(`Template is not compiled: ${e}`);
45-
console.log({
46-
content: this.content,
47-
_cleanRenderingContext: this._cleanRenderingContext(renderingContext),
48-
});
49-
}
50-
}
51-
},
52-
getRenderedJSON(context) {
53-
this.render(context);
54-
return this.toJSON();
55-
},
56-
// Remove "bulky" items and JSON stringify before passing it to rendering engine (eg. jinja) to compile.
57-
// This way the context should still be passed in full to contextProviders, but not to final text template.
58-
// eslint-disable-next-line class-methods-use-this
59-
_cleanRenderingContext(object) {
60-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
61-
const { job, ...clone } = object;
62-
return utils_1.Utils.clone.deepClone(clone);
63-
},
64-
/*
65-
* @summary Initializes context provider class instances. `providerContext` is used to pass the data about any
66-
* previously stored values. That is if data was previously saved in database, the context provider
67-
* shall receive it on initialization through providerContext and prioritize this value over the default.
68-
*/
69-
getContextProvidersAsClassInstances(providerContext) {
70-
return this.contextProviders.map((p) => {
71-
var _a;
72-
const providerInstance = (_a = this.constructor.contextProviderRegistry) === null || _a === void 0 ? void 0 : _a.findProviderInstanceByName(p.name);
73-
if (!providerInstance) {
74-
throw new Error(`Provider ${p.name} not found`);
75-
}
76-
const clsInstance = new providerInstance.constructor({
77-
...providerInstance.config,
78-
context: providerContext,
79-
});
80-
return clsInstance;
81-
});
82-
},
83-
/*
84-
* @summary Extracts the the data from all context providers for further use during render.
85-
*/
86-
getDataFromProvidersForRenderingContext(providerContext) {
87-
const result = {};
88-
this.getContextProvidersAsClassInstances(providerContext).forEach((contextProvider) => {
89-
const context = contextProvider.yieldDataForRendering();
90-
Object.keys(context).forEach((key) => {
91-
// merge context keys if they are objects otherwise override them.
92-
result[key] =
93-
result[key] !== null && typeof result[key] === "object"
94-
? // @ts-ignore
95-
{ ...result[key], ...context[key] }
96-
: context[key];
97-
});
98-
});
99-
return result;
100-
},
101-
/*
102-
* @summary Extracts the the data from all context providers for further save in persistent context.
103-
*/
104-
// TODO: optimize logic to prevent re-initializing the context provider classes again below, reuse above function
105-
getDataFromProvidersForPersistentContext(providerContext) {
106-
const result = {};
107-
this.getContextProvidersAsClassInstances(providerContext).forEach((contextProvider) => {
108-
// only save in the persistent context the data from providers that were edited (or able to be edited)
109-
Object.assign(result, contextProvider.isEdited ? contextProvider.yieldData() : {});
110-
});
111-
return result;
112-
},
113-
/*
114-
* @summary Combines rendering context (in order of preference):
115-
* - context from templates initialized with external context
116-
* - "external" context and
117-
*/
118-
getRenderingContext(externalContext) {
119-
return {
120-
...externalContext,
121-
...this.getDataFromProvidersForRenderingContext(externalContext),
122-
};
123-
},
124-
};
125-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
126-
}
12710
function templateStaticMixin(item) {
12811
// @ts-ignore
12912
const properties = {
130-
contextProviderRegistry: null,
13113
get jsonSchema() {
13214
return JSONSchemasInterface_1.default.getSchemaById("software/template");
13315
},
134-
setContextProvidersConfig(classConfigMap) {
135-
const contextProviderRegistry = new ContextProviderRegistryContainer_1.default();
136-
Object.entries(classConfigMap).forEach(([name, { providerCls, config }]) => {
137-
contextProviderRegistry.addProvider({
138-
instance: providerCls.getConstructorConfig(config),
139-
name,
140-
});
141-
});
142-
this.contextProviderRegistry = contextProviderRegistry;
143-
},
14416
};
14517
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
14618
}
14719
function templateMixin(Item) {
14820
(0, TemplateSchemaMixin_1.templateSchemaMixin)(Item.prototype);
149-
templatePropertiesMixin(Item.prototype);
15021
templateStaticMixin(Item);
15122
}

0 commit comments

Comments
 (0)