alpinesmuseum-public/assets/js/utils.js

116 lines
3.5 KiB
JavaScript

import { UPDATE_URLS } from "./redux/actions/urls";
const locales = ['de', 'fr', 'it', 'en'];
export function getSiteDomain() {
const pathComponents = window.location.pathname.split("/").slice(1, -1);
let [locale, site, ...rest] = [undefined, undefined, undefined];
if (pathComponents.length > 0) {
if (locales.includes(pathComponents[0])) {
[locale, site, ...rest] = pathComponents;
}
else {
[locale, site, ...rest] = ["en", ...pathComponents];
}
}
site = site === undefined ? window.location.pathname : "/" + site + "/";
return site;
}
export const defineLocale = isPreLaunch => {
if (isPreLaunch) {
locales = ['de', 'fr'];
}
let userLang = localStorage.getItem('userLang');
if (!userLang) {
userLang = navigator.language || navigator.userLanguage;
userLang = userLang.split('-')[0];
}
if (!locales.includes(userLang)) {
userLang = 'de';
}
localStorage.setItem('userLang', userLang);
return userLang;
};
export function updateURLS(locale, dispatch) {
const date_in_future = new Date(new Date().setFullYear(new Date().getFullYear() + 2))
document.cookie = `django_language=${locale}; expires=${date_in_future.toUTCString()}; path=/`
fetch(`/${locale}/sites/`).then(response => response.json()).then(sitesAndURLS => {
let defaultSite = Object.keys(sitesAndURLS).filter(key => sitesAndURLS[key].default);
defaultSite = defaultSite.length == 0 ? "/" : defaultSite[0];
const siteDomain = getSiteDomain();
const currentSite = siteDomain in sitesAndURLS ? siteDomain : defaultSite;
if (currentSite in sitesAndURLS) {
dispatch({
type: UPDATE_URLS, payload: {
id: sitesAndURLS[currentSite].id,
name: sitesAndURLS[currentSite].name,
color: sitesAndURLS[currentSite].color,
scrollingText: sitesAndURLS[currentSite].scrolling_text,
allSites: sitesAndURLS,
currentSite: currentSite
}
});
}
});
}
export function mostStrictSiteMatch(allSites, site) {
const matchedSites = Object.keys(allSites).filter(s => site.startsWith(s));
if (matchedSites.length > 0) {
return matchedSites.reduce((a, b) => a.length > b.length ? a : b);
}
return allSites[0];
}
export function getLocaleFromURL(url) {
return doesURLHaveLocale(url) ? url.split("/").filter(p => p)[0] : undefined;
}
export function doesURLHaveLocale(url) {
return locales.includes(url.split("/").filter(p => p)[0])
}
export function urlWithoutLocale(url) {
return doesURLHaveLocale(url) ? [""].concat(url.split("/").splice(2)).join("/") : url;
}
export function isDjangoContentPresent() {
const djangoContent = document.getElementById("django_content");
return djangoContent !== null && djangoContent.innerHTML !== "";
}
export function shouldDisplayDynamicComponent(urls) {
// I think we don't need the following, because django would itself throw an error if the page is not found
// const urlExists = Object.getOwnPropertyNames(urls).filter(item => urls[item][0] === window.location.pathname).length !== 0;
return isDjangoContentPresent();
}
export function routePathForDynamicComponent(basename) {
let route_path = window.location.pathname.slice();
if (window.location.pathname.startsWith(basename)) {
route_path = route_path.replace(basename, "");
}
if (route_path.endsWith("/"))
{
route_path = route_path.slice(0, route_path.length - 1);
}
if (!route_path.startsWith("/")) {
route_path = "/" + route_path;
}
return route_path;
}
export function getDjangoView() {
let django_content = document.getElementById("django_content");
let django_view = django_content ? django_content.innerHTML : django_content;
return django_view;
}