From dca2e78c27b1d73dfbecdeb1a697f6785981db91 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 26 Apr 2022 02:42:42 -0700 Subject: web: rewrite prompt handlers in c --- web/static/dartboat.js | 135 ++++++++++--------------------------------------- 1 file changed, 27 insertions(+), 108 deletions(-) (limited to 'web/static') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 9cedc4a..f0855c2 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -26,128 +26,47 @@ function scheduleCCall(f, ms, ...args) { setTimeout(() => Module.ccall(func, null, types, vals), ms); } -let prompt_handler; -let prompt_handlers = { - default: { - _get() { - return document.getElementById('prompt-input').textContent; - }, - - _get_and_clear() { - let val = this._get(); - this.clear(); - return val; - }, - - _pre(action) { - clearOi(); - }, - - append(val) { - let e = document.getElementById('prompt-input'); - if (e.textContent.length < 3) { - let tc = e.textContent += val; - this._on_change?.(tc); - } - }, - - backspace() { - let e = document.getElementById('prompt-input'); - let tc = e.textContent = e.textContent.slice(0, -1); - this._on_change?.(tc); - }, - - clear() { - let tc = document.getElementById('prompt-input').textContent = ''; - this._on_change?.(tc); - } - } +function promptGet() { + let tc = document.getElementById('prompt-input').textContent; + let len = lengthBytesUTF8(tc) + 1; + let str = _malloc(len); + stringToUTF8(tc, str, len); + return str; } -prompt_handlers.default_with_undo = { - __proto__: prompt_handlers.default, - - _pre(action) { - super._pre(action); - if (action != 'undo') - document.getElementById('key_undo').classList.remove('active'); - }, - - undo() { - this.clear(); - let cl = document.getElementById('key_undo').classList; - if (cl.contains('active')) - Module.ccall('user_undo'); - cl.toggle('active'); - } +function promptClear() { + document.getElementById('prompt-input').textContent = ''; } -prompt_handlers.visit = { - __proto__: prompt_handlers.default_with_undo, - - _on_change(val) { - Module.ccall('update_user_rem_from_pts', null, - ['number'], [val || this._get()]); - }, - - submit() { - let pts = this._get_and_clear(); - if (pts) - Module.ccall('user_visit', null, ['number'], [pts]); - }, - - submit_rem() { - let rem = this._get_and_clear(); - if (rem) - Module.ccall('user_visit_to_rem', null, ['number'], [rem]); - } -}; - -prompt_handlers.num_darts = { - __proto__: prompt_handlers.default_with_undo, - - submit() { - let n = this._get_and_clear(); - if (n) - Module.ccall('user_num_darts', null, ['number'], [n]); - } -}; - -prompt_handlers.end_match = { - __proto__: prompt_handlers.default_with_undo, - - append() {}, +function isUndoActive() { + return document.getElementById('key_undo').classList.contains('active'); +} - submit() { - Module.ccall('end_match'); - } -}; +function toggleUndo() { + document.getElementById('key_undo').classList.toggle('active'); +} -prompt_handlers.select_mode = { - __proto__: prompt_handlers.default, - _keypad: 'select_mode', +function deactivateUndo() { + document.getElementById('key_undo').classList.remove('active'); +} - submit() { - let v = this._get_and_clear(); - if (v) - Module.ccall('start_match', null, ['number'], [v]); - } -}; +function setPromptActive() { + document.getElementById('prompt').classList.add('active') +} -function setPromptHandler(ptr) { - prompt_handler = prompt_handlers[UTF8ToString(ptr)]; - document.getElementById('prompt').classList[ - prompt_handler ? 'add' : 'remove']('active'); +function setPromptInactive() { + document.getElementById('prompt').classList.remove('active') +} - let keypad_id = `keypad-${prompt_handler?._keypad || 'default'}`; +function setKeypad(keypad) { + let keypad_id = `keypad-${UTF8ToString(keypad)}`; document.querySelectorAll('.keypad').forEach(e => { e.style.display = e.id === keypad_id ? '' : 'none'; }); } -function promptHandle(action, ...args) { - prompt_handler?._pre?.(action); - prompt_handler?.[action]?.(...args); +function promptHandle(action, data) { + Module.ccall('prompt_handle', null, ['string', 'string'], [action, data]); } function setPromptInput(ptr) { -- cgit v1.2.3-70-g09d2