gitbook-plugin-search
Advanced tools
Comparing version
@@ -34,26 +34,2 @@ require([ | ||
// Return true if search is open | ||
function isSearchOpen() { | ||
return $body.hasClass('with-search'); | ||
} | ||
// Toggle the search | ||
function toggleSearch(_state) { | ||
if (isSearchOpen() === _state) return; | ||
$body.toggleClass('with-search', _state); | ||
// If search bar is open: focus input | ||
if (isSearchOpen()) { | ||
if (gitbook.sidebar) { | ||
gitbook.sidebar.toggle(true); | ||
} | ||
$searchInput.focus(); | ||
} else { | ||
$searchInput.blur(); | ||
$searchInput.val(''); | ||
$bookSearchResults.removeClass('open'); | ||
} | ||
} | ||
function displayResults(res) { | ||
@@ -81,3 +57,3 @@ $bookSearchResults.addClass('open'); | ||
var $link = $('<a>', { | ||
'href': gitbook.state.root+res.url, | ||
'href': gitbook.state.basePath + '/' + res.url, | ||
'text': res.title | ||
@@ -101,3 +77,5 @@ }); | ||
// Add class for loading | ||
$body.addClass('with-search'); | ||
$body.addClass('search-loading'); | ||
// Launch search query | ||
@@ -113,9 +91,13 @@ throttle(gitbook.search.query(q, 0, MAX_RESULTS) | ||
function closeSearch() { | ||
$body.removeClass('with-search'); | ||
$bookSearchResults.removeClass('open'); | ||
} | ||
function launchSearchFromQueryString() { | ||
var q = getParameterByName('q'); | ||
if (q && q.length > 0) { | ||
// Toggle search | ||
toggleSearch(true); | ||
// Update search input | ||
$searchInput.val(q); | ||
// Launch search | ||
@@ -128,3 +110,3 @@ launchSearch(q); | ||
// Bind DOM | ||
$searchInput = $('#book-search-input'); | ||
$searchInput = $('#book-search-input input'); | ||
$bookSearchResults = $('#book-search-results'); | ||
@@ -136,17 +118,8 @@ $searchList = $bookSearchResults.find('.search-results-list'); | ||
// Detect escape key in search input | ||
$('#book-search-input').on('keyup', function(e) { | ||
var key = (e.keyCode ? e.keyCode : e.which); | ||
if (key == 27) { | ||
e.preventDefault(); | ||
toggleSearch(false); | ||
} | ||
}); | ||
// Launch query based on input content | ||
function handleUpdate(element) { | ||
var q = element.val(); | ||
function handleUpdate() { | ||
var q = $searchInput.val(); | ||
if (q.length == 0) { | ||
$bookSearchResults.removeClass('open'); | ||
closeSearch(); | ||
} | ||
@@ -161,5 +134,5 @@ else { | ||
var propertyChangeUnbound = false; | ||
$('#book-search-input').on('propertychange', function(e) { | ||
$searchInput.on('propertychange', function(e) { | ||
if (e.originalEvent.propertyName == 'value') { | ||
handleUpdate($(this)); | ||
handleUpdate(); | ||
} | ||
@@ -169,3 +142,3 @@ }); | ||
// HTML5 (IE9 & others) | ||
$('#book-search-input').on('input', function(e) { | ||
$searchInput.on('input', function(e) { | ||
// Unbind propertychange event for IE9+ | ||
@@ -177,7 +150,7 @@ if (!propertyChangeUnbound) { | ||
handleUpdate($(this)); | ||
handleUpdate(); | ||
}); | ||
// Push to history on blur | ||
$('#book-search-input').on('blur', function(e) { | ||
$searchInput.on('blur', function(e) { | ||
// Update history state | ||
@@ -189,24 +162,6 @@ if (usePushState) { | ||
}); | ||
toggleSearch(false); | ||
} | ||
gitbook.events.on('start', function() { | ||
// Create the toggle search button | ||
if (gitbook.toolbar) { | ||
gitbook.toolbar.createButton({ | ||
icon: 'fa fa-search', | ||
label: 'Search', | ||
position: 'left', | ||
onClick: toggleSearch | ||
}); | ||
} | ||
// Bind keyboard to toggle search | ||
if (gitbook.keyboard) { | ||
gitbook.keyboard.bind(['f'], toggleSearch); | ||
} | ||
}); | ||
gitbook.events.on('page.change', function() { | ||
closeSearch(); | ||
bindSearch(); | ||
@@ -269,3 +224,1 @@ | ||
}); | ||
@@ -5,3 +5,3 @@ { | ||
"main": "index.js", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"engines": { | ||
@@ -8,0 +8,0 @@ "gitbook": ">=3.0.0-pre.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22530
-7.81%267
-20.06%