-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
66 lines (54 loc) · 1.85 KB
/
search.js
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
function removeAccents(str) {
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
str = str.split('');
var strLen = str.length;
var i, x;
for (i = 0; i < strLen; i++) {
if ((x = accents.indexOf(str[i])) != -1) {
str[i] = accentsOut[x];
}
}
return str.join('');
}
function resultItem(phrase, key, values) {
var div = document.createElement("div");
var b = document.createElement("b");
var matchlength = phrase.length + key.substr(0, phrase.length + 1).split('"').length - 1
b.appendChild(document.createTextNode(key.substr(0, matchlength)))
div.appendChild(b)
div.appendChild(document.createTextNode(key.substr(matchlength)))
div.appendChild(document.createElement("br"))
div.appendChild(document.createTextNode(values.join("; ")))
return div
}
function search(input) {
document.getElementById("searchresults").innerHTML = "";
if (input.value.length == 0)
return;
var document_fragment = document.createDocumentFragment();
var resultsindex = index[input.value[0]]
var strippedphrase = removeAccents(input.value).toLowerCase().replace('"', '')
var results_start = -1
for (i = resultsindex; i < lang.length; ++i) {
if (lang[i][0].substr(0, strippedphrase.length) == strippedphrase) {
if (results_start < 0) {
results_start = i;
} else if (i - results_start > 200) {
break;
}
document_fragment.appendChild(resultItem(input.value, lang[i][1], lang[i][2]));
} else if (results_start >= 0) {
break;
}
}
document.getElementById("searchresults").appendChild(document_fragment);
}
document.getElementById('search').addEventListener('keyup',function(e){
if (e.which == 13) {
this.blur();
return;
} else {
search(this)
}
});