1
1
import { deepMerge } from './objects' ;
2
2
import { fetchFileAsync } from './files' ;
3
- //import template_manifest from '../../web/templates/manifest.json';
4
3
import { menu , getSchema } from 'schemas' ;
5
4
6
5
@@ -24,28 +23,12 @@ export function getTemplatePathInScope() {
24
23
return templatePath ;
25
24
}
26
25
27
- const isSchema = ( el ) => el . name === 'schema.json' ;
28
- const isLocale = ( el ) => el . name === 'locales' ;
29
-
30
26
const schemaFromChildPath = ( childPath ) =>
31
27
childPath . replace ( / \/ t e m p l a t e s \/ ( .+ ) \/ s c h e m a .j s o n / , '$1' ) ;
32
28
33
- // Actually this is accessSchema - and not language variant.
34
-
35
- // Returns default template built from schema
36
- export async function accessTemplate ( schema_folder ) { // e.g. canada_covid19
37
-
38
- const template = template_manifest . children . find (
39
- ( el ) => el . name === schema_folder
40
- ) ;
41
- console . log ( "template from manifest" , template )
42
- return template ? processNode ( template ) : null ;
43
- }
44
-
45
29
// Returns default template built from schema
46
30
async function compileSchema ( schema_folder ) { // e.g. canada_covid19
47
31
48
- //////////////////////////////////////////////////
49
32
for ( const [ schema_name , schema_obj ] of Object . entries ( menu ) ) {
50
33
if ( schema_obj . folder === schema_folder ) {
51
34
var schema = await fetchSchema ( `/templates/${ schema_folder } /schema.json` ) ;
@@ -86,134 +69,6 @@ async function fetchSchema(path) {
86
69
return null ;
87
70
88
71
}
89
- /*
90
- // processNode() only used here.
91
- async function processNode(node) {
92
- const { name, children } = node;
93
-
94
- // Base case
95
- if (!children || children.length === 0) {
96
- return [];
97
- }
98
-
99
- let schema = [];
100
- let locales = [];
101
-
102
- await Promise.all(
103
- children.map(async (child) => {
104
- if (isSchema(child)) {
105
- let schemaData = null;
106
- // Running locally so get schema from schemas.json
107
- if (window.location.protocol.startsWith('file')) {
108
- // child.path e.g. /templates/SDRF/schema.json
109
- schemaData = await getSchema(schemaFromChildPath(child.path));
110
- } else if (window.location.protocol.startsWith('http')) {
111
- schemaData = await fetchFileAsync(child.path);
112
- }
113
-
114
- schema.push(schemaData);
115
- } else if (isLocale(child)) {
116
- const processedLocale = await processNode(child);
117
- if (processedLocale.length) {
118
- locales.push(processedLocale);
119
- }
120
- }
121
- else {
122
- const processedChild = await processNode(child);
123
- // CRITICAL FOR LOCAL HIERARCHY SCAN
124
- if (processedChild.length) {
125
- locales.push(processedChild);
126
- }
127
- }
128
- })
129
- );
130
-
131
- const folded_template_spec = [name, [schema], locales];
132
- console.log(folded_template_spec);
133
- return folded_template_spec;
134
- }
135
-
136
- // This returns template from input [[][][]]; and nothing else
137
- export function buildTemplate(input) {
138
- // Check if the input format is correct
139
- if (!Array.isArray(input) || input.length !== 3) {
140
- throw new Error('Invalid input format. Expected an array of 3 elements.');
141
- }
142
-
143
- let [name, innerArray, childFolders] = input;
144
-
145
- // Check the innerArray format
146
- if (!Array.isArray(innerArray) || innerArray.length !== 1) {
147
- throw new Error(
148
- 'Invalid format for the second parameter. Expected an array of 2 elements.'
149
- );
150
- }
151
-
152
- const [schemas] = innerArray;
153
-
154
- if (!Array.isArray(schemas)) {
155
- throw new Error('Schemas must be arrays.');
156
- }
157
-
158
- const schema = schemas[0];
159
-
160
- if (!Array.isArray(childFolders) || childFolders.length === 0) {
161
- childFolders = [[], [], []];
162
- }
163
-
164
- const locales_folder = childFolders.filter((ln) => ln[0] === 'locales')[0];
165
- let locales = [];
166
- if (typeof locales_folder !== 'undefined') {
167
- try {
168
- if (Array.isArray(locales_folder) && locales_folder.length > 2) {
169
- locales = locales_folder[2];
170
- } else {
171
- throw new Error(
172
- 'locales_folder is not an array or does not have enough elements.'
173
- );
174
- }
175
- } catch (err) {
176
- throw new Error(`Error extracting locales from childFolders: ${err}`);
177
- }
178
-
179
- if (!Array.isArray(locales)) {
180
- throw new Error('Locales is not an array.');
181
- }
182
- }
183
-
184
- let template = {
185
- name,
186
- default: {
187
- schema
188
- },
189
- locales: {},
190
- };
191
-
192
- try {
193
- template.locales = locales.reduce((acc, localization) => {
194
- if (!Array.isArray(localization) || localization.length < 3) {
195
- throw new Error(
196
- 'Invalid localization format. Expected an array with at least 3 elements.'
197
- );
198
- }
199
- const locale_label = localization[0];
200
- const schemaMerge =
201
- localization[1][0] && localization[1][0][0]
202
- ? deepMerge(schemas[0], localization[1][0][0])
203
- : null;
204
- return Object.assign(acc, {
205
- [locale_label]: {
206
- schema: schemaMerge
207
- },
208
- });
209
- }, {});
210
- } catch (err) {
211
- throw new Error('Error processing locales. ' + err.message);
212
- }
213
-
214
- return template;
215
- }
216
- */
217
72
218
73
//Used only in this script in create()
219
74
function buildTemplateFromUploadedSchema ( schema ) {
@@ -312,16 +167,13 @@ class TemplateProxy {
312
167
313
168
let template ;
314
169
if ( forced_schema !== null ) {
315
- // returns {name:..., default: {schema}, locales: {[locale]:{ schema}} }
316
170
template = buildTemplateFromUploadedSchema ( forced_schema ) ;
317
171
} else {
318
- // access schema_folder from directory in the manifest
319
- //const fetchedTemplateData = await accessTemplate(schema_folder);
320
- //template = buildTemplate(fetchedTemplateData);
321
-
172
+ // Access schema_folder via menu content
322
173
template = await compileSchema ( schema_folder ) ;
323
174
}
324
175
176
+ // template = {name:..., default: {schema}, locales: {[locale]:{ schema}}}
325
177
await instance . _init ( template , locale ) ;
326
178
327
179
// NOTE: Why Proxy? it allows us to call all of the template's properties in an "inherited" fashion
@@ -490,7 +342,5 @@ export const Template = TemplateProxy;
490
342
export default {
491
343
TemplateProxy,
492
344
Template,
493
- //accessTemplate,
494
- //buildTemplate,
495
345
findBestLocaleMatch,
496
346
} ;
0 commit comments