From fe9e91a9f6c05ee6c093bedea9479bef3c223845 Mon Sep 17 00:00:00 2001 From: tholulomo Date: Wed, 29 Mar 2023 13:39:18 -0400 Subject: [PATCH] fix(charts): Fix chart URI sharable link bug issue --- app/src/components/nanomine/ImageUpload.vue | 2 +- app/src/modules/vega-chart.js | 76 ++++++- app/src/pages/explorer/chart/editor/Chart.vue | 209 ++++++++++++++++++ .../explorer/chart/view/vega-view-script.js | 5 +- .../pages/explorer/chart/view/vega-view.html | 2 +- .../modules/explorer/curation/actions.js | 53 +++++ docker-compose.yml | 2 +- 7 files changed, 339 insertions(+), 10 deletions(-) create mode 100644 app/src/pages/explorer/chart/editor/Chart.vue diff --git a/app/src/components/nanomine/ImageUpload.vue b/app/src/components/nanomine/ImageUpload.vue index aecdff7b..76ecfc46 100644 --- a/app/src/components/nanomine/ImageUpload.vue +++ b/app/src/components/nanomine/ImageUpload.vue @@ -321,7 +321,7 @@ export default { } else if (this.inputtedDimensions.units === 'millimeters') { ratio = ratio / 1000 } - // console.log(this.displayedFiles[0].size.width, this.displayedFiles[0].pixelSize.width) + this.selectedOptions.dimensions = { units: this.inputtedDimensions.units, width: this.displayedFiles[0].size.width, height: this.displayedFiles[0].size.height, ratio: ratio } } else { this.selectedOptions.dimensions = { units: this.inputtedDimensions.units, width: parseInt(this.inputtedDimensions.width), height: parseInt(this.inputtedDimensions.height), ratio: null } diff --git a/app/src/modules/vega-chart.js b/app/src/modules/vega-chart.js index 624f50e3..fd7e5748 100644 --- a/app/src/modules/vega-chart.js +++ b/app/src/modules/vega-chart.js @@ -36,6 +36,49 @@ const defaultChart = { depiction: null } +const chartType = 'http://semanticscience.org/resource/Chart' +// const lodPrefix = window.location.origin +const chartUriPrefix = 'http://nanomine.org/viz/' +const foafDepictionUri = 'http://xmlns.com/foaf/0.1/depiction' +const hasContentUri = 'http://vocab.rpi.edu/whyis/hasContent' + +const chartFieldPredicates = { + baseSpec: 'http://semanticscience.org/resource/hasValue', + query: 'http://schema.org/query', + title: 'http://purl.org/dc/terms/title', + description: 'http://purl.org/dc/terms/description', + dataset: 'http://www.w3.org/ns/prov#used' +} + +const chartIdLen = 16 + +function generateChartId () { + const intArr = new Uint8Array(chartIdLen / 2) + window.crypto.getRandomValues(intArr) + const chartId = Array.from(intArr, (dec) => ('0' + dec.toString(16)).substr(-2)).join('') + + return `${chartUriPrefix}${chartId}` +} + +function buildChartLd (chart) { + chart = Object.assign({}, chart) + chart.baseSpec = JSON.stringify(chart.baseSpec) + const chartLd = { + '@id': chart.uri, + '@type': [chartType], + [foafDepictionUri]: { + '@id': `${chart.uri}_depiction`, + [hasContentUri]: chart.depiction + } + } + Object.entries(chart) + .filter(([field, value]) => chartFieldPredicates[field]) + .forEach(([field, value]) => { + chartLd[chartFieldPredicates[field]] = [{ '@value': value }] + }) + return chartLd +} + function getDefaultChart () { return Object.assign({}, defaultChart) } @@ -62,7 +105,17 @@ const chartQuery = ` ` async function loadChart (chartUri) { - const singleChartQuery = chartQuery + `\n VALUES (?uri) { (<${chartUri}>) }` + // Check the chart uri before running query + let chartUrl = chartUri + if (chartUrl.includes('view/')) { + // We should not get in this if logic. The issue with chart URI using a different + // lodPrefix is now fixed (e.g. http://nanomine.org/viz/chartId instead of http://purl.org/chart/view/chartId) + // Todo (ticket-xx): Remove this if logic if response is consistent + chartUrl = decodeURIComponent(chartUri.split('view/')[1]) + } + + const valuesBlock = `\n VALUES (?uri) { (<${chartUri}>) }` + const singleChartQuery = chartQuery.replace(/(where\s*{)/i, '$1' + valuesBlock) const { results } = await querySparql(singleChartQuery) const rows = results.bindings if (rows.length < 1) { @@ -102,17 +155,28 @@ function buildCsvSpec (baseSpec, csvResults) { return spec } -const chartUriPrefix = 'http://nanomine.org/viz/' - function toChartId (chartUri) { - if (!chartUri.startsWith(chartUriPrefix)) { - throw new Error(`Unexpected chart uri "${chartUri}". Was expecting prefix "${chartUriPrefix}"`) + if (chartUri && !chartUri.startsWith(chartUriPrefix)) { + // We should not get in this if logic. The issue with chart URI using a different + // lodPrefix is now fixed (e.g. http://nanomine.org/viz/chartId instead of http://purl.org/chart/view/chartId) + // Todo (ticket-xx): Remove this if logic if response is consistent + return chartUri } + if (!chartUri) return + return chartUri.substring(chartUriPrefix.length) } function toChartUri (chartId) { + if (chartId.includes('viz')) { + return `${window.location.origin}/explorer/chart/view/${encodeURIComponent(chartId)}` + } + return chartUriPrefix + chartId } -export { getDefaultChart, loadChart, buildSparqlSpec, buildCsvSpec, toChartId, toChartUri, chartUriPrefix } +function shareChartUri (chartId) { + return `${window.location.origin}/explorer/chart/view/${chartId}` +} + +export { getDefaultChart, saveChart, loadChart, copyChart, buildSparqlSpec, buildCsvSpec, toChartId, toChartUri, shareChartUri, chartUriPrefix } diff --git a/app/src/pages/explorer/chart/editor/Chart.vue b/app/src/pages/explorer/chart/editor/Chart.vue new file mode 100644 index 00000000..a4c0294f --- /dev/null +++ b/app/src/pages/explorer/chart/editor/Chart.vue @@ -0,0 +1,209 @@ + + + diff --git a/app/src/pages/explorer/chart/view/vega-view-script.js b/app/src/pages/explorer/chart/view/vega-view-script.js index 73a70c3f..6e46a97f 100644 --- a/app/src/pages/explorer/chart/view/vega-view-script.js +++ b/app/src/pages/explorer/chart/view/vega-view-script.js @@ -1,6 +1,6 @@ import VJsoneditor from 'v-jsoneditor' import Dialog from '@/components/Dialog.vue' -import { loadChart, buildSparqlSpec, toChartUri } from '@/modules/vega-chart' +import { loadChart, buildSparqlSpec, toChartUri, shareChartUri } from '@/modules/vega-chart' import VegaLite from '@/components/explorer/VegaLiteWrapper.vue' import yasqe from '@/components/explorer/yasqe' import yasr from '@/components/explorer/yasr' @@ -56,6 +56,9 @@ export default { }, fullChartUri () { return toChartUri(this.chartId) + }, + shareChartUri () { + return shareChartUri(this.chartId) } }, methods: { diff --git a/app/src/pages/explorer/chart/view/vega-view.html b/app/src/pages/explorer/chart/view/vega-view.html index d28f1ff3..235015d8 100644 --- a/app/src/pages/explorer/chart/view/vega-view.html +++ b/app/src/pages/explorer/chart/view/vega-view.html @@ -92,7 +92,7 @@