From ac4730a56d7af9eaf379bb5a52d8578b34e369a4 Mon Sep 17 00:00:00 2001 From: Garrett Date: Thu, 2 Nov 2023 10:04:29 -0700 Subject: [PATCH 1/4] Allow for enumerable values to have arbitrary labels specified --- schemas/base-metadata.schema.ts | 8 ++++++++ src/renderer/src/stories/JSONSchemaInput.js | 2 +- src/renderer/src/stories/Table.js | 21 ++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/schemas/base-metadata.schema.ts b/schemas/base-metadata.schema.ts index 87d237af9..e68ac1921 100644 --- a/schemas/base-metadata.schema.ts +++ b/schemas/base-metadata.schema.ts @@ -5,6 +5,14 @@ export const preprocessMetadataSchema = (schema: any = baseMetadataSchema) => { // Add unit to weight schema.properties.Subject.properties.weight.unit = 'kg' + schema.properties.Subject.properties.sex.enumLabels = { + O: 'Other', + M: 'Male', + F: 'Female', + U: 'Unknown' + } + + // Override description of keywords schema.properties.NWBFile.properties.keywords.description = 'Terms to describe your dataset (e.g. Neural circuits, V1, etc.)' // Add description to keywords return schema diff --git a/src/renderer/src/stories/JSONSchemaInput.js b/src/renderer/src/stories/JSONSchemaInput.js index 753aeef26..0344f585d 100644 --- a/src/renderer/src/stories/JSONSchemaInput.js +++ b/src/renderer/src/stories/JSONSchemaInput.js @@ -335,7 +335,7 @@ export class JSONSchemaInput extends LitElement { > ${info.enum.map( - (item, i) => html`` + (item, i) => html`` )} `; diff --git a/src/renderer/src/stories/Table.js b/src/renderer/src/stories/Table.js index 968d3577f..f2169d536 100644 --- a/src/renderer/src/stories/Table.js +++ b/src/renderer/src/stories/Table.js @@ -215,6 +215,14 @@ export class Table extends LitElement { const displayHeaders = [...colHeaders].map(header); + const getValue = (value, colInfo) => { + + // Handle enums + if (colInfo.enumLabels) return Object.keys(colInfo.enumLabels).find((k) => colInfo.enumLabels[k] === value) ?? value; + + return value; + }; + const columns = colHeaders.map((k, i) => { const info = { type: "text" }; @@ -223,7 +231,7 @@ export class Table extends LitElement { // Enumerate Possible Values if (colInfo.enum) { - info.source = colInfo.enum; + info.source = colInfo.enumLabels ? Object.values(colInfo.enumLabels) : colInfo.enum; if (colInfo.strict === false) info.type = "autocomplete"; else info.type = "dropdown"; } @@ -265,7 +273,12 @@ export class Table extends LitElement { let ogThis = this; const isRequired = this.isRequired(k); + const validator = async function (value, callback) { + + value = getValue(value, colInfo) + console.log(value) + if (!value) { if (!ogThis.validateEmptyCells) { ogThis.#handleValidationResult( @@ -409,6 +422,9 @@ export class Table extends LitElement { const isUserUpdate = initialCellsToUpdate <= validated; + value = getValue(value, entries[header]) + + // Transfer data to object if (header === this.keyColumn) { if (value && value !== rowName) { @@ -422,6 +438,9 @@ export class Table extends LitElement { // Update data on passed object else { + + + const globalValue = this.globals[header]; if (value == undefined || value === "") { From 0d90acf78c43bf394114ff05d1417106696c76f7 Mon Sep 17 00:00:00 2001 From: Garrett Date: Thu, 2 Nov 2023 10:12:36 -0700 Subject: [PATCH 2/4] Update Table.js --- src/renderer/src/stories/Table.js | 40 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/renderer/src/stories/Table.js b/src/renderer/src/stories/Table.js index f2169d536..b38115f57 100644 --- a/src/renderer/src/stories/Table.js +++ b/src/renderer/src/stories/Table.js @@ -215,14 +215,6 @@ export class Table extends LitElement { const displayHeaders = [...colHeaders].map(header); - const getValue = (value, colInfo) => { - - // Handle enums - if (colInfo.enumLabels) return Object.keys(colInfo.enumLabels).find((k) => colInfo.enumLabels[k] === value) ?? value; - - return value; - }; - const columns = colHeaders.map((k, i) => { const info = { type: "text" }; @@ -276,7 +268,7 @@ export class Table extends LitElement { const validator = async function (value, callback) { - value = getValue(value, colInfo) + value = ogThis.#getValue(value, colInfo) console.log(value) if (!value) { @@ -375,8 +367,10 @@ export class Table extends LitElement { if (this.table) this.table.destroy(); + console.log('Rendered data', this.#getRenderedData(data)) + const table = new Handsontable(div, { - data, + data: this.#getRenderedData(data), // rowHeaders: rowHeaders.map(v => `sub-${v}`), colHeaders: displayHeaders, columns, @@ -422,8 +416,7 @@ export class Table extends LitElement { const isUserUpdate = initialCellsToUpdate <= validated; - value = getValue(value, entries[header]) - + value = this.#getValue(value, entries[header]) // Transfer data to object if (header === this.keyColumn) { @@ -492,8 +485,29 @@ export class Table extends LitElement { data.forEach((row, i) => this.#setRow(i, row)); } + #getRenderedValue = (value, colInfo) => { + // Handle enums + if (colInfo.enumLabels) return colInfo.enumLabels[value] ?? value; + return value; + } + + #getRenderedData = (data) => { + return Object.values(data).map((row) => row.map((value, j) => this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]))) + } + + #getValue = (value, colInfo) => { + + // Handle enums + if (colInfo.enumLabels) return Object.keys(colInfo.enumLabels).find((k) => colInfo.enumLabels[k] === value) ?? value; + + return value; + }; + #setRow(row, data) { - data.forEach((value, j) => this.table.setDataAtCell(row, j, value)); + data.forEach((value, j) => { + value = this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]) + this.table.setDataAtCell(row, j, value) + }); } #handleValidationResult = (result, row, prop) => { From 775410244a02a5338b0fae1ace1c5cc82e739284 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Nov 2023 17:15:18 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/renderer/src/stories/JSONSchemaInput.js | 7 +++-- src/renderer/src/stories/Table.js | 31 ++++++++++----------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/renderer/src/stories/JSONSchemaInput.js b/src/renderer/src/stories/JSONSchemaInput.js index 0344f585d..76b105a68 100644 --- a/src/renderer/src/stories/JSONSchemaInput.js +++ b/src/renderer/src/stories/JSONSchemaInput.js @@ -110,7 +110,6 @@ export class JSONSchemaInput extends LitElement { // onValidate = () => {} updateData(value, forceValidate = false) { - if (this.value === value && !forceValidate) { const el = this.getElement(); if (el.type === "checkbox") el.checked = value; @@ -130,7 +129,6 @@ export class JSONSchemaInput extends LitElement { this.#triggerValidation(name, path); this.#updateData(fullPath, value); - return true; } @@ -335,7 +333,10 @@ export class JSONSchemaInput extends LitElement { > ${info.enum.map( - (item, i) => html`` + (item, i) => + html`` )} `; diff --git a/src/renderer/src/stories/Table.js b/src/renderer/src/stories/Table.js index b38115f57..f062d80b6 100644 --- a/src/renderer/src/stories/Table.js +++ b/src/renderer/src/stories/Table.js @@ -265,11 +265,9 @@ export class Table extends LitElement { let ogThis = this; const isRequired = this.isRequired(k); - const validator = async function (value, callback) { - - value = ogThis.#getValue(value, colInfo) - console.log(value) + value = ogThis.#getValue(value, colInfo); + console.log(value); if (!value) { if (!ogThis.validateEmptyCells) { @@ -367,7 +365,7 @@ export class Table extends LitElement { if (this.table) this.table.destroy(); - console.log('Rendered data', this.#getRenderedData(data)) + console.log("Rendered data", this.#getRenderedData(data)); const table = new Handsontable(div, { data: this.#getRenderedData(data), @@ -416,7 +414,7 @@ export class Table extends LitElement { const isUserUpdate = initialCellsToUpdate <= validated; - value = this.#getValue(value, entries[header]) + value = this.#getValue(value, entries[header]); // Transfer data to object if (header === this.keyColumn) { @@ -431,9 +429,6 @@ export class Table extends LitElement { // Update data on passed object else { - - - const globalValue = this.globals[header]; if (value == undefined || value === "") { @@ -489,24 +484,26 @@ export class Table extends LitElement { // Handle enums if (colInfo.enumLabels) return colInfo.enumLabels[value] ?? value; return value; - } + }; #getRenderedData = (data) => { - return Object.values(data).map((row) => row.map((value, j) => this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]))) - } + return Object.values(data).map((row) => + row.map((value, j) => this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]])) + ); + }; #getValue = (value, colInfo) => { - // Handle enums - if (colInfo.enumLabels) return Object.keys(colInfo.enumLabels).find((k) => colInfo.enumLabels[k] === value) ?? value; - + if (colInfo.enumLabels) + return Object.keys(colInfo.enumLabels).find((k) => colInfo.enumLabels[k] === value) ?? value; + return value; }; #setRow(row, data) { data.forEach((value, j) => { - value = this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]) - this.table.setDataAtCell(row, j, value) + value = this.#getRenderedValue(value, this.schema.properties[this.colHeaders[j]]); + this.table.setDataAtCell(row, j, value); }); } From d3f86e6d7d786228ba556eba7fc36868ba3daa41 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Thu, 2 Nov 2023 11:04:53 -0700 Subject: [PATCH 4/4] Update schemas/base-metadata.schema.ts Co-authored-by: Cody Baker <51133164+CodyCBakerPhD@users.noreply.github.com> --- schemas/base-metadata.schema.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schemas/base-metadata.schema.ts b/schemas/base-metadata.schema.ts index e68ac1921..fc7ccf792 100644 --- a/schemas/base-metadata.schema.ts +++ b/schemas/base-metadata.schema.ts @@ -6,10 +6,10 @@ export const preprocessMetadataSchema = (schema: any = baseMetadataSchema) => { schema.properties.Subject.properties.weight.unit = 'kg' schema.properties.Subject.properties.sex.enumLabels = { - O: 'Other', M: 'Male', F: 'Female', - U: 'Unknown' + U: 'Unknown', + O: 'Other' }