From 9a0f3d15dfaac41657bb549c7e246d2ac81fdf4c Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 3 May 2022 12:29:09 -0700 Subject: web: use generic JS functions for manipulating DOM --- web/static/dartboat.js | 86 ++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 62 deletions(-) (limited to 'web/static') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 359abbd..b420543 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -29,37 +29,37 @@ function toCString(str) { // caller must free return ptr; } -function promptGet() { // caller must free - return toCString($('#prompt-input').textContent); +function elemGetContent(sel) { // caller must free + return toCString($(UTF8ToString(sel)).textContent); } -function promptHandle(command, data) { - const str_c = toCString(command); - const str_d = data && toCString(data); - _prompt_handle(str_c, str_d); - - _free(str_c); - if (str_d) _free(str_d); +function elemSetContent(sel, str) { + $(UTF8ToString(sel)).textContent = str ? UTF8ToString(str) : ''; } -function setPromptActive() { - $('#prompt').classList.add('active') +function elemHasClass(sel, c) { + return $(UTF8ToString(sel)).classList.contains(UTF8ToString(c)); } -function setPromptInactive() { - $('#prompt').classList.remove('active') +function elemAddClass(sel, c) { + $(UTF8ToString(sel)).classList.add(UTF8ToString(c)); } -function setPromptMsgL(ptr) { - $('#prompt-msg-l').textContent = ptr ? UTF8ToString(ptr) : ''; +function elemRemoveClass(sel, c) { + $(UTF8ToString(sel)).classList.remove(UTF8ToString(c)); } -function setPromptMsgR(ptr) { - $('#prompt-msg-r').textContent = ptr ? UTF8ToString(ptr) : ''; +function elemToggleClass(sel, c) { + $(UTF8ToString(sel)).classList.toggle(UTF8ToString(c)); } -function setPromptInput(ptr) { - $('#prompt-input').textContent = ptr ? UTF8ToString(ptr) : ''; +function promptHandle(command, data) { + const str_c = toCString(command); + const str_d = data && toCString(data); + _prompt_handle(str_c, str_d); + + _free(str_c); + if (str_d) _free(str_d); } function setKeypad(keypad) { @@ -68,22 +68,6 @@ function setKeypad(keypad) { e.style.display = e.id === keypad_id ? '' : 'none'); } -function isKeyActive(k) { - return $(`#key-${UTF8ToString(k)}`).classList.contains('active'); -} - -function toggleKey(k) { - $(`#key-${UTF8ToString(k)}`).classList.toggle('active'); -} - -function deactivateKey(k) { - $(`#key-${UTF8ToString(k)}`).classList.remove('active'); -} - -function setKeyLabel(k, ptr) { - $(`#key-${UTF8ToString(k)}`).textContent = UTF8ToString(ptr); -} - function setPlayerActive(n) { $$('[id$=-info]').forEach(e => e.classList[n && e.id == `p${n}-info` ? 'add' : 'remove']('active')); @@ -97,26 +81,6 @@ function hidePlayerInfo(n) { $(`#p${n}-info-inner`).style.visibility = 'hidden'; } -function updatePlayerName(n, ptr) { - $(`#p${n}-name`).textContent = ptr ? UTF8ToString(ptr) : ''; -} - -function updatePlayerRem(n, ptr) { - $(`#p${n}-rem`).textContent = ptr ? UTF8ToString(ptr) : ''; -} - -function updatePlayerSugg(n, ptr) { - $(`#p${n}-sugg`).textContent = ptr ? UTF8ToString(ptr) : ''; -} - -function updatePlayerAvg(n, avg) { - $(`#p${n}-avg`).textContent = avg.toFixed(2); -} - -function clearVisits() { - $('#visits').textContent = ''; -} - const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 0]; function drawVisit(visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts) { const e = $('#visits'); @@ -136,8 +100,10 @@ function drawVisit(visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts) { e.scrollTop = e.scrollHeight; } -function svgDrawElemv(layer, elemc, elemv, off_name, off_n_attrs, off_attr_names, - off_attr_vals, off_content) { +function svgAppendElemv(sel, elemc, elemv, off_name, off_n_attrs, + off_attr_names, off_attr_vals, off_content) { + target = $(UTF8ToString(sel)); + for (let ptr = elemv; ptr < elemv + 4*elemc; ptr += 4) { const struct = HEAP32[ptr>>2]; @@ -156,14 +122,10 @@ function svgDrawElemv(layer, elemc, elemv, off_name, off_n_attrs, off_attr_names const content = HEAP32[(struct + off_content)>>2]; if (content) e.textContent = UTF8ToString(content); - $(`#dartboard .${layer ? 'overlay' : 'base'}`).appendChild(e); + target.appendChild(e); } } -function svgClearPoints() { - $('#dartboard .overlay').textContent = ''; -} - function updateDelay(val) { $('#delay').value = val; } -- cgit v1.2.3-70-g09d2