// 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 = `<dl><dt class="glossary-origin">${origin}</dt>` ;
|
for(let p of phrases) {
|
html += `<dd class="glossary-target">${p["text"]}</dd>`
|
}
|
html += "</dl>";
|
const container = document.getElementById("translation");
|
container.innerHTML = html;
|
} else { // there is no translation for the origin
|
let html = `Keine Übersetzung für <span class="glossary-origin">${origin}</span> 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);
|
})
|
});
|