1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-15 09:35:21 +01:00
blog/_src/_assets/js/_search.js

101 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-09-09 02:44:03 +02:00
/* global SimpleJekyllSearch */
2017-09-09 19:22:04 +02:00
/* exported krlcSearch */
2015-12-02 00:19:46 +01:00
2017-09-09 02:44:03 +02:00
/* eslint-disable spaced-comment */
2017-09-08 13:56:04 +02:00
//=require simple-jekyll-search/dest/simple-jekyll-search.js
2017-09-09 02:44:03 +02:00
/* eslint-enable spaced-comment */
2017-09-09 19:22:04 +02:00
const krlcSearch = (() => { // eslint-disable-line no-unused-vars
const _config = {
searchlink: $('.search-btn, .js-search-init'),
searcharea: $('.search-area'),
searchfield: $('#search-input'),
searchpop: $('#search-popover')
2017-09-09 02:44:03 +02:00
}
2017-09-09 19:22:04 +02:00
const _private = {
searchHide() {
// Revert all search elements
_config.searcharea
.removeClass('animation-slidedown')
.addClass('animation-bounceOutUp')
.on('animationend webkitAnimationEnd oAnimationEnd', () => {
$('body').removeClass('has-search-open')
})
_config.searchpop.addClass('hide')
},
searchClose() {
$('.search-close').on('click', e => {
e.preventDefault()
_private.searchHide()
// Empty search field
_config.searchfield.val('').blur()
})
},
searchShow() {
_config.searchlink.on('click', e => {
e.preventDefault()
// Show search field
_config.searcharea
.removeClass('is-ready animation-bounceOutUp')
.addClass('is-ready animation-slidedown')
.on('animationend webkitAnimationEnd oAnimationEnd', () => {
$('body').addClass('has-search-open')
})
_config.searchfield.focus()
SimpleJekyllSearch({ // eslint-disable-line new-cap
searchInput: document.getElementById('search-input'),
resultsContainer: document.getElementById('search-results'),
json: '/api/search.json',
searchResultTemplate: '<a class="search-link" href="{url}">{title}</a>',
fuzzy: false
})
// Hide menu too just in case
if ($('body').hasClass('has-menu-open')) {
$('body').removeClass('has-menu-open')
}
// Bind the hide controls
$(document).bind('click.hidethepop', () => {
_private.searchHide()
// Unbind the hide controls
$(document).unbind('click.hidethepop')
})
// Dont close thepop when click on thepop
_config.searchpop.on('click', e => {
e.stopPropagation()
})
// Dont close thepop when click on search field
_config.searchfield.on('click', e => {
e.stopPropagation()
})
// And dont close thepop now
e.stopPropagation()
2017-09-09 02:44:03 +02:00
})
2017-09-09 19:22:04 +02:00
// Show popup upon first keypress
_config.searchfield.on('keyup', () => {
_config.searchpop.removeClass('hide')
})
2017-09-09 02:44:03 +02:00
2017-09-09 19:22:04 +02:00
// Listen for close icon
_private.searchClose()
2015-11-19 16:52:44 +01:00
}
2017-09-09 19:22:04 +02:00
}
2015-11-19 16:52:44 +01:00
2017-09-09 19:22:04 +02:00
return {
init: _private.searchShow
}
})(); // eslint-disable-line semi