1 | 'use strict';
|
2 |
|
3 | import $ from 'jquery';
|
4 | import { Motion } from './foundation.util.motion';
|
5 | import { Plugin } from './foundation.plugin';
|
6 | import { Triggers } from './foundation.util.triggers';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class Toggler extends Plugin {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | _setup(element, options) {
|
25 | this.$element = element;
|
26 | this.options = $.extend({}, Toggler.defaults, element.data(), options);
|
27 | this.className = '';
|
28 | this.className = 'Toggler';
|
29 |
|
30 |
|
31 | Triggers.init($);
|
32 |
|
33 | this._init();
|
34 | this._events();
|
35 | }
|
36 |
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 | _init() {
|
43 | var input;
|
44 |
|
45 | if (this.options.animate) {
|
46 | input = this.options.animate.split(' ');
|
47 |
|
48 | this.animationIn = input[0];
|
49 | this.animationOut = input[1] || null;
|
50 | }
|
51 |
|
52 | else {
|
53 | input = this.$element.data('toggler');
|
54 |
|
55 | this.className = input[0] === '.' ? input.slice(1) : input;
|
56 | }
|
57 |
|
58 |
|
59 | var id = this.$element[0].id;
|
60 | $(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`)
|
61 | .attr('aria-controls', id);
|
62 |
|
63 | this.$element.attr('aria-expanded', this.$element.is(':hidden') ? false : true);
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 | _events() {
|
72 | this.$element.off('toggle.zf.trigger').on('toggle.zf.trigger', this.toggle.bind(this));
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | toggle() {
|
82 | this[ this.options.animate ? '_toggleAnimate' : '_toggleClass']();
|
83 | }
|
84 |
|
85 | _toggleClass() {
|
86 | this.$element.toggleClass(this.className);
|
87 |
|
88 | var isOn = this.$element.hasClass(this.className);
|
89 | if (isOn) {
|
90 | |
91 |
|
92 |
|
93 |
|
94 | this.$element.trigger('on.zf.toggler');
|
95 | }
|
96 | else {
|
97 | |
98 |
|
99 |
|
100 |
|
101 | this.$element.trigger('off.zf.toggler');
|
102 | }
|
103 |
|
104 | this._updateARIA(isOn);
|
105 | this.$element.find('[data-mutate]').trigger('mutateme.zf.trigger');
|
106 | }
|
107 |
|
108 | _toggleAnimate() {
|
109 | var _this = this;
|
110 |
|
111 | if (this.$element.is(':hidden')) {
|
112 | Motion.animateIn(this.$element, this.animationIn, function() {
|
113 | _this._updateARIA(true);
|
114 | this.trigger('on.zf.toggler');
|
115 | this.find('[data-mutate]').trigger('mutateme.zf.trigger');
|
116 | });
|
117 | }
|
118 | else {
|
119 | Motion.animateOut(this.$element, this.animationOut, function() {
|
120 | _this._updateARIA(false);
|
121 | this.trigger('off.zf.toggler');
|
122 | this.find('[data-mutate]').trigger('mutateme.zf.trigger');
|
123 | });
|
124 | }
|
125 | }
|
126 |
|
127 | _updateARIA(isOn) {
|
128 | this.$element.attr('aria-expanded', isOn ? true : false);
|
129 | }
|
130 |
|
131 | |
132 |
|
133 |
|
134 |
|
135 | _destroy() {
|
136 | this.$element.off('.zf.toggler');
|
137 | }
|
138 | }
|
139 |
|
140 | Toggler.defaults = {
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | animate: false
|
148 | };
|
149 |
|
150 | export {Toggler};
|