twbs--bootstrap/js/dist/tab.js

360 lines
11 KiB
JavaScript
Raw Normal View History

2018-11-13 06:41:12 +00:00
/*!
2019-02-13 16:01:40 +00:00
* Bootstrap tab.js v4.3.1 (https://getbootstrap.com/)
2019-01-04 16:29:45 +00:00
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2018-11-13 06:41:12 +00:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
2018-07-24 00:51:14 +00:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../dom/data.js'), require('../dom/event-handler.js'), require('../dom/selector-engine.js')) :
typeof define === 'function' && define.amd ? define(['../dom/data.js', '../dom/event-handler.js', '../dom/selector-engine.js'], factory) :
2019-03-01 16:31:34 +00:00
(global = global || self, global.Tab = factory(global.Data, global.EventHandler, global.SelectorEngine));
}(this, function (Data, EventHandler, SelectorEngine) { 'use strict';
2018-07-24 00:51:14 +00:00
2019-03-01 16:31:34 +00:00
Data = Data && Data.hasOwnProperty('default') ? Data['default'] : Data;
EventHandler = EventHandler && EventHandler.hasOwnProperty('default') ? EventHandler['default'] : EventHandler;
SelectorEngine = SelectorEngine && SelectorEngine.hasOwnProperty('default') ? SelectorEngine['default'] : SelectorEngine;
2018-07-24 00:51:14 +00:00
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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
2017-09-06 04:05:12 +00:00
2019-03-01 16:31:34 +00:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
var MILLISECONDS_MULTIPLIER = 1000;
var TRANSITION_END = 'transitionend';
2019-03-11 15:13:30 +00:00
var _window = window,
jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 16:31:34 +00:00
var getSelectorFromElement = function getSelectorFromElement(element) {
var selector = element.getAttribute('data-target');
if (!selector || selector === '#') {
var hrefAttr = element.getAttribute('href');
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
}
try {
return document.querySelector(selector) ? selector : null;
2019-03-11 15:13:30 +00:00
} catch (error) {
2019-03-01 16:31:34 +00:00
return null;
}
};
var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
if (!element) {
return 0;
} // Get transition-duration of the element
var _window$getComputedSt = window.getComputedStyle(element),
transitionDuration = _window$getComputedSt.transitionDuration,
transitionDelay = _window$getComputedSt.transitionDelay;
var floatTransitionDuration = parseFloat(transitionDuration);
var floatTransitionDelay = parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
if (!floatTransitionDuration && !floatTransitionDelay) {
return 0;
} // If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0];
transitionDelay = transitionDelay.split(',')[0];
return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
};
var triggerTransitionEnd = function triggerTransitionEnd(element) {
2019-04-18 11:47:52 +00:00
var evt = document.createEvent('HTMLEvents');
evt.initEvent(TRANSITION_END, true, true);
element.dispatchEvent(evt);
2019-03-01 16:31:34 +00:00
};
var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
var called = false;
var durationPadding = 5;
var emulatedDuration = duration + durationPadding;
function listener() {
called = true;
element.removeEventListener(TRANSITION_END, listener);
}
element.addEventListener(TRANSITION_END, listener);
setTimeout(function () {
if (!called) {
triggerTransitionEnd(element);
}
}, emulatedDuration);
};
var makeArray = function makeArray(nodeList) {
if (!nodeList) {
return [];
}
return [].slice.call(nodeList);
};
var reflow = function reflow(element) {
return element.offsetHeight;
};
2018-11-13 06:41:12 +00:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'tab';
2019-02-13 16:01:40 +00:00
var VERSION = '4.3.1';
2018-11-13 06:41:12 +00:00
var DATA_KEY = 'bs.tab';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
2019-04-18 11:47:52 +00:00
var Event = {
2018-11-13 06:41:12 +00:00
HIDE: "hide" + EVENT_KEY,
HIDDEN: "hidden" + EVENT_KEY,
SHOW: "show" + EVENT_KEY,
SHOWN: "shown" + EVENT_KEY,
CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
};
var ClassName = {
DROPDOWN_MENU: 'dropdown-menu',
ACTIVE: 'active',
DISABLED: 'disabled',
FADE: 'fade',
SHOW: 'show'
};
var Selector = {
DROPDOWN: '.dropdown',
NAV_LIST_GROUP: '.nav, .list-group',
ACTIVE: '.active',
2019-03-01 16:31:34 +00:00
ACTIVE_UL: ':scope > li > .active',
2018-11-13 06:41:12 +00:00
DATA_TOGGLE: '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]',
DROPDOWN_TOGGLE: '.dropdown-toggle',
2019-03-01 16:31:34 +00:00
DROPDOWN_ACTIVE_CHILD: ':scope > .dropdown-menu .active'
2017-09-13 05:24:15 +00:00
/**
* ------------------------------------------------------------------------
2018-11-13 06:41:12 +00:00
* Class Definition
2017-09-13 05:24:15 +00:00
* ------------------------------------------------------------------------
*/
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
};
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
var Tab =
/*#__PURE__*/
function () {
function Tab(element) {
this._element = element;
2019-03-01 16:31:34 +00:00
Data.setData(this._element, DATA_KEY, this);
2018-11-13 06:41:12 +00:00
} // Getters
2015-08-13 04:12:03 +00:00
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
var _proto = Tab.prototype;
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
// Public
_proto.show = function show() {
var _this = this;
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && this._element.classList.contains(ClassName.ACTIVE) || this._element.classList.contains(ClassName.DISABLED)) {
2018-11-13 06:41:12 +00:00
return;
}
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
var target;
var previous;
2019-03-01 16:31:34 +00:00
var listElement = SelectorEngine.closest(this._element, Selector.NAV_LIST_GROUP);
var selector = getSelectorFromElement(this._element);
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (listElement) {
2018-11-24 16:22:59 +00:00
var itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE;
2019-03-01 16:31:34 +00:00
previous = makeArray(SelectorEngine.find(itemSelector, listElement));
2018-11-13 06:41:12 +00:00
previous = previous[previous.length - 1];
}
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
var hideEvent = null;
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (previous) {
2019-04-18 11:47:52 +00:00
hideEvent = EventHandler.trigger(previous, Event.HIDE, {
2019-03-01 16:31:34 +00:00
relatedTarget: this._element
});
2018-11-13 06:41:12 +00:00
}
2015-05-11 19:29:06 +00:00
2019-04-18 11:47:52 +00:00
var showEvent = EventHandler.trigger(this._element, Event.SHOW, {
2019-03-01 16:31:34 +00:00
relatedTarget: previous
});
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
if (showEvent.defaultPrevented || hideEvent !== null && hideEvent.defaultPrevented) {
2018-11-13 06:41:12 +00:00
return;
}
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (selector) {
2019-03-01 16:31:34 +00:00
target = SelectorEngine.findOne(selector);
2018-11-13 06:41:12 +00:00
}
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
this._activate(this._element, listElement);
var complete = function complete() {
2019-04-18 11:47:52 +00:00
EventHandler.trigger(previous, Event.HIDDEN, {
2018-11-13 06:41:12 +00:00
relatedTarget: _this._element
});
2019-04-18 11:47:52 +00:00
EventHandler.trigger(_this._element, Event.SHOWN, {
2018-11-13 06:41:12 +00:00
relatedTarget: previous
});
2017-09-30 21:28:03 +00:00
};
2018-11-13 06:41:12 +00:00
if (target) {
this._activate(target, target.parentNode, complete);
} else {
complete();
}
};
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
_proto.dispose = function dispose() {
2019-03-01 16:31:34 +00:00
Data.removeData(this._element, DATA_KEY);
2018-11-13 06:41:12 +00:00
this._element = null;
2019-01-04 16:29:45 +00:00
} // Private
;
2017-08-13 19:59:27 +00:00
2018-11-13 06:41:12 +00:00
_proto._activate = function _activate(element, container, callback) {
var _this2 = this;
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
var activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? SelectorEngine.find(Selector.ACTIVE_UL, container) : SelectorEngine.children(container, Selector.ACTIVE);
2018-11-13 06:41:12 +00:00
var active = activeElements[0];
2019-03-01 16:31:34 +00:00
var isTransitioning = callback && active && active.classList.contains(ClassName.FADE);
2016-10-10 00:26:51 +00:00
2018-11-13 06:41:12 +00:00
var complete = function complete() {
return _this2._transitionComplete(element, active, callback);
2017-09-30 21:28:03 +00:00
};
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (active && isTransitioning) {
2019-03-01 16:31:34 +00:00
var transitionDuration = getTransitionDurationFromElement(active);
active.classList.remove(ClassName.SHOW);
EventHandler.one(active, TRANSITION_END, complete);
emulateTransitionEnd(active, transitionDuration);
2018-11-13 06:41:12 +00:00
} else {
complete();
}
};
2017-09-06 04:05:12 +00:00
2018-11-13 06:41:12 +00:00
_proto._transitionComplete = function _transitionComplete(element, active, callback) {
if (active) {
2019-03-01 16:31:34 +00:00
active.classList.remove(ClassName.ACTIVE);
var dropdownChild = SelectorEngine.findOne(Selector.DROPDOWN_ACTIVE_CHILD, active.parentNode);
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (dropdownChild) {
2019-03-01 16:31:34 +00:00
dropdownChild.classList.remove(ClassName.ACTIVE);
2017-09-06 04:05:12 +00:00
}
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (active.getAttribute('role') === 'tab') {
active.setAttribute('aria-selected', false);
2018-07-24 00:51:14 +00:00
}
2018-11-13 06:41:12 +00:00
}
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
element.classList.add(ClassName.ACTIVE);
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (element.getAttribute('role') === 'tab') {
element.setAttribute('aria-selected', true);
}
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
reflow(element);
if (element.classList.contains(ClassName.FADE)) {
element.classList.add(ClassName.SHOW);
}
2015-05-11 19:29:06 +00:00
2019-03-01 16:31:34 +00:00
if (element.parentNode && element.parentNode.classList.contains(ClassName.DROPDOWN_MENU)) {
var dropdownElement = SelectorEngine.closest(element, Selector.DROPDOWN);
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
if (dropdownElement) {
2019-03-01 16:31:34 +00:00
makeArray(SelectorEngine.find(Selector.DROPDOWN_TOGGLE)).forEach(function (dropdown) {
return dropdown.classList.add(ClassName.ACTIVE);
});
2018-07-24 00:51:14 +00:00
}
2016-10-10 00:26:51 +00:00
2018-11-13 06:41:12 +00:00
element.setAttribute('aria-expanded', true);
}
2017-09-06 04:05:12 +00:00
2018-11-13 06:41:12 +00:00
if (callback) {
callback();
}
2019-01-04 16:29:45 +00:00
} // Static
;
2017-09-06 04:05:12 +00:00
2018-11-13 06:41:12 +00:00
Tab._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
2019-03-01 16:31:34 +00:00
var data = Data.getData(this, DATA_KEY) || new Tab(this);
2017-09-06 04:05:12 +00:00
2018-11-13 06:41:12 +00:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError("No method named \"" + config + "\"");
2017-09-06 04:05:12 +00:00
}
2017-09-30 21:28:03 +00:00
2018-11-13 06:41:12 +00:00
data[config]();
2017-09-30 21:28:03 +00:00
}
2018-11-13 06:41:12 +00:00
});
};
2017-09-30 21:28:03 +00:00
2019-03-01 16:31:34 +00:00
Tab._getInstance = function _getInstance(element) {
return Data.getData(element, DATA_KEY);
};
2018-11-13 06:41:12 +00:00
_createClass(Tab, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}]);
2015-05-11 19:29:06 +00:00
2018-11-13 06:41:12 +00:00
return Tab;
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-11 19:29:06 +00:00
2017-09-30 21:28:03 +00:00
2019-04-18 11:47:52 +00:00
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
2018-11-13 06:41:12 +00:00
event.preventDefault();
2019-03-01 16:31:34 +00:00
var data = Data.getData(this, DATA_KEY) || new Tab(this);
data.show();
2018-11-13 06:41:12 +00:00
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2019-03-01 16:31:34 +00:00
* add .tab to jQuery only if jQuery is present
2018-11-13 06:41:12 +00:00
*/
2015-05-11 19:29:06 +00:00
/* istanbul ignore if */
2019-03-01 16:31:34 +00:00
if (typeof jQuery !== 'undefined') {
var JQUERY_NO_CONFLICT = jQuery.fn[NAME];
jQuery.fn[NAME] = Tab._jQueryInterface;
jQuery.fn[NAME].Constructor = Tab;
2017-09-30 21:28:03 +00:00
2019-03-01 16:31:34 +00:00
jQuery.fn[NAME].noConflict = function () {
jQuery.fn[NAME] = JQUERY_NO_CONFLICT;
return Tab._jQueryInterface;
};
}
2015-05-11 19:29:06 +00:00
return Tab;
2018-07-24 00:51:14 +00:00
2019-01-04 16:29:45 +00:00
}));
2018-07-24 00:51:14 +00:00
//# sourceMappingURL=tab.js.map