This repository was archived by the owner on Nov 1, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsearch.js
More file actions
188 lines (158 loc) · 5.46 KB
/
search.js
File metadata and controls
188 lines (158 loc) · 5.46 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
$(function() {
var searchIndex,
searchHits,
searchWorker = new Worker("/assets/javascripts/search_worker.js");
$('.help-search .search-box').focus(function(){
$(this).css('background-position','0px -25px')
});
$('.help-search .search-box').focusout(function(){
if($(this).val() == ''){
$(this).css('background-position','0px 0px')
}
});
// Load the JSON containing all pages
// Has it been loaded before (and stored with localstorage)?
if (localStorage['searchIndex']) {
searchIndex = JSON.parse(localStorage['searchIndex']);
if (localStorageHasExpired()) {
loadSearchIndex();
}
else {
searchIndex.type = "index";
searchWorker.postMessage(searchIndex);
}
} else {
loadSearchIndex();
}
function loadSearchIndex() {
$.getJSON('/search/search-index.json', function(data) {
data.type = { index: true };
searchWorker.postMessage(data);
searchIndex = data;
localStorage['searchIndex'] = JSON.stringify(searchIndex);
localStorage['updated'] = new Date().getTime();
});
}
function localStorageHasExpired() {
// Expires in one day (86400000 ms)
if (new Date().getTime() - parseInt(localStorage['updated'],10) > 86400000) {
return true;
}
return false;
}
// Expand and activate search if the page loaded with a value set for the search field
if ($("#searchfield").val().length > 0) {
$("#search-container").addClass("active");
searchForString($("#searchfield").val());
}
// On input change, update the search results
$("#searchfield").on("input", function(e) {
$(this).val().length > 0 ? $("#search-container").addClass("active") : $("#search-container").removeClass("active");
searchForString($(this).val());
});
// Global keyboard shortcuts
$("body").keyup(function(e) {
if (e.keyCode == 83) {
// S key
if ($("#searchfield").is(":focus"))
return;
e.preventDefault();
$("#searchfield").focus();
}
});
// Keyboard support for the search field
$("#searchfield").keyup(function(e) {
if (e.keyCode == 27) {
// ESC
e.preventDefault();
$("#searchfield").val().length > 0 ? cancelSearch() : $("#searchfield").blur();
} else if (e.keyCode == 13) {
// Return/enter
e.preventDefault();
goToSelectedSearchResult();
} else if (e.keyCode == 8 || e.keyCode == 46) {
// Update search if backspace/delete was pressed
// IE9 doesn't trigger the input event on backspace/delete,
// but they do trigger keyUp
$(this).val().length > 0 ? $("#search-container").addClass("active") : $("#search-container").removeClass("active");
searchForString($(this).val());
}
}).keydown(function(e) {
if (e.keyCode == 38) {
// Arrow up
e.preventDefault();
moveSearchSelectionUp();
} else if (e.keyCode == 40) {
// Arrow down
e.preventDefault();
moveSearchSelectionDown();
} else if (e.keyCode == 27) {
// Prevent default on ESC key
// IE inputs come with some native behaviors that will
// prevent the DOM from updating correctly unless prevented
e.preventDefault();
}
});
// Make clicking the label focus the input label
// for browsers (IE) that doesn't support pointer-events: none
$("#search-container .search-placeholder").click(function(e) {
$("#searchfield").focus();
});
$(".cancel-search").click(function(e) {
cancelSearch();
});
function cancelSearch() {
$("#searchfield").val("");
$("#search-container").removeClass("active");
}
function searchForString(searchString) {
searchHits = [];
searchString = searchString.toLowerCase();
searchWorker.postMessage({ query: searchString, type: "search" })
}
searchWorker.addEventListener("message", function (e) {
if (e.data.type.search) {
renderResultsForSearch(e.data.query, e.data.results);
}
});
// Update the UI representation of the search hits
function renderResultsForSearch(searchString, searchHits){
$("#search-results").empty();
// Check if there are any results. If not, show placeholder and exit
if (searchHits.length < 1) {
$('<li class="placeholder">No results for <em></em></li>').appendTo("#search-results").find("em").text(searchString);
return;
}
// Render results (max 8)
for (var i = 0; i < Math.min(searchHits.length, 8); i++) {
var page = searchHits[i];
$('<li class="result"><a href="' + page.url + '"><em>' + page.title + '</em></a></li>').appendTo("#search-results");
}
// Select the first alternative
$("#search-results li:first-child").addClass("selected");
}
// Move the selected list item when hovering
$("#search-results").on("mouseenter", "li", function(e) {
$(this).parent().find(".selected").removeClass("selected").end().end()
.addClass("selected");
});
function moveSearchSelectionUp() {
$prev = $("#search-results .selected").prev();
if ($prev.length < 1)
return;
$("#search-results .selected").removeClass("selected");
$prev.addClass("selected");
}
function moveSearchSelectionDown() {
$next = $("#search-results .selected").next();
if ($next.length < 1)
return;
$("#search-results .selected").removeClass("selected");
$next.addClass("selected");
}
function goToSelectedSearchResult() {
var href = $("#search-results .selected a").attr("href");
if (href)
window.location.href = href;
}
});