|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>Mojifinder</title> |
| 6 | + <style> |
| 7 | + body {font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;} |
| 8 | + table {font-family: "Lucida Console", "Monaco", monospace; |
| 9 | + text-align: left; min-width: 300px} |
| 10 | + td.code {min-width: 40px; text-align: right;} |
| 11 | + td.char {min-width: 50px; text-align: center;} |
| 12 | + caption {background: lightgray; } |
| 13 | + </style> |
| 14 | + <script> |
| 15 | + "use strict"; |
| 16 | + |
| 17 | + function appendCell(row, text, class_) { |
| 18 | + let cell = document.createElement('td'); |
| 19 | + cell.appendChild(document.createTextNode(text)); |
| 20 | + if (class_ !== undefined) { |
| 21 | + cell.setAttribute('class', class_); |
| 22 | + } |
| 23 | + row.appendChild(cell); |
| 24 | + } |
| 25 | + |
| 26 | + function fillTable(results) { |
| 27 | + const table = document.querySelector('table'); |
| 28 | + while (table.lastElementChild.tagName === 'TR') { |
| 29 | + table.removeChild(table.lastElementChild); |
| 30 | + } |
| 31 | + let count = 0; |
| 32 | + results.forEach((item) => { |
| 33 | + let row = document.createElement('tr'); |
| 34 | + let code = item.char.codePointAt(0); |
| 35 | + let uCode = 'U+' + code.toString(16).toUpperCase().padStart(4, '0'); |
| 36 | + appendCell(row, uCode, 'code'); |
| 37 | + appendCell(row, item.char, 'char'); |
| 38 | + appendCell(row, item.name); |
| 39 | + table.appendChild(row); |
| 40 | + count++; |
| 41 | + }); |
| 42 | + let plural = "s"; |
| 43 | + if (count===1) plural = ""; |
| 44 | + let msg = `${count} character${plural} found`; |
| 45 | + document.querySelector('caption').textContent = msg; |
| 46 | + } |
| 47 | + |
| 48 | + async function fetchResults(query) { |
| 49 | + let url = location.href.replace(location.search, ''); |
| 50 | + const response = await fetch(`${url}search?q=${query}`); |
| 51 | + if (response.ok) { |
| 52 | + return response.json(); |
| 53 | + } else { |
| 54 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function updateTable(event) { |
| 59 | + const input = document.getElementById('query'); |
| 60 | + fetchResults(input.value) |
| 61 | + .then(fillTable) |
| 62 | + .catch(error => console.log(error)); |
| 63 | + } |
| 64 | + |
| 65 | + window.addEventListener('DOMContentLoaded', (event) => { |
| 66 | + const input = document.getElementById('query'); |
| 67 | + input.addEventListener('change', updateTable); |
| 68 | + }); |
| 69 | + </script> |
| 70 | + |
| 71 | +</head> |
| 72 | +<body> |
| 73 | + <div> |
| 74 | + <input id="query" type="search" name="q" value=""> |
| 75 | + <button onClick="updateTable()">Search</button> |
| 76 | + </div> |
| 77 | + <table> |
| 78 | + <caption></caption> |
| 79 | + </table> |
| 80 | +</body> |
| 81 | +</html> |
0 commit comments