diff options
author | Max Magorsch <arzano@gentoo.org> | 2020-03-04 23:21:25 +0100 |
---|---|---|
committer | Max Magorsch <arzano@gentoo.org> | 2020-03-04 23:21:25 +0100 |
commit | 6063f563294f591562bc8adc8bf90811fe438e51 (patch) | |
tree | 06b7f3cebe3a4a84c9fdd84b758bee009787b436 /js | |
parent | Adjust the appearance to match the old planet (diff) | |
download | planet-tyrian-6063f563294f591562bc8adc8bf90811fe438e51.tar.gz planet-tyrian-6063f563294f591562bc8adc8bf90811fe438e51.tar.bz2 planet-tyrian-6063f563294f591562bc8adc8bf90811fe438e51.zip |
Add a search functionality
Signed-off-by: Max Magorsch <arzano@gentoo.org>
Diffstat (limited to 'js')
-rw-r--r-- | js/planet-search.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/js/planet-search.js b/js/planet-search.js new file mode 100644 index 0000000..2d38c0d --- /dev/null +++ b/js/planet-search.js @@ -0,0 +1,53 @@ +function getParameter(param) { + var result = null, + tmp = []; + location.search + .substr(1) + .split("&") + .forEach(function (item) { + tmp = item.split("="); + if (tmp[0] === param) result = decodeURIComponent(tmp[1]); + }); + return result; +} + + +if(getParameter('q') == null){ + document.getElementById("spinner").style.display = "none"; +} + + +var xhttp = new XMLHttpRequest(); +xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + + var data = JSON.parse(this.responseText) + + var idx = lunr(function () { + this.ref('title') + this.field('author') + this.field('nickname') + this.field('date') + this.field('scope') + this.field('content') + + data.forEach(function (doc) { + this.add(doc) + }, this) + + }) + + var searchterm = getParameter('q'); + if(searchterm == null){ + console.log("No search term given"); + }else{ + idx.search(searchterm).forEach(function (doc) { + $("#articles").append("<li>" + doc.ref + "</li>"); + }, this) + document.getElementById("spinner").style.display = "none"; + } + + } +}; +xhttp.open("GET", "data.json", true); +xhttp.send(); |