Skip to content

Commit

Permalink
添加全文搜索功能 (neo-project#953)
Browse files Browse the repository at this point in the history
* 添加全文搜索功能

* 对搜索参数进行 encodeURIComponent 编码
  • Loading branch information
陈志同 authored and nicolegys committed Nov 1, 2019
1 parent 1e97b03 commit 6185c8f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
4 changes: 2 additions & 2 deletions template/articles/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
Expand Down
2 changes: 1 addition & 1 deletion template/css/site.min.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions template/docs/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
Expand Down
4 changes: 2 additions & 2 deletions template/faq/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
Expand Down
4 changes: 2 additions & 2 deletions template/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
Expand Down
63 changes: 61 additions & 2 deletions template/template.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
Expand Down Expand Up @@ -69,6 +69,13 @@
<div class="container-fluid" style="margin-top: 55px">
<div class="row d-flex">
<div class="d-none d-md-block catalog py-2 py-md-5 pl-md-3">
<div class="search-bar">
<span class="search-de">X</span>
<input id="sInput" placeholder="search" />
<div class="search-re">
<ul id="sResult"></ul>
</div>
</div>
{catalog}
</div>
<main class="p-3 p-sm-4 p-md-5">
Expand Down Expand Up @@ -115,6 +122,58 @@
$(e.target).parents("h2").next().toggle("fast");
});
}

$("#sInput").bind({
focus: function () {
searchBar();
},
keyup: function (e) {
clearTimeout();
if (e.which == 13) {
searchBar();
}
setTimeout(searchBar, 300);
},
paste: function () {
searchBar();
}
});

$(".search-de").click(function () {
$("#sInput").val("");
$("#sResult").html("");
})

var url = window.location.origin;

function searchBar() {
k = $("#sInput").val();
l = localStorage.getItem("lang") || navigator.language || "en-us";
if (!k) $("#sResult").html("");
$.ajax({
type: "GET",
url: url + "/?k=" + encodeURIComponent(k) + "&l=" + encodeURIComponent(l),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
var html = ""

if (data.length == 0) {
html += "<li><a><span> No results found. </span></a></li > ";
}
for (i in data) {
var _data = data[i];
html += "<li><a href=" + _data.Link + "><strong>" + _data.Title + "</strong><br />";
html += "<span>" + _data.Line + "</span></a></li>";
}
$("#sResult").html(html);
},
fail: function () {
alert("fail");
}
});
}

</script>
</body>
</html>
</html>

0 comments on commit 6185c8f

Please sign in to comment.