diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-09-18 19:50:32 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-09-18 19:50:32 -0700 |
commit | b44d19b35e120133a4bb751e176aec4d14b6cbc5 (patch) | |
tree | 05b23ef7e70e57f2cf38584a20c864ff0d7e4853 /ext/fg/js/frame.js | |
parent | 7bd642e53868e0a3154b285be969cc2e6068f46f (diff) | |
parent | 4e2d7327c5b1aa8eed035dc4c177a3b9b49c5ab7 (diff) |
Merge branch 'dev'
Diffstat (limited to 'ext/fg/js/frame.js')
-rw-r--r-- | ext/fg/js/frame.js | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 8f3d3877..8a99a405 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -17,48 +17,39 @@ */ +function invokeApi(action, params, target) { + target.postMessage({action, params}, '*'); +} + function registerKanjiLinks() { for (const link of Array.from(document.getElementsByClassName('kanji-link'))) { - link.addEventListener('click', (e) => { + link.addEventListener('click', e => { e.preventDefault(); - window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*'); + invokeApi('displayKanji', e.target.innerHTML, window.parent); }); } } function registerAddNoteLinks() { for (const link of Array.from(document.getElementsByClassName('action-add-note'))) { - link.addEventListener('click', (e) => { + link.addEventListener('click', e => { e.preventDefault(); const ds = e.currentTarget.dataset; - window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*'); + invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent); }); } } function registerAudioLinks() { for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) { - link.addEventListener('click', (e) => { + link.addEventListener('click', e => { e.preventDefault(); const ds = e.currentTarget.dataset; - window.parent.postMessage({action: 'playAudio', params: ds.index}, '*'); + invokeApi('playAudio', ds.index, window.parent); }); } } -function onDomContentLoaded() { - registerKanjiLinks(); - registerAddNoteLinks(); - registerAudioLinks(); -} - -function onMessage(e) { - const {action, params} = e.data, method = window['api_' + action]; - if (typeof(method) === 'function') { - method(params); - } -} - function api_setActionState({index, state, sequence}) { for (const mode in state) { const matches = document.querySelectorAll(`.action-bar[data-sequence="${sequence}"] .action-add-note[data-index="${index}"][data-mode="${mode}"]`); @@ -75,5 +66,15 @@ function api_setActionState({index, state, sequence}) { } } -document.addEventListener('DOMContentLoaded', onDomContentLoaded, false); -window.addEventListener('message', onMessage); +document.addEventListener('DOMContentLoaded', () => { + registerKanjiLinks(); + registerAddNoteLinks(); + registerAudioLinks(); +}); + +window.addEventListener('message', e => { + const {action, params} = e.data, method = window['api_' + action]; + if (typeof(method) === 'function') { + method(params); + } +}); |