forked from qiubaiying/qiubaiying.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
141 lines (111 loc) · 3.79 KB
/
search.js
File metadata and controls
141 lines (111 loc) · 3.79 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(function () {
var G = window || this,
even = G.BLOG.even,
$ = G.BLOG.$,
searchIco = $('#search'),
searchWrap = $('#search-wrap'),
keyInput = $('#key'),
back = $('#back'),
searchPanel = $('#search-panel'),
searchResult = $('#search-result'),
searchTpl = $('#search-tpl').innerHTML,
JSON_DATA = (G.BLOG.ROOT + '/content.json').replace(/\/{2}/g, '/'),
searchData;
function loadData(success) {
if (!searchData) {
var xhr = new XMLHttpRequest();
xhr.open('GET', JSON_DATA, true);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
var res = JSON.parse(this.response);
searchData = res instanceof Array ? res : res.posts;
success(searchData);
} else {
console.error(this.statusText);
}
};
xhr.onerror = function () {
console.error(this.statusText);
};
xhr.send();
} else {
success(searchData);
}
}
function tpl(html, data) {
return html.replace(/\{\w+\}/g, function (str) {
var prop = str.replace(/\{|\}/g, '');
return data[prop] || '';
});
}
var noop = G.BLOG.noop;
var root = $('html');
var Control = {
show: function () {
G.innerWidth < 760 ? root.classList.add('lock-size') : noop;
searchPanel.classList.add('in');
},
hide: function () {
G.innerWidth < 760 ? root.classList.remove('lock-size') : noop;
searchPanel.classList.remove('in');
}
};
function render(data) {
var html = '';
if (data.length) {
html = data.map(function (post) {
return tpl(searchTpl, {
title: post.title,
path: (G.BLOG.ROOT + '/' + post.path).replace(/\/{2,}/g, '/'),
date: new Date(post.date).toLocaleDateString(),
tags: post.tags.map(function (tag) {
return '<span>#' + tag.name + '</span>';
}).join('')
});
}).join('');
} else {
html = '<li class="tips"><i class="icon icon-coffee icon-3x"></i><p>Results not found!</p></li>';
}
searchResult.innerHTML = html;
}
function regtest(raw, regExp) {
regExp.lastIndex = 0;
return regExp.test(raw);
}
function matcher(post, regExp) {
return regtest(post.title, regExp) || post.tags.some(function (tag) {
return regtest(tag.name, regExp);
}) || regtest(post.text, regExp);
}
function search(e) {
var key = this.value.trim();
if (!key) {
return;
}
var regExp = new RegExp(key.replace(/[ ]/g, '|'), 'gmi');
loadData(function (data) {
var result = data.filter(function (post) {
return matcher(post, regExp);
});
render(result);
Control.show();
});
e.preventDefault();
}
searchIco.addEventListener(even, function () {
searchWrap.classList.toggle('in');
keyInput.value = '';
searchWrap.classList.contains('in') ? keyInput.focus() : keyInput.blur();
});
back.addEventListener(even, function () {
searchWrap.classList.remove('in');
Control.hide();
});
document.addEventListener(even, function (e) {
if (e.target.id !== 'key' && even === 'click') {
Control.hide();
}
});
keyInput.addEventListener('input', search);
keyInput.addEventListener(even, search);
}).call(this);