search.js
3.43 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
require([
"gitbook",
"lodash"
], function(gitbook, _) {
var index = null;
var $searchInput, $searchForm;
// Use a specific index
function loadIndex(data) {
index = lunr.Index.load(data);
}
// Fetch the search index
function fetchIndex() {
$.getJSON(gitbook.state.basePath+"/search_index.json")
.then(loadIndex);
}
// Search for a term and return results
function search(q) {
if (!index) return;
var results = _.chain(index.search(q))
.map(function(result) {
var parts = result.ref.split("#")
return {
path: parts[0],
hash: parts[1]
}
})
.value();
return results;
}
// Create search form
function createForm(value) {
if ($searchForm) $searchForm.remove();
$searchForm = $('<div>', {
'class': 'book-search',
'role': 'search'
});
$searchInput = $('<input>', {
'type': 'text',
'class': 'form-control',
'val': value,
'placeholder': 'Type to search'
});
$searchInput.appendTo($searchForm);
$searchForm.prependTo(gitbook.state.$book.find('.book-summary'));
}
// Return true if search is open
function isSearchOpen() {
return gitbook.state.$book.hasClass("with-search");
}
// Toggle the search
function toggleSearch(_state) {
if (isSearchOpen() === _state) return;
gitbook.state.$book.toggleClass("with-search", _state);
// If search bar is open: focus input
if (isSearchOpen()) {
gitbook.sidebar.toggle(true);
$searchInput.focus();
} else {
$searchInput.blur();
$searchInput.val("");
gitbook.sidebar.filter(null);
}
}
// Recover current search when page changed
function recoverSearch() {
var keyword = gitbook.storage.get("keyword", "");
createForm(keyword);
if (keyword.length > 0) {
if(!isSearchOpen()) {
toggleSearch();
}
gitbook.sidebar.filter(_.pluck(search(keyword), "path"));
}
};
gitbook.events.bind("start", function(config) {
// Pre-fetch search index and create the form
fetchIndex();
createForm();
// Type in search bar
$(document).on("keyup", ".book-search input", function(e) {
var key = (e.keyCode ? e.keyCode : e.which);
var q = $(this).val();
if (key == 27) {
e.preventDefault();
toggleSearch(false);
return;
}
if (q.length == 0) {
gitbook.sidebar.filter(null);
gitbook.storage.remove("keyword");
} else {
var results = search(q);
gitbook.sidebar.filter(
_.pluck(results, "path")
);
gitbook.storage.set("keyword", q);
}
});
// Create the toggle search button
gitbook.toolbar.createButton({
icon: 'fa fa-search',
label: 'Search',
position: 'left',
onClick: toggleSearch
});
// Bind keyboard to toggle search
gitbook.keyboard.bind(['f'], toggleSearch)
});
gitbook.events.bind("page.change", recoverSearch);
});