Skip to content

Commit 400c02e

Browse files
committed
rename cls to class_name
and remove unused findSlotNamesForClass()
1 parent a3f9fdb commit 400c02e

File tree

3 files changed

+57
-80
lines changed

3 files changed

+57
-80
lines changed

lib/AppContext.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { DataHarmonizer } from '.';
55
import { findLocalesForLangcodes } from './utils/i18n';
66
import {
77
Template,
8-
findSlotNamesForClass,
98
getTemplatePathInScope,
109
} from '../lib/utils/templates';
1110
import { wait } from '../lib/utils/general';
@@ -158,13 +157,13 @@ function updateWithChildrenAndSharedKeys(data) {
158157
* @returns {Object|null} The schema tree object, or null if no "Container" classes are found.
159158
*/
160159
function buildSchemaTree(schema) {
161-
const isContainerMissing = typeof schema.classes['Container'] === 'undefined';
162160

163161
const classNames = Object.keys(schema.classes).filter(
164-
(key) => key !== 'dh_interface' && key !== 'Container'
162+
(key) => !(key == 'dh_interface' || key == 'Container')
165163
);
166164

167-
if (isContainerMissing) {
165+
if (!('Container' in schema.classes)) {
166+
// FIX THIS to pay attention to class foreign keys
168167
return createFlatSchemaTree(classNames);
169168
}
170169

@@ -399,11 +398,7 @@ export default class AppContext {
399398
});
400399
});
401400
}
402-
/*
403-
getExportFormats() {
404-
return this.export_formats;
405-
}
406-
*/
401+
407402
clearContext() {
408403
this.schema_tree = {};
409404
this.dhs = {};
@@ -432,7 +427,9 @@ export default class AppContext {
432427
makeDataHarmonizersFromSchemaTree(schema, template_name) {
433428
let data_harmonizers = {};
434429
const template = this.appConfig.template_path.split('/')[1];
430+
435431
console.log("SCHEMA TREE", this.schema_tree)
432+
436433
Object.entries(this.schema_tree)
437434
// Container class is only used in input and output file coordination.
438435
.filter(([class_name]) => class_name !== 'Container')
@@ -464,9 +461,10 @@ export default class AppContext {
464461
// NOTE: this may be running twice?.
465462
// in 1-M, different DataHarmonizers have fewer rows to start with
466463
// and visible if a child. Override the default HoT settings.
467-
data_harmonizers[class_name] = new DataHarmonizer(dhSubroot, this, {
464+
const dh = new DataHarmonizer(dhSubroot, this, {
468465
loadingScreenRoot: document.body,
469466
class_assignment: class_name,
467+
schema: schema, // assign during constructor so validator can init on it.
470468
hot_override_settings: {
471469
minRows: is_child ? 0 : 100,
472470
minSpareRows: is_child ? 0 : 100,
@@ -477,12 +475,15 @@ export default class AppContext {
477475
},
478476
});
479477

480-
data_harmonizers[class_name].useSchema(schema, template_name);
478+
data_harmonizers[class_name] = dh;
479+
dh.useTemplate(class_name);
480+
dh.validator.useTargetClass(class_name);
481+
481482

482483
// Initialization of each child table is to hide all rows until
483484
// parent primary key record is selected as a foreign key index.
484485
if (is_child) {
485-
data_harmonizers[class_name].filterAll();
486+
dh.filterAll();
486487
}
487488
}
488489
}

lib/DataHarmonizer.js

+35-47
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import i18next from 'i18next';
99
import { utils as XlsxUtils, read as xlsxRead } from 'xlsx/xlsx.js';
1010
import { renderContent, urlToClickableAnchor } from './utils/content';
1111
import { readFileAsync, updateSheetRange } from '../lib/utils/files';
12-
import { findSlotNamesForClass } from '../lib/utils/templates';
12+
//import { findSlotNamesForClass } from '../lib/utils/templates';
1313
import {
1414
isValidHeaderRow,
1515
rowIsEmpty,
@@ -38,7 +38,6 @@ import {
3838
itemCompare,
3939
validateValAgainstVocab,
4040
validateValsAgainstVocab,
41-
// validateUniqueValues,
4241
} from './utils/validation';
4342

4443
import 'handsontable/dist/handsontable.full.css';
@@ -116,7 +115,8 @@ class DataHarmonizer {
116115

117116
this.class_assignment = options.class_assignment || null;
118117
this.field_settings = options.fieldSettings || {};
119-
this.slot_names = options.slot_names || [];
118+
this.slots = null;
119+
this.slot_names = null;
120120
this.hot_override_settings = options.hot_override_settings || {};
121121

122122
this.helpSidebarOptions = Object.assign(
@@ -187,20 +187,9 @@ class DataHarmonizer {
187187
this.hot.render();
188188
}
189189

190-
useSchema(schema, template_name) {
191-
console.log("useSchema", template_name)
192-
this.schema = schema;
193-
this.validator = new Validator(this.schema, MULTIVALUED_DELIMITER, {
194-
dateFormat: this.dateFormat,
195-
datetimeFormat: this.datetimeFormat,
196-
timeFormat: this.timeFormat,
197-
});
198-
199-
this.useTemplate(template_name);
200-
}
201190

202191
/**
203-
* Revise user interface elements to match template name
192+
* Revise user interface elements to match template content
204193
*
205194
* @param {String} template_name: name of template within current schema
206195
*/
@@ -221,8 +210,8 @@ class DataHarmonizer {
221210
)
222211
.filter(
223212
([cls_key]) =>
224-
cls_key !== 'dh_interface' &&
225-
cls_key !== 'Container' &&
213+
//cls_key !== 'dh_interface' &&
214+
//cls_key !== 'Container' &&
226215
cls_key === this.class_assignment
227216
)
228217
.reduce((acc, [, spec]) => {
@@ -498,15 +487,35 @@ class DataHarmonizer {
498487
this.sections[ptr]['children'].sort((a, b) => a.rank - b.rank);
499488
}
500489

501-
490+
// Easy lookup arrays.
502491
this.slots = this.getFields();
503-
this.slot_names = findSlotNamesForClass(this.context.template.default.schema, template_name),
504-
//console.log(this.slot_names)
505-
this.validator.useTargetClass(this.class_assignment);
492+
this.slot_names = this.slots.map((field) => field.name);
506493

507494
this.createHot();
508495
}
509496

497+
498+
/**
499+
* Get a flat array of all fields (slot definitions) in TEMPLATE.
500+
* @return {Array<Object>} Array of all objects under `children` in TEMPLATE.
501+
*/
502+
getFields() {
503+
let fields = Array.prototype.concat.apply(
504+
[],
505+
this.sections.map((section) => section.children)
506+
);
507+
508+
/* NOTE: Possibly a result of attempting to add other slots besides one in
509+
* template.
510+
511+
fields =
512+
this.slot_names.length > 0
513+
? fields.filter((field) => this.slot_names.includes(field.name))
514+
: fields;
515+
*/
516+
return fields;
517+
}
518+
510519
/**
511520
* Open file specified by user.
512521
* Only opens `xlsx`, `xlsx`, `csv` and `tsv` files. Will launch the specify
@@ -960,10 +969,12 @@ class DataHarmonizer {
960969
this.hideColumns(hiddenColumns);
961970
}
962971

972+
/*
963973
showColumnsByNames(names) {
964974
if (names.length > 0)
965975
this._updateVisibility((field) => !names.includes(field.name));
966976
}
977+
*/
967978

968979
showRequiredColumns() {
969980
this._updateVisibility((field) => !field.required);
@@ -1630,25 +1641,6 @@ class DataHarmonizer {
16301641
};
16311642
}
16321643

1633-
/**
1634-
* Get a flat array of all fields (slot definitions) in TEMPLATE.
1635-
* @return {Array<Object>} Array of all objects under `children` in TEMPLATE.
1636-
*/
1637-
getFields() {
1638-
let fields = Array.prototype.concat.apply(
1639-
[],
1640-
this.sections.map((section) => section.children)
1641-
);
1642-
1643-
fields =
1644-
this.slot_names.length > 0
1645-
? fields.filter((field) => this.slot_names.includes(field.name))
1646-
: fields;
1647-
1648-
console.log("FILTERS", this.slot_names)
1649-
console.log("FIELDS", fields )
1650-
return fields;
1651-
}
16521644

16531645
/**
16541646
* Create a matrix containing the nested headers supplied to Handsontable.
@@ -2184,7 +2176,6 @@ class DataHarmonizer {
21842176
...options,
21852177
};
21862178
const listData = this.hot.getData();
2187-
//const fields = this.getFields();
21882179
const arrayOfObjects = listData
21892180
.filter((row) => !rowIsEmpty(row))
21902181
.map((row) =>
@@ -2689,11 +2680,8 @@ class DataHarmonizer {
26892680
* @return {Array<Array<String>>} Modified matrix.
26902681
*/
26912682
matrixFieldChangeRules(matrix) {
2692-
//const fields = this.slots;
2693-
Object.entries(this.slots).forEach((field, col) => {
26942683

2695-
//for (let col = 0; col < this.slots.length; col++) {
2696-
// const field = fields[col];
2684+
Object.entries(this.slots).forEach((field, col) => {
26972685

26982686
// Test field against capitalization change.
26992687
if (field.capitalize) {
@@ -2877,8 +2865,8 @@ class DataHarmonizer {
28772865
* `{0: {0: 'Required cells cannot be empty'}}`
28782866
*/
28792867
getInvalidCells(data) {
2880-
const fieldNames = this.getFields().map((field) => field.name);
2881-
return this.validator.validate(data, fieldNames);
2868+
//const fieldNames = this.slots.map((field) => field.name);
2869+
return this.validator.validate(data, this.slot_names); // fieldNames);
28822870
}
28832871

28842872
/**

lib/utils/templates.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,25 @@ class TemplateProxy {
220220
async _init(template, locale = null) {
221221

222222
// Handling case where DataHarmonizer < 2.0 schema is being loaded.
223-
// 1 tab per dh_interface case here.
223+
// Dynamically adding a Container class, with 1 tabular holding
224+
// class for each underlying class within schema classes (in range).
224225
if (typeof template.default.schema.classes['Container'] === 'undefined') {
225226
let fake_container = {
226227
name: 'Container',
227228
from_schema: template.default.schema.id,
228229
attributes: {},
229230
};
230231

231-
const class_names = Object.keys(template.default.schema.classes).filter(
232-
(key) => key !== 'dh_interface'
233-
);
232+
const class_names = Object.keys(template.default.schema.classes)
233+
.filter((key) => key !== 'dh_interface');
234234

235-
class_names.forEach((cls) => {
236-
fake_container.attributes[cls] = {
235+
class_names.forEach((class_name) => {
236+
fake_container.attributes[class_name] = {
237237
owner: 'Container',
238238
domain_of: ['Container'],
239-
name: cls,
240-
range: cls,
241-
alias: cls,
239+
name: class_name+'s', // plural
240+
range: class_name,
241+
//alias: class_name,
242242
from_schema: template.default.schema.id,
243243
multivalued: false,
244244
inlined_as_list: true,
@@ -321,18 +321,6 @@ export const rangeToContainerClass = (Container, cls_key) => {
321321
}
322322
};
323323

324-
/**
325-
* Finds the slot names for a given class within the schema.
326-
* @param {Object} schema - The schema object containing "classes".
327-
* @param {string} class_name - The name of the class to search for slot names.
328-
* @returns {Array} An array of slot names.
329-
*/
330-
export function findSlotNamesForClass(schema, class_name) {
331-
return Object.keys(schema.classes[class_name].slot_usage).map((field) => {
332-
return schema.classes[class_name].slot_usage[field].name;
333-
});
334-
}
335-
336324
// Usage example:
337325
// const proxyInstance = await TemplateProxy.create('template_name', 'en-US');
338326
// proxyInstance.updateLocale('fr-FR'); // IETF language code designations

0 commit comments

Comments
 (0)