scinote-web/app/assets/javascripts/sidebar_toggle.js.erb
aignatov-bio 6a44e23697
Add projects sidebar [SCI-5178] (#2995)
* Add projects sidebar navigation [SCI-5178]
2020-12-08 16:07:26 +01:00

59 lines
1.6 KiB
Text

var SideBarToggle = (function() {
var STORAGE_TOGGLE_KEY = "scinote-sidebar-toggled";
var WRAPPER = '#wrapper'
var SIDEBAR_CONTAINER = ".sidebar-container"
function show() {
$(SIDEBAR_CONTAINER).removeClass('collapsed');
$(WRAPPER).css('paddingLeft', 'var(--wrapper-width)');
$('.navbar-secondary').removeClass("navbar-without-sidebar");
sessionStorage.setItem(STORAGE_TOGGLE_KEY, "un-toggled");
$(WRAPPER).trigger('sideBar::show');
$(WRAPPER).one("transitionend", function() {
$(WRAPPER).trigger('sideBar::shown');
})
}
function hide() {
$(SIDEBAR_CONTAINER).addClass('collapsed');
$(WRAPPER).css('paddingLeft', '0');
$('.navbar-secondary').addClass("navbar-without-sidebar");
sessionStorage.setItem(STORAGE_TOGGLE_KEY, "toggled");
$(WRAPPER).trigger('sideBar::hide');
$(WRAPPER).one("transitionend", function() {
$(WRAPPER).trigger('sideBar::hidden');
})
}
function isToggledStorage() {
var val = sessionStorage.getItem(STORAGE_TOGGLE_KEY);
if (val === null) {
return null;
}
return val === "toggled";
}
function init() {
$(document).on('click', `${SIDEBAR_CONTAINER} .close-sidebar`, function() {
hide();
}).on('click', `${SIDEBAR_CONTAINER} .show-sidebar`, function() {
show();
}).on('turbolinks:load', function() {
if (isToggledStorage()) {
hide();
} else {
show();
}
$(WRAPPER).show();
})
}
return Object.freeze({
init: init,
show: show,
hide: hide,
isToggledStorage: isToggledStorage
})
}());
SideBarToggle.init();