add search page
This commit is contained in:
parent
4be03b0b96
commit
458a1c04d9
14
contrib/static/search.html
Normal file
14
contrib/static/search.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title> search for posts </title>
|
||||||
|
<link rel="stylesheet" href="site.css"></link>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<noscript>search requires javascript for now</noscript>
|
||||||
|
<div id="search"></div>
|
||||||
|
<script src="search.js" type="text/javascript">
|
||||||
|
var e = document.getElementById("search");
|
||||||
|
inject_search(e);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
53
contrib/static/search.js
Normal file
53
contrib/static/search.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** inject search widget */
|
||||||
|
function inject_search(elem) {
|
||||||
|
var inner = document.createElement("div");
|
||||||
|
var button = document.createElement("button");
|
||||||
|
var input = document.createElement("input");
|
||||||
|
var status = document.createElement("span");
|
||||||
|
var output = document.createElement("div");
|
||||||
|
|
||||||
|
function inject_search_result(r) {
|
||||||
|
var e = document.createElement("div");
|
||||||
|
e.innerHTML = r.PostMarkup;
|
||||||
|
output.appendChild(e);
|
||||||
|
output.appendChild(document.createElement("hr"));
|
||||||
|
}
|
||||||
|
|
||||||
|
botton.onclick = function(ev) {
|
||||||
|
var text = input.value;
|
||||||
|
input.value = "";
|
||||||
|
while(output.children.length > 0)
|
||||||
|
output.children[0].remove();
|
||||||
|
var ajax = new XMLHttpRequest();
|
||||||
|
ajax.onreadystatechange = function() {
|
||||||
|
if (ajax.readyState == XMLHttpRequest.DONE) {
|
||||||
|
// done
|
||||||
|
if(ajax.status == 200) {
|
||||||
|
// good
|
||||||
|
var result = JSON.parse(ajax.responseText);
|
||||||
|
if (result.length == 0) {
|
||||||
|
status.innerHTML = "no results";
|
||||||
|
} else {
|
||||||
|
status.innerHTML = "found "+result.length+"results";
|
||||||
|
for (var idx = 0 ; idx < result.length; idx ++ ) {
|
||||||
|
inject_search_result(result[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
status.innerHTML = "HTTP "+ajax.status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inner.appendChild(input);
|
||||||
|
inner.appendChild(button);
|
||||||
|
inner.appendChild(status);
|
||||||
|
|
||||||
|
elem.appendChild(inner);
|
||||||
|
elem.appendChild(output);
|
||||||
|
}
|
Reference in New Issue
Block a user