Browse Source

Let's clean up a bit

Piotr Czajkowski 4 years ago
parent
commit
0a4e7db4cd
2 changed files with 68 additions and 63 deletions
  1. 65 0
      html/processResults.js
  2. 3 63
      html/results.html

+ 65 - 0
html/processResults.js

@@ -0,0 +1,65 @@
+let app = {};
+
+app.filterTable = function(value) {
+	const filter = value.toUpperCase();
+	let table = document.getElementsByTagName("tbody")[0];
+	let tr = table.getElementsByTagName("tr");
+
+	for (let i = 0; i < tr.length; i++) {
+		tr[i].style.display = "none";
+
+		let td = tr[i].getElementsByTagName("td");
+		for (var j = 0; j < td.length; j++) {
+			let cell = tr[i].getElementsByTagName("td")[j];
+			if (cell) {
+				if (cell.innerText.toUpperCase().indexOf(filter) > -1) {
+					tr[i].style.display = "";
+					break;
+				} 
+			}
+		}
+	}
+};
+
+app.clearFilter = function() {
+	let table = document.getElementsByTagName("tbody")[0];
+	let tr = table.getElementsByTagName("tr");
+
+	for (let i = 0; i < tr.length; i++) {
+		tr[i].style.display = "";
+	}
+
+	let filter = document.getElementById("filter");
+	filter.value = "";
+};
+
+app.sortTable = function() {
+	// borrowed from https://stackoverflow.com/a/49041392/7342859
+	const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
+
+	const comparer = (idx, asc) => (a, b) => ((v1, v2) => 
+		v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
+	)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
+
+	// do the work...
+	document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
+		const table = th.closest('table');
+		Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
+			.sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
+			.forEach(tr => table.appendChild(tr) );
+
+		const sorted = " (sorted)";
+		if (!th.textContent.includes(sorted)) {
+			th.textContent += sorted;
+		}
+
+		th.parentNode.childNodes.forEach((child) => {
+			if (child != th) {
+				child.textContent = child.textContent.replace(sorted, "");
+			}
+		});
+	})));
+
+};
+
+app.sortTable();

+ 3 - 63
html/results.html

@@ -11,46 +11,13 @@
 				cursor: pointer;
 			}
 		</style>
-		<script>
-			let filterTable = function(value) {
-				const filter = value.toUpperCase();
-				let table = document.getElementsByTagName("tbody")[0];
-				let tr = table.getElementsByTagName("tr");
-
-				for (let i = 0; i < tr.length; i++) {
-					tr[i].style.display = "none";
-
-					let td = tr[i].getElementsByTagName("td");
-					for (var j = 0; j < td.length; j++) {
-						let cell = tr[i].getElementsByTagName("td")[j];
-						if (cell) {
-							if (cell.innerText.toUpperCase().indexOf(filter) > -1) {
-								tr[i].style.display = "";
-								break;
-							} 
-						}
-					}
-				}
-			};
-
-			let clearFilter = function() {
-				let table = document.getElementsByTagName("tbody")[0];
-				let tr = table.getElementsByTagName("tr");
-
-				for (let i = 0; i < tr.length; i++) {
-					tr[i].style.display = "";
-				}
-
-				let filter = document.getElementById("filter");
-				filter.value = "";
-			};
-		</script>
+		<script src="./processResults.js"></script>
 	</head>
 	<body>
 		<a href="/">Search again</a>
 		<div>Filter: 
-			<input id="filter" type="text" name="name" onchange="filterTable(this.value)" autocomplete="off">
-			<button class="btn" onclick="clearFilter()">Clear</button>
+			<input id="filter" type="text" name="name" onchange="app.filterTable(this.value)" autocomplete="off">
+			<button class="btn" onclick="app.clearFilter()">Clear</button>
 		</div>
 		<table>
 			<thead>
@@ -74,32 +41,5 @@
 		</table>
 		<br/>
 		<a href="/">Search again</a>
-		<script>
-			// borrowed from https://stackoverflow.com/a/49041392/7342859
-			const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
-
-			const comparer = (idx, asc) => (a, b) => ((v1, v2) => 
-				v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
-				)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
-
-			// do the work...
-			document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
-				const table = th.closest('table');
-				Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
-					.sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
-					.forEach(tr => table.appendChild(tr) );
-
-				const sorted = " (sorted)";
-				if (!th.textContent.includes(sorted)) {
-					th.textContent += sorted;
-				}
-
-				th.parentNode.childNodes.forEach((child) => {
-					if (child != th) {
-						child.textContent = child.textContent.replace(sorted, "");
-					}
-				});
-			})));
-		</script>
 	</body>
 </html>