]> git.ipfire.org Git - ipfire.org.git/blame - src/scss/bootstrap-4.0.0-alpha.6/js/dist/collapse.js
Introduce autotools
[ipfire.org.git] / src / scss / bootstrap-4.0.0-alpha.6 / js / dist / collapse.js
CommitLineData
91e44d91
S
1var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
2
3var _createClass = function () { function 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7/**
8 * --------------------------------------------------------------------------
9 * Bootstrap (v4.0.0-alpha.6): collapse.js
10 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
11 * --------------------------------------------------------------------------
12 */
13
14var Collapse = function ($) {
15
16 /**
17 * ------------------------------------------------------------------------
18 * Constants
19 * ------------------------------------------------------------------------
20 */
21
22 var NAME = 'collapse';
23 var VERSION = '4.0.0-alpha.6';
24 var DATA_KEY = 'bs.collapse';
25 var EVENT_KEY = '.' + DATA_KEY;
26 var DATA_API_KEY = '.data-api';
27 var JQUERY_NO_CONFLICT = $.fn[NAME];
28 var TRANSITION_DURATION = 600;
29
30 var Default = {
31 toggle: true,
32 parent: ''
33 };
34
35 var DefaultType = {
36 toggle: 'boolean',
37 parent: 'string'
38 };
39
40 var Event = {
41 SHOW: 'show' + EVENT_KEY,
42 SHOWN: 'shown' + EVENT_KEY,
43 HIDE: 'hide' + EVENT_KEY,
44 HIDDEN: 'hidden' + EVENT_KEY,
45 CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY
46 };
47
48 var ClassName = {
49 SHOW: 'show',
50 COLLAPSE: 'collapse',
51 COLLAPSING: 'collapsing',
52 COLLAPSED: 'collapsed'
53 };
54
55 var Dimension = {
56 WIDTH: 'width',
57 HEIGHT: 'height'
58 };
59
60 var Selector = {
61 ACTIVES: '.card > .show, .card > .collapsing',
62 DATA_TOGGLE: '[data-toggle="collapse"]'
63 };
64
65 /**
66 * ------------------------------------------------------------------------
67 * Class Definition
68 * ------------------------------------------------------------------------
69 */
70
71 var Collapse = function () {
72 function Collapse(element, config) {
73 _classCallCheck(this, Collapse);
74
75 this._isTransitioning = false;
76 this._element = element;
77 this._config = this._getConfig(config);
78 this._triggerArray = $.makeArray($('[data-toggle="collapse"][href="#' + element.id + '"],' + ('[data-toggle="collapse"][data-target="#' + element.id + '"]')));
79
80 this._parent = this._config.parent ? this._getParent() : null;
81
82 if (!this._config.parent) {
83 this._addAriaAndCollapsedClass(this._element, this._triggerArray);
84 }
85
86 if (this._config.toggle) {
87 this.toggle();
88 }
89 }
90
91 // getters
92
93 // public
94
95 Collapse.prototype.toggle = function toggle() {
96 if ($(this._element).hasClass(ClassName.SHOW)) {
97 this.hide();
98 } else {
99 this.show();
100 }
101 };
102
103 Collapse.prototype.show = function show() {
104 var _this = this;
105
106 if (this._isTransitioning) {
107 throw new Error('Collapse is transitioning');
108 }
109
110 if ($(this._element).hasClass(ClassName.SHOW)) {
111 return;
112 }
113
114 var actives = void 0;
115 var activesData = void 0;
116
117 if (this._parent) {
118 actives = $.makeArray($(this._parent).find(Selector.ACTIVES));
119 if (!actives.length) {
120 actives = null;
121 }
122 }
123
124 if (actives) {
125 activesData = $(actives).data(DATA_KEY);
126 if (activesData && activesData._isTransitioning) {
127 return;
128 }
129 }
130
131 var startEvent = $.Event(Event.SHOW);
132 $(this._element).trigger(startEvent);
133 if (startEvent.isDefaultPrevented()) {
134 return;
135 }
136
137 if (actives) {
138 Collapse._jQueryInterface.call($(actives), 'hide');
139 if (!activesData) {
140 $(actives).data(DATA_KEY, null);
141 }
142 }
143
144 var dimension = this._getDimension();
145
146 $(this._element).removeClass(ClassName.COLLAPSE).addClass(ClassName.COLLAPSING);
147
148 this._element.style[dimension] = 0;
149 this._element.setAttribute('aria-expanded', true);
150
151 if (this._triggerArray.length) {
152 $(this._triggerArray).removeClass(ClassName.COLLAPSED).attr('aria-expanded', true);
153 }
154
155 this.setTransitioning(true);
156
157 var complete = function complete() {
158 $(_this._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).addClass(ClassName.SHOW);
159
160 _this._element.style[dimension] = '';
161
162 _this.setTransitioning(false);
163
164 $(_this._element).trigger(Event.SHOWN);
165 };
166
167 if (!Util.supportsTransitionEnd()) {
168 complete();
169 return;
170 }
171
172 var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
173 var scrollSize = 'scroll' + capitalizedDimension;
174
175 $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
176
177 this._element.style[dimension] = this._element[scrollSize] + 'px';
178 };
179
180 Collapse.prototype.hide = function hide() {
181 var _this2 = this;
182
183 if (this._isTransitioning) {
184 throw new Error('Collapse is transitioning');
185 }
186
187 if (!$(this._element).hasClass(ClassName.SHOW)) {
188 return;
189 }
190
191 var startEvent = $.Event(Event.HIDE);
192 $(this._element).trigger(startEvent);
193 if (startEvent.isDefaultPrevented()) {
194 return;
195 }
196
197 var dimension = this._getDimension();
198 var offsetDimension = dimension === Dimension.WIDTH ? 'offsetWidth' : 'offsetHeight';
199
200 this._element.style[dimension] = this._element[offsetDimension] + 'px';
201
202 Util.reflow(this._element);
203
204 $(this._element).addClass(ClassName.COLLAPSING).removeClass(ClassName.COLLAPSE).removeClass(ClassName.SHOW);
205
206 this._element.setAttribute('aria-expanded', false);
207
208 if (this._triggerArray.length) {
209 $(this._triggerArray).addClass(ClassName.COLLAPSED).attr('aria-expanded', false);
210 }
211
212 this.setTransitioning(true);
213
214 var complete = function complete() {
215 _this2.setTransitioning(false);
216 $(_this2._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).trigger(Event.HIDDEN);
217 };
218
219 this._element.style[dimension] = '';
220
221 if (!Util.supportsTransitionEnd()) {
222 complete();
223 return;
224 }
225
226 $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
227 };
228
229 Collapse.prototype.setTransitioning = function setTransitioning(isTransitioning) {
230 this._isTransitioning = isTransitioning;
231 };
232
233 Collapse.prototype.dispose = function dispose() {
234 $.removeData(this._element, DATA_KEY);
235
236 this._config = null;
237 this._parent = null;
238 this._element = null;
239 this._triggerArray = null;
240 this._isTransitioning = null;
241 };
242
243 // private
244
245 Collapse.prototype._getConfig = function _getConfig(config) {
246 config = $.extend({}, Default, config);
247 config.toggle = Boolean(config.toggle); // coerce string values
248 Util.typeCheckConfig(NAME, config, DefaultType);
249 return config;
250 };
251
252 Collapse.prototype._getDimension = function _getDimension() {
253 var hasWidth = $(this._element).hasClass(Dimension.WIDTH);
254 return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT;
255 };
256
257 Collapse.prototype._getParent = function _getParent() {
258 var _this3 = this;
259
260 var parent = $(this._config.parent)[0];
261 var selector = '[data-toggle="collapse"][data-parent="' + this._config.parent + '"]';
262
263 $(parent).find(selector).each(function (i, element) {
264 _this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
265 });
266
267 return parent;
268 };
269
270 Collapse.prototype._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
271 if (element) {
272 var isOpen = $(element).hasClass(ClassName.SHOW);
273 element.setAttribute('aria-expanded', isOpen);
274
275 if (triggerArray.length) {
276 $(triggerArray).toggleClass(ClassName.COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
277 }
278 }
279 };
280
281 // static
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 var _config = $.extend({}, Default, $this.data(), (typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object' && config);
293
294 if (!data && _config.toggle && /show|hide/.test(config)) {
295 _config.toggle = false;
296 }
297
298 if (!data) {
299 data = new Collapse(this, _config);
300 $this.data(DATA_KEY, data);
301 }
302
303 if (typeof config === 'string') {
304 if (data[config] === undefined) {
305 throw new Error('No method named "' + config + '"');
306 }
307 data[config]();
308 }
309 });
310 };
311
312 _createClass(Collapse, null, [{
313 key: 'VERSION',
314 get: function get() {
315 return VERSION;
316 }
317 }, {
318 key: 'Default',
319 get: function get() {
320 return Default;
321 }
322 }]);
323
324 return Collapse;
325 }();
326
327 /**
328 * ------------------------------------------------------------------------
329 * Data Api implementation
330 * ------------------------------------------------------------------------
331 */
332
333 $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
334 event.preventDefault();
335
336 var target = Collapse._getTargetFromElement(this);
337 var data = $(target).data(DATA_KEY);
338 var config = data ? 'toggle' : $(this).data();
339
340 Collapse._jQueryInterface.call($(target), config);
341 });
342
343 /**
344 * ------------------------------------------------------------------------
345 * jQuery
346 * ------------------------------------------------------------------------
347 */
348
349 $.fn[NAME] = Collapse._jQueryInterface;
350 $.fn[NAME].Constructor = Collapse;
351 $.fn[NAME].noConflict = function () {
352 $.fn[NAME] = JQUERY_NO_CONFLICT;
353 return Collapse._jQueryInterface;
354 };
355
356 return Collapse;
357}(jQuery);
358//# sourceMappingURL=collapse.js.map