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

258 lines
7.4 KiB
JavaScript
Raw Normal View History

2016-10-10 02:26:51 +02:00
var _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; }; }();
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2015-05-11 21:29:06 +02:00
/**
* --------------------------------------------------------------------------
2016-10-19 17:27:41 +02:00
* Bootstrap (v4.0.0-alpha.5): tab.js
2015-05-11 21:29:06 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2016-10-10 02:26:51 +02:00
var Tab = function ($) {
2015-05-11 21:29:06 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'tab';
2016-10-19 17:27:41 +02:00
var VERSION = '4.0.0-alpha.5';
2015-05-11 21:29:06 +02:00
var DATA_KEY = 'bs.tab';
2015-05-13 21:48:34 +02:00
var EVENT_KEY = '.' + DATA_KEY;
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 = {
2015-05-13 21:48:34 +02:00
HIDE: 'hide' + EVENT_KEY,
HIDDEN: 'hidden' + EVENT_KEY,
SHOW: 'show' + EVENT_KEY,
SHOWN: 'shown' + EVENT_KEY,
2015-08-13 06:12:03 +02:00
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',
FADE: 'fade',
2016-12-20 06:48:24 +01:00
SHOW: 'show'
2015-05-11 21:29:06 +02:00
};
var Selector = {
A: 'a',
LI: 'li',
2015-08-19 07:03:34 +02:00
DROPDOWN: '.dropdown',
2016-10-27 18:51:22 +02:00
LIST: 'ul:not(.dropdown-menu), ol:not(.dropdown-menu)',
2015-08-19 07:03:34 +02:00
FADE_CHILD: '> .nav-item .fade, > .fade',
2015-05-11 21:29:06 +02:00
ACTIVE: '.active',
2015-08-19 07:03:34 +02:00
ACTIVE_CHILD: '> .nav-item > .active, > .active',
2015-05-11 21:29:06 +02:00
DATA_TOGGLE: '[data-toggle="tab"], [data-toggle="pill"]',
2015-08-19 07:03:34 +02:00
DROPDOWN_TOGGLE: '.dropdown-toggle',
DROPDOWN_ACTIVE_CHILD: '> .dropdown-menu .active'
2015-05-11 21:29:06 +02:00
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2016-10-10 02:26:51 +02:00
var Tab = function () {
2015-05-11 21:29:06 +02:00
function Tab(element) {
_classCallCheck(this, Tab);
this._element = element;
}
2015-08-13 06:12:03 +02:00
// getters
2016-10-10 02:26:51 +02:00
// public
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
Tab.prototype.show = function show() {
var _this = this;
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && $(this._element).hasClass(ClassName.ACTIVE)) {
return;
}
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
var target = void 0;
var previous = void 0;
2016-10-27 18:51:22 +02:00
var listElement = $(this._element).closest(Selector.LIST)[0];
2016-10-10 02:26:51 +02:00
var selector = Util.getSelectorFromElement(this._element);
2015-05-11 21:29:06 +02:00
2016-10-27 18:51:22 +02:00
if (listElement) {
previous = $.makeArray($(listElement).find(Selector.ACTIVE));
2016-10-10 02:26:51 +02:00
previous = previous[previous.length - 1];
}
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
var hideEvent = $.Event(Event.HIDE, {
relatedTarget: this._element
});
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
var showEvent = $.Event(Event.SHOW, {
relatedTarget: previous
});
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (previous) {
$(previous).trigger(hideEvent);
}
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
$(this._element).trigger(showEvent);
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) {
return;
}
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (selector) {
target = $(selector)[0];
}
2015-05-11 21:29:06 +02:00
2016-10-27 18:51:22 +02:00
this._activate(this._element, listElement);
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
var complete = function complete() {
var hiddenEvent = $.Event(Event.HIDDEN, {
relatedTarget: _this._element
});
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
var shownEvent = $.Event(Event.SHOWN, {
relatedTarget: previous
});
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
$(previous).trigger(hiddenEvent);
$(_this._element).trigger(shownEvent);
};
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (target) {
this._activate(target, target.parentNode, complete);
} else {
complete();
2015-05-13 21:48:34 +02:00
}
2016-10-10 02:26:51 +02:00
};
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
Tab.prototype.dispose = function dispose() {
$.removeClass(this._element, DATA_KEY);
this._element = null;
};
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
// private
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
Tab.prototype._activate = function _activate(element, container, callback) {
2016-11-01 05:14:23 +01:00
var _this2 = this;
2016-10-10 02:26:51 +02:00
var active = $(container).find(Selector.ACTIVE_CHILD)[0];
var isTransitioning = callback && Util.supportsTransitionEnd() && (active && $(active).hasClass(ClassName.FADE) || Boolean($(container).find(Selector.FADE_CHILD)[0]));
2015-05-11 21:29:06 +02:00
2016-11-01 05:14:23 +01:00
var complete = function complete() {
return _this2._transitionComplete(element, active, isTransitioning, callback);
};
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (active && isTransitioning) {
$(active).one(Util.TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
} else {
complete();
2015-05-11 21:29:06 +02:00
}
2016-10-10 02:26:51 +02:00
if (active) {
2016-12-20 06:48:24 +01:00
$(active).removeClass(ClassName.SHOW);
2016-10-10 02:26:51 +02:00
}
};
2015-08-19 07:03:34 +02:00
2016-10-10 02:26:51 +02:00
Tab.prototype._transitionComplete = function _transitionComplete(element, active, isTransitioning, callback) {
if (active) {
$(active).removeClass(ClassName.ACTIVE);
2016-11-26 00:00:23 +01:00
var dropdownChild = $(active.parentNode).find(Selector.DROPDOWN_ACTIVE_CHILD)[0];
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (dropdownChild) {
$(dropdownChild).removeClass(ClassName.ACTIVE);
2015-05-11 21:29:06 +02:00
}
2016-10-10 02:26:51 +02:00
active.setAttribute('aria-expanded', false);
}
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
$(element).addClass(ClassName.ACTIVE);
element.setAttribute('aria-expanded', true);
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (isTransitioning) {
Util.reflow(element);
2016-12-20 06:48:24 +01:00
$(element).addClass(ClassName.SHOW);
2016-10-10 02:26:51 +02:00
} else {
$(element).removeClass(ClassName.FADE);
}
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (element.parentNode && $(element.parentNode).hasClass(ClassName.DROPDOWN_MENU)) {
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
var dropdownElement = $(element).closest(Selector.DROPDOWN)[0];
if (dropdownElement) {
$(dropdownElement).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE);
2015-05-11 21:29:06 +02:00
}
2016-10-10 02:26:51 +02:00
element.setAttribute('aria-expanded', true);
2015-05-11 21:29:06 +02:00
}
2016-10-10 02:26:51 +02:00
if (callback) {
callback();
}
};
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
// static
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
Tab._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var $this = $(this);
var data = $this.data(DATA_KEY);
2015-05-11 21:29:06 +02:00
2016-10-10 02:26:51 +02:00
if (!data) {
2016-11-26 00:00:23 +01:00
data = new Tab(this);
2016-10-10 02:26:51 +02:00
$this.data(DATA_KEY, data);
}
if (typeof config === 'string') {
if (data[config] === undefined) {
throw new Error('No method named "' + config + '"');
2015-05-11 21:29:06 +02:00
}
2016-10-10 02:26:51 +02:00
data[config]();
}
});
};
_createClass(Tab, null, [{
2015-08-13 06:12:03 +02:00
key: 'VERSION',
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
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();
Tab._jQueryInterface.call($(this), 'show');
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Tab._jQueryInterface;
$.fn[NAME].Constructor = Tab;
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Tab._jQueryInterface;
};
return Tab;
2016-10-10 02:26:51 +02:00
}(jQuery);
2015-08-13 06:12:03 +02:00
//# sourceMappingURL=tab.js.map