saturneric.github.io/js/schemes/muse.js

134 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-10-25 06:36:20 +00:00
/* global NexT, CONFIG, Velocity */
window.addEventListener('DOMContentLoaded', () => {
var isRight = CONFIG.sidebar.position === 'right';
var SIDEBAR_WIDTH = CONFIG.sidebar.width || 320;
var SIDEBAR_DISPLAY_DURATION = 400;
var mousePos = {}; var touchPos = {};
var sidebarToggleLines = {
lines: document.querySelector('.sidebar-toggle'),
init : function() {
this.lines.classList.remove('toggle-arrow', 'toggle-close');
},
arrow: function() {
this.lines.classList.remove('toggle-close');
this.lines.classList.add('toggle-arrow');
},
close: function() {
this.lines.classList.remove('toggle-arrow');
this.lines.classList.add('toggle-close');
}
};
var sidebarToggleMotion = {
sidebarEl : document.querySelector('.sidebar'),
isSidebarVisible: false,
init : function() {
sidebarToggleLines.init();
window.addEventListener('mousedown', this.mousedownHandler.bind(this));
window.addEventListener('mouseup', this.mouseupHandler.bind(this));
document.querySelector('#sidebar-dimmer').addEventListener('click', this.clickHandler.bind(this));
document.querySelector('.sidebar-toggle').addEventListener('click', this.clickHandler.bind(this));
document.querySelector('.sidebar-toggle').addEventListener('mouseenter', this.mouseEnterHandler.bind(this));
document.querySelector('.sidebar-toggle').addEventListener('mouseleave', this.mouseLeaveHandler.bind(this));
this.sidebarEl.addEventListener('touchstart', this.touchstartHandler.bind(this));
this.sidebarEl.addEventListener('touchend', this.touchendHandler.bind(this));
this.sidebarEl.addEventListener('touchmove', event => event.preventDefault());
window.addEventListener('sidebar:show', this.showSidebar.bind(this));
window.addEventListener('sidebar:hide', this.hideSidebar.bind(this));
},
mousedownHandler: function(event) {
mousePos.X = event.pageX;
mousePos.Y = event.pageY;
},
mouseupHandler: function(event) {
var deltaX = event.pageX - mousePos.X;
var deltaY = event.pageY - mousePos.Y;
var clickingBlankPart = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY)) < 20 && event.target.matches('.main');
if (this.isSidebarVisible && (clickingBlankPart || event.target.matches('img.medium-zoom-image, .fancybox img'))) {
this.hideSidebar();
}
},
clickHandler: function() {
this.isSidebarVisible ? this.hideSidebar() : this.showSidebar();
},
mouseEnterHandler: function() {
if (!this.isSidebarVisible) {
sidebarToggleLines.arrow();
}
},
mouseLeaveHandler: function() {
if (!this.isSidebarVisible) {
sidebarToggleLines.init();
}
},
touchstartHandler: function(event) {
touchPos.X = event.touches[0].clientX;
touchPos.Y = event.touches[0].clientY;
},
touchendHandler: function(event) {
var deltaX = event.changedTouches[0].clientX - touchPos.X;
var deltaY = event.changedTouches[0].clientY - touchPos.Y;
var effectiveSliding = Math.abs(deltaY) < 20 && ((deltaX > 30 && isRight) || (deltaX < -30 && !isRight));
if (this.isSidebarVisible && effectiveSliding) {
this.hideSidebar();
}
},
showSidebar: function() {
this.isSidebarVisible = true;
this.sidebarEl.classList.add('sidebar-active');
if (typeof Velocity === 'function') {
Velocity(document.querySelectorAll('.sidebar .motion-element'), isRight ? 'transition.slideRightIn' : 'transition.slideLeftIn', {
stagger: 50,
drag : true
});
}
sidebarToggleLines.close();
NexT.utils.isDesktop() && window.anime(Object.assign({
targets : document.body,
duration: SIDEBAR_DISPLAY_DURATION,
easing : 'linear'
}, isRight ? {
'padding-right': SIDEBAR_WIDTH
} : {
'padding-left': SIDEBAR_WIDTH
}));
},
hideSidebar: function() {
this.isSidebarVisible = false;
this.sidebarEl.classList.remove('sidebar-active');
sidebarToggleLines.init();
NexT.utils.isDesktop() && window.anime(Object.assign({
targets : document.body,
duration: SIDEBAR_DISPLAY_DURATION,
easing : 'linear'
}, isRight ? {
'padding-right': 0
} : {
'padding-left': 0
}));
}
};
sidebarToggleMotion.init();
function updateFooterPosition() {
var containerHeight = document.querySelector('.container').offsetHeight;
var footer = document.getElementById('footer');
if (footer.classList.contains('footer-fixed')) containerHeight += footer.outerHeight(true);
if (containerHeight > window.innerHeight) {
footer.classList.remove('footer-fixed');
} else {
footer.classList.add('footer-fixed');
}
}
updateFooterPosition();
window.addEventListener('resize', updateFooterPosition);
window.addEventListener('scroll', updateFooterPosition);
});