aboutsummaryrefslogtreecommitdiff
path: root/web/script/player/popup.ts
blob: 394f572a25543dc6007d08dc16e4d8cea7c555ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/

export class Popup {
    trigger_hov = false
    content_hov = false
    content?: HTMLElement
    shown = false

    constructor(
        public trigger: HTMLElement,
        public container: HTMLElement,
        public new_content: () => HTMLElement
    ) {
        trigger.onmouseenter = () => {
            this.trigger_hov = true;
            this.update_hov()
        }
        trigger.onmouseleave = () => {
            this.trigger_hov = false;
            this.update_hov()
        }
    }

    set_shown(s: boolean) {
        if (this.shown == s) return
        if (s) {
            this.content = this.new_content()
            this.content.addEventListener("mouseenter", () => {
                this.content_hov = true;
                this.update_hov()
            })
            this.content.addEventListener("mouseleave", () => {
                this.content_hov = false;
                this.update_hov()
            })
            this.content.classList.add("jsp-popup")
            this.container.append(this.content)
        } else {
            const content = this.content!
            content.classList.add("jsp-popup-out")
            setTimeout(() => {
                this.container.removeChild(content)
            }, 100)
            this.content = undefined
        }
        this.shown = s
    }

    hide_timeout?: number
    update_hov() {
        if (this.content_hov || this.trigger_hov) {
            this.set_shown(true)
            if (this.hide_timeout !== undefined) {
                clearTimeout(this.hide_timeout)
                this.hide_timeout = undefined
            }
        } else {
            if (this.hide_timeout === undefined) this.hide_timeout = setTimeout(() => {
                this.set_shown(false)
                this.hide_timeout = undefined
            }, 100)
        }
    }
}