@@ -6,146 +6,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
66exports . templateStaticMixin = templateStaticMixin ;
77exports . templateMixin = templateMixin ;
88const 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" ) ) ;
129const 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- }
12710function 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}
14719function templateMixin ( Item ) {
14820 ( 0 , TemplateSchemaMixin_1 . templateSchemaMixin ) ( Item . prototype ) ;
149- templatePropertiesMixin ( Item . prototype ) ;
15021 templateStaticMixin ( Item ) ;
15122}
0 commit comments