0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00
Bootstrap/js/dist/tab.js

259 lines
7.5 KiB
JavaScript
Raw Normal View History

2017-09-30 23:28:03 +02: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); } }
2017-09-06 06:05:12 +02:00
2017-09-30 23:28:03 +02:00
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
2015-05-11 21:29:06 +02:00
/**
* --------------------------------------------------------------------------
2017-10-19 18:25:55 +02:00
* Bootstrap (vv4.0.0-beta.2): tab.js
2015-05-11 21:29:06 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2017-09-06 06:05:12 +02:00
var Tab = function () {
2015-05-11 21:29:06 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'tab';
2017-10-19 18:25:55 +02:00
var VERSION = 'v4.0.0-beta.2';
2015-05-11 21:29:06 +02:00
var DATA_KEY = 'bs.tab';
2017-09-30 23:28:03 +02:00
var EVENT_KEY = "." + DATA_KEY;
2015-05-13 21:48:34 +02:00
var DATA_API_KEY = '.data-api';
2015-05-11 21:29:06 +02:00
var JQUERY_NO_CONFLICT = $.fn[NAME];
var TRANSITION_DURATION = 150;
var Event = {
2017-09-30 23:28:03 +02: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
2015-05-11 21:29:06 +02:00
};
var ClassName = {
DROPDOWN_MENU: 'dropdown-menu',
ACTIVE: 'active',
2016-12-26 00:47:03 +01:00
DISABLED: 'disabled',
2015-05-11 21:29:06 +02:00
FADE: 'fade',
2016-12-20 06:48:24 +01:00
SHOW: 'show'
2015-05-11 21:29:06 +02:00
};
var Selector = {
2015-08-19 07:03:34 +02:00
DROPDOWN: '.dropdown',
2017-04-08 22:22:53 +02:00
NAV_LIST_GROUP: '.nav, .list-group',
2015-05-11 21:29:06 +02:00
ACTIVE: '.active',
2017-08-13 21:59:27 +02:00
ACTIVE_UL: '> li > .active',
2017-04-08 22:22:53 +02:00
DATA_TOGGLE: '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]',
2015-08-19 07:03:34 +02:00
DROPDOWN_TOGGLE: '.dropdown-toggle',
DROPDOWN_ACTIVE_CHILD: '> .dropdown-menu .active'
2017-09-13 07:24:15 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-11 21:29:06 +02:00
2017-09-13 07:24:15 +02:00
};
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
var Tab =
/*#__PURE__*/
function () {
function Tab(element) {
2015-05-11 21:29:06 +02:00
this._element = element;
2017-09-30 23:28:03 +02:00
} // getters
2015-05-11 21:29:06 +02:00
2015-08-13 06:12:03 +02:00
2017-09-30 23:28:03 +02:00
var _proto = Tab.prototype;
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
// public
_proto.show = function show() {
var _this = this;
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && $(this._element).hasClass(ClassName.ACTIVE) || $(this._element).hasClass(ClassName.DISABLED)) {
return;
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
var target;
var previous;
var listElement = $(this._element).closest(Selector.NAV_LIST_GROUP)[0];
var selector = Util.getSelectorFromElement(this._element);
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (listElement) {
var itemSelector = listElement.nodeName === 'UL' ? Selector.ACTIVE_UL : Selector.ACTIVE;
previous = $.makeArray($(listElement).find(itemSelector));
previous = previous[previous.length - 1];
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
var hideEvent = $.Event(Event.HIDE, {
relatedTarget: this._element
});
var showEvent = $.Event(Event.SHOW, {
relatedTarget: previous
});
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (previous) {
$(previous).trigger(hideEvent);
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
$(this._element).trigger(showEvent);
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) {
return;
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (selector) {
target = $(selector)[0];
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
this._activate(this._element, listElement);
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
var complete = function complete() {
var hiddenEvent = $.Event(Event.HIDDEN, {
relatedTarget: _this._element
});
var shownEvent = $.Event(Event.SHOWN, {
relatedTarget: previous
});
$(previous).trigger(hiddenEvent);
$(_this._element).trigger(shownEvent);
};
if (target) {
this._activate(target, target.parentNode, complete);
} else {
complete();
}
};
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
_proto.dispose = function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
}; // private
2016-11-01 05:14:23 +01:00
2017-08-13 21:59:27 +02:00
2017-09-30 23:28:03 +02:00
_proto._activate = function _activate(element, container, callback) {
var _this2 = this;
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
var activeElements;
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (container.nodeName === 'UL') {
activeElements = $(container).find(Selector.ACTIVE_UL);
} else {
activeElements = $(container).children(Selector.ACTIVE);
2016-10-10 02:26:51 +02:00
}
2015-08-19 07:03:34 +02:00
2017-09-30 23:28:03 +02:00
var active = activeElements[0];
var isTransitioning = callback && Util.supportsTransitionEnd() && active && $(active).hasClass(ClassName.FADE);
2016-10-10 02:26:51 +02:00
2017-09-30 23:28:03 +02:00
var complete = function complete() {
return _this2._transitionComplete(element, active, isTransitioning, callback);
};
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (active && isTransitioning) {
$(active).one(Util.TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
} else {
complete();
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (active) {
$(active).removeClass(ClassName.SHOW);
}
};
2017-09-06 06:05:12 +02:00
2017-09-30 23:28:03 +02:00
_proto._transitionComplete = function _transitionComplete(element, active, isTransitioning, callback) {
if (active) {
$(active).removeClass(ClassName.ACTIVE);
var dropdownChild = $(active.parentNode).find(Selector.DROPDOWN_ACTIVE_CHILD)[0];
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (dropdownChild) {
$(dropdownChild).removeClass(ClassName.ACTIVE);
2017-09-06 06:05:12 +02:00
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (active.getAttribute('role') === 'tab') {
active.setAttribute('aria-selected', false);
2017-09-06 06:05:12 +02:00
}
2016-10-10 02:26:51 +02:00
}
2017-09-06 06:05:12 +02:00
2017-09-30 23:28:03 +02:00
$(element).addClass(ClassName.ACTIVE);
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (element.getAttribute('role') === 'tab') {
element.setAttribute('aria-selected', true);
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (isTransitioning) {
Util.reflow(element);
$(element).addClass(ClassName.SHOW);
} else {
$(element).removeClass(ClassName.FADE);
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (element.parentNode && $(element.parentNode).hasClass(ClassName.DROPDOWN_MENU)) {
var dropdownElement = $(element).closest(Selector.DROPDOWN)[0];
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
if (dropdownElement) {
$(dropdownElement).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE);
2017-09-06 06:05:12 +02:00
}
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
element.setAttribute('aria-expanded', true);
}
2016-10-10 02:26:51 +02:00
2017-09-30 23:28:03 +02:00
if (callback) {
callback();
2017-09-06 06:05:12 +02:00
}
2017-09-30 23:28:03 +02:00
}; // static
2017-09-06 06:05:12 +02:00
2016-10-10 02:26:51 +02:00
2017-09-30 23:28:03 +02:00
Tab._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var $this = $(this);
var data = $this.data(DATA_KEY);
2017-09-06 06:05:12 +02:00
2017-09-30 23:28:03 +02:00
if (!data) {
data = new Tab(this);
$this.data(DATA_KEY, data);
}
2017-09-06 06:05:12 +02:00
2017-09-30 23:28:03 +02:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new Error("No method named \"" + config + "\"");
2017-09-06 06:05:12 +02:00
}
2017-09-30 23:28:03 +02:00
data[config]();
}
});
};
_createClass(Tab, null, [{
key: "VERSION",
2015-08-13 06:12:03 +02:00
get: function get() {
return VERSION;
}
2015-05-11 21:29:06 +02:00
}]);
return Tab;
2016-10-10 02:26:51 +02:00
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
2015-05-13 21:48:34 +02:00
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
2015-05-11 21:29:06 +02:00
event.preventDefault();
2017-09-30 23:28:03 +02:00
2015-05-11 21:29:06 +02:00
Tab._jQueryInterface.call($(this), 'show');
2017-07-02 19:40:27 +02:00
});
2015-05-11 21:29:06 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2017-07-02 19:40:27 +02:00
$.fn[NAME] = Tab._jQueryInterface;
2015-05-11 21:29:06 +02:00
$.fn[NAME].Constructor = Tab;
2017-09-30 23:28:03 +02:00
2015-05-11 21:29:06 +02:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Tab._jQueryInterface;
};
return Tab;
2017-10-16 00:51:44 +02:00
}($);
2017-04-22 08:58:09 +02:00
//# sourceMappingURL=tab.js.map