Skip to content

Commit

Permalink
Allow opening item directly via URI (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Sep 16, 2024
1 parent cd3a771 commit 2ebdf7d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/client/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const schemeFetchPromise = fetch(
})

export const schemes = computed(() => state.schemes)
export const schemesAsConceptSchemes = computed(() => state.schemes?.map(scheme => new jskos.ConceptScheme(scheme)) || [])

import { cdk } from "cocoda-sdk"

Expand Down
3 changes: 3 additions & 0 deletions src/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import config from "@/config.js"
export const routerBasePath = "/" + config.namespace.pathname.replace(import.meta.env.BASE_URL, "")

export function getRouterUrl({ scheme, concept, params = {} }) {
if (!scheme && concept?.inScheme?.[0]) {
scheme = concept.inScheme[0]
}
let url = (() => {
if (concept?.uri.startsWith(config.namespace) && (!scheme?.uri || concept?.uri.startsWith(scheme?.uri))) {
return `${routerBasePath}${concept.uri.replace(config.namespace, "")}`
Expand Down
27 changes: 25 additions & 2 deletions src/client/views/HomeView.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script setup>
import { useRoute } from "vue-router"
import { useRoute, useRouter } from "vue-router"
import * as jskos from "jskos-tools"
import { schemes, quickSelection, publisherSelection, typeSelection, registry } from "@/store.js"
import { schemes, schemesAsConceptSchemes, quickSelection, publisherSelection, typeSelection, registry } from "@/store.js"
import { ref, computed, watch } from "vue"
import { getRouterUrl } from "@/utils.js"
import CategoryButton from "@/components/CategoryButton.vue"
import SchemeButton from "@/components/SchemeButton.vue"
const route = useRoute()
const router = useRouter()
const mode = computed(() => {
if (route.query?.publisher) {
Expand All @@ -25,6 +26,28 @@ const mode = computed(() => {
return "default"
})
watch(() => ([schemesAsConceptSchemes.value, route.query.uri]), ([schemes, uri]) => {
if (schemes?.length > 0 && uri) {
// Try to find scheme or concept that fits URI
const scheme = schemes.find(s => jskos.compare(s, { uri }))
if (scheme) {
// Scheme found, redirect
router.push(getRouterUrl({ scheme }))
return
}
for (const scheme of schemes) {
const concept = scheme.conceptFromUri(uri)
if (concept) {
// Concept found, redirect
router.push(getRouterUrl({ concept, scheme }))
return
}
}
// Report error on console
console.error(`Could find neither vocabulary or concept that fits given URI ${uri}`)
}
}, { immediate: true })
const publisherGroups = computed(() => {
const groups = []
publisherSelection.value.filter(p => p.id !== "__others__").forEach(publisher => {
Expand Down

0 comments on commit 2ebdf7d

Please sign in to comment.