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

182 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-09-06 06:05:12 +02:00
'use strict';
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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2015-05-08 02:07:38 +02:00
/**
* --------------------------------------------------------------------------
2017-08-11 05:56:35 +02:00
* Bootstrap (v4.0.0-beta): button.js
2015-05-08 02:07:38 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2017-09-06 06:05:12 +02:00
var Button = function () {
2015-05-08 02:07:38 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'button';
2017-08-11 05:56:35 +02:00
var VERSION = '4.0.0-beta';
2015-05-08 02:07:38 +02:00
var DATA_KEY = 'bs.button';
2015-05-13 21:48:34 +02:00
var EVENT_KEY = '.' + DATA_KEY;
var DATA_API_KEY = '.data-api';
2015-05-08 02:07:38 +02:00
var JQUERY_NO_CONFLICT = $.fn[NAME];
var ClassName = {
ACTIVE: 'active',
BUTTON: 'btn',
FOCUS: 'focus'
};
var Selector = {
DATA_TOGGLE_CARROT: '[data-toggle^="button"]',
DATA_TOGGLE: '[data-toggle="buttons"]',
INPUT: 'input',
ACTIVE: '.active',
BUTTON: '.btn'
};
var Event = {
2015-08-13 06:12:03 +02:00
CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY,
FOCUS_BLUR_DATA_API: 'focus' + EVENT_KEY + DATA_API_KEY + ' ' + ('blur' + EVENT_KEY + DATA_API_KEY)
2017-09-10 01:59:37 +02:00
};
2015-05-08 02:07:38 +02:00
2017-09-10 01:59:37 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
var Button = function () {
2015-05-08 02:07:38 +02:00
function Button(element) {
_classCallCheck(this, Button);
2015-05-08 07:26:40 +02:00
this._element = element;
2015-05-08 02:07:38 +02:00
}
2015-08-13 06:12:03 +02:00
// getters
2017-09-06 06:05:12 +02:00
_createClass(Button, [{
key: 'toggle',
// public
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
value: function toggle() {
var triggerChangeEvent = true;
var addAriaPressed = true;
var rootElement = $(this._element).closest(Selector.DATA_TOGGLE)[0];
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
if (rootElement) {
var input = $(this._element).find(Selector.INPUT)[0];
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
if (input) {
if (input.type === 'radio') {
if (input.checked && $(this._element).hasClass(ClassName.ACTIVE)) {
triggerChangeEvent = false;
} else {
var activeElement = $(rootElement).find(Selector.ACTIVE)[0];
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
if (activeElement) {
$(activeElement).removeClass(ClassName.ACTIVE);
}
2015-05-08 02:07:38 +02:00
}
}
2017-09-06 06:05:12 +02:00
if (triggerChangeEvent) {
if (input.hasAttribute('disabled') || rootElement.hasAttribute('disabled') || input.classList.contains('disabled') || rootElement.classList.contains('disabled')) {
return;
}
input.checked = !$(this._element).hasClass(ClassName.ACTIVE);
$(input).trigger('change');
2017-05-16 09:59:44 +02:00
}
2017-09-06 06:05:12 +02:00
input.focus();
addAriaPressed = false;
2015-05-08 02:07:38 +02:00
}
2017-09-06 06:05:12 +02:00
}
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
if (addAriaPressed) {
this._element.setAttribute('aria-pressed', !$(this._element).hasClass(ClassName.ACTIVE));
2015-05-08 02:07:38 +02:00
}
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
if (triggerChangeEvent) {
$(this._element).toggleClass(ClassName.ACTIVE);
}
2017-04-16 22:54:07 +02:00
}
2017-09-06 06:05:12 +02:00
}, {
key: 'dispose',
value: function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
2015-05-13 21:48:34 +02:00
}
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
// static
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
}], [{
key: '_jQueryInterface',
value: function _jQueryInterface(config) {
return this.each(function () {
var data = $(this).data(DATA_KEY);
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
if (!data) {
data = new Button(this);
$(this).data(DATA_KEY, data);
}
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
if (config === 'toggle') {
data[config]();
}
});
}
}, {
2015-08-13 06:12:03 +02:00
key: 'VERSION',
get: function get() {
return VERSION;
}
2015-05-08 02:07:38 +02:00
}]);
return Button;
2016-10-10 02:26:51 +02:00
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
2015-05-13 21:48:34 +02:00
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
2015-05-08 02:07:38 +02:00
event.preventDefault();
var button = event.target;
if (!$(button).hasClass(ClassName.BUTTON)) {
button = $(button).closest(Selector.BUTTON);
}
Button._jQueryInterface.call($(button), 'toggle');
2015-05-13 21:48:34 +02:00
}).on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
2015-05-08 02:07:38 +02:00
var button = $(event.target).closest(Selector.BUTTON)[0];
$(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type));
2017-07-02 19:40:27 +02:00
});
2015-05-08 02:07:38 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2017-07-02 19:40:27 +02:00
$.fn[NAME] = Button._jQueryInterface;
2015-05-08 02:07:38 +02:00
$.fn[NAME].Constructor = Button;
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Button._jQueryInterface;
};
return Button;
2016-10-10 02:26:51 +02:00
}(jQuery);
2017-04-22 08:58:09 +02:00
//# sourceMappingURL=button.js.map