sidebar.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import $ from 'jquery';
  2. import Cookies from '../utils/cookies';
  3. const MOBILE_BREAKPOINT = 48 - 0.062;
  4. const DESKTOP_BREAKPOINT = 75 + 0.063;
  5. const EVENTS = 'touchstart._grav click._grav';
  6. const TARGETS = '[data-sidebar-mobile-toggle], #overlay';
  7. const MOBILE_QUERY = `(max-width: ${MOBILE_BREAKPOINT}em)`;
  8. const DESKTOP_QUERY = `(min-width: ${DESKTOP_BREAKPOINT}em)`;
  9. let map = new global.Map();
  10. export default class Sidebar {
  11. constructor() {
  12. this.timeout = null;
  13. this.isOpen = false;
  14. this.body = $('body');
  15. this.matchMedia = global.matchMedia(MOBILE_QUERY);
  16. this.enable();
  17. }
  18. enable() {
  19. const sidebar = $('#admin-sidebar');
  20. this.matchMedia.addListener(this._getBound('checkMatch'));
  21. this.checkMatch(this.matchMedia);
  22. this.body.on(EVENTS, '[data-sidebar-toggle]', this._getBound('toggleSidebarState'));
  23. if (sidebar.data('quickopen')) {
  24. sidebar.hover(this._getBound('quickOpenIn'), this._getBound('quickOpenOut'));
  25. }
  26. }
  27. disable() {
  28. const sidebar = $('#admin-sidebar');
  29. this.close();
  30. this.matchMedia.removeListener(this._getBound('checkMatch'));
  31. this.body.off(EVENTS, '[data-sidebar-toggle]', this._getBound('toggleSidebarState'));
  32. if (sidebar.data('quickopen')) {
  33. sidebar.off('mouseenter mouseleave');
  34. }
  35. }
  36. attach() {
  37. this.body.on(EVENTS, TARGETS, this._getBound('toggle'));
  38. }
  39. detach() {
  40. this.body.off(EVENTS, TARGETS, this._getBound('toggle'));
  41. }
  42. quickOpenIn(/* event */) {
  43. let isDesktop = global.matchMedia(DESKTOP_QUERY).matches;
  44. let delay = $('#admin-sidebar').data('quickopen-delay') || 500;
  45. if (this.body.hasClass('sidebar-mobile-open')) { return; }
  46. let shouldQuickOpen = isDesktop ? this.body.hasClass('sidebar-closed') : !this.body.hasClass('sidebar-open');
  47. if (!shouldQuickOpen && !this.body.hasClass('sidebar-quickopen')) { return this.quickOpenOut(); }
  48. this.timeout = setTimeout(() => {
  49. this.body.addClass('sidebar-open sidebar-quickopen');
  50. $(global).trigger('sidebar_state._grav', isDesktop);
  51. }, delay);
  52. }
  53. quickOpenOut(/* event */) {
  54. clearTimeout(this.timeout);
  55. if (this.body.hasClass('sidebar-quickopen')) {
  56. this.body.removeClass('sidebar-open sidebar-quickopen');
  57. }
  58. return true;
  59. }
  60. open(event, quick = false) {
  61. if (event) { event.preventDefault(); }
  62. let overlay = $('#overlay');
  63. let sidebar = $('#admin-sidebar');
  64. this.body.addClass('sidebar-mobile-open');
  65. overlay.css('display', 'block');
  66. if (!quick) {
  67. sidebar.css('display', 'block').animate({
  68. opacity: 1
  69. }, 200, () => {
  70. this.isOpen = true;
  71. });
  72. } else {
  73. sidebar.css({ display: 'block', opacity: 1 });
  74. this.isOpen = true;
  75. }
  76. }
  77. close(event, quick = false) {
  78. if (event) { event.preventDefault(); }
  79. let overlay = $('#overlay');
  80. let sidebar = $('#admin-sidebar');
  81. this.body.removeClass('sidebar-mobile-open');
  82. overlay.css('display', 'none');
  83. if (!quick) {
  84. sidebar.animate({
  85. opacity: 0
  86. }, 200, () => {
  87. sidebar.css('display', 'none');
  88. this.isOpen = false;
  89. });
  90. } else {
  91. sidebar.css({ opacity: 0, display: 'none' });
  92. this.isOpen = false;
  93. }
  94. }
  95. toggle(event) {
  96. if (event) { event.preventDefault(); }
  97. return this[this.isOpen ? 'close' : 'open'](event);
  98. }
  99. toggleSidebarState(event) {
  100. if (event) { event.preventDefault(); }
  101. clearTimeout(this.timeout);
  102. let isDesktop = global.matchMedia(DESKTOP_QUERY).matches;
  103. let cookie = null;
  104. if (isDesktop) {
  105. this.body.removeClass('sidebar-open');
  106. }
  107. if (!isDesktop) {
  108. this.body.removeClass('sidebar-closed');
  109. this.body.removeClass('sidebar-mobile-open');
  110. }
  111. this.body.toggleClass(`sidebar-${isDesktop ? 'closed' : 'open'}`);
  112. $(global).trigger('sidebar_state._grav', isDesktop);
  113. if (isDesktop) {
  114. cookie = !this.body.hasClass('sidebar-closed');
  115. } else {
  116. cookie = this.body.hasClass('sidebar-open');
  117. }
  118. Cookies.set('grav-admin-sidebar', cookie, { expires: Infinity });
  119. }
  120. checkMatch(data) {
  121. let sidebar = $('#admin-sidebar');
  122. let overlay = $('#overlay');
  123. this.isOpen = false;
  124. overlay.css('display', 'none');
  125. sidebar.css({
  126. display: data.matches ? 'none' : 'inherit',
  127. opacity: data.matches ? 0 : 1
  128. });
  129. if (data.matches) {
  130. this.body.removeClass('sidebar-open sidebar-closed');
  131. }
  132. this[data.matches ? 'attach' : 'detach']();
  133. }
  134. _resetMap() {
  135. return map.clear();
  136. }
  137. _getBound(fn) {
  138. if (map.has(fn)) {
  139. return map.get(fn);
  140. }
  141. return map.set(fn, this[fn].bind(this)).get(fn);
  142. }
  143. }
  144. export let Instance = new Sidebar();