UNPKG

11.9 kBJavaScriptView Raw
1function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
6
7function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
8
9/**
10 * --------------------------------------------------------------------------
11 * Bootstrap (v4.1.0): collapse.js
12 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
13 * --------------------------------------------------------------------------
14 */
15var Collapse = function ($) {
16 /**
17 * ------------------------------------------------------------------------
18 * Constants
19 * ------------------------------------------------------------------------
20 */
21 var NAME = 'collapse';
22 var VERSION = '4.1.0';
23 var DATA_KEY = 'bs.collapse';
24 var EVENT_KEY = "." + DATA_KEY;
25 var DATA_API_KEY = '.data-api';
26 var JQUERY_NO_CONFLICT = $.fn[NAME];
27 var Default = {
28 toggle: true,
29 parent: ''
30 };
31 var DefaultType = {
32 toggle: 'boolean',
33 parent: '(string|element)'
34 };
35 var Event = {
36 SHOW: "show" + EVENT_KEY,
37 SHOWN: "shown" + EVENT_KEY,
38 HIDE: "hide" + EVENT_KEY,
39 HIDDEN: "hidden" + EVENT_KEY,
40 CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
41 };
42 var ClassName = {
43 SHOW: 'show',
44 COLLAPSE: 'collapse',
45 COLLAPSING: 'collapsing',
46 COLLAPSED: 'collapsed'
47 };
48 var Dimension = {
49 WIDTH: 'width',
50 HEIGHT: 'height'
51 };
52 var Selector = {
53 ACTIVES: '.show, .collapsing',
54 DATA_TOGGLE: '[data-toggle="collapse"]'
55 /**
56 * ------------------------------------------------------------------------
57 * Class Definition
58 * ------------------------------------------------------------------------
59 */
60
61 };
62
63 var Collapse =
64 /*#__PURE__*/
65 function () {
66 function Collapse(element, config) {
67 this._isTransitioning = false;
68 this._element = element;
69 this._config = this._getConfig(config);
70 this._triggerArray = $.makeArray($("[data-toggle=\"collapse\"][href=\"#" + element.id + "\"]," + ("[data-toggle=\"collapse\"][data-target=\"#" + element.id + "\"]")));
71 var tabToggles = $(Selector.DATA_TOGGLE);
72
73 for (var i = 0; i < tabToggles.length; i++) {
74 var elem = tabToggles[i];
75 var selector = Util.getSelectorFromElement(elem);
76
77 if (selector !== null && $(selector).filter(element).length > 0) {
78 this._selector = selector;
79
80 this._triggerArray.push(elem);
81 }
82 }
83
84 this._parent = this._config.parent ? this._getParent() : null;
85
86 if (!this._config.parent) {
87 this._addAriaAndCollapsedClass(this._element, this._triggerArray);
88 }
89
90 if (this._config.toggle) {
91 this.toggle();
92 }
93 } // Getters
94
95
96 var _proto = Collapse.prototype;
97
98 // Public
99 _proto.toggle = function toggle() {
100 if ($(this._element).hasClass(ClassName.SHOW)) {
101 this.hide();
102 } else {
103 this.show();
104 }
105 };
106
107 _proto.show = function show() {
108 var _this = this;
109
110 if (this._isTransitioning || $(this._element).hasClass(ClassName.SHOW)) {
111 return;
112 }
113
114 var actives;
115 var activesData;
116
117 if (this._parent) {
118 actives = $.makeArray($(this._parent).find(Selector.ACTIVES).filter("[data-parent=\"" + this._config.parent + "\"]"));
119
120 if (actives.length === 0) {
121 actives = null;
122 }
123 }
124
125 if (actives) {
126 activesData = $(actives).not(this._selector).data(DATA_KEY);
127
128 if (activesData && activesData._isTransitioning) {
129 return;
130 }
131 }
132
133 var startEvent = $.Event(Event.SHOW);
134 $(this._element).trigger(startEvent);
135
136 if (startEvent.isDefaultPrevented()) {
137 return;
138 }
139
140 if (actives) {
141 Collapse._jQueryInterface.call($(actives).not(this._selector), 'hide');
142
143 if (!activesData) {
144 $(actives).data(DATA_KEY, null);
145 }
146 }
147
148 var dimension = this._getDimension();
149
150 $(this._element).removeClass(ClassName.COLLAPSE).addClass(ClassName.COLLAPSING);
151 this._element.style[dimension] = 0;
152
153 if (this._triggerArray.length > 0) {
154 $(this._triggerArray).removeClass(ClassName.COLLAPSED).attr('aria-expanded', true);
155 }
156
157 this.setTransitioning(true);
158
159 var complete = function complete() {
160 $(_this._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).addClass(ClassName.SHOW);
161 _this._element.style[dimension] = '';
162
163 _this.setTransitioning(false);
164
165 $(_this._element).trigger(Event.SHOWN);
166 };
167
168 var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
169 var scrollSize = "scroll" + capitalizedDimension;
170 var transitionDuration = Util.getTransitionDurationFromElement(this._element);
171 $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
172 this._element.style[dimension] = this._element[scrollSize] + "px";
173 };
174
175 _proto.hide = function hide() {
176 var _this2 = this;
177
178 if (this._isTransitioning || !$(this._element).hasClass(ClassName.SHOW)) {
179 return;
180 }
181
182 var startEvent = $.Event(Event.HIDE);
183 $(this._element).trigger(startEvent);
184
185 if (startEvent.isDefaultPrevented()) {
186 return;
187 }
188
189 var dimension = this._getDimension();
190
191 this._element.style[dimension] = this._element.getBoundingClientRect()[dimension] + "px";
192 Util.reflow(this._element);
193 $(this._element).addClass(ClassName.COLLAPSING).removeClass(ClassName.COLLAPSE).removeClass(ClassName.SHOW);
194
195 if (this._triggerArray.length > 0) {
196 for (var i = 0; i < this._triggerArray.length; i++) {
197 var trigger = this._triggerArray[i];
198 var selector = Util.getSelectorFromElement(trigger);
199
200 if (selector !== null) {
201 var $elem = $(selector);
202
203 if (!$elem.hasClass(ClassName.SHOW)) {
204 $(trigger).addClass(ClassName.COLLAPSED).attr('aria-expanded', false);
205 }
206 }
207 }
208 }
209
210 this.setTransitioning(true);
211
212 var complete = function complete() {
213 _this2.setTransitioning(false);
214
215 $(_this2._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).trigger(Event.HIDDEN);
216 };
217
218 this._element.style[dimension] = '';
219 var transitionDuration = Util.getTransitionDurationFromElement(this._element);
220 $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
221 };
222
223 _proto.setTransitioning = function setTransitioning(isTransitioning) {
224 this._isTransitioning = isTransitioning;
225 };
226
227 _proto.dispose = function dispose() {
228 $.removeData(this._element, DATA_KEY);
229 this._config = null;
230 this._parent = null;
231 this._element = null;
232 this._triggerArray = null;
233 this._isTransitioning = null;
234 }; // Private
235
236
237 _proto._getConfig = function _getConfig(config) {
238 config = _objectSpread({}, Default, config);
239 config.toggle = Boolean(config.toggle); // Coerce string values
240
241 Util.typeCheckConfig(NAME, config, DefaultType);
242 return config;
243 };
244
245 _proto._getDimension = function _getDimension() {
246 var hasWidth = $(this._element).hasClass(Dimension.WIDTH);
247 return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT;
248 };
249
250 _proto._getParent = function _getParent() {
251 var _this3 = this;
252
253 var parent = null;
254
255 if (Util.isElement(this._config.parent)) {
256 parent = this._config.parent; // It's a jQuery object
257
258 if (typeof this._config.parent.jquery !== 'undefined') {
259 parent = this._config.parent[0];
260 }
261 } else {
262 parent = $(this._config.parent)[0];
263 }
264
265 var selector = "[data-toggle=\"collapse\"][data-parent=\"" + this._config.parent + "\"]";
266 $(parent).find(selector).each(function (i, element) {
267 _this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
268 });
269 return parent;
270 };
271
272 _proto._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
273 if (element) {
274 var isOpen = $(element).hasClass(ClassName.SHOW);
275
276 if (triggerArray.length > 0) {
277 $(triggerArray).toggleClass(ClassName.COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
278 }
279 }
280 }; // Static
281
282
283 Collapse._getTargetFromElement = function _getTargetFromElement(element) {
284 var selector = Util.getSelectorFromElement(element);
285 return selector ? $(selector)[0] : null;
286 };
287
288 Collapse._jQueryInterface = function _jQueryInterface(config) {
289 return this.each(function () {
290 var $this = $(this);
291 var data = $this.data(DATA_KEY);
292
293 var _config = _objectSpread({}, Default, $this.data(), typeof config === 'object' && config);
294
295 if (!data && _config.toggle && /show|hide/.test(config)) {
296 _config.toggle = false;
297 }
298
299 if (!data) {
300 data = new Collapse(this, _config);
301 $this.data(DATA_KEY, data);
302 }
303
304 if (typeof config === 'string') {
305 if (typeof data[config] === 'undefined') {
306 throw new TypeError("No method named \"" + config + "\"");
307 }
308
309 data[config]();
310 }
311 });
312 };
313
314 _createClass(Collapse, null, [{
315 key: "VERSION",
316 get: function get() {
317 return VERSION;
318 }
319 }, {
320 key: "Default",
321 get: function get() {
322 return Default;
323 }
324 }]);
325
326 return Collapse;
327 }();
328 /**
329 * ------------------------------------------------------------------------
330 * Data Api implementation
331 * ------------------------------------------------------------------------
332 */
333
334
335 $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
336 // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
337 if (event.currentTarget.tagName === 'A') {
338 event.preventDefault();
339 }
340
341 var $trigger = $(this);
342 var selector = Util.getSelectorFromElement(this);
343 $(selector).each(function () {
344 var $target = $(this);
345 var data = $target.data(DATA_KEY);
346 var config = data ? 'toggle' : $trigger.data();
347
348 Collapse._jQueryInterface.call($target, config);
349 });
350 });
351 /**
352 * ------------------------------------------------------------------------
353 * jQuery
354 * ------------------------------------------------------------------------
355 */
356
357 $.fn[NAME] = Collapse._jQueryInterface;
358 $.fn[NAME].Constructor = Collapse;
359
360 $.fn[NAME].noConflict = function () {
361 $.fn[NAME] = JQUERY_NO_CONFLICT;
362 return Collapse._jQueryInterface;
363 };
364
365 return Collapse;
366}($);
367//# sourceMappingURL=collapse.js.map
\No newline at end of file