import { FlowWindowConfig } from '../types'
import { sanitize } from '../utils'
import nullIcon from '../assets/icons/application-default-icon.svg'
import ProcessLib from './ProcessLib'
/**
* Makes an element draggable.
*
* @param element The element to become draggable.
* @param container The draggable element container.
*/
function dragElement (element: HTMLElement, container: HTMLElement): void {
let posX = 0; let posY = 0
element.querySelector('window-header')?.addEventListener('mousedown', dragMouseDown)
function dragMouseDown (e: MouseEvent): void {
e.preventDefault()
closeAll()
posX = e.clientX
posY = e.clientY
document.onmouseup = closeDragElement
document.onmousemove = elementDrag
}
function elementDrag (e: MouseEvent): void {
e.preventDefault()
const dx = e.clientX - posX
const dy = e.clientY - posY
const newTop = element.offsetTop + dy
const newLeft = element.offsetLeft + dx
const containerWidth = container.offsetWidth
const containerHeight = container.offsetHeight
if (newTop >= 0 && newTop + element.offsetHeight <= containerHeight) {
element.style.top = `${newTop}px`
}
if (newLeft >= 0 && newLeft + element.offsetWidth <= containerWidth) {
element.style.left = `${newLeft}px`
}
posX = e.clientX
posY = e.clientY
}
function closeDragElement (): void {
document.onmouseup = null
document.onmousemove = null
container.onmouseleave = null
}
function closeAll (): void {
closeDragElement()
container.onmouseenter = null
}
}
class FlowWindow {
element: HTMLElement
private readonly header: HTMLElement
private readonly realContent: HTMLElement
content: HTMLElement
maximized: boolean
minimized: boolean
width: number
height: number
isMinimized = false
isMaximized = false
wm: any
readonly process: ProcessLib
onClose: () => void
config: FlowWindowConfig
/**
* Creates a window session.
*
* @param wm The current window manager session.
* @param config The window's pre-set config.
*/
constructor (process: ProcessLib, wm: any, config: FlowWindowConfig, onClose = () => {}) {
this.process = process
this.wm = wm
this.config = config
this.onClose = onClose
this.element = document.createElement('window')
this.element.style.zIndex = (wm.getHighestZIndex() as number + 1).toString()
this.element.style.position = 'absolute'
this.focus()
this.element.onmousedown = () => {
this.focus()
}
if (config.canResize === undefined || config.canResize === null) config.canResize = true
if (!config.canResize) this.element.style.resize = 'none'
this.element.style.width = `${config.width ?? 300}px`
this.element.style.height = `${config.height ?? 200}px`
this.header = document.createElement('window-header')
this.header.innerHTML = `