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

91 lines
3.1 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 = {
2017-09-09 20:02:12 +02:00
searchlink: document.getElementsByClassName('search-btn')[0],
searcharea: document.getElementsByClassName('search-area')[0],
searchclose: document.getElementsByClassName('search-close')[0],
searchfield: document.getElementById('search-input'),
searchpop: document.getElementById('search-popover'),
searchresults: document.getElementById('search-results')
2017-09-09 02:44:03 +02:00
}
2017-09-09 19:22:04 +02:00
const _private = {
searchHide() {
// Revert all search elements
2017-09-09 20:02:12 +02:00
_config.searcharea.classList.remove('animation-slidedown')
_config.searcharea.classList.add('animation-bounceOutUp')
_config.searchpop.classList.add('hide')
document.body.classList.remove('has-search-open')
2017-09-09 19:22:04 +02:00
},
searchClose() {
2017-09-09 20:02:12 +02:00
_config.searchclose.addEventListener('click', e => {
2017-09-09 19:22:04 +02:00
e.preventDefault()
_private.searchHide()
// Empty search field
2017-09-09 20:02:12 +02:00
_config.searchfield.value = ''
_config.searchfield.blur()
2017-09-09 19:22:04 +02:00
})
},
searchShow() {
2017-09-09 20:02:12 +02:00
_config.searchlink.addEventListener('click', e => {
2017-09-09 19:22:04 +02:00
e.preventDefault()
// Show search field
2017-09-09 20:02:12 +02:00
_config.searcharea.classList.remove('is-ready', 'animation-bounceOutUp')
_config.searcharea.classList.add('is-ready', 'animation-slidedown')
document.body.classList.add('has-search-open')
2017-09-09 19:22:04 +02:00
_config.searchfield.focus()
SimpleJekyllSearch({ // eslint-disable-line new-cap
2017-09-09 20:02:12 +02:00
searchInput: _config.searchfield,
resultsContainer: _config.searchresults,
2017-09-09 19:22:04 +02:00
json: '/api/search.json',
searchResultTemplate: '<a class="search-link" href="{url}">{title}</a>',
fuzzy: false
})
// Hide menu too just in case
2017-09-09 20:02:12 +02:00
if (document.body.classList.contains('has-menu-open')) {
document.body.classList.remove('has-menu-open')
2017-09-09 19:22:04 +02:00
}
// Dont close thepop when click on thepop
2017-09-09 20:02:12 +02:00
_config.searchpop.addEventListener('click', e => {
2017-09-09 19:22:04 +02:00
e.stopPropagation()
})
2017-09-09 20:02:12 +02:00
2017-09-09 19:22:04 +02:00
// Dont close thepop when click on search field
2017-09-09 20:02:12 +02:00
_config.searchfield.addEventListener('click', e => {
2017-09-09 19:22:04 +02:00
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
2017-09-09 20:02:12 +02:00
_config.searchfield.addEventListener('keyup', () => {
_config.searchpop.classList.remove('hide')
2017-09-09 19:22:04 +02:00
})
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