2022-04-27 05:50:07 +00:00
|
|
|
import type { AnySvelteComponent, AnyComponent, PopupAlignment, PopupPositionElement } from './types'
|
2022-02-04 09:03:24 +00:00
|
|
|
import { getResource } from '@anticrm/platform'
|
|
|
|
import { writable } from 'svelte/store'
|
|
|
|
|
|
|
|
interface CompAndProps {
|
|
|
|
id: string
|
|
|
|
is: AnySvelteComponent
|
|
|
|
props: any
|
|
|
|
element?: PopupAlignment
|
|
|
|
onClose?: (result: any) => void
|
2022-03-17 06:07:38 +00:00
|
|
|
onUpdate?: (result: any) => void
|
2022-02-04 09:03:24 +00:00
|
|
|
close: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const popupstore = writable<CompAndProps[]>([])
|
|
|
|
function addPopup (props: CompAndProps): void {
|
|
|
|
popupstore.update((popups) => {
|
|
|
|
popups.push(props)
|
|
|
|
return popups
|
|
|
|
})
|
|
|
|
}
|
|
|
|
let popupId: number = 0
|
|
|
|
export function showPopup (
|
|
|
|
component: AnySvelteComponent | AnyComponent,
|
|
|
|
props: any,
|
|
|
|
element?: PopupAlignment,
|
2022-03-17 06:07:38 +00:00
|
|
|
onClose?: (result: any) => void,
|
|
|
|
onUpdate?: (result: any) => void
|
2022-02-04 09:03:24 +00:00
|
|
|
): () => void {
|
|
|
|
const id = `${popupId++}`
|
|
|
|
const closePopupOp = (): void => {
|
|
|
|
popupstore.update((popups) => {
|
2022-04-21 15:05:09 +00:00
|
|
|
const pos = popups.findIndex((p) => p.id === id)
|
2022-02-04 09:03:24 +00:00
|
|
|
if (pos !== -1) {
|
|
|
|
popups.splice(pos, 1)
|
|
|
|
}
|
|
|
|
return popups
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (typeof component === 'string') {
|
2022-04-21 15:05:09 +00:00
|
|
|
getResource(component)
|
|
|
|
.then((resolved) => addPopup({ id, is: resolved, props, element, onClose, onUpdate, close: closePopupOp }))
|
|
|
|
.catch((err) => console.log(err))
|
2022-02-04 09:03:24 +00:00
|
|
|
} else {
|
2022-03-17 06:07:38 +00:00
|
|
|
addPopup({ id, is: component, props, element, onClose, onUpdate, close: closePopupOp })
|
2022-02-04 09:03:24 +00:00
|
|
|
}
|
|
|
|
return closePopupOp
|
|
|
|
}
|
|
|
|
|
|
|
|
export function closePopup (): void {
|
|
|
|
popupstore.update((popups) => {
|
|
|
|
popups.pop()
|
|
|
|
return popups
|
|
|
|
})
|
|
|
|
}
|
2022-04-07 10:02:01 +00:00
|
|
|
|
|
|
|
interface IDatePopup {
|
|
|
|
component: AnySvelteComponent | undefined
|
|
|
|
currentDate: Date | undefined
|
|
|
|
anchor: HTMLElement | undefined
|
|
|
|
popup: HTMLElement | undefined
|
|
|
|
frendlyFocus: HTMLElement[] | undefined
|
|
|
|
onClose?: (result: any) => void
|
|
|
|
onChange?: (result: any) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const dpstore = writable<IDatePopup>({
|
|
|
|
component: undefined,
|
|
|
|
currentDate: undefined,
|
|
|
|
anchor: undefined,
|
|
|
|
popup: undefined,
|
|
|
|
frendlyFocus: undefined,
|
|
|
|
onClose: undefined,
|
|
|
|
onChange: undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
export function showDatePopup (
|
|
|
|
component: AnySvelteComponent | undefined,
|
|
|
|
currentDate: Date,
|
|
|
|
anchor?: HTMLElement,
|
|
|
|
popup?: HTMLElement,
|
|
|
|
frendlyFocus?: HTMLElement[] | undefined,
|
|
|
|
onClose?: (result: any) => void,
|
|
|
|
onChange?: (result: any) => void
|
|
|
|
): void {
|
|
|
|
dpstore.set({
|
|
|
|
component: component,
|
|
|
|
currentDate: currentDate,
|
|
|
|
anchor: anchor,
|
|
|
|
popup: popup,
|
|
|
|
frendlyFocus: frendlyFocus,
|
|
|
|
onClose: onClose,
|
|
|
|
onChange: onChange
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function closeDatePopup (): void {
|
|
|
|
dpstore.set({
|
|
|
|
component: undefined,
|
|
|
|
currentDate: undefined,
|
|
|
|
anchor: undefined,
|
|
|
|
popup: undefined,
|
|
|
|
frendlyFocus: undefined,
|
|
|
|
onClose: undefined,
|
|
|
|
onChange: undefined
|
|
|
|
})
|
|
|
|
}
|
2022-04-09 16:00:48 +00:00
|
|
|
|
2022-04-21 15:05:09 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* Place element based on position and element.
|
|
|
|
*
|
|
|
|
* return boolean to show or not modal overlay.
|
|
|
|
*/
|
2022-04-24 16:41:09 +00:00
|
|
|
export function fitPopupPositionedElement (modalHTML: HTMLElement, alignment: PopupPositionElement, newProps: Record<string, string|number>): boolean {
|
2022-04-21 15:05:09 +00:00
|
|
|
const rect = alignment.getBoundingClientRect()
|
|
|
|
const rectPopup = modalHTML.getBoundingClientRect()
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.left = newProps.right = newProps.top = newProps.bottom = ''
|
|
|
|
newProps.maxHeight = newProps.height = ''
|
|
|
|
newProps.maxWidth = newProps.width = ''
|
2022-04-24 05:18:03 +00:00
|
|
|
if (alignment.position !== undefined) {
|
2022-04-21 15:05:09 +00:00
|
|
|
if (alignment.position.v === 'top') {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = `${rect.top}px`
|
2022-04-21 15:05:09 +00:00
|
|
|
} else if (alignment.position.v === 'bottom') {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = `${rect.bottom - rectPopup.height}px`
|
2022-04-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alignment.position.h === 'right') {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.left = `calc(${rect.right}px + .125rem)`
|
2022-04-21 15:05:09 +00:00
|
|
|
} else if (alignment.position.h === 'left') {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.left = `calc(${rect.left - rectPopup.width}px - .125rem)`
|
2022-04-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Vertical
|
|
|
|
if (rect.bottom + rectPopup.height + 28 <= document.body.clientHeight) {
|
2022-04-27 05:50:07 +00:00
|
|
|
newProps.top = `${rect.bottom + 1}px`
|
2022-04-21 15:05:09 +00:00
|
|
|
} else if (rectPopup.height + 28 < rect.top) {
|
2022-04-27 05:50:07 +00:00
|
|
|
newProps.bottom = `${document.body.clientHeight - rect.top + 1}px`
|
2022-04-21 15:05:09 +00:00
|
|
|
} else {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = modalHTML.style.bottom = '1rem'
|
2022-04-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontal
|
|
|
|
if (rect.left + rectPopup.width + 16 > document.body.clientWidth) {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.right = `${document.body.clientWidth - rect.right}px`
|
2022-04-21 15:05:09 +00:00
|
|
|
} else {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.left = `${rect.left}px`
|
2022-04-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:41:09 +00:00
|
|
|
function applyStyle (values: Record<string, string| number>, modalHTML: HTMLElement): void {
|
|
|
|
for (const [k, v] of Object.entries(values)) {
|
|
|
|
const old = (modalHTML.style as any)[k]
|
|
|
|
if (old !== v) {
|
|
|
|
(modalHTML.style as any)[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 16:00:48 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* Place element based on position and underline content element.
|
|
|
|
*
|
|
|
|
* return boolean to show or not modal overlay.
|
|
|
|
*/
|
|
|
|
export function fitPopupElement (modalHTML: HTMLElement, element?: PopupAlignment, contentPanel?: HTMLElement): boolean {
|
|
|
|
let show = true
|
2022-04-24 16:41:09 +00:00
|
|
|
const newProps: Record<string, string|number> = {}
|
2022-04-09 16:00:48 +00:00
|
|
|
if (element != null) {
|
|
|
|
show = false
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.left = newProps.right = newProps.top = newProps.bottom = ''
|
|
|
|
newProps.maxHeight = newProps.height = ''
|
2022-04-27 05:50:07 +00:00
|
|
|
newProps.maxWidth = newProps.width = ''
|
2022-04-09 16:00:48 +00:00
|
|
|
if (typeof element !== 'string') {
|
2022-04-24 16:41:09 +00:00
|
|
|
const result = fitPopupPositionedElement(modalHTML, element, newProps)
|
|
|
|
applyStyle(newProps, modalHTML)
|
|
|
|
return result
|
2022-04-09 16:00:48 +00:00
|
|
|
} else if (element === 'right' && contentPanel !== undefined) {
|
|
|
|
const rect = contentPanel.getBoundingClientRect()
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = `calc(${rect.top}px + 0.5rem)`
|
|
|
|
newProps.bottom = '0.75rem'
|
|
|
|
newProps.right = '0.75rem'
|
|
|
|
newProps.maxWidth = '50%'
|
2022-04-09 16:00:48 +00:00
|
|
|
show = true
|
|
|
|
} else if (element === 'top') {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = '15vh'
|
|
|
|
newProps.left = '50%'
|
|
|
|
newProps.transform = 'translateX(-50%)'
|
2022-04-09 16:00:48 +00:00
|
|
|
show = true
|
|
|
|
} else if (element === 'account') {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.bottom = '2.75rem'
|
|
|
|
newProps.left = '5rem'
|
2022-04-09 16:00:48 +00:00
|
|
|
} else if (element === 'full' && contentPanel !== undefined) {
|
|
|
|
const rect = contentPanel.getBoundingClientRect()
|
2022-04-27 05:50:07 +00:00
|
|
|
newProps.top = `${rect.top + 1}px`
|
|
|
|
newProps.bottom = '.25rem'
|
|
|
|
newProps.left = '.25rem'
|
|
|
|
newProps.right = '.25rem'
|
2022-04-09 16:00:48 +00:00
|
|
|
show = true
|
|
|
|
} else if (element === 'content' && contentPanel !== undefined) {
|
|
|
|
const rect = contentPanel.getBoundingClientRect()
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = `${rect.top + 1}px`
|
2022-04-27 05:50:07 +00:00
|
|
|
// newProps.bottom = `${Math.min(document.body.clientHeight - rect.bottom + 1, window.innerHeight - rect.top - 1)}px`
|
|
|
|
newProps.height = `${Math.min(rect.height, window.innerHeight - rect.top)}px`
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.left = `${rect.left + 1}px`
|
2022-04-27 05:50:07 +00:00
|
|
|
// newProps.right = `${Math.min(document.body.clientWidth - rect.right, window.innerWidth - rect.left - 5)}px`
|
|
|
|
newProps.width = `${Math.min(rect.width, window.innerWidth - rect.left)}px`
|
2022-04-09 16:00:48 +00:00
|
|
|
} else if (element === 'middle') {
|
|
|
|
if (contentPanel !== undefined) {
|
|
|
|
const rect = contentPanel.getBoundingClientRect()
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = `calc(${rect.top}px)`
|
2022-04-09 16:00:48 +00:00
|
|
|
} else {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = '15%'
|
2022-04-09 16:00:48 +00:00
|
|
|
}
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.bottom = '0.75rem'
|
|
|
|
newProps.left = '50%'
|
|
|
|
newProps.transform = 'translateX(-50%)'
|
2022-04-09 16:00:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-24 16:41:09 +00:00
|
|
|
newProps.top = '50%'
|
|
|
|
newProps.left = '50%'
|
|
|
|
newProps.transform = 'translate(-50%, -50%)'
|
2022-04-09 16:00:48 +00:00
|
|
|
show = true
|
|
|
|
}
|
2022-04-24 16:41:09 +00:00
|
|
|
applyStyle(newProps, modalHTML)
|
2022-04-09 16:00:48 +00:00
|
|
|
return show
|
|
|
|
}
|
2022-04-19 09:38:31 +00:00
|
|
|
|
|
|
|
export function eventToHTMLElement (evt: MouseEvent): HTMLElement {
|
|
|
|
return evt.target as HTMLElement
|
|
|
|
}
|