UNPKG

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