-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.html
647 lines (576 loc) · 19.5 KB
/
old.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="description" content="jsbee" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <!-- mobile-friendly -->
<title>JSBee</title>
<style>
a:link {
color: maroon;
}
a:visited {
color: maroon;
}
a:active {
color: maroon;
}
button {
font-weight: bold;
color: maroon;
border: 0;
padding: 4px;
border-radius: 4px;
margin: 4px;
}
button:hover {
font-weight: bold;
background-color: #d8d8d8;
border: 0;
padding: 4px;
border-radius: 4px;
margin: 4px;
}
button:active {
font-weight: bold;
background-color: #c0c0c0;
border: 0;
padding: 4px;
border-radius: 4px;
margin: 4px;
}
.boxed-text {
border: 1px solid black;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 8px;
padding-right: 8px;
}
.center {
text-align: center;
}
.maroon {
color: maroon;
}
.boldmaroon {
font-weight: bold;
color: maroon;
}
body {
color: black;
background-color: white;
font-family: Verdana, sans-serif;
font-size: 150%;
letter-spacing: 0.25px;
line-height: 125%;
}
h1 {
font-weight: bold;
color: maroon;
font-size: 110%;
text-align: center;
margin: 40px;
}
h2 {
font-weight: bold;
color: maroon;
font-size: 105%
}
h3 {
font-weight: bold;
color: maroon;
font-size: 100%
}
input {
font-weight: bold;
color: black;
}
.switch {
position: relative;
display: inline-block;
width: 30px;
height: 17px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 13px;
width: 13px;
left: 2px;
bottom: 2px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(13px);
-ms-transform: translateX(13px);
transform: translateX(13px);
}
/* Rounded sliders */
.slider.round {
border-radius: 17px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
</head>
<!-- ================================================================ -->
<body>
<h1><span class="boxed-text">JSBee</span></h1>
<table>
<tr>
<td>
<button class="maroon" id="jsbee-clear-letters-button"> Clear </button>
</td>
<td>
<input type=text tabindex=1 placeholder="..." size=10 autocapitalize="characters" id="jsbee-required-letters"></input>
</td>
<td>
Required letter(s)
</td>
</tr>
<tr>
<td>
<button class="maroon" id="jsbee-example-button"> Example </button>
</td>
<td>
<input type=text tabindex=2 placeholder="..." size=10 autocapitalize="characters" id="jsbee-other-letters"></input>
</td>
<td>
Other letter(s)
</td>
</tr>
<tr>
<td>
<button class="maroon" id="jsbee-all-others-button">A-Z</button>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<select tabindex=3 id="jsbee-word-list-dropdown">
<option value="./usual-suspects.txt">Usual suspects</option>
<option value="./long-list.txt">Long list</option>
</select>
</td>
<td>
Word list
</td>
</tr>
<tr>
<td>
<button class="maroon" id="jsbee-reset-match-lengths-button"> Reset </button>
</td>
<td>
<span>
<input tabindex=4 type="number" id="jsbee-min-match-length" name="jsbee-min-match-length" min="1" max="16"/>
<input tabindex=5 type="number" id="jsbee-max-match-length" name="jsbee-max-match-length" min="1" max="16"/>
</span>
</td>
<td>
<label for="jsbee-min-match-length">Minimum and maximum word lengths</label>
</td>
</tr>
</table>
<p/>
<div>
<button class="maroon" id="jsbee-output-show-hide-button"> Hide results </button>
</div>
<div>
<span id="jsbee-output-count" style="display:none"> </span>
</div>
<div>
<span id="jsbee-output" style="display:block"> </span>
</div>
<!-- ================================================================ -->
<hr/>
<div>
Source and documentation: <a href="https://github.com/johnkerl/jsbee">https://github.com/johnkerl/jsbee</a>
</div>
<p/>
<div>
<label class="switch">
<input type="checkbox" id="jsbee-light-dark-mode-toggle">
<span class="slider round"></span>
</label>
<span id="jsbee-light-dark-mode-label"> Switch to dark mode </span>
</div>
<!-- ================================================================ -->
<script>
// ----------------------------------------------------------------
// LOGGING
function logAt0(...args) {
logAt(0, ...args)
}
function logAt1(...args) {
logAt(1, ...args)
}
function logAt2(...args) {
logAt(2, ...args)
}
function logAt3(...args) {
logAt(3, ...args)
}
function logAt(level, ...args) {
if (state.logLevel >= level) {
console.log(...args)
}
}
// ----------------------------------------------------------------
// UTILITIES
function isInteger(text) {
// TODO: this accepts '3.4' and should not
return !isNaN(parseInt(text))
}
// ----------------------------------------------------------------
// APPLICATION LOGIC
function setWordList(state, url) {
// CORS make us unable to load ./anything when doing local-disk dev
if (document.URL.startsWith("file://")) {
state.wordListURL = "local-in-memory"
state.wordList = [
"abaci", "acacia", "acai", "acetate", "affiant", "aioli", "alfalfa", "alga", "algae", "algal",
"allele", "ameba", "amoeba", "amoebae", "amyl", "anneal", "anyhoo", "apian", "apnea", "arancini",
"arhat", "aria", "aril", "arnica", "atilt", "atria", "attaboy", "baaed", "baba", "babel", "babka",
"bailiff", "banana", "bandana", "bandanna", "barbacoa", "blin", "blini", "boll", "bombe", "bonbon",
"bonobo", "booboo", "bootee", "bootie", "bowwow", "cabala", "cabana", "cacao", "cacti", "caddie",
"caddied", "caiman", "cairn", "calla", "callaloo", "cami", "cancan", "canid", "canna", "canola",
"cantata", "canteen", "captcha", "carioca", "celeb", "celli", "cellule", "cetacean", "chai", "chia",
"chic", "chica", "chichi", "ciao", "cirri", "coca", "cocci", "cocoa", "cocoon", "codon", "coho",
"coir", "colcannon", "collab", "croon", "deicide", "ditto", "dorado", "dunno", "electee", "emcee",
"emceed", "epee", "falafel", "fantail", "froufrou", "galangal", "gavage", "gelee", "gooey",
"google", "googol", "gotta", "haka", "hake", "halal", "hamachi", "haram", "hatha", "hoodoo",
"iambi", "icemen", "imam", "infill", "innie", "iota", "laic", "laical", "lallygag", "lanai",
"lanolin", "lantana", "lavage", "libelee", "llano", "logroll", "lollygag", "lookback", "lookbook",
"loonie", "luging", "lunula", "magma", "mahatma", "malic", "mama", "melee", "mentee", "minim",
"momma", "muumuu", "naan", "nada", "naiad", "nana", "nankeen", "natant", "nene", "neocon", "niacin",
"noncom", "nonillion", "nuncio", "oleo", "olio", "onion", "paean", "paella", "palapa", "pampa",
"panini", "papa", "payee", "peahen", "peke", "pica", "pinniped", "pintail", "pipit", "pippin",
"pitapat", "pooh", "poplin", "poppa", "powwow", "raffia", "raita", "rapini", "ratatat", "ribbit",
"rococo", "tahini", "tali", "tannic", "tannin", "tantara", "tartan", "tartar", "tattoo", "tilth",
"tinct", "titmice", "toccata", "tomtit", "toon", "toonie", "tori", "toric", "torii", "tutee",
"tutti", "tutu", "undid", "vanilla", "vanillin", "woohoo",
]
showMatches(state)
} else {
logAt0("FETCHING WORD LIST FROM URL:", url)
state.wordListURL = url
fetch(url).then((response) => response.text().then((text) => {
state.wordList = text.split("\n")
// Must be called from this callback, since this is async
showMatches(state)
}))
}
}
function checkWordList(state) {
if (state.wordListURL == null) {
outputElement.textContent = "No word list set"
return false
}
if (state.wordList == null) {
outputElement.textContent = "Failed to fetch word list from \"" + url + "\""
return false
}
return true
}
function showMatches(state) {
if (!checkWordList(state)) {
return
}
requiredLetters = requiredLettersElement.value
otherLetters = otherLettersElement.value
matches = getMatchesFromWordList(state, requiredLetters, otherLetters)
matches = matches.map(match => match.toUpperCase())
matches = matches.sort()
logAt2(matches)
outputCountElement.textContent = "(" + String(matches.length) + " hidden)"
if (requiredLetters.length == 0 && otherLetters.length == 0) {
outputElement.textContent = ""
} else if (matches.length == 0) {
outputElement.textContent = "(No matches)"
} else {
outputElement.textContent = matches.join(" ")
}
}
function getMatchesFromWordList(state, requiredLetters, optionalLetters) {
logAt1("MATCHER ENTER")
logAt1("requiredLetters ", requiredLetters)
logAt1("optionalLetters", optionalLetters)
logAt1("wordList.length", state.wordList.length)
logAt3("wordList", state.wordList)
requiredLetters = requiredLetters.toLowerCase()
optionalLetters = optionalLetters.toLowerCase()
matches = []
state.wordList.forEach((word) => {
logAt3("WORD <<", word, ">>")
if (wordMatches(state, requiredLetters, optionalLetters, word)) {
logAt2("MATCH", word)
matches.push(word)
}
})
return matches
}
function wordMatches(state, requiredLetters, optionalLetters, word) {
let allLetters = requiredLetters + optionalLetters
if (state.minMatchLength != null && word.length < state.minMatchLength) {
logAt3("-- match out 1: word", word, "with length", word.length, "less than min", state.minMatchLength)
return false
}
if (state.maxMatchLength != null && word.length > state.maxMatchLength) {
logAt3("-- match out 2: word", word, "with length", word.length, "more than max", state.maxMatchLength)
return false
}
for (let i = 0; i < requiredLetters.length; i++) {
requiredLetter = requiredLetters[i]
if (word.indexOf(requiredLetter) < 0) {
logAt3("-- match out 3: word", word, "missing required letter", requiredLetter, "from", requiredLetters)
return false
}
}
for (let i = 0; i < word.length; i++) {
let letter = word[i]
if (allLetters.indexOf(letter) < 0) {
logAt3("-- match out 4: word", word, "missing letter", letter, "from", allLetters)
return false
}
}
logAt2("-- match:", word)
return true
}
function setAllOthers() {
requiredLettersElement.value = ""
otherLettersElement.value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
outputElement.textContent = ""
}
function setExample() {
requiredLettersElement.value = "E"
otherLettersElement.value = "XAMPLS"
outputElement.textContent = ""
}
function setLightMode() {
document.body.style.color = "black"
document.body.style.backgroundColor = "white"
lightDarkModeLabel.textContent = "Switch to dark mode"
localStorage.setItem(state.localStorageModeKey, "false")
}
function setDarkMode() {
document.body.style.color = "white"
document.body.style.backgroundColor = "black"
lightDarkModeLabel.textContent = "Switch to light mode"
localStorage.setItem(state.localStorageModeKey, "true")
}
// ----------------------------------------------------------------
// ELEMENTS
const requiredLettersElement = document.getElementById("jsbee-required-letters")
const otherLettersElement = document.getElementById("jsbee-other-letters")
const wordListDropdown = document.getElementById("jsbee-word-list-dropdown")
const minMatchLengthElement = document.getElementById("jsbee-min-match-length")
const maxMatchLengthElement = document.getElementById("jsbee-max-match-length")
const clearLettersButton = document.getElementById("jsbee-clear-letters-button")
const allOthersButton = document.getElementById("jsbee-all-others-button")
const exampleButton = document.getElementById("jsbee-example-button")
const resetMatchLengthsButton = document.getElementById("jsbee-reset-match-lengths-button")
const outputShowHideButton = document.getElementById("jsbee-output-show-hide-button")
const outputCountElement = document.getElementById("jsbee-output-count")
const outputElement = document.getElementById("jsbee-output")
const lightDarkModeElement = document.getElementById("jsbee-light-dark-mode-toggle")
const lightDarkModeLabel = document.getElementById("jsbee-light-dark-mode-label")
// Dev notes: I've used React before and it's powerful. Sometimes for fun
// I like to use plain ES6, HTML, CSS, and the browser API -- nothing
// "in the middle".
//
// That said, JS libraries like React do simplify things. Factors as of this writing:
//
// * Some initial state (such as visibilities) is spread between HTML and JS init logic.
// * The min-match-length and max-match-length buttons, individually, have
// identical logic for when the user types in non-numeric things. This
// could be put into a class.
// * The min-match-length and max-match-length buttons, as a pair, have a
// cross-check: the left's value cannot exceed the right's.
// * There is some controller/controllee coupling, e.g. which button resets
// which other widgets' values.
// * There is duplication of information (and referential-integrity work)
// between the `state` object and widget state.
//
// Food for thought. This has been a quick holiday project; nothing more.
// ----------------------------------------------------------------
// EVENT LISTENERS
requiredLettersElement.addEventListener("input", function(event) {
logAt1("REQUIRED-LETTERS INPUT: <<", event.target.value, ">>")
requiredLettersElement.value = event.target.value.toUpperCase()
showMatches(state)
})
otherLettersElement.addEventListener("input", function(event) {
logAt1("OTHER-LETTERS INPUT: <<", event.target.value, ">>")
otherLettersElement.value = event.target.value.toUpperCase()
showMatches(state)
})
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number
minMatchLengthElement.addEventListener("change", function(event) {
let self = minMatchLengthElement
logAt1("MIN LENGTH CHANGE", event.target.value, "; CURRENT", state.minMatchLength, state.maxMatchLength)
// The up-and-down sliders won't let the user choose outside the widget's min/max.
// But the user can still type "999" or "aaa" into the widget. Here we protect against this.
if (!isInteger(event.target.value)) {
self.value = self.defaultValue
state.minMatchLength = self.value
showMatches(state)
return;
}
let requestedValue = Number(event.target.value)
// Check self-bounds
if (requestedValue < self.min) {
state.minMatchLength = self.min
self.value = self.min
} else if (requestedValue > self.max) {
state.minMatchLength = self.max
self.value = self.max
// Check other-bounds
} else if (requestedValue > state.maxMatchLength) {
self.value = state.maxMatchLength
} else {
state.minMatchLength = requestedValue
}
showMatches(state)
})
maxMatchLengthElement.addEventListener("change", function(event) {
let self = maxMatchLengthElement
logAt1("MAX LENGTH CHANGE", event.target.value, "; CURRENT", state.minMatchLength, state.maxMatchLength)
// The up-and-down sliders won't let the user choose outside the widget's min/max.
// But the user can still type "999" or "aaa" into the widget. Here we protect against this.
if (!isInteger(event.target.value)) {
self.value = self.defaultValue
state.maxMatchLength = self.value
showMatches(state)
return;
}
let requestedValue = Number(event.target.value)
// Check self-bounds
if (requestedValue < self.min) {
state.maxMatchLength = self.min
self.value = self.min
} else if (requestedValue > self.max) {
state.maxMatchLength = self.max
self.value = self.max
// Check other-bounds
} else if (requestedValue < state.minMatchLength) {
self.value = state.minMatchLength
} else {
state.maxMatchLength = requestedValue
}
showMatches(state)
})
clearLettersButton.addEventListener("click", function(event) {
logAt1("CLEAR-LETTERS BUTTON WAS CLICKED")
requiredLettersElement.value = ""
otherLettersElement.value = ""
showMatches(state)
})
allOthersButton.addEventListener("click", function(event) {
logAt1("ALL-OTHERS BUTTON WAS CLICKED")
setAllOthers()
showMatches(state)
})
exampleButton.addEventListener("click", function(event) {
logAt1("EXAMPLE BUTTON WAS CLICKED")
setExample()
showMatches(state)
})
resetMatchLengthsButton.addEventListener("click", function(event) {
logAt1("RESET-MATCH-LENGTHS BUTTON WAS CLICKED")
minMatchLengthElement.value = state.defaultMinMatchLength
maxMatchLengthElement.value = state.defaultMaxMatchLength
state.minMatchLength = state.defaultMinMatchLength
state.maxMatchLength = state.defaultMaxMatchLength
showMatches(state)
})
outputShowHideButton.addEventListener("click", function(event) {
let self = outputShowHideButton
logAt1("OUTPUT SHOW-HIDE BUTTON WAS CLICKED")
if (state.outputHidden) {
state.outputHidden = false
self.textContent = "Hide results"
outputCountElement.style.display = "none"
outputElement.style.display = "block"
} else {
state.outputHidden = true
self.textContent = "Show results"
outputCountElement.style.display = "block"
outputElement.style.display = "none"
}
})
wordListDropdown.addEventListener("change", function() {
url = wordListDropdown.value
logAt1("SELECTED WORD LIST: <<", url, ">>")
setWordList(state, url)
showMatches(state)
})
lightDarkModeElement.addEventListener("change", function(e) {
if (lightDarkModeElement.checked) {
setDarkMode()
} else {
setLightMode()
}
})
// ----------------------------------------------------------------
// APPLICATION STATE
state = {
"logLevel": 0, // 0,1,2,3
"defaultMinMatchLength": 4,
"defaultMaxMatchLength": 16,
"minMatchLength": Number(minMatchLengthElement.value),
"maxMatchLength": Number(maxMatchLengthElement.value),
"wordListURL": null,
"wordList": null,
"outputHidden": false,
"localStorageModeKey": document.URL + ":jsbee-dark-mode-enabled",
}
minMatchLengthElement.value = state.defaultMinMatchLength
maxMatchLengthElement.value = state.defaultMaxMatchLength
state.minMatchLength = state.defaultMinMatchLength
state.maxMatchLength = state.defaultMaxMatchLength
setWordList(state, wordListDropdown.value)
if (localStorage.getItem(state.localStorageModeKey) == "true") {
setDarkMode()
lightDarkModeElement.checked = true
} else {
setLightMode()
lightDarkModeElement.checked = false
}
requiredLettersElement.focus()
</script>
<!-- ================================================================ -->
</body>
</html>