git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
28f7e7c
Branches
Tags
develop-client_server
master
typescript
fs-words.git
client
source
logic.js
[mod] client:translate
Christian Fraß
commited
28f7e7c
at 2021-03-12 00:49:43
logic.js
Blame
History
Raw
async function api_query ( action_name, input ) { const client = lib_comm.client_http_construct ( { "protocol": "http", "host": "localhost", "port": 7777, "path": "/foo/bar", } ); const message = JSON.stringify ( { "action": action_name, "input": input, } ); const answer = await lib_comm.client_http_send(client, message); if (! (answer.code === 200)) { return Promise.reject("server side error"); } else { const output = JSON.parse(answer.text); return Promise.resolve(output); } } /** */ function main ( ) { // tabify { for (let dom_tab of document.querySelectorAll(".tab")) { for (let dom_tab_select of dom_tab.querySelectorAll(".tab-select li")) { dom_tab_select.addEventListener ( "click", (event) => { const tab_name = dom_tab_select.getAttribute("rel"); dom_tab.setAttribute("rel", tab_name); } ); } } } // show { document.querySelector(".tab-content[rel=\"show\"] button").addEventListener ( "click", async function () { const concept_id = document.querySelector(".tab-content[rel=\"show\"] inupt").value; const result = await api_query ( "show", { "concept_id": concept_id, } ); let dom_result = document.querySelector("#show_result") dom_result.textContent = JSON.stringify(result, undefined, " "); } ); } // translate { document.querySelector(".tab-content[rel=\"translate\"] button").addEventListener ( "click", async function () { const language_from = document.querySelector(".tab-content[rel=\"translate\"] select[name=language_from]").value; const language_to = document.querySelector(".tab-content[rel=\"translate\"] select[name=language_to]").value; const token = document.querySelector(".tab-content[rel=\"translate\"] input[name=token]").value; const result = await api_query ( "translate", { "language_from": language_from, "language_to": language_to, "token": token, } ); const dom_ul = document.querySelector("#translate_result"); dom_ul.textContent = ""; for (const entry of result) { let dom_li = document.createElement("li"); dom_li.textContent = (`[${entry.language_from}] ${entry.value_from} ~ [${entry.language_to}] ${entry.value_to}`); dom_ul.appendChild(dom_li); } } ); } } document.addEventListener("DOMContentLoaded", function (event) {main();});