1 | function _typeof(obj) {
|
2 | if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
3 | _typeof = function _typeof(obj) {
|
4 | return typeof obj;
|
5 | };
|
6 | } else {
|
7 | _typeof = function _typeof(obj) {
|
8 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
9 | };
|
10 | }
|
11 |
|
12 | return _typeof(obj);
|
13 | }
|
14 |
|
15 | function _classCallCheck(instance, Constructor) {
|
16 | if (!(instance instanceof Constructor)) {
|
17 | throw new TypeError("Cannot call a class as a function");
|
18 | }
|
19 | }
|
20 |
|
21 | function _defineProperties(target, props) {
|
22 | for (var i = 0; i < props.length; i++) {
|
23 | var descriptor = props[i];
|
24 | descriptor.enumerable = descriptor.enumerable || false;
|
25 | descriptor.configurable = true;
|
26 | if ("value" in descriptor) descriptor.writable = true;
|
27 | Object.defineProperty(target, descriptor.key, descriptor);
|
28 | }
|
29 | }
|
30 |
|
31 | function _createClass(Constructor, protoProps, staticProps) {
|
32 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
33 | if (staticProps) _defineProperties(Constructor, staticProps);
|
34 | return Constructor;
|
35 | }
|
36 |
|
37 | function _possibleConstructorReturn(self, call) {
|
38 | if (call && (_typeof(call) === "object" || typeof call === "function")) {
|
39 | return call;
|
40 | }
|
41 |
|
42 | return _assertThisInitialized(self);
|
43 | }
|
44 |
|
45 | function _assertThisInitialized(self) {
|
46 | if (self === void 0) {
|
47 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
48 | }
|
49 |
|
50 | return self;
|
51 | }
|
52 |
|
53 | function _getPrototypeOf(o) {
|
54 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
55 | return o.__proto__ || Object.getPrototypeOf(o);
|
56 | };
|
57 | return _getPrototypeOf(o);
|
58 | }
|
59 |
|
60 | function _inherits(subClass, superClass) {
|
61 | if (typeof superClass !== "function" && superClass !== null) {
|
62 | throw new TypeError("Super expression must either be null or a function");
|
63 | }
|
64 |
|
65 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
66 | constructor: {
|
67 | value: subClass,
|
68 | writable: true,
|
69 | configurable: true
|
70 | }
|
71 | });
|
72 | if (superClass) _setPrototypeOf(subClass, superClass);
|
73 | }
|
74 |
|
75 | function _setPrototypeOf(o, p) {
|
76 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
77 | o.__proto__ = p;
|
78 | return o;
|
79 | };
|
80 |
|
81 | return _setPrototypeOf(o, p);
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | import settings from '../../globals/js/settings';
|
92 | import mixin from '../../globals/js/misc/mixin';
|
93 | import createComponent from '../../globals/js/mixins/create-component';
|
94 | import initComponentBySearch from '../../globals/js/mixins/init-component-by-search';
|
95 | import eventedState from '../../globals/js/mixins/evented-state';
|
96 | import handles from '../../globals/js/mixins/handles';
|
97 | import eventMatches from '../../globals/js/misc/event-matches';
|
98 | import on from '../../globals/js/misc/on';
|
99 |
|
100 | var toArray = function toArray(arrayLike) {
|
101 | return Array.prototype.slice.call(arrayLike);
|
102 | };
|
103 |
|
104 | var ContentSwitcher =
|
105 |
|
106 | function (_mixin) {
|
107 | _inherits(ContentSwitcher, _mixin);
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function ContentSwitcher(element, options) {
|
127 | var _this;
|
128 |
|
129 | _classCallCheck(this, ContentSwitcher);
|
130 |
|
131 | _this = _possibleConstructorReturn(this, _getPrototypeOf(ContentSwitcher).call(this, element, options));
|
132 |
|
133 | _this.manage(on(_this.element, 'click', function (event) {
|
134 | _this._handleClick(event);
|
135 | }));
|
136 |
|
137 | return _this;
|
138 | }
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | _createClass(ContentSwitcher, [{
|
147 | key: "_handleClick",
|
148 | value: function _handleClick(event) {
|
149 | var button = eventMatches(event, this.options.selectorButton);
|
150 |
|
151 | if (button) {
|
152 | this.changeState({
|
153 | group: 'selected',
|
154 | item: button,
|
155 | launchingEvent: event
|
156 | });
|
157 | }
|
158 | }
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | }, {
|
168 | key: "_changeState",
|
169 | value: function _changeState(_ref, callback) {
|
170 | var _this2 = this;
|
171 |
|
172 | var item = _ref.item;
|
173 |
|
174 | var itemLink = item.querySelector(this.options.selectorLink);
|
175 |
|
176 | if (itemLink) {
|
177 | toArray(this.element.querySelectorAll(this.options.selectorLink)).forEach(function (link) {
|
178 | if (link !== itemLink) {
|
179 | link.setAttribute('aria-selected', 'false');
|
180 | }
|
181 | });
|
182 | itemLink.setAttribute('aria-selected', 'true');
|
183 | }
|
184 |
|
185 | var selectorButtons = toArray(this.element.querySelectorAll(this.options.selectorButton));
|
186 | selectorButtons.forEach(function (button) {
|
187 | if (button !== item) {
|
188 | button.setAttribute('aria-selected', false);
|
189 | button.classList.toggle(_this2.options.classActive, false);
|
190 | toArray(button.ownerDocument.querySelectorAll(button.dataset.target)).forEach(function (element) {
|
191 | element.setAttribute('hidden', '');
|
192 | element.setAttribute('aria-hidden', 'true');
|
193 | });
|
194 | }
|
195 | });
|
196 | item.classList.toggle(this.options.classActive, true);
|
197 | item.setAttribute('aria-selected', true);
|
198 | toArray(item.ownerDocument.querySelectorAll(item.dataset.target)).forEach(function (element) {
|
199 | element.removeAttribute('hidden');
|
200 | element.setAttribute('aria-hidden', 'false');
|
201 | });
|
202 |
|
203 | if (callback) {
|
204 | callback();
|
205 | }
|
206 | }
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | }, {
|
217 | key: "setActive",
|
218 | value: function setActive(item, callback) {
|
219 | this.changeState({
|
220 | group: 'selected',
|
221 | item: item
|
222 | }, function (error) {
|
223 | if (error) {
|
224 | if (callback) {
|
225 | callback(Object.assign(error, {
|
226 | item: item
|
227 | }));
|
228 | }
|
229 | } else if (callback) {
|
230 | callback(null, item);
|
231 | }
|
232 | });
|
233 | }
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | }], [{
|
241 | key: "options",
|
242 |
|
243 | |
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | get: function get() {
|
260 | var prefix = settings.prefix;
|
261 | return {
|
262 | selectorInit: '[data-content-switcher]',
|
263 | selectorButton: "input[type=\"radio\"], .".concat(prefix, "--content-switcher-btn"),
|
264 | classActive: "".concat(prefix, "--content-switcher--selected"),
|
265 | eventBeforeSelected: 'content-switcher-beingselected',
|
266 | eventAfterSelected: 'content-switcher-selected'
|
267 | };
|
268 | }
|
269 | }]);
|
270 |
|
271 | ContentSwitcher.components = new WeakMap();
|
272 | return ContentSwitcher;
|
273 | }(mixin(createComponent, initComponentBySearch, eventedState, handles));
|
274 |
|
275 | export default ContentSwitcher; |
\ | No newline at end of file |