Repository navigation
Expand file tree
/
Copy pathsearch.json.js
More file actions
114 lines (114 loc) · 4.04 KB
/
Copy pathsearch.json.js
File metadata and controls
114 lines (114 loc) · 4.04 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
(function() {
var keyInput = document.getElementById('search-key'),
// searchForm = document.getElementById('search-form'),
searchWrap = document.getElementById('result-wrap'),
searchMask = document.getElementById('result-mask'),
searchResult = document.getElementById('search-result'),
searchTpl = document.getElementById('search-tpl').innerHTML,
winWidth, winHeight, searchData;
if (window.innerWidth) {
winWidth = parseInt(window.innerWidth);
} else if ((document.body) && (document.body.clientWidth)) {
winWidth = parseInt(document.body.clientWidth);
}
if (window.innerHeight) {
winHeight = parseInt(window.innerHeight);
} else if ((document.body) && (document.body.clientHeight)) {
winHeight = parseInt(document.body.clientHeight);
}
searchMask.style.width = winWidth + 'px';
searchMask.style.height = winHeight + 'px';
function loadData(success) {
if (!searchData) {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/content.json', true);
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
var res = JSON.parse(this.response||this.responseText);
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 matcher(post, regExp) {
return regtest(post.title, regExp) || post.tags.some(function(tag) {
return regtest(tag.name, regExp);
}) || regtest(post.text, regExp);
}
function regtest(raw, regExp) {
regExp.lastIndex = 0;
return regExp.test(raw);
}
function render(data) {
var html = '';
if (data.length) {
html = data.map(function(post) {
return tpl(searchTpl, {
title: post.title,
path: post.path,
date: new Date(post.date).toLocaleDateString(),
tags: post.tags.map(function(tag) {
return '<span>' + tag.name + '</span>';
}).join('')
});
}).join('');
} else {
html = '<div class="tips"><i class="fa fa-empty"></i><p>Results not found!</p></div>';
}
searchResult.innerHTML = html;
}
function tpl(html, data) {
return html.replace(/\{\w+\}/g, function(str) {
var prop = str.replace(/\{|\}/g, '');
return data[prop] || '';
});
}
function hasClass(obj, cls) {
return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
function addClass(obj, cls) {
if (!hasClass(obj, cls)) obj.className += " " + cls;
}
function removeClass(obj, cls) {
if (hasClass(obj, cls)) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
obj.className = obj.className.replace(reg, ' ');
}
}
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);
});
e.preventDefault();
removeClass(searchWrap, 'hide');
removeClass(searchMask, 'hide');
keyInput.onfocus=function() {
removeClass(searchWrap, 'hide');
removeClass(searchMask, 'hide');
};
}
keyInput.onfocus=function(){
keyInput.addEventListener('input', search);
};
searchMask.onclick=function(){
addClass(searchWrap, 'hide');
addClass(searchMask, 'hide');
};
})();