summaryrefslogtreecommitdiffstats
path: root/tests.mjs
blob: e7095f890525af087ab86e95ac3702da5421728b (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { chromaticScale } from "./scale.mjs";

customElements.define('x-string', class extends HTMLElement {
    static observedAttributes = ['tonic', 'value', 'frets'];
    constructor() {
        super();
        console.debug('x-string#constructor', this);
        const template = this.querySelector('template').content;
        const shadowRoot = this.attachShadow({mode: 'open'}).appendChild(template.cloneNode(true));
        console.debug('attached', template, 'to shadow root', shadowRoot);

        this.muted.onclick = this.muteClicked.bind(this);
        this.tonic.onclick = this.fretClicked.bind(this);
    }

    get tonic() {
        return this.fretList.querySelector('.tonic');
    }

    get fretList() {
        return this.querySelector('.fretlist');
    }

    get fretInputs() {
        return this.querySelectorAll('.fretlist input[name="string"]');
    }

    get muted() {
        return this.querySelector('input[name="muted"]');
    }

    get selected() {
        return this.querySelector('[slot="selected"]');
    }

    get chromIndex() {
        return chromaticScale.indexOf(this.getAttribute('tonic'));
    }

    muteClicked(e) {
        e.preventDefault();
        console.debug('x-string#muteClicked', this, e.target.checked);
        if (e.target.checked) {
            this.setAttribute('value', 'x');
        } else {
            this.setAttribute('value', this.getAttribute('tonic'));
        }
    }

    fretClicked(e) {
        e.preventDefault();
        console.debug('x-string#fretClicked', this, e.target.getAttribute('value'));
        this.setAttribute('value', e.target.getAttribute('value'));
    }

    attributeChangedCallback(name, old, v) {
        console.debug('x-string#attributeChangedCallback', this, name, old, v);
        switch (name) {
        case 'tonic':
            return this.updateTonic(v);
        case 'value':
            return this.updateValue(v);
        case 'frets':
            return this.updateFrets(Number(old || '0'), Number(v));
        }
    }

    updateTonic(tonic) {
        console.debug('x-string#updateTonic', this, tonic);

        // for compat with the radio inputs.
        this.tonic.setAttribute('value', tonic);
        this.tonic.textContent = tonic;

        // update all fret values
        this.fretInputs
            .forEach((fret, i) => {
                console.debug(' -- fret', i, fret);
                fret.setAttribute('value', i.toString());
            });
    }

    updateValue(value) {
        console.debug('x-string#updateValue', this, value);
        this.selected.textContent = value;
        if (value === 'x') {
            this.classList.add('muted');
            this.muted.checked = true;
            return;
        } else {
            this.classList.remove('muted');
            this.muted.checked = false;
        }

        this.fretInputs
            .forEach(fret => {
                fret.checked = this.getAttribute('value') === fret.getAttribute('value');
                if (fret.checked) {
                    fret.classList.add('checked');
                } else {
                    fret.classList.remove('checked');
                }
                console.debug(' -- fret', fret, fret.checked);
            });
    }

    updateFrets(old, frets) {
        console.debug('x-string#updateFrets', this, old, frets);
        if (old < frets) {
            const tonic = this.getAttribute('tonic');
            if (!tonic) {
                return;
            }

            const chromIndex = this.chromIndex;
            const fretList = this.fretList;
            for (let i = old+1; i <= frets; i++) {
                const note = chromaticScale[chromIndex + i];
                console.debug(' -- appending fret', i, 'from', note, fretList);

                const input = document.createElement('input');
                input.setAttribute('type', 'radio');
                input.setAttribute('name', 'string');
                input.setAttribute('value', note);
                input.onclick = this.fretClicked.bind(this);

                const li = document.createElement('li');
                li.appendChild(input);
                fretList.appendChild(li);
            }
        }
    }
});