-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
93 lines (71 loc) · 2.91 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<html>
<head>
<link href="css/layout.css" rel="stylesheet" type="text/css">
<link href="css/spinner.css" rel="stylesheet" type="text/css" media="all">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header class="masthead">
<h1 class="site-title">Word Unjumble</h1>
</header>
<main>
<div class="card">
<p>Type the jumbled word</p>
<div>
<input type="text" id="jumbledWord"/>
<button type="button" id="unjumble">Unjumble</button>
</div>
<p>Unjumbled word(s):</p>
<div>
<ul id="spinner" style="display: none">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
</ul>
<div id="result"/>
</div>
</div>
</main>
<script type="module">
import { unjumble, unjumblePromise } from './unjumble.js';
document.getElementById("unjumble").addEventListener('click', function() {
// Show the spinner
let resultEl = document.getElementById("result");
resultEl.replaceChild(document.createElement("div"), resultEl.firstChild);
document.getElementById("spinner").style.display = 'block';
// Setup a callback to do the processing, as it may take time
// and we want to make the sure spinner shows
setTimeout(() => {
unjumblePromise(document.getElementById("jumbledWord").value).then((result) => {
document.getElementById("spinner").style.display = 'none';
let lb = document.createElement("ul");
if (result.length > 0) {
result.forEach(r => {
let li = document.createElement("li");
// textContent is safer as it strips out tags
li.innerHTML = r;
lb.appendChild(li);
});
} else {
let li = document.createElement("li");
li.innerHTML = "<i>no word found</i>";
lb.appendChild(li);
}
resultEl.replaceChild(lb, resultEl.firstChild);
});
}, 250);
});
// Make the button be clicked when user pressed 'enter'
document.querySelector("#jumbledWord").addEventListener("keyup", event => {
if (event.key !== "Enter")
return;
document.querySelector("#unjumble").click();
event.preventDefault();
});
</script>
</body>
</html>