UNPKG

9.68 kBJavaScriptView Raw
1function _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
15function _classCallCheck(instance, Constructor) {
16 if (!(instance instanceof Constructor)) {
17 throw new TypeError("Cannot call a class as a function");
18 }
19}
20
21function _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
31function _createClass(Constructor, protoProps, staticProps) {
32 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
33 if (staticProps) _defineProperties(Constructor, staticProps);
34 return Constructor;
35}
36
37function _possibleConstructorReturn(self, call) {
38 if (call && (_typeof(call) === "object" || typeof call === "function")) {
39 return call;
40 }
41
42 return _assertThisInitialized(self);
43}
44
45function _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
53function _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
60function _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
75function _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 * Copyright IBM Corp. 2016, 2018
85 *
86 * This source code is licensed under the Apache-2.0 license found in the
87 * LICENSE file in the root directory of this source tree.
88 */
89
90
91import settings from '../../globals/js/settings';
92import mixin from '../../globals/js/misc/mixin';
93import createComponent from '../../globals/js/mixins/create-component';
94import initComponentBySearch from '../../globals/js/mixins/init-component-by-search';
95import eventedState from '../../globals/js/mixins/evented-state';
96import handles from '../../globals/js/mixins/handles';
97import eventMatches from '../../globals/js/misc/event-matches';
98import on from '../../globals/js/misc/on';
99
100var toArray = function toArray(arrayLike) {
101 return Array.prototype.slice.call(arrayLike);
102};
103
104var ContentSwitcher =
105/*#__PURE__*/
106function (_mixin) {
107 _inherits(ContentSwitcher, _mixin);
108 /**
109 * Set of content switcher buttons.
110 * @extends CreateComponent
111 * @extends InitComponentBySearch
112 * @extends EventedState
113 * @extends Handles
114 * @param {HTMLElement} element The element working as a set of content switcher buttons.
115 * @param {Object} [options] The component options.
116 * @param {string} [options.selectorButton] The CSS selector to find switcher buttons.
117 * @param {string} [options.selectorButtonSelected] The CSS selector to find the selected switcher button.
118 * @param {string} [options.classActive] The CSS class for switcher button's selected state.
119 * @param {string} [options.eventBeforeSelected]
120 * The name of the custom event fired before a switcher button is selected.
121 * Cancellation of this event stops selection of content switcher button.
122 * @param {string} [options.eventAfterSelected] The name of the custom event fired after a switcher button is selected.
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 * Handles click on content switcher button set.
141 * If the click is on a content switcher button, activates it.
142 * @param {Event} event The event triggering this method.
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 * Internal method of {@linkcode ContentSwitcher#setActive .setActive()}, to select a content switcher button.
161 * @private
162 * @param {Object} detail The detail of the event trigging this action.
163 * @param {HTMLElement} detail.item The button to be selected.
164 * @param {Function} callback Callback called when change in state completes.
165 */
166
167 }, {
168 key: "_changeState",
169 value: function _changeState(_ref, callback) {
170 var _this2 = this;
171
172 var item = _ref.item; // `options.selectorLink` is not defined in this class itself, code here primary is for inherited classes
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 * Selects a content switcher button.
209 * If the selected button has `data-target` attribute, DOM elements it points to as a CSS selector will be shown.
210 * DOM elements associated with unselected buttons in the same way will be hidden.
211 * @param {HTMLElement} item The button to be selected.
212 * @param {ChangeState~callback} callback The callback is called once selection is finished
213 * or is canceled. Will only invoke callback if it's passed in.
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 * The map associating DOM element and content switcher set instance.
236 * @member ContentSwitcher.components
237 * @type {WeakMap}
238 */
239
240 }], [{
241 key: "options",
242
243 /**
244 * The component options.
245 * If `options` is specified in the constructor,
246 * {@linkcode ContentSwitcher.create .create()}, or {@linkcode ContentSwitcher.init .init()},
247 * properties in this object are overriden for the instance being create and how {@linkcode ContentSwitcher.init .init()} works.
248 * @member ContentSwitcher.options
249 * @type {Object}
250 * @property {string} selectorInit The CSS selector to find content switcher button set.
251 * @property {string} [selectorButton] The CSS selector to find switcher buttons.
252 * @property {string} [selectorButtonSelected] The CSS selector to find the selected switcher button.
253 * @property {string} [classActive] The CSS class for switcher button's selected state.
254 * @property {string} [eventBeforeSelected]
255 * The name of the custom event fired before a switcher button is selected.
256 * Cancellation of this event stops selection of content switcher button.
257 * @property {string} [eventAfterSelected] The name of the custom event fired after a switcher button is selected.
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
275export default ContentSwitcher;
\No newline at end of file