1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-09-24 18:18:49 +02:00
blog/_src/_assets/js/_search.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-11-19 16:52:44 +01:00
var Search = (function(w, d) {
var content = $('.site__content'),
searchlink = $('.search-btn'),
searcharea = $('.search-area'),
searchfield = $('#search-input'),
searchresults = $('#search-results'),
searchpop = $('#search-popover');
var app, _private;
_private = {
searchShow: function() {
searchlink.on('click', function(e) {
e.preventDefault();
SimpleJekyllSearch({
searchInput: document.getElementById('search-input'),
resultsContainer: document.getElementById('search-results'),
json: '/search.json',
searchResultTemplate: '<li class="grid__col"><a class="search-link" href="{url}">{title}</a></li>',
fuzzy: false
});
// show search field
searcharea
.removeClass('ready bounceOutUp')
.addClass('ready slideDown')
.on('animationend webkitAnimationEnd oAnimationEnd', function(){
2015-11-19 17:53:50 +01:00
$('body').addClass('search-open');
2015-11-19 16:52:44 +01:00
});
searchfield.focus();
// hide menu too just in case
if ($('body').hasClass('menu-open')) {
$('body').removeClass('menu-open');
}
// show search results upon typing
if (searchfield.val().length) {
searchpop.removeClass('hide');
}
// bind the hide controls
$(document).bind('click.hidethepop', function() {
_private.searchReset();
// unbind the hide controls
$(document).unbind('click.hidethepop');
});
// dont close thepop when click on thepop
searchpop.on('click', function(e) {
e.stopPropagation();
});
// dont close thepop when click on search field
searchfield.on('click', function(e) {
e.stopPropagation();
});
// and dont close thepop now
e.stopPropagation();
2015-11-19 16:07:27 +01:00
});
2015-11-19 14:55:49 +01:00
2015-11-19 16:52:44 +01:00
// finally show popup upon first keypress
searchfield.on('keyup', function() {
searchpop.removeClass('hide');
});
},
2015-11-19 14:55:49 +01:00
2015-11-19 16:52:44 +01:00
searchHide: function() {
$('.search-close').on('click', function(e) {
e.preventDefault();
2015-11-19 14:55:49 +01:00
2015-11-19 16:52:44 +01:00
_private.searchReset();
2015-11-19 14:55:49 +01:00
2015-11-19 16:52:44 +01:00
// empty search field
searchfield.val('').blur();
2015-11-19 14:55:49 +01:00
});
2015-11-19 16:52:44 +01:00
},
searchReset: function() {
// revert all search elements
searcharea
.removeClass('slideDown')
.addClass('bounceOutUp')
.on('animationend webkitAnimationEnd oAnimationEnd', function(){
2015-11-19 17:53:50 +01:00
$('body').removeClass('search-open');
2015-11-19 16:52:44 +01:00
});
searchpop.addClass('hide');
}
};
app = {
init: function() {
_private.searchShow();
_private.searchHide();
}
};
return app;
})(document, window);