2023-03-20 08:45:52 +00:00
|
|
|
import { get, writable } from 'svelte/store'
|
|
|
|
import { location, navigate } from './location'
|
2022-02-04 09:03:24 +00:00
|
|
|
import { AnyComponent, PopupAlignment } from './types'
|
|
|
|
|
|
|
|
export interface PanelProps {
|
|
|
|
component: AnyComponent
|
|
|
|
_id: string
|
|
|
|
_class: string
|
|
|
|
element?: PopupAlignment
|
2022-02-07 09:03:14 +00:00
|
|
|
rightSection?: AnyComponent
|
2022-02-04 09:03:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
export const panelstore = writable<{ panel?: PanelProps | undefined }>({ panel: undefined })
|
2022-02-04 09:03:24 +00:00
|
|
|
let currentLocation: string | undefined
|
|
|
|
|
2023-01-29 15:34:11 +00:00
|
|
|
export function getPanelURI (component: AnyComponent, _id: string, _class: string, element?: PopupAlignment): string {
|
2022-04-09 16:00:48 +00:00
|
|
|
const panelProps = [component, _id, _class]
|
|
|
|
if (typeof element === 'string') {
|
|
|
|
panelProps.push(element)
|
|
|
|
}
|
|
|
|
return encodeURIComponent(panelProps.join('|'))
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:03:24 +00:00
|
|
|
export function showPanel (
|
|
|
|
component: AnyComponent,
|
|
|
|
_id: string,
|
|
|
|
_class: string,
|
2022-02-07 09:03:14 +00:00
|
|
|
element?: PopupAlignment,
|
|
|
|
rightSection?: AnyComponent
|
2023-03-15 14:06:03 +00:00
|
|
|
): void {
|
|
|
|
openPanel(component, _id, _class, element, rightSection)
|
2023-03-20 08:45:52 +00:00
|
|
|
const loc = get(location)
|
|
|
|
if (loc.fragment !== currentLocation) {
|
|
|
|
loc.fragment = currentLocation
|
|
|
|
navigate(loc)
|
2023-03-15 14:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function openPanel (
|
|
|
|
component: AnyComponent,
|
|
|
|
_id: string,
|
|
|
|
_class: string,
|
|
|
|
element?: PopupAlignment,
|
|
|
|
rightSection?: AnyComponent
|
2022-02-04 09:03:24 +00:00
|
|
|
): void {
|
2023-01-29 15:34:11 +00:00
|
|
|
const newLoc = getPanelURI(component, _id, _class, element)
|
2022-02-04 09:03:24 +00:00
|
|
|
if (currentLocation === newLoc) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentLocation = newLoc
|
|
|
|
panelstore.update(() => {
|
2022-02-07 09:03:14 +00:00
|
|
|
return { panel: { component, _id, _class, element, rightSection } }
|
2022-02-04 09:03:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:06:03 +00:00
|
|
|
export function closePanel (shoulRedirect: boolean = true): void {
|
2022-02-04 09:03:24 +00:00
|
|
|
panelstore.update(() => {
|
|
|
|
return { panel: undefined }
|
|
|
|
})
|
2023-03-15 14:06:03 +00:00
|
|
|
if (shoulRedirect) {
|
2023-03-20 08:45:52 +00:00
|
|
|
const loc = get(location)
|
|
|
|
loc.fragment = undefined
|
2023-03-15 14:06:03 +00:00
|
|
|
currentLocation = undefined
|
2023-03-20 08:45:52 +00:00
|
|
|
navigate(loc)
|
2023-03-15 14:06:03 +00:00
|
|
|
}
|
2022-02-04 09:03:24 +00:00
|
|
|
}
|