-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.html
52 lines (45 loc) · 1.43 KB
/
search.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
title: Search
layout: default
---
<input type="search" id="search" placeholder="Search">
<div class="results"></div>
<script src="{{ site.baseurl }}/public/js/lib/github.js"></script>
<script src="{{ site.baseurl }}/public/js/repo.js"></script>
<script>
var params = location.search.substr(1).split('&').reduce(function(p, e) {
e = e.split('=');
p[decodeURIComponent(e[0])] = decodeURIComponent(e[1]);
return p;
}, {});
var search = document.getElementById('search');
var results = document.querySelector('.results');
search.focus();
search.addEventListener('keypress', function(e) {
if (e.keyCode == 13) {
doSearch();
}
}, true);
if (params.q) {
search.value = params.q;
doSearch();
}
function doSearch() {
var searchText = search.value;
results.innerHTML = '<h2>Searching ' + searchText + '</h2>';
if (history.replaceState)
history.replaceState({}, 'Search ' + searchText, '?q=' + encodeURIComponent(searchText));
MyLinks.search(searchText, function(err, posts) {
results.innerHTML =
'<h2>' + posts.length + ' results for <b>' + searchText + '</b></h2>' +
(
err ? '<p>Error: ' + err + '</p>' :
posts.map(function(post) {
return '<a class="link" href="' + post.url + '">' +
'<h3 class="link-title">' + post.title + '</h3>' +
'</a>';
}).join('')
);
});
}
</script>