// Client code, import {translation} from "./glossary-async.js"; const domCallbacks = { start() { console.log("TODO: show an indication that browser is loading translation from server"); }, finish() { console.log("TODO: remove the loading indication"); }, onSuccess(json) { const origin = json["origin"]["text"]; const target = json["target"]; if(target.length > 0) { console.log(target); const phrases = target[0]["phrases"]; let html = `
${origin}
` ; for(let p of phrases) { html += `
${p["text"]}
` } html += "
"; const container = document.getElementById("translation"); container.innerHTML = html; } else { // there is no translation for the origin let html = `Keine Übersetzung für ${origin} in französisch :(`; const container = document.getElementById("translation"); container.innerHTML = html; } }, onError(response) { console.log("TODO: shows HTTP-Status and some explaination on browser"); } } function onGlossaryWordClick(event) { const targetElement = event.target; const phrase = targetElement.dataset["phrase"]; // match value of attribute data-phrase const targetLanguage = "fr"; translation(phrase, targetLanguage, domCallbacks); } document.addEventListener("DOMContentLoaded", () => { const glossaryWords = document.querySelectorAll(".glossary-online"); glossaryWords.forEach(element => { element.addEventListener("click", onGlossaryWordClick); }) });