summaryrefslogtreecommitdiff
path: root/scripts/gui-common/widgets.js
blob: e365f56353d2c30bb5d752226a4bf00048907a88 (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
/**
 * @typedef {{
 *  el: HTMLElement;
 *  value: T;
 *  set: (value: T) => void;
 *  get: () => T}} Widget<T>
 * @template {any} T
 */

/**
 * @typedef {"button" | "toggle" | "slider" | "checkbox"} WidgetType
 */

/**
 * @template {any} T
 * @param {WidgetType} type
 * @returns {Widget<T>}
 */
function Widget (type = "button") {
    /** @type {Widget<Number>} */
    let out = {
        el: document.createElement("div"),
        value: 0,
        set: (e) => {value = e},
        get: () => this.value,
    };

    out.el.classList = ["widget", type];
    if (type == "checkbox" || type == "toggle")
    {
        out.el.classList.add("button");
    }

    return out;
}

export { Widget };