Skip to content

Commit

Permalink
A tiny fix
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelvaara committed Dec 19, 2024
1 parent b02ecfe commit de55604
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions resource/js/vocab-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,11 @@ function startVocabSearchApp () {
this.changeLang(paramLang)
return paramLang
}
// use searchLangCookie if it can be found, otherwise pick content lang from SKOSMOS object
const cookies = document.cookie.split('; ')
const searchLangCookie = cookies.find(cookie =>
cookie.startsWith('SKOSMOS_SEARCH_LANG='))
if (searchLangCookie) {
const selectedLanguage = searchLangCookie.split('=')[1]
if (selectedLanguage !== 'all') {
window.SKOSMOS.content_lang = selectedLanguage
}
return selectedLanguage
} else {
// otherwise pick content lang from SKOSMOS object (it should always exist)
if (window.SKOSMOS.content_lang) {
return window.SKOSMOS.content_lang
}
return null
},
renderMatchingPart (searchTerm, label, lang = null) {
if (label) {
Expand Down Expand Up @@ -161,6 +153,9 @@ function startVocabSearchApp () {
if ('uri' in result) { // create relative Skosmos page URL from the search result URI
result.pageUrl = window.SKOSMOS.vocab + '/' + window.SKOSMOS.lang + '/page?'
const urlParams = new URLSearchParams({ uri: result.uri })
if (this.selectedLanguage !== window.SKOSMOS.lang) { // add content language parameter
urlParams.append('clang', this.selectedLanguage)
}
result.pageUrl += urlParams.toString()
}
// render search result renderedTypes
Expand All @@ -171,7 +166,7 @@ function startVocabSearchApp () {
result.renderedType = result.type.map(this.translateType).join(', ')
result.showNotation = this.showNotation
})

if (this.renderedResultsList.length === 0) { // show no results message
this.renderedResultsList.push({
prefLabel: this.noResults,
Expand All @@ -186,27 +181,27 @@ function startVocabSearchApp () {
},
gotoSearchPage () {
if (!this.searchTerm) return

const currentVocab = window.SKOSMOS.vocab + '/' + window.SKOSMOS.lang + '/'
const vocabHref = window.location.href.substring(0, window.location.href.lastIndexOf(window.SKOSMOS.vocab)) + currentVocab
const searchUrlParams = new URLSearchParams({ clang: window.SKOSMOS.content_lang, q: this.searchTerm })
if (this.selectedLanguage === 'all') searchUrlParams.set('anylang', 'true')
const searchUrl = vocabHref + 'search?' + searchUrlParams.toString()
window.location.href = searchUrl
},
changeLang (lang) {
this.selectedLanguage = lang
this.setSearchLangCookie(lang)
changeLang (clang) {
this.selectedLanguage = clang
window.SKOSMOS.content_lang = clang
this.resetSearchTermAndHideDropdown()
},
changeContentLangAndReload (lang) {
this.changeLang(lang)
changeContentLangAndReload (clang) {
this.changeLang(clang)
const params = new URLSearchParams(window.location.search)
if (lang === 'all') {
if (clang === 'all') {
params.set('anylang', 'true')
} else {
params.delete('anylang')
params.set('clang', lang)
params.set('clang', clang)
}
this.$forceUpdate()
window.location.search = params.toString()
Expand All @@ -223,14 +218,6 @@ function startVocabSearchApp () {
this.showDropdown = true
this.$forceUpdate()
},
setSearchLangCookie (lang) {
// The cookie path should be relative if the baseHref is known
let cookiePath = '/'
if (window.SKOSMOS.baseHref && window.SKOSMOS.baseHref.replace(window.origin, '')) {
cookiePath = window.SKOSMOS.baseHref.replace(window.origin, '')
}
document.cookie = `SKOSMOS_SEARCH_LANG=${this.selectedLanguage};path=${cookiePath}`
}
},
template: `
<div class="d-flex my-auto ms-auto">
Expand Down

0 comments on commit de55604

Please sign in to comment.