UNPKG

7.95 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports", "../../globals/js/settings", "../../globals/js/misc/mixin", "../../globals/js/mixins/create-component", "../../globals/js/mixins/init-component-by-search", "../../globals/js/mixins/handles", "../../globals/js/misc/event-matches", "../../globals/js/misc/on"], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require("../../globals/js/settings"), require("../../globals/js/misc/mixin"), require("../../globals/js/mixins/create-component"), require("../../globals/js/mixins/init-component-by-search"), require("../../globals/js/mixins/handles"), require("../../globals/js/misc/event-matches"), require("../../globals/js/misc/on"));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.settings, global.mixin, global.createComponent, global.initComponentBySearch, global.handles, global.eventMatches, global.on);
11 global.accordion = mod.exports;
12 }
13})(this, function (_exports, _settings, _mixin2, _createComponent, _initComponentBySearch, _handles, _eventMatches, _on) {
14 "use strict";
15
16 Object.defineProperty(_exports, "__esModule", {
17 value: true
18 });
19 _exports.default = void 0;
20 _settings = _interopRequireDefault(_settings);
21 _mixin2 = _interopRequireDefault(_mixin2);
22 _createComponent = _interopRequireDefault(_createComponent);
23 _initComponentBySearch = _interopRequireDefault(_initComponentBySearch);
24 _handles = _interopRequireDefault(_handles);
25 _eventMatches = _interopRequireDefault(_eventMatches);
26 _on = _interopRequireDefault(_on);
27
28 function _interopRequireDefault(obj) {
29 return obj && obj.__esModule ? obj : {
30 default: obj
31 };
32 }
33
34 function _typeof(obj) {
35 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
36 _typeof = function _typeof(obj) {
37 return typeof obj;
38 };
39 } else {
40 _typeof = function _typeof(obj) {
41 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
42 };
43 }
44
45 return _typeof(obj);
46 }
47
48 function _classCallCheck(instance, Constructor) {
49 if (!(instance instanceof Constructor)) {
50 throw new TypeError("Cannot call a class as a function");
51 }
52 }
53
54 function _defineProperties(target, props) {
55 for (var i = 0; i < props.length; i++) {
56 var descriptor = props[i];
57 descriptor.enumerable = descriptor.enumerable || false;
58 descriptor.configurable = true;
59 if ("value" in descriptor) descriptor.writable = true;
60 Object.defineProperty(target, descriptor.key, descriptor);
61 }
62 }
63
64 function _createClass(Constructor, protoProps, staticProps) {
65 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
66 if (staticProps) _defineProperties(Constructor, staticProps);
67 return Constructor;
68 }
69
70 function _possibleConstructorReturn(self, call) {
71 if (call && (_typeof(call) === "object" || typeof call === "function")) {
72 return call;
73 }
74
75 return _assertThisInitialized(self);
76 }
77
78 function _assertThisInitialized(self) {
79 if (self === void 0) {
80 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
81 }
82
83 return self;
84 }
85
86 function _getPrototypeOf(o) {
87 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
88 return o.__proto__ || Object.getPrototypeOf(o);
89 };
90 return _getPrototypeOf(o);
91 }
92
93 function _inherits(subClass, superClass) {
94 if (typeof superClass !== "function" && superClass !== null) {
95 throw new TypeError("Super expression must either be null or a function");
96 }
97
98 subClass.prototype = Object.create(superClass && superClass.prototype, {
99 constructor: {
100 value: subClass,
101 writable: true,
102 configurable: true
103 }
104 });
105 if (superClass) _setPrototypeOf(subClass, superClass);
106 }
107
108 function _setPrototypeOf(o, p) {
109 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
110 o.__proto__ = p;
111 return o;
112 };
113
114 return _setPrototypeOf(o, p);
115 }
116
117 var Accordion =
118 /*#__PURE__*/
119 function (_mixin) {
120 _inherits(Accordion, _mixin);
121 /**
122 * Accordion.
123 * @extends CreateComponent
124 * @extends InitComponentBySearch
125 * @extends Handles
126 * @param {HTMLElement} element The element working as an accordion.
127 */
128
129
130 function Accordion(element, options) {
131 var _this;
132
133 _classCallCheck(this, Accordion);
134
135 _this = _possibleConstructorReturn(this, _getPrototypeOf(Accordion).call(this, element, options));
136
137 _this.manage((0, _on.default)(_this.element, 'click', function (event) {
138 var item = (0, _eventMatches.default)(event, _this.options.selectorAccordionItem);
139
140 if (item && !(0, _eventMatches.default)(event, _this.options.selectorAccordionContent)) {
141 _this._toggle(item);
142 }
143 }));
144 /**
145 *
146 * DEPRECATE in v8
147 *
148 * Swapping to a button elemenet instead of a div
149 * automatically maps click events to keypress as well
150 * This event listener now is only added if user is using
151 * the older markup
152 */
153
154
155 if (!_this._checkIfButton()) {
156 _this.manage((0, _on.default)(_this.element, 'keypress', function (event) {
157 var item = (0, _eventMatches.default)(event, _this.options.selectorAccordionItem);
158
159 if (item && !(0, _eventMatches.default)(event, _this.options.selectorAccordionContent)) {
160 _this._handleKeypress(event);
161 }
162 }));
163 }
164
165 return _this;
166 }
167
168 _createClass(Accordion, [{
169 key: "_checkIfButton",
170 value: function _checkIfButton() {
171 return this.element.firstElementChild.firstElementChild.nodeName === 'BUTTON';
172 }
173 /**
174 * Handles toggling of active state of accordion via keyboard
175 * @param {Event} event The event triggering this method.
176 */
177
178 }, {
179 key: "_handleKeypress",
180 value: function _handleKeypress(event) {
181 if (event.which === 13 || event.which === 32) {
182 this._toggle(event.target);
183 }
184 }
185 }, {
186 key: "_toggle",
187 value: function _toggle(element) {
188 var heading = element.querySelector(this.options.selectorAccordionItemHeading);
189 var expanded = heading.getAttribute('aria-expanded');
190
191 if (expanded !== null) {
192 heading.setAttribute('aria-expanded', expanded === 'true' ? 'false' : 'true');
193 }
194
195 element.classList.toggle(this.options.classActive);
196 }
197 /**
198 * The component options.
199 * If `options` is specified in the constructor,
200 * {@linkcode NumberInput.create .create()}, or {@linkcode NumberInput.init .init()},
201 * properties in this object are overriden for the instance being create and how {@linkcode NumberInput.init .init()} works.
202 * @property {string} selectorInit The CSS selector to find accordion UIs.
203 */
204
205 }], [{
206 key: "options",
207 get: function get() {
208 var prefix = _settings.default.prefix;
209 return {
210 selectorInit: '[data-accordion]',
211 selectorAccordionItem: ".".concat(prefix, "--accordion__item"),
212 selectorAccordionItemHeading: ".".concat(prefix, "--accordion__heading"),
213 selectorAccordionContent: ".".concat(prefix, "--accordion__content"),
214 classActive: "".concat(prefix, "--accordion__item--active")
215 };
216 }
217 /**
218 * The map associating DOM element and accordion UI instance.
219 * @type {WeakMap}
220 */
221
222 }]);
223
224 Accordion.components = new WeakMap();
225 return Accordion;
226 }((0, _mixin2.default)(_createComponent.default, _initComponentBySearch.default, _handles.default));
227
228 var _default = Accordion;
229 _exports.default = _default;
230});
\No newline at end of file