0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/js/dist/button.js

214 lines
7.4 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
2020-08-06 18:22:43 +02:00
* Bootstrap button.js v4.5.2 (https://getbootstrap.com/)
2020-05-12 18:53:07 +02:00
* Copyright 2011-2020 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-08-04 18:24:33 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 07:41:12 +01:00
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
typeof define === 'function' && define.amd ? define(['jquery'], factory) :
2020-08-04 18:24:33 +02:00
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Button = factory(global.jQuery));
2019-11-26 18:12:00 +01:00
}(this, (function ($) { 'use strict';
2018-07-24 02:51:14 +02:00
2020-05-12 18:53:07 +02:00
$ = $ && Object.prototype.hasOwnProperty.call($, 'default') ? $['default'] : $;
2018-07-24 02:51:14 +02:00
2020-08-04 18:24:33 +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); } }
2016-10-10 02:26:51 +02:00
2020-08-04 18:24:33 +02:00
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'button';
2020-08-06 18:22:43 +02:00
var VERSION = '4.5.2';
2018-11-13 07:41:12 +01:00
var DATA_KEY = 'bs.button';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
var JQUERY_NO_CONFLICT = $.fn[NAME];
2020-05-12 18:53:07 +02:00
var CLASS_NAME_ACTIVE = 'active';
var CLASS_NAME_BUTTON = 'btn';
var CLASS_NAME_FOCUS = 'focus';
var SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]';
var SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]';
var SELECTOR_DATA_TOGGLE = '[data-toggle="button"]';
var SELECTOR_DATA_TOGGLES_BUTTONS = '[data-toggle="buttons"] .btn';
var SELECTOR_INPUT = 'input:not([type="hidden"])';
var SELECTOR_ACTIVE = '.active';
var SELECTOR_BUTTON = '.btn';
var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
var EVENT_FOCUS_BLUR_DATA_API = "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY);
var EVENT_LOAD_DATA_API = "load" + EVENT_KEY + DATA_API_KEY;
2019-11-26 18:12:00 +01:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
2020-05-12 18:53:07 +02:00
var Button = /*#__PURE__*/function () {
2018-11-13 07:41:12 +01:00
function Button(element) {
this._element = element;
} // Getters
2015-08-13 06:12:03 +02:00
2017-09-06 06:05:12 +02:00
2018-11-13 07:41:12 +01:00
var _proto = Button.prototype;
2017-09-06 06:05:12 +02:00
2018-11-13 07:41:12 +01:00
// Public
_proto.toggle = function toggle() {
var triggerChangeEvent = true;
var addAriaPressed = true;
2020-05-12 18:53:07 +02:00
var rootElement = $(this._element).closest(SELECTOR_DATA_TOGGLES)[0];
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
if (rootElement) {
2020-05-12 18:53:07 +02:00
var input = this._element.querySelector(SELECTOR_INPUT);
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
if (input) {
if (input.type === 'radio') {
2020-05-12 18:53:07 +02:00
if (input.checked && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
2018-11-13 07:41:12 +01:00
triggerChangeEvent = false;
} else {
2020-05-12 18:53:07 +02:00
var activeElement = rootElement.querySelector(SELECTOR_ACTIVE);
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
if (activeElement) {
2020-05-12 18:53:07 +02:00
$(activeElement).removeClass(CLASS_NAME_ACTIVE);
2015-05-08 02:07:38 +02:00
}
}
2018-11-13 07:41:12 +01:00
}
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
if (triggerChangeEvent) {
2020-05-12 18:53:07 +02:00
// if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
if (input.type === 'checkbox' || input.type === 'radio') {
input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE);
}
2018-11-13 07:41:12 +01:00
$(input).trigger('change');
2015-05-08 02:07:38 +02:00
}
2018-11-13 07:41:12 +01:00
input.focus();
addAriaPressed = false;
2015-05-08 02:07:38 +02:00
}
2018-11-13 07:41:12 +01:00
}
2016-10-10 02:26:51 +02:00
2019-11-26 18:12:00 +01:00
if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
if (addAriaPressed) {
2020-05-12 18:53:07 +02:00
this._element.setAttribute('aria-pressed', !this._element.classList.contains(CLASS_NAME_ACTIVE));
2019-11-26 18:12:00 +01:00
}
2017-09-30 23:28:03 +02:00
2019-11-26 18:12:00 +01:00
if (triggerChangeEvent) {
2020-05-12 18:53:07 +02:00
$(this._element).toggleClass(CLASS_NAME_ACTIVE);
2019-11-26 18:12:00 +01:00
}
2018-11-13 07:41:12 +01:00
}
};
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
_proto.dispose = function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
2019-01-04 17:29:45 +01:00
} // Static
;
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
Button._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var data = $(this).data(DATA_KEY);
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
if (!data) {
data = new Button(this);
$(this).data(DATA_KEY, data);
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (config === 'toggle') {
data[config]();
2017-09-30 23:28:03 +02:00
}
2018-11-13 07:41:12 +01:00
});
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_createClass(Button, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}]);
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
return Button;
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
2017-09-30 23:28:03 +02:00
2020-05-12 18:53:07 +02:00
$(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
2018-11-13 07:41:12 +01:00
var button = event.target;
2020-05-12 18:53:07 +02:00
var initialButton = button;
2015-05-08 02:07:38 +02:00
2020-05-12 18:53:07 +02:00
if (!$(button).hasClass(CLASS_NAME_BUTTON)) {
button = $(button).closest(SELECTOR_BUTTON)[0];
2018-11-13 07:41:12 +01:00
}
2015-05-08 02:07:38 +02:00
2019-11-26 18:12:00 +01:00
if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
event.preventDefault(); // work around Firefox bug #1540995
} else {
2020-05-12 18:53:07 +02:00
var inputBtn = button.querySelector(SELECTOR_INPUT);
2019-11-26 18:12:00 +01:00
if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
event.preventDefault(); // work around Firefox bug #1540995
return;
}
2020-08-04 18:24:33 +02:00
if (initialButton.tagName !== 'LABEL' || inputBtn && inputBtn.type !== 'checkbox') {
Button._jQueryInterface.call($(button), 'toggle');
2020-05-12 18:53:07 +02:00
}
2019-11-26 18:12:00 +01:00
}
2020-05-12 18:53:07 +02:00
}).on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
var button = $(event.target).closest(SELECTOR_BUTTON)[0];
$(button).toggleClass(CLASS_NAME_FOCUS, /^focus(in)?$/.test(event.type));
2018-11-13 07:41:12 +01:00
});
2020-05-12 18:53:07 +02:00
$(window).on(EVENT_LOAD_DATA_API, function () {
2019-11-26 18:12:00 +01:00
// ensure correct active class is set to match the controls' actual values/states
// find all checkboxes/readio buttons inside data-toggle groups
2020-05-12 18:53:07 +02:00
var buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLES_BUTTONS));
2019-11-26 18:12:00 +01:00
for (var i = 0, len = buttons.length; i < len; i++) {
var button = buttons[i];
2020-05-12 18:53:07 +02:00
var input = button.querySelector(SELECTOR_INPUT);
2019-11-26 18:12:00 +01:00
if (input.checked || input.hasAttribute('checked')) {
2020-05-12 18:53:07 +02:00
button.classList.add(CLASS_NAME_ACTIVE);
2019-11-26 18:12:00 +01:00
} else {
2020-05-12 18:53:07 +02:00
button.classList.remove(CLASS_NAME_ACTIVE);
2019-11-26 18:12:00 +01:00
}
} // find all button toggles
2020-05-12 18:53:07 +02:00
buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
2019-11-26 18:12:00 +01:00
for (var _i = 0, _len = buttons.length; _i < _len; _i++) {
var _button = buttons[_i];
if (_button.getAttribute('aria-pressed') === 'true') {
2020-05-12 18:53:07 +02:00
_button.classList.add(CLASS_NAME_ACTIVE);
2019-11-26 18:12:00 +01:00
} else {
2020-05-12 18:53:07 +02:00
_button.classList.remove(CLASS_NAME_ACTIVE);
2019-11-26 18:12:00 +01:00
}
}
});
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
$.fn[NAME] = Button._jQueryInterface;
$.fn[NAME].Constructor = Button;
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Button._jQueryInterface;
};
2015-05-08 02:07:38 +02:00
return Button;
2018-07-24 02:51:14 +02:00
2019-11-26 18:12:00 +01:00
})));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=button.js.map