From e3e0707bb4933721602b7ec66fabce15caff3ee6 Mon Sep 17 00:00:00 2001 From: Poy Chang Date: Sat, 16 Nov 2024 10:40:00 +0800 Subject: [PATCH] refactor result card creation and close floating window functionality --- index.html | 60 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index 3b41c5d..777e8dc 100644 --- a/index.html +++ b/index.html @@ -47,7 +47,16 @@

Microsoft Terminology Lookup

+
+
+
+ @@ -78,16 +87,7 @@

Microsoft Terminology Lookup

if (results.length > 0) { let resultHTML = ""; results.forEach(result => { - const item = result.item; - resultHTML += ` -
-
-
${item.english} / ${item.chinese}
-

Definition: ${item.definition}

- Translate Definition with Bing -
-
- `; + resultHTML += createResultCard(result.item); }); document.getElementById("result").innerHTML = resultHTML; } else { @@ -96,6 +96,28 @@
${item.english} / ${item.chinese}
} } + // Function to create a result card. + function createResultCard(item) { + return ` +
+
+
${item.english} / ${item.chinese}
+

Definition: ${item.definition}

+ Translate Definition with Bing +
+
+ `; + } + + // Function to close the floating window. + function closeFloatingWindow() { + const floatingWindow = document.getElementById("floatingWindow"); + floatingWindow.style.display = "none"; + // Remove the iframe. + const iframe = document.getElementById("floatingIframe"); + if (iframe) iframe.remove(); + } + // Event listener for the search button. document.getElementById("searchButton").addEventListener("click", performSearch); @@ -128,13 +150,13 @@
${item.english} / ${item.chinese}
// Event listener to close the floating window. document.getElementById("closeFloatingWindow").addEventListener("click", function () { - const floatingWindow = document.getElementById("floatingWindow"); - floatingWindow.style.display = "none"; + closeFloatingWindow(); + }); - // Remove the iframe. - const iframe = document.getElementById("floatingIframe"); - if (iframe) { - iframe.remove(); + // Event listener for the ESC key to close the floating window. + document.addEventListener("keydown", function (e) { + if (e.key === "Escape") { + closeFloatingWindow(); } }); }) @@ -142,12 +164,6 @@
${item.english} / ${item.chinese}
console.error('Error fetching terms.xml:', error); }); - \ No newline at end of file