Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: UI configuration #359

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/DataHarmonizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
dataArrayToObject,
dataObjectToArray,
fieldUnitBinTest,
getFieldSettingsFromUiConfig,
} from './utils/fields';
import { parseDatatype } from './utils/datatypes';
import {
Expand Down Expand Up @@ -51,8 +52,9 @@ class DataHarmonizer {
this.schema = options.schema;
this.loadingScreenRoot = options.loadingScreenRoot || this.root;
this.modalsRoot = options.modalsRoot || document.querySelector('body');
this.field_settings = options.fieldSettings || {};
this.self = this;

const fieldSettings = getFieldSettingsFromUiConfig(options.uiConfig);
this.field_settings = Object.assign(fieldSettings, options.fieldSettings);

this.injectLoadingScreen();

Expand Down
59 changes: 59 additions & 0 deletions lib/utils/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,62 @@ export function dataObjectToArray(dataObject, fields, options = {}) {
}
return dataArray;
}

function olsAutocompleteFieldSettings(config) {
const { ontology } = config;
return {
getColumn: (dh, col) => {
let timer = null;
return {
...col,
type: 'autocomplete',
strict: true,
allowInvalid: false,
sortByRelevance: false,
source: (query, next) => {
clearTimeout(timer);
if (!query) {
return next([]);
}
timer = setTimeout(() => {
fetch(
`https://www.ebi.ac.uk/ols/api/select?q=${query}&ontology=${ontology}&type=class&rows=50`
)
.then((response) => {
if (!response.ok) {
throw new Error('API Error: ' + response.status);
}
return response.json();
})
.then((body) => {
const options = body.response.docs.map((doc) => doc.label);
next(options);
})
.catch(() => {
next([]);
});
}, 300);
},
};
},
};
}

const WIDGET_REGISTRY = {
'ols-autocomplete': olsAutocompleteFieldSettings,
};

export function getFieldSettingsFromUiConfig(uiConfig) {
const fieldSettings = {};
if (uiConfig && uiConfig.fields) {
for (const [field, config] of Object.entries(uiConfig.fields)) {
if (config.widget in WIDGET_REGISTRY) {
const settingsFn = WIDGET_REGISTRY[config.widget];
fieldSettings[field] = settingsFn(config);
} else {
console.warn(`Unknown widget type: ${config.widget}`);
}
}
}
return fieldSettings;
}
12 changes: 12 additions & 0 deletions web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ document.addEventListener('DOMContentLoaded', function () {
const dhFooterRoot = document.querySelector('#data-harmonizer-footer');
const dhToolbarRoot = document.querySelector('#data-harmonizer-toolbar');

// this is defined inline for convenience but could just as easily be
// imported from a JSON file
const uiConfig = {
fields: {
'third party lab service provider name': {
widget: 'ols-autocomplete',
ontology: 'zfa,zfs',
},
},
};

const dh = new DataHarmonizer(dhRoot, {
loadingScreenRoot: document.querySelector('body'),
uiConfig: uiConfig,
});

new Footer(dhFooterRoot, dh);
Expand Down