web/assets/js/_src/_commons/search-display.js

154 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-09-30 15:38:41 +03:00
/*
* This script make #search-result-wrapper switch to unloaded or shown automatically.
2019-09-30 15:38:41 +03:00
* © 2018-2019 Cotes Chung
* MIT License
*/
$(function() {
var btnSbTrigger = $('#sidebar-trigger');
var btnSearchTrigger = $('#search-trigger');
var btnCancel = $('#search-wrapper + a');
var btnClear = $('#search-cleaner');
2019-09-30 15:38:41 +03:00
var main = $('#main');
var topbarTitle = $('#topbar-title');
var searchWrapper = $('#search-wrapper');
var resultWrapper = $('#search-result-wrapper');
var results = $('#search-results');
var input = $('#search-input');
var hints = $('#search-hints');
/*--- Actions in small screens (Sidebar unloaded) ---*/
var scrollBlocker = (function() {
var offset = 0;
return {
block: function() {
offset = $(window).scrollTop();
$('body').addClass('no-scroll');
},
release: function() {
$('body').removeClass('no-scroll');
$('html,body').scrollTop(offset);
},
getOffset: function() {
return offset;
}
}
})();
var mobileSearchBar = (function() {
return {
on: function() {
btnSbTrigger.addClass('unloaded');
topbarTitle.addClass('unloaded');
btnSearchTrigger.addClass('unloaded');
searchWrapper.addClass('d-flex');
btnCancel.addClass('loaded');
},
off: function() {
btnCancel.removeClass('loaded');
searchWrapper.removeClass('d-flex');
btnSbTrigger.removeClass('unloaded');
topbarTitle.removeClass('unloaded');
btnSearchTrigger.removeClass('unloaded');
}
}
})();
var resultSwitch = (function() {
var visable = false;
return {
on: function() {
if (!visable) {
resultWrapper.removeClass('unloaded');
main.addClass('hidden');
visable = true;
scrollBlocker.block();
}
},
off: function() {
if (visable) {
results.empty();
if (hints.hasClass('unloaded')) {
hints.removeClass('unloaded');
}
resultWrapper.addClass('unloaded');
btnClear.removeClass('visable');
main.removeClass('hidden');
input.val('');
visable = false;
scrollBlocker.release();
}
},
isVisable: function() {
return visable;
}
}
})();
2019-09-30 15:38:41 +03:00
function isMobileView() {
return btnCancel.hasClass('loaded');
}
2019-09-30 15:38:41 +03:00
btnSearchTrigger.click(function() {
mobileSearchBar.on();
resultSwitch.on();
input.focus();
2019-09-30 15:38:41 +03:00
});
btnCancel.click(function() {
mobileSearchBar.off();
resultSwitch.off();
2019-09-30 15:38:41 +03:00
});
input.focus(function() {
searchWrapper.addClass('input-focus');
});
2019-09-30 15:38:41 +03:00
input.focusout(function() {
searchWrapper.removeClass('input-focus');
});
2019-09-30 15:38:41 +03:00
input.on('keyup', function(e) {
if (e.keyCode == 8 && input.val() == '') {
if (!isMobileView()) {
resultSwitch.off();
} else {
hints.removeClass('unloaded');
}
} else {
if (input.val() != '') {
resultSwitch.on();
if (!btnClear.hasClass('visible')) {
btnClear.addClass('visable');
}
if (isMobileView()) {
hints.addClass('unloaded');
}
}
2019-09-30 15:38:41 +03:00
}
});
btnClear.on('click', function() {
input.val('');
if (isMobileView()) {
hints.removeClass('unloaded');
results.empty();
} else {
resultSwitch.off();
2019-09-30 15:38:41 +03:00
}
input.focus();
btnClear.removeClass('visable');
2019-09-30 15:38:41 +03:00
});
});