vail

Internet morse code repeater
git clone https://git.woozle.org/neale/vail.git

vail / static / scripts
Neale Pickett  ·  2023-01-28

icon.mjs

 1import * as time from "./time.mjs"
 2
 3const defaultIcon = "default"
 4
 5class Icon {
 6    /**
 7     * @param {Number} timeoutDuration Duration of timeout
 8     */
 9    constructor(timeoutDuration = 2*time.Second) {
10        this.timeoutDuration = timeoutDuration
11    }
12
13    /**
14     * Set the icon type
15     * 
16     * @param {String} iconType Icon to set to
17     */
18    Set(iconType=defaultIcon) {
19        if (iconType != defaultIcon) {
20            clearTimeout(this.cleanupTimer)
21            this.cleanupTimer = setTimeout(() => this.Set(), this.timeoutDuration)
22        }
23
24        for (let e of document.querySelectorAll("link[rel=icon]")) {
25			if (! e.dataset[defaultIcon]) {
26				e.dataset[defaultIcon] = e.href
27			}
28			let url = e.dataset[iconType]
29            if (url) {
30                e.href = url
31            } else {
32                console.warn(`No data-${iconType} attribute`, e)
33            }
34		}
35    }
36
37    /**
38     * Set icon at the provided time.
39     *
40     * @param {String} iconType Icon to set to
41     * @param {Number} when Time to set the value
42     */
43    SetAt(iconType, when=null) {
44        setTimeout(() => this.Set(iconType), when - Date.now())
45    }
46}
47
48export {
49    Icon,
50}