muse.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* global NexT, CONFIG, Velocity */
  2. window.addEventListener('DOMContentLoaded', () => {
  3. var isRight = CONFIG.sidebar.position === 'right';
  4. var SIDEBAR_WIDTH = CONFIG.sidebar.width || 320;
  5. var SIDEBAR_DISPLAY_DURATION = 200;
  6. var mousePos = {};
  7. var sidebarToggleLines = {
  8. lines: document.querySelector('.sidebar-toggle'),
  9. init : function() {
  10. this.lines.classList.remove('toggle-arrow', 'toggle-close');
  11. },
  12. arrow: function() {
  13. this.lines.classList.remove('toggle-close');
  14. this.lines.classList.add('toggle-arrow');
  15. },
  16. close: function() {
  17. this.lines.classList.remove('toggle-arrow');
  18. this.lines.classList.add('toggle-close');
  19. }
  20. };
  21. var sidebarToggleMotion = {
  22. sidebarEl : document.querySelector('.sidebar'),
  23. isSidebarVisible: false,
  24. init : function() {
  25. sidebarToggleLines.init();
  26. window.addEventListener('mousedown', this.mousedownHandler.bind(this));
  27. window.addEventListener('mouseup', this.mouseupHandler.bind(this));
  28. document.querySelector('#sidebar-dimmer').addEventListener('click', this.clickHandler.bind(this));
  29. document.querySelector('.sidebar-toggle').addEventListener('click', this.clickHandler.bind(this));
  30. document.querySelector('.sidebar-toggle').addEventListener('mouseenter', this.mouseEnterHandler.bind(this));
  31. document.querySelector('.sidebar-toggle').addEventListener('mouseleave', this.mouseLeaveHandler.bind(this));
  32. window.addEventListener('sidebar:show', this.showSidebar.bind(this));
  33. window.addEventListener('sidebar:hide', this.hideSidebar.bind(this));
  34. },
  35. mousedownHandler: function(event) {
  36. mousePos.X = event.pageX;
  37. mousePos.Y = event.pageY;
  38. },
  39. mouseupHandler: function(event) {
  40. var deltaX = event.pageX - mousePos.X;
  41. var deltaY = event.pageY - mousePos.Y;
  42. var clickingBlankPart = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY)) < 20 && event.target.matches('.main');
  43. if (this.isSidebarVisible && (clickingBlankPart || event.target.matches('img.medium-zoom-image, .fancybox img'))) {
  44. this.hideSidebar();
  45. }
  46. },
  47. clickHandler: function() {
  48. this.isSidebarVisible ? this.hideSidebar() : this.showSidebar();
  49. },
  50. mouseEnterHandler: function() {
  51. if (!this.isSidebarVisible) {
  52. sidebarToggleLines.arrow();
  53. }
  54. },
  55. mouseLeaveHandler: function() {
  56. if (!this.isSidebarVisible) {
  57. sidebarToggleLines.init();
  58. }
  59. },
  60. showSidebar: function() {
  61. this.isSidebarVisible = true;
  62. this.sidebarEl.classList.add('sidebar-active');
  63. if (typeof Velocity === 'function') {
  64. Velocity(document.querySelectorAll('.sidebar .motion-element'), isRight ? 'transition.slideRightIn' : 'transition.slideLeftIn', {
  65. stagger: 50,
  66. drag : true
  67. });
  68. }
  69. sidebarToggleLines.close();
  70. NexT.utils.isDesktop() && window.anime(Object.assign({
  71. targets : document.body,
  72. duration: SIDEBAR_DISPLAY_DURATION,
  73. easing : 'linear'
  74. }, isRight ? {
  75. 'padding-right': SIDEBAR_WIDTH
  76. } : {
  77. 'padding-left': SIDEBAR_WIDTH
  78. }));
  79. },
  80. hideSidebar: function() {
  81. this.isSidebarVisible = false;
  82. this.sidebarEl.classList.remove('sidebar-active');
  83. sidebarToggleLines.init();
  84. NexT.utils.isDesktop() && window.anime(Object.assign({
  85. targets : document.body,
  86. duration: SIDEBAR_DISPLAY_DURATION,
  87. easing : 'linear'
  88. }, isRight ? {
  89. 'padding-right': 0
  90. } : {
  91. 'padding-left': 0
  92. }));
  93. }
  94. };
  95. sidebarToggleMotion.init();
  96. function updateFooterPosition() {
  97. var footer = document.querySelector('.footer');
  98. var containerHeight = document.querySelector('.header').offsetHeight + document.querySelector('.main').offsetHeight + footer.offsetHeight;
  99. footer.classList.toggle('footer-fixed', containerHeight <= window.innerHeight);
  100. }
  101. updateFooterPosition();
  102. window.addEventListener('resize', updateFooterPosition);
  103. window.addEventListener('scroll', updateFooterPosition);
  104. });