diff --git a/.gitignore b/.gitignore index d85d8417d..0c5093fa7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Local App Config -vue-client/.env.development +vue-client/.env.development* # Common OS, Tool and Editor Files .vscode/* diff --git a/lxljs/display.js b/lxljs/display.js index 87f13aba5..9eecaeefa 100644 --- a/lxljs/display.js +++ b/lxljs/display.js @@ -200,6 +200,9 @@ export function getItemLabel(item, resources, quoted, settings, inClass = '') { // Assume this is already a label. return item; } + if (typeof item === 'number') { + return `${item}`; + } if (!item || typeof item === 'undefined') { throw new Error('getItemLabel was called with an undefined object.'); } diff --git a/lxljs/tests/string.test.js b/lxljs/tests/string.test.js index 976d71d3c..eac8fd4f3 100644 --- a/lxljs/tests/string.test.js +++ b/lxljs/tests/string.test.js @@ -1,5 +1,8 @@ +import * as VocabUtil from '../vocab.js'; import * as StringUtil from '../string.js'; -import context from '../../test-resources/context'; +import testContext from '../../test-resources/context'; + +const context = VocabUtil.preprocessContext(testContext); describe('StringUtil', () => { diff --git a/lxljs/tests/vocab.test.js b/lxljs/tests/vocab.test.js index 144a0afc6..d2798e48f 100644 --- a/lxljs/tests/vocab.test.js +++ b/lxljs/tests/vocab.test.js @@ -4,7 +4,7 @@ import vocab from '../../test-resources/vocab'; const resources = { vocab, - context: context['@context'], + context: VocabUtil.preprocessContext(context)['@context'], }; describe('VocabUtil', () => { diff --git a/lxljs/vocab.js b/lxljs/vocab.js index 0a74db52d..387bc91e4 100644 --- a/lxljs/vocab.js +++ b/lxljs/vocab.js @@ -74,8 +74,8 @@ export function getTermObject(term, vocab, context) { tries.push(cn); } } - if (!_class && term.endsWith('ByLang')) { - cn = cn.replace('ByLang', ''); + if (!_class && getContextValue(term, '@container', context) === '@language') { + cn = getContextValue(term, '@id', context); _class = vocab.get(cn); tries.push(cn); } @@ -600,6 +600,24 @@ export function getBaseUriFromPrefix(prefix, context) { return baseUri; } +export function getContainedBaseUri(uri, context) { + // If uri contains a baseUri defined in the context, return that baseUri + const contextList = context[0]; + let baseUri = ''; + forOwn(contextList, (value) => { + if (uri.includes(value)) { + baseUri = value; + } + }); + return baseUri; +} + +export function getContainedPrefix(uri, context) { + // If uri contains a prefix defined in the context, return that prefix + const baseUri = getContainedBaseUri(uri, context); + return getPrefixFromBaseUri(baseUri, context); +} + export function getPrefixFromBaseUri(baseUri, context) { // Returns prefix that corresponds to the provided baseUri. const contextList = context[0]; @@ -643,6 +661,9 @@ export function isAbstract(termObject) { export function getTree(term, vocab, context, counter = 0, parentChainString = '') { const termObj = getTermObject(term, vocab, context); + if (typeof termObj === 'undefined') { + return {}; + } const treeNode = { id: term, labels: termObj.labelByLang || termObj.prefLabelByLang, @@ -686,11 +707,39 @@ export function printTree(term, vocab, context) { } export function preprocessContext(context) { - const computed = { containerMap: computeContainerMap(context['@context'][1]) }; - context['@context'].push(computed); + // Internal structure by index: 0 = prefixes, 1 = terms, 2 = computed + const prefixes = {}; + const terms = {}; + + const ctx = context['@context']; + const ctxArray = isArray(ctx) ? ctx : [ctx]; + for (const oneCtx of ctxArray) { + if (!isPlainObject(oneCtx)) { + continue; + } + forOwn(oneCtx, (value, key) => { + if (isPrefix(value)) { + prefixes[key] = value; + } else { + terms[key] = value; + } + }); + } + + const computed = { containerMap: computeContainerMap(terms) }; + context['@context'] = [prefixes, terms, computed]; + return context; } +function isPrefix(value) { + if (isPlainObject(value) && value['@prefix'] === true) { + return true; + } + const id = isPlainObject(value) ? value['@id'] : value; + return typeof id === 'string' && id.match(/[/#:]$/) !== null; +} + export function computeContainerMap(contextList) { function forContextList(closure) { forOwn(contextList, (value, key) => { diff --git a/nuxt-app/components/EntityNode.vue b/nuxt-app/components/EntityNode.vue index 66159fcdc..82d79b78e 100644 --- a/nuxt-app/components/EntityNode.vue +++ b/nuxt-app/components/EntityNode.vue @@ -1,10 +1,7 @@