diff --git a/lib/Toolbar.js b/lib/Toolbar.js index 92806d56..7b82e520 100644 --- a/lib/Toolbar.js +++ b/lib/Toolbar.js @@ -373,6 +373,10 @@ class Toolbar { ); } else { if (ext === 'json') { + // JSON is the only format that contains reference to schema to + // utilize by "schema" URI, as well as "in_language" locale, and + // its Container class contains template spec itself. + // It may have several dhs, one for each Container class mentioned. const contentBuffer = await readFileAsync(file); let jsonData; @@ -382,8 +386,7 @@ class Toolbar { throw new Error('Invalid JSON data', error); } - const { schema, in_language = null } = importJsonFile(jsonData); - + const { schema_uri, in_language = null} = importJsonFile(jsonData); const translationSelect = $('#select-translation-localization'); const previous_language = i18next.language; // ensure is localized in the same language as the file @@ -398,15 +401,20 @@ class Toolbar { $(document).localize(); } - const template_path = await templatePathForSchemaURI(schema); + // If we're loading a JSON file, we have to match its schema_uri to a schema. Check loaded schema, + // but if not there, lookup in menu. Then if provided, load schema in_language locale. + // Future: automate detection of json file schema via menu datastructure. + // In which case template_path below has to be changed to match. - if (!template_path) { - alert(`The schema ${schema} cannot be found within the manifest.`); + // Currently loaded schema_uri: this.context.template.default.schema.id + if (schema_uri != this.context.template.default.schema.id) { + alert(`The current json file's schema "${schema_uri}" is required, but one must select this template from menu first, if available. Online retrieval is not yet available.`); return false; } const locale = in_language; console.log('reload 2: openfile'); + const template_path = this.context.appConfig.template_path; // e.g. canada_covid19/CanCOGeN_Covid-19 await this.context .reload(template_path, { locale }) .then((context) => { @@ -415,8 +423,7 @@ class Toolbar { 'Error: JSON data file does not have Container dictionary.' ); } else { - // NOTE: the data is possibly *sparse*. where does it make sense to fill in its missing alues? - // It doesn't appear to matter + // The data is possibly *sparse*. loadDataObjects() fills in missing values. for (const dh in context.dhs) { const container_class = rangeToContainerClass( context.template.default.schema.classes.Container, @@ -858,32 +865,34 @@ class Toolbar { // TODO: implement template_path, schema_name to make the loader types consistent // The "local" part is a fake folder but is used in AppContext.js reload() template_path: `local/${template_name}`, - //schema_name: template_name.toLowerCase(), - schema_name: schema.name, - template_name, + //schema_name: template_name.toLowerCase(), + //schema_name: schema.name, + //schema_uri: schema.uri, + //template_name, schema, // No access to export.js export formats - exportFormats: {}, + //exportFormats: {}, // ISSUE: we don't have access to locale files via a file upload of a // single schema, so override schema's language selection. - languages: [], + //languages: [], }; }; const loadFromMenu = async () => { const template_path = this.$selectTemplate.val(); - const [schema_name, template_name] = template_path.split('/'); - const schema = await this.getSchema(schema_name, template_name); - const exportFormats = await this.getExportFormats( + const [schema_folder, template_name] = template_path.split('/'); + const schema = await this.getSchema(schema_folder, template_name); + /*const exportFormats = await this.getExportFormats( schema_name, template_name - ); + );*/ return { template_path, - schema_name, - template_name, + //schema_name, + //schema_uri: schema.uri, + //template_name, schema, - exportFormats, + //exportFormats, }; }; diff --git a/lib/utils/templates.js b/lib/utils/templates.js index abb686a3..8071f0f3 100644 --- a/lib/utils/templates.js +++ b/lib/utils/templates.js @@ -20,30 +20,6 @@ export function getTemplatePathInScope() { return templatePath; } -export async function templatePathForSchemaURI(schemaURI) { - // resolve the schema URI - // for now, this is just the manifest - for (let i = 0; i < template_manifest.children.length; i++) { - const template = template_manifest.children[i]; - if ( - typeof template.children !== 'undefined' && - template.children.length > 0 && - template.children.some((c) => c.name.includes('schema.json')) - ) { - const schema = await getSchema(template.name); - if (schema.id === schemaURI) { - const templatePath = `${template.path.split('/').slice(-1)}/${schemaURI - .split('/') - .slice(-1)}`; - return templatePath; - } else { - continue; - } - } - } - return null; -} - const isSchema = (el) => el.name === 'schema.json'; const isDocumentation = (el) => el.extension === '.md'; const isLocale = (el) => el.name === 'locales';