foundation.responsiveToggle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. import $ from 'jquery';
  3. import { MediaQuery } from './foundation.util.mediaQuery';
  4. import { Motion } from './foundation.util.motion';
  5. import { Plugin } from './foundation.core.plugin';
  6. /**
  7. * ResponsiveToggle module.
  8. * @module foundation.responsiveToggle
  9. * @requires foundation.util.mediaQuery
  10. * @requires foundation.util.motion
  11. */
  12. class ResponsiveToggle extends Plugin {
  13. /**
  14. * Creates a new instance of Tab Bar.
  15. * @class
  16. * @name ResponsiveToggle
  17. * @fires ResponsiveToggle#init
  18. * @param {jQuery} element - jQuery object to attach tab bar functionality to.
  19. * @param {Object} options - Overrides to the default plugin settings.
  20. */
  21. _setup(element, options) {
  22. this.$element = $(element);
  23. this.options = $.extend({}, ResponsiveToggle.defaults, this.$element.data(), options);
  24. this.className = 'ResponsiveToggle'; // ie9 back compat
  25. this._init();
  26. this._events();
  27. }
  28. /**
  29. * Initializes the tab bar by finding the target element, toggling element, and running update().
  30. * @function
  31. * @private
  32. */
  33. _init() {
  34. MediaQuery._init();
  35. var targetID = this.$element.data('responsive-toggle');
  36. if (!targetID) {
  37. console.error('Your tab bar needs an ID of a Menu as the value of data-tab-bar.');
  38. }
  39. this.$targetMenu = $(`#${targetID}`);
  40. this.$toggler = this.$element.find('[data-toggle]').filter(function() {
  41. var target = $(this).data('toggle');
  42. return (target === targetID || target === "");
  43. });
  44. this.options = $.extend({}, this.options, this.$targetMenu.data());
  45. // If they were set, parse the animation classes
  46. if(this.options.animate) {
  47. let input = this.options.animate.split(' ');
  48. this.animationIn = input[0];
  49. this.animationOut = input[1] || null;
  50. }
  51. this._update();
  52. }
  53. /**
  54. * Adds necessary event handlers for the tab bar to work.
  55. * @function
  56. * @private
  57. */
  58. _events() {
  59. var _this = this;
  60. this._updateMqHandler = this._update.bind(this);
  61. $(window).on('changed.zf.mediaquery', this._updateMqHandler);
  62. this.$toggler.on('click.zf.responsiveToggle', this.toggleMenu.bind(this));
  63. }
  64. /**
  65. * Checks the current media query to determine if the tab bar should be visible or hidden.
  66. * @function
  67. * @private
  68. */
  69. _update() {
  70. // Mobile
  71. if (!MediaQuery.atLeast(this.options.hideFor)) {
  72. this.$element.show();
  73. this.$targetMenu.hide();
  74. }
  75. // Desktop
  76. else {
  77. this.$element.hide();
  78. this.$targetMenu.show();
  79. }
  80. }
  81. /**
  82. * Toggles the element attached to the tab bar. The toggle only happens if the screen is small enough to allow it.
  83. * @function
  84. * @fires ResponsiveToggle#toggled
  85. */
  86. toggleMenu() {
  87. if (!MediaQuery.atLeast(this.options.hideFor)) {
  88. /**
  89. * Fires when the element attached to the tab bar toggles.
  90. * @event ResponsiveToggle#toggled
  91. */
  92. if(this.options.animate) {
  93. if (this.$targetMenu.is(':hidden')) {
  94. Motion.animateIn(this.$targetMenu, this.animationIn, () => {
  95. this.$element.trigger('toggled.zf.responsiveToggle');
  96. this.$targetMenu.find('[data-mutate]').triggerHandler('mutateme.zf.trigger');
  97. });
  98. }
  99. else {
  100. Motion.animateOut(this.$targetMenu, this.animationOut, () => {
  101. this.$element.trigger('toggled.zf.responsiveToggle');
  102. });
  103. }
  104. }
  105. else {
  106. this.$targetMenu.toggle(0);
  107. this.$targetMenu.find('[data-mutate]').trigger('mutateme.zf.trigger');
  108. this.$element.trigger('toggled.zf.responsiveToggle');
  109. }
  110. }
  111. };
  112. _destroy() {
  113. this.$element.off('.zf.responsiveToggle');
  114. this.$toggler.off('.zf.responsiveToggle');
  115. $(window).off('changed.zf.mediaquery', this._updateMqHandler);
  116. }
  117. }
  118. ResponsiveToggle.defaults = {
  119. /**
  120. * The breakpoint after which the menu is always shown, and the tab bar is hidden.
  121. * @option
  122. * @type {string}
  123. * @default 'medium'
  124. */
  125. hideFor: 'medium',
  126. /**
  127. * To decide if the toggle should be animated or not.
  128. * @option
  129. * @type {boolean}
  130. * @default false
  131. */
  132. animate: false
  133. };
  134. export { ResponsiveToggle };