diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-24 00:01:15 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-24 00:01:15 -0700 |
commit | ffcf6b76ce72f6080c99b124d1f1b4b23c1ee6e4 (patch) | |
tree | f5aa9fae666e776c15986e7cb06954141c439788 /web/static/dartboat.js | |
parent | e3afbd56399e12beb37246a879dcf817de9b4ca1 (diff) | |
download | dartboat-ffcf6b76ce72f6080c99b124d1f1b4b23c1ee6e4.tar.gz dartboat-ffcf6b76ce72f6080c99b124d1f1b4b23c1ee6e4.tar.xz |
web: reduce repetition in prompt handlers
Diffstat (limited to 'web/static/dartboat.js')
-rw-r--r-- | web/static/dartboat.js | 189 |
1 files changed, 75 insertions, 114 deletions
diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 358446a..d1463b2 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -26,158 +26,119 @@ function scheduleCCall(f, ms, ...args) { setTimeout(() => Module.ccall(func, null, types, vals), ms); } -function _promptGet() { - return document.getElementById('prompt-input').textContent; -} - -function _promptGetAndClear() { - let val = _promptGet(); - promptHandle('clear'); - return val; -} - -function _promptDeleteLast() { - let e = document.getElementById('prompt-input'); - e.textContent = e.textContent.slice(0, -1); -} - -function _promptClear() { - document.getElementById('prompt-input').textContent = ''; -} - let prompt_handler; let prompt_handlers = { - visit: { + default: { append(val) { let e = document.getElementById('prompt-input'); if (e.textContent.length < 3) { e.textContent += val; - this._update_rem(); + this._on_change?.(); } }, backspace() { - _promptDeleteLast(); - this._update_rem(); + let e = document.getElementById('prompt-input'); + e.textContent = e.textContent.slice(0, -1); + this._on_change?.(); }, clear() { - _promptClear(); - this._update_rem(); + document.getElementById('prompt-input').textContent = ''; + this._on_change?.(); }, - submit() { - let pts = _promptGetAndClear(); - if (pts) - Module.ccall('user_visit', 'number', ['number'], [pts]); + pre(action) { + clearOi(); }, - submit_rem() { - let rem = _promptGetAndClear(); - if (rem) - Module.ccall('user_visit_to_rem', 'number', ['number'], [rem]); + _get() { + return document.getElementById('prompt-input').textContent; }, - undo() { + _get_and_clear() { + let val = this._get(); this.clear(); - let cl = document.getElementById('key_undo').classList; - if (cl.contains('active')) - Module.ccall('user_undo'); - cl.toggle('active'); - }, - - pre(action) { - clearOi(); - if (action != 'undo') - document.getElementById('key_undo').classList.remove('active'); - }, - - _update_rem(val) { - Module.ccall('update_user_rem_from_pts', null, - ['number'], [val || _promptGet()]); + return val; } - }, + } +} - num_darts: { - append(val) { - let e = document.getElementById('prompt-input'); - if (e.textContent.length < 3) - e.textContent += val; - }, +prompt_handlers.visit = { + __proto__: prompt_handlers.default, - backspace() { - _promptDeleteLast(); - }, + submit() { + let pts = this._get_and_clear(); + if (pts) + Module.ccall('user_visit', 'number', ['number'], [pts]); + }, - clear() { - _promptClear(); - }, + submit_rem() { + let rem = this._get_and_clear(); + if (rem) + Module.ccall('user_visit_to_rem', 'number', ['number'], [rem]); + }, - submit() { - let n = _promptGetAndClear(); - if (n) - Module.ccall('resp_numdarts', null, ['number'], [n]); - }, + undo() { + this.clear(); + let cl = document.getElementById('key_undo').classList; + if (cl.contains('active')) + Module.ccall('user_undo'); + cl.toggle('active'); + }, - pre(action) { - clearOi(); - } + pre(action) { + super.pre(action); + if (action != 'undo') + document.getElementById('key_undo').classList.remove('active'); }, - match_over: { - undo() { - let cl = document.getElementById('key_undo').classList; - if (cl.contains('active')) - Module.ccall('user_undo'); - cl.toggle('active'); - }, + _on_change(val) { + Module.ccall('update_user_rem_from_pts', null, + ['number'], [val || this._get()]); + } +}; - submit() { - Module.ccall('match_init'); - }, +prompt_handlers.num_darts = { + __proto__: prompt_handlers.default, - pre(action) { - clearOi(); - } - }, + submit() { + let n = this._get_and_clear(); + if (n) + Module.ccall('resp_numdarts', null, ['number'], [n]); + } +}; - init: { - append(val) { - let e = document.getElementById('prompt-input'); - if (e.textContent.length < 1) - e.textContent += val; - }, +prompt_handlers.match_over = { + __proto__: prompt_handlers.default, - backspace() { - _promptDeleteLast(); - }, + append() {}, - clear() { - _promptClear(); - }, + undo() { prompt_handlers.visit.undo(); }, + pre(action) { prompt_handlers.visit.pre(action); }, - submit() { - let v = _promptGetAndClear(); - if (v) - Module.ccall('start_match', null, ['number'], [v]); - }, + submit() { + Module.ccall('match_init'); + } +}; - pre(action) { - clearOi(); - } +prompt_handlers.init = { + __proto__: prompt_handlers.default, + + submit() { + let v = this._get_and_clear(); + if (v) + Module.ccall('start_match', null, ['number'], [v]); } -} +}; function setPromptHandler(ptr) { - prompt_handler = prompt_handlers[UTF8ToString(ptr)]; - - if (UTF8ToString(ptr) === "init") { - document.querySelectorAll('.keypad').forEach(e => e.style.display = 'none'); - document.getElementById('keypad-init').style.removeProperty('display'); - } else { - document.querySelectorAll('.keypad').forEach(e => e.style.display = 'none'); - document.getElementById('keypad-default').style.removeProperty('display'); - } + let str = UTF8ToString(ptr); + prompt_handler = prompt_handlers[str]; + + document.querySelectorAll('.keypad').forEach(e => e.style.display = 'none'); + document.getElementById(`keypad-${str === 'init' ? 'init' : 'default'}`) + .style.removeProperty('display'); } function promptHandle(action, ...args) { |