diff options
| -rw-r--r-- | fretboard.mjs | 16 | ||||
| -rw-r--r-- | main.mjs | 25 |
2 files changed, 21 insertions, 20 deletions
diff --git a/fretboard.mjs b/fretboard.mjs index 38fdc0f..d862952 100644 --- a/fretboard.mjs +++ b/fretboard.mjs @@ -83,16 +83,15 @@ function openClick(e) { formChanged(elt.closest('form')); } -// export default function (form, { onSave }) { -// } - export default class extends HTMLElement { - static name = 'x-fretboard' + static name = 'x-fretboard'; static register() { console.debug('Registering Element', this.name, this); customElements.define(this.name, this); } + saveEvent = 'x-fretboard-save'; + constructor() { super(); @@ -113,10 +112,17 @@ export default class extends HTMLElement { elt.onmousedown = fretMousedown; elt.onclick = fretClick; }); + form.querySelectorAll('.save').forEach(elt => { elt.onclick = (e) => { - e.preventDefault(); console.log('Fretboard#onSave'); + e.preventDefault(); + const event = new CustomEvent(this.saveEvent, { + bubbles: true, + cancelable: true, + detail: new FormData(form) + }); + this.dispatchEvent(event); }; }) formChanged(form); @@ -2,16 +2,6 @@ import Fretboard, { fretToNote } from './fretboard.mjs'; import KeyPicker from './key-picker.mjs'; import History from './history.mjs'; -function memoize(fn) { - let val = undefined; - return function () { - if (val === undefined) { - val = fn(); - } - return val; - } -} - function notes(form) { const strings = ['string1', 'string2', 'string3', 'string4', 'string5', 'string6']; const formData = new FormData(form); @@ -19,15 +9,20 @@ function notes(form) { return strings.map((klass) => fretToNote(form, klass, formData.get(klass))); } -function onSave(form) { - console.debug('we done saved', form, notes(form)); -} - function init() { - console.debug('App#init', this); + console.debug('init()', this); Fretboard.register(); KeyPicker.register(); History.register(); + + // todo: maybe just attach the listener to document? + document.querySelectorAll(Fretboard.name).forEach(f => { + f.addEventListener(f.saveEvent, e => { + document.querySelectorAll(History.name).forEach(h => { + h.add(e.detail); + }); + }); + }); } document.addEventListener('DOMContentLoaded', init); |
