2016-11-02 20:16:31 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 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");
|
|
|
|
|
2016-11-02 20:22:18 +05:00
|
|
|
button.innerHTML = "search";
|
|
|
|
|
2016-11-02 20:16:31 +05:00
|
|
|
function inject_search_result(r) {
|
|
|
|
var e = document.createElement("div");
|
|
|
|
e.innerHTML = r.PostMarkup;
|
|
|
|
output.appendChild(document.createElement("hr"));
|
2016-11-02 20:23:17 +05:00
|
|
|
output.appendChild(e);
|
2016-11-02 20:16:31 +05:00
|
|
|
}
|
|
|
|
|
2016-11-02 20:21:14 +05:00
|
|
|
button.onclick = function(ev) {
|
2016-11-02 20:16:31 +05:00
|
|
|
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 {
|
2016-11-02 20:23:17 +05:00
|
|
|
status.innerHTML = "found "+result.length+" results";
|
2016-11-03 01:11:43 +05:00
|
|
|
for (var idx = 0 ; idx < result.length; idx++ ) {
|
2016-11-02 20:16:31 +05:00
|
|
|
inject_search_result(result[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status.innerHTML = "HTTP "+ajax.status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 20:18:11 +05:00
|
|
|
ajax.open("GET", "/api/find?text="+text);
|
|
|
|
ajax.send();
|
2016-11-02 20:16:31 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
inner.appendChild(input);
|
|
|
|
inner.appendChild(button);
|
|
|
|
inner.appendChild(status);
|
|
|
|
|
|
|
|
elem.appendChild(inner);
|
|
|
|
elem.appendChild(output);
|
|
|
|
}
|