/vendor/foundation-sites/dist/js/plugins/foundation.responsiveToggle.js

https://gitlab.com/infogenix/foundation-wp · JavaScript · 177 lines · 93 code · 32 blank · 52 comment · 17 complexity · e9c7119f9f972bed80ce050a7f8bed8b MD5 · raw file

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