fixed language switcher update on vue route change

This commit is contained in:
2021-03-06 22:02:48 +01:00
parent 8dadb8d3a5
commit ee908b4603
4 changed files with 110 additions and 17 deletions

View File

@ -53,6 +53,8 @@ Vue.use(VueSimpleAccordion, {
// ... Options go here
});
import { MA } from 'vuejs/api/ma-axios'
(function (Drupal, drupalSettings, drupalDecoupled) {
const MaterioTheme = function () {
let _v_sitebranding_block, _v_user_block, _v_header_menu,
@ -163,27 +165,38 @@ Vue.use(VueSimpleAccordion, {
}
document.querySelector('body').classList.add(...classes)
// update block language selection
let links = document.querySelectorAll('#block-languageswitcher a.language-link')
let path = to.path.replace(/^\/\D{2,3}\//, '')
// remove language relative prefix from path classes (fr, en, etc)
.replace(/^\/\D{2,3}$/, '')
if(path !== ''){
path = '/'+path
}
links.forEach((link, i) => {
console.log("language link",path , link)
link.setAttribute('href', `/${link.getAttribute('hreflang')}${path}`)
});
updateLanguageLinksBlock(to.path);
// trigger router
next()
})
}
function updateLanguageLinksBlock(path){
// update block language selection
console.log("updateLanguageLinksBlock, path:", path);
let links = document.querySelectorAll('#block-languageswitcher a.language-link')
let params = {
path: path
// XDEBUG_SESSION_START: true
}
MA.post(`materio_decoupled/path_translation_links?`, params)
.then(({ data }) => {
console.log('Path translations links', data)
links.forEach((link, i) => {
console.log("language link",path , link)
let hreflang = link.getAttribute('hreflang')
link.setAttribute('href', data.links[hreflang].url)
link.innerHTML = data.links[hreflang].title
});
})
.catch(error => {
console.warn('Path translations links', error)
})
}
function initVSiteBrandingBlock () {
_v_sitebranding_block = new Vue({
store,