platform/packages/ui/src/panelup.ts
Denis Bykhov ae84626521
TSK-865 Сделать красивый вид ссылок (#2771)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
2023-03-20 14:45:52 +06:00

67 lines
1.7 KiB
TypeScript

import { get, writable } from 'svelte/store'
import { location, navigate } from './location'
import { AnyComponent, PopupAlignment } from './types'
export interface PanelProps {
component: AnyComponent
_id: string
_class: string
element?: PopupAlignment
rightSection?: AnyComponent
}
export const panelstore = writable<{ panel?: PanelProps | undefined }>({ panel: undefined })
let currentLocation: string | undefined
export function getPanelURI (component: AnyComponent, _id: string, _class: string, element?: PopupAlignment): string {
const panelProps = [component, _id, _class]
if (typeof element === 'string') {
panelProps.push(element)
}
return encodeURIComponent(panelProps.join('|'))
}
export function showPanel (
component: AnyComponent,
_id: string,
_class: string,
element?: PopupAlignment,
rightSection?: AnyComponent
): void {
openPanel(component, _id, _class, element, rightSection)
const loc = get(location)
if (loc.fragment !== currentLocation) {
loc.fragment = currentLocation
navigate(loc)
}
}
export function openPanel (
component: AnyComponent,
_id: string,
_class: string,
element?: PopupAlignment,
rightSection?: AnyComponent
): void {
const newLoc = getPanelURI(component, _id, _class, element)
if (currentLocation === newLoc) {
return
}
currentLocation = newLoc
panelstore.update(() => {
return { panel: { component, _id, _class, element, rightSection } }
})
}
export function closePanel (shoulRedirect: boolean = true): void {
panelstore.update(() => {
return { panel: undefined }
})
if (shoulRedirect) {
const loc = get(location)
loc.fragment = undefined
currentLocation = undefined
navigate(loc)
}
}