summaryrefslogtreecommitdiff
path: root/deck-button.js
blob: 793c83b3222ff8f3199c02d40d1a2c2d4eff413b (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
const sfx = new Audio("audio/button-press.mp3");

export function DeckButton(name) {
    const button = document.getElementById(name);
    const img = document.getElementById(`${name}-img`);
    const images = { up: `images/${name}-button-up.jpg`, down: `images/${name}-button-down.jpg` };

    let state = "off";

    const turnOn = function () {
        state = "on";
        img.src = images.down;
    };

    const turnOff = function () {
        state = "off";
        img.src = images.up;
    };

    return {
        state: () => state,
        toggle: (opts = {sound: true}) => {
            state === "off" ? turnOn(opts) : turnOff(opts);
            if (opts.sound) sfx.play();
        },
        disable: () => { button.disabled = true; },
        enable: () => { button.disabled = false; },
    };
}