-
Notifications
You must be signed in to change notification settings - Fork 39
/
master.js
162 lines (131 loc) · 4.39 KB
/
master.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// set up code blocks to get hit by prettyprint:
for (var pre = document.getElementsByTagName('pre'), len = pre.len, i = -1; ++i < len; ){
pre[i].classList.add('prettyprint');
}
// when prettify is loaded, run it!
(function loopsiloop(){
if (window.prettyPrint) prettyPrint();
else setTimeout(loopsiloop,100);
})();
(function(){
//
// Find as you type search
//
// get elements
var qc = document.getElementById('q-container'),
q = document.getElementById('q'),
r = document.getElementById('r');
// basic feature tests for the livesearch:
if (![].forEach || !document.getElementsByClassName){
var search = qc.parentNode;
search.parentNode.removeChild(search);
return;
}
// request json
window.ajax = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
window.jsonResponses = [];
ajax.onreadystatechange = function (responseText) {
if (ajax.readyState == 4) {
window.jsonResponses = eval('('+ajax.responseText+')');
}
}
ajax.open('GET', 'search_index.json', true);
ajax.send(null);
// search on keyup
addEvent('keyup',q, function(e) {
var queryLowerCase = q.value.toLowerCase(),
queryStartRe = new RegExp('^'+queryLowerCase, 'i'),
resultList = [],
startI = -1,
allI = -1,
maxResults = 10,
jsonResponse,
keyCode = e.keyCode;
// quit if we're just navigating.
if (keyCode == 40 || keyCode == 38) return;
while ((jsonResponse = jsonResponses[++startI])) {
var resultText = jsonResponse.text,
resultTextLowerCase = resultText.toLowerCase(),
resultURL = jsonResponse.uri,
resultSection = jsonResponse.section;
if (maxResults && queryStartRe.test(resultTextLowerCase)) {
resultList.push('<li><a href="http://developers.whatwg.org/'+resultURL+'">'+resultText+' <span>Section '+resultSection+'</span></a></li>');
--maxResults;
}
}
while ((jsonResponse = jsonResponses[++allI])) {
var resultText = jsonResponse.text,
resultTextLowerCase = resultText.toLowerCase(),
resultURL = jsonResponse.uri,
resultSection = jsonResponse.section;
if (maxResults && resultTextLowerCase.indexOf(queryLowerCase) > -1) {
resultList.push('<li><a href="http://developers.whatwg.org/'+resultURL+'">'+resultText+' <span>Section '+resultSection+'</span></a></li>');
--maxResults;
}
}
r.innerHTML = resultList.join('');
}
);
// focus search on click on search area
addEvent('click', qc, function(e) {
q.focus();
}
);
// move between query and results with keyboard
addEvent('keydown', q, function(e) {
var keyCode = e.keyCode;
var results = r.getElementsByTagName('a');
var selected = r.getElementsByClassName('selected')[0];
if (keyCode == 13) { // enter
location.href = selected.href;
}
Array.prototype.slice.call(results).forEach(function(v,k,arra){
v.classList.remove('selected');
});
try {
if (keyCode == 40) { // down
var elem = !selected ? results[0] : selected.parentNode.nextSibling.firstChild;
elem.classList.add('selected');
}
if (keyCode == 38) { // up
var elem = !selected ? results[results.length - 1] : selected.parentNode.previousSibling.firstChild;
elem.classList.add('selected');
}
} catch(e){}
}
);
// focus element on mouse over
addEvent('mousemove', r, function(e) {
var target = e.target || e.srcElement;
if (target && target.nodeName == 'A') {
Array.prototype.slice.call(r.getElementsByTagName('a')).forEach(function(v,k,arra){
v.classList.remove('selected');
});
target.classList.add('selected');
}
}
);
// Update offline version if there is one
if(!!window.applicationCache) {
var body = document.querySelector("body"),
appCache = window.applicationCache;
appCache.addEventListener('downloading', function () {
appCache.update();
body.className += "syncing";
}, false);
appCache.addEventListener('progress', function (progressEvent) {
if (progressEvent.loaded) {
document.querySelector("#sync span").innerHTML = "("+Math.round((progressEvent.loaded/progressEvent.total)*100) + "%)";
}
}, false);
appCache.addEventListener('updateready', function () {
body.className = body.className.replace("syncing", "");
appCache.swapCache();
}, false)
}
// TOC
var toc_toggle = document.getElementById("toc-toggle");
addEvent('click', toc_toggle, function(e) {
toc_toggle.className = (toc_toggle.className == "active") ? "" : "active";
});
})();