1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-09-24 01:58:50 +02:00

more js modularization

This commit is contained in:
Matthias Kretschmann 2015-11-19 16:52:44 +01:00
parent 6fed71618a
commit 60dd4498aa
3 changed files with 119 additions and 107 deletions

View File

@ -1,38 +1,43 @@
var s, Menu = { var Menu = (function(w, d) {
settings: { var thesite = $('.site'),
thesite: $('.site'), thelink = $('.menu-btn'),
thelink: $('.menu-btn'), thepop = $('.nav-popover');
thepop: $('.nav-popover')
},
init: function() { var app, _private;
this.menuShow();
},
menuShow: function() { _private = {
var s = this.settings; menuShow: function() {
thelink.on('click', function(e) {
e.preventDefault();
s.thelink.on('click', function(e) { // toggle menu
e.preventDefault(); thesite.toggleClass('menu-open');
// toggle menu // bind the hide controls
s.thesite.toggleClass('menu-open'); $(document).bind('click.hidethepop', function() {
thesite.removeClass('menu-open');
// unbind the hide controls
$(document).unbind('click.hidethepop');
});
// bind the hide controls // dont close thepop when you click on thepop
$(document).bind('click.hidethepop', function() { thepop.on('click', function(e) {
s.thesite.removeClass('menu-open'); e.stopPropagation();
// unbind the hide controls });
$(document).unbind('click.hidethepop');
});
// dont close thepop when you click on thepop // and dont close thepop now
s.thepop.on('click', function(e) {
e.stopPropagation(); e.stopPropagation();
}); });
}
};
// and dont close thepop now app = {
e.stopPropagation(); init: function() {
}); _private.menuShow();
} }
}; };
return app;
})(window, document);

View File

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

View File

@ -11,7 +11,7 @@
//=include _menu.js //=include _menu.js
//=include _search.js //=include _search.js
(function() { (function($) {
// //
// init modules // init modules
@ -23,4 +23,4 @@
nosvg: false nosvg: false
}); });
}()); }(jQuery));