Files
kontextwerk/frontend/src/lib/store.ts
2025-10-02 09:03:39 +00:00

130 lines
4.0 KiB
TypeScript

import { get, writable } from "svelte/store"
import { baseURL } from "../config"
/*********** location **************************/
const initLoc = {
path: (typeof window !== "undefined" && window.location?.pathname) || "/",
url: `${baseURL}${(typeof window !== "undefined" && window.location?.pathname) || "/"}`,
search: (typeof window !== "undefined" && window.location?.search) || "",
hash: (typeof window !== "undefined" && window.location?.hash) || "",
push: false,
pop: false,
}
export const location = writable<LocationStore>(initLoc)
const publishLocation = (_p?: string) => {
let _s: string
let _h: string
if (_p) {
const parts = _p.split("#")
_p = parts.shift()
_h = parts.join()
if (_h) _h = "#" + _h
const parts2 = _p.split("?")
_p = parts2.shift()
_s = parts2.join()
if (_s) _s = "?" + _s
}
const newLocation: LocationStore = {
path: _p || (typeof window !== "undefined" && window.location?.pathname),
search: _p ? _s : typeof window !== "undefined" && window.location?.search,
hash: _p ? _h : typeof window !== "undefined" && window.location?.hash,
push: !!_p,
pop: !_p,
previousLocation: get(location),
url: "",
}
newLocation.url = `${baseURL}${newLocation.path}${newLocation.search ? "?" + newLocation.search : ""}`
location.set(newLocation)
}
if (typeof history !== "undefined") {
if (typeof Proxy !== "undefined") {
// modern browser
const historyApply = (
target: (this: any, ...args: readonly any[]) => unknown,
thisArg: any,
argumentsList: string | readonly any[]
) => {
publishLocation(argumentsList && argumentsList.length >= 2 && argumentsList[2])
Reflect.apply(target, thisArg, argumentsList)
}
history.pushState = new Proxy(history.pushState, {
apply: historyApply,
})
history.replaceState = new Proxy(history.replaceState, {
apply: historyApply,
})
} else {
// ie11
const pushStateFn = history.pushState
const replaceStateFn = history.replaceState
history.pushState = function (data: any, title: string, url?: string) {
publishLocation(url)
// @ts-ignore
return pushStateFn.apply(history, arguments)
}
history.replaceState = function (data: any, title: string, url?: string) {
publishLocation(url)
// @ts-ignore
return replaceStateFn.apply(history, arguments)
}
}
} // else ssr -> no history handling
typeof window !== "undefined" &&
window.addEventListener("popstate", (event) => {
publishLocation()
})
/********************** override for admin ui *****************/
export const apiBaseOverride = writable<string | null>(null)
/********************** navigation cache **********************/
export const navigationCache = writable<{ [path: string]: NavigationElement }>({})
///// Notifications
export const notifications = writable<NotificationEntry[]>([])
export function newNotification(n: NotificationEntry): NotificationEntry {
notifications.update((_n) => {
_n.push(n)
let timeout = n.timeout
if (!timeout) {
if (n.class === "error") timeout = 6000
else timeout = 3500
}
if (timeout > 0) {
setTimeout(() => {
removeNotification(n)
}, timeout)
}
return _n
})
return n
}
export function removeNotification(n: NotificationEntry) {
notifications.update((_n) => {
return _n.filter((v) => {
return v !== n
})
})
}
location.subscribe((l) => {
notifications.update((_n) => {
return _n.filter((v) => {
return !v.removeOnNewLocation
})
})
})
export const openModal = writable<boolean>(false)
export const isMobile = writable<boolean>(false)
export const actionApproval = writable<ActionApproval | null>(null)