Skip to content

Commit

Permalink
enable .json file to be loaded
Browse files Browse the repository at this point in the history
without relying on templatePathForSchemaURI() which is deprecated.  Part of plan to remove manifest.json
  • Loading branch information
ddooley committed Jan 18, 2025
1 parent b52340f commit 0f40301
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
47 changes: 28 additions & 19 deletions lib/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
};
};

Expand Down
24 changes: 0 additions & 24 deletions lib/utils/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 0f40301

Please sign in to comment.