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
|
function matchSubstrs(methodName) {
var tokens = [];
var len = methodName.length;
for (var size = 1; size <= len; size++){
for (var i = 0; i+size<= len; i++){
tokens.push(methodName.substr(i, size));
}
}
return tokens;
}
$(function initSearchBar() {
var methodNames = new Bloodhound({
datumTokenizer: matchSubstrs,
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: './methodNames.json',
cache: false
}
});
var sourceFiles = new Bloodhound({
datumTokenizer: matchSubstrs,
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: './sourceFiles.json',
cache: false
}
});
var githubIssues = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'https://api.github.com/search/issues?q=%QUERY+repo:caolan/async',
cache: true,
wildcard: '%QUERY',
transform: function(response) {
return $.map(response.items, function(issue) {
// if (issue.state !== 'open') {
// return null;
// }
return {
url: issue.html_url,
name: issue.number + ': ' + issue.title,
number: issue.number
};
}).sort(function(a, b) {
return b.number - a.number;
});
}
}
});
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'Methods',
source: methodNames,
templates: {
header: '<h3 class="search-bar-header-first">Methods</h3>'
}
},
{
name: 'Files',
source: sourceFiles,
templates: {
header: '<h3 class="search-bar-header">Source Files</h3>'
}
},
{
name: 'Issues',
source: githubIssues,
display: 'name',
templates: {
header: '<h3 class="search-bar-header">Issues</h3>'
}
}).on('typeahead:select', function(ev, suggestion) {
var protocol = window.location.protocol
var host = window.location.host;
var currentPath = window.location.pathname;
// handle issues
if (typeof suggestion !== 'string') {
window.location.href = suggestion.url;
// handle source files
} else if (suggestion.indexOf('.html') !== -1) {
window.location.href = protocol + '//' + host + '/' + suggestion;
// handle searching from one of the source files or the home page
} else if (currentPath !== '/docs.html') {
window.location.href = protocol + '//' + host + '/docs.html#.' + suggestion;
} else {
var $el = document.getElementById('.' + suggestion);
$('#main').animate({ scrollTop: $el.offsetTop }, 500);
}
});
});
|