<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unicode character lookup</title>
<style>
body {
	font-size: 12pt;
	font-family: sans-serif;
}
.table > div:nth-child(odd) {
	background-color: #eee;
}
.table {
	max-height: 300px;
	overflow-y: scroll;
}
.invisible {
	display: none;
}
#notfound_container {
	margin-top: 15px;
}
code {
	background-color: #eee;
	padding: 3px;
	border-radius: 5px;
}
</style>
<script>
function do_lookup(override) {
	var input;
	if (override != null) {
		if (override.length == 0) return;
		input = override;
		document.getElementById("input").value = override;
	} else {
		input = document.getElementById("input").value;
		if (input.length == 0) return;
	}

	location.hash = input;

	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				handleResponse(JSON.parse(xhr.responseText), input);
			} else {
				alert("Request failed: " + xhr.responseText);
			}
		}
	};

	xhr.open("GET", "/unicode/lookup/" + encodeURIComponent(input));
	xhr.responseType = "text";
	xhr.setRequestHeader("Content-Type", "text/plain");
	xhr.send();
}

function handleResponse(json, input) {
	var elem;

	/* elem = document.getElementById("json");
	elem.innerHTML = "";
	elem.appendChild(document.createTextNode(JSON.stringify(json))); */

	var index_container = document.getElementById("index_container");
	if ("index" in json) {
		index_container.classList.remove("invisible");
		document.getElementById("index_input").innerHTML = input;
		elem = document.getElementById("index");
		elem.innerHTML = "";
		elem.appendChild(makeCodepointDiv(json["index"]));
	} else if (!index_container.classList.contains("invisible")) {
		index_container.classList.add("invisible");
	}

	setTableRows("codepoints", json.codepoints);
	setTableRows("search", json.search);

	if (json.notfound.length > 0) {
		document.getElementById("notfound_container").classList.remove("invisible");
		document.getElementById("notfound").innerText = json.notfound;
	} else {
		document.getElementById("notfound_container").classList.add("invisible");
		document.getElementById("notfound").innerText = "";
	}
}

function setTableRows(key, rows) {
	document.getElementById(key + "_num").innerHTML = "(" + rows.length + ")";

	var elem = document.getElementById(key);
	elem.innerHTML = "";
	for (var j = 0; j < rows.length; j++) {
		elem.appendChild(makeCodepointDiv(rows[j]));
	}
}

function makeCodepointDiv(row) {
	var div = document.createElement("div");
	populateCodepoint(div, row);
	return div;
}

function populateCodepoint(div, row) {
	var span = document.createElement("span");
	span.setAttribute("style", "display: inline-block; width: 7em; font-weight: bold;");
	span.appendChild(document.createTextNode("U+" + row[0]));
	div.appendChild(span);

	span = document.createElement("span");
	span.setAttribute("style", "display: inline-block; width: 2em;");
	span.appendChild(document.createTextNode(String.fromCodePoint(parseInt(row[0], 16))));
	div.appendChild(span);

	span = document.createElement("span");
	span.setAttribute("style", "display: inline-block;");
	span.appendChild(document.createTextNode(row[1]));
	div.appendChild(span);
}

window.addEventListener("load", function() {
	if (location.hash.length > 0) {
		do_lookup(decodeURIComponent(location.hash.slice(1)));
	}

	document.getElementById("input").focus();
});
</script>
</head>
<body>
	<h1>Unicode character lookup</h1>

	<input type="text" id="input" placeholder="Character(s) to look up" onkeypress="if (event.keyCode == 10 || event.keyCode == 13) do_lookup()">
	<input type="button" onclick="do_lookup()" value="Lookup">
	<br>

	<!-- <pre id="json"></pre><br> -->

	<div id="notfound_container" class="invisible">
		These were not found, perhaps the server needs to be update its unicode table? <code id="notfound"></code>
	</div>

	<div id="index_container" class="invisible">
		<h3>Codepoint <span id="index_input"></span></h3>
		<div id="index" class="table"></div>
	</div>

	<h3>Codepoints <span id="codepoints_num"></span></h3>
	<div id="codepoints" class="table"></div>

	<h3>Found in descriptions <span id="search_num"></span></h3>
	<div id="search" class="table"></div>
</body>
</html>