1 | 'use strict';
|
2 |
|
3 | import $ from 'jquery';
|
4 |
|
5 | import { MediaQuery } from './foundation.util.mediaQuery';
|
6 | import { GetYoDigits } from './foundation.util.core';
|
7 | import { Plugin } from './foundation.plugin';
|
8 |
|
9 | import { DropdownMenu } from './foundation.dropdownMenu';
|
10 | import { Drilldown } from './foundation.drilldown';
|
11 | import { AccordionMenu } from './foundation.accordionMenu';
|
12 |
|
13 | let MenuPlugins = {
|
14 | dropdown: {
|
15 | cssClass: 'dropdown',
|
16 | plugin: DropdownMenu
|
17 | },
|
18 | drilldown: {
|
19 | cssClass: 'drilldown',
|
20 | plugin: Drilldown
|
21 | },
|
22 | accordion: {
|
23 | cssClass: 'accordion-menu',
|
24 | plugin: AccordionMenu
|
25 | }
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | class ResponsiveMenu extends Plugin {
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | _setup(element, options) {
|
48 | this.$element = $(element);
|
49 | this.rules = this.$element.data('responsive-menu');
|
50 | this.currentMq = null;
|
51 | this.currentPlugin = null;
|
52 | this.className = 'ResponsiveMenu';
|
53 |
|
54 | this._init();
|
55 | this._events();
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | _init() {
|
64 |
|
65 | MediaQuery._init();
|
66 |
|
67 | if (typeof this.rules === 'string') {
|
68 | let rulesTree = {};
|
69 |
|
70 |
|
71 | let rules = this.rules.split(' ');
|
72 |
|
73 |
|
74 | for (let i = 0; i < rules.length; i++) {
|
75 | let rule = rules[i].split('-');
|
76 | let ruleSize = rule.length > 1 ? rule[0] : 'small';
|
77 | let rulePlugin = rule.length > 1 ? rule[1] : rule[0];
|
78 |
|
79 | if (MenuPlugins[rulePlugin] !== null) {
|
80 | rulesTree[ruleSize] = MenuPlugins[rulePlugin];
|
81 | }
|
82 | }
|
83 |
|
84 | this.rules = rulesTree;
|
85 | }
|
86 |
|
87 | if (!$.isEmptyObject(this.rules)) {
|
88 | this._checkMediaQueries();
|
89 | }
|
90 |
|
91 | this.$element.attr('data-mutate', (this.$element.attr('data-mutate') || GetYoDigits(6, 'responsive-menu')));
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | _events() {
|
100 | var _this = this;
|
101 |
|
102 | $(window).on('changed.zf.mediaquery', function() {
|
103 | _this._checkMediaQueries();
|
104 | });
|
105 |
|
106 |
|
107 |
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 | _checkMediaQueries() {
|
116 | var matchedMq, _this = this;
|
117 |
|
118 | $.each(this.rules, function(key) {
|
119 | if (MediaQuery.atLeast(key)) {
|
120 | matchedMq = key;
|
121 | }
|
122 | });
|
123 |
|
124 |
|
125 | if (!matchedMq) return;
|
126 |
|
127 |
|
128 | if (this.currentPlugin instanceof this.rules[matchedMq].plugin) return;
|
129 |
|
130 |
|
131 | $.each(MenuPlugins, function(key, value) {
|
132 | _this.$element.removeClass(value.cssClass);
|
133 | });
|
134 |
|
135 |
|
136 | this.$element.addClass(this.rules[matchedMq].cssClass);
|
137 |
|
138 |
|
139 | if (this.currentPlugin) this.currentPlugin.destroy();
|
140 | this.currentPlugin = new this.rules[matchedMq].plugin(this.$element, {});
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | _destroy() {
|
148 | this.currentPlugin.destroy();
|
149 | $(window).off('.zf.ResponsiveMenu');
|
150 | }
|
151 | }
|
152 |
|
153 | ResponsiveMenu.defaults = {};
|
154 |
|
155 | export {ResponsiveMenu};
|