1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/button.js

234 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-11-13 01:41:12 -05:00
/*!
2019-02-13 11:01:40 -05:00
* Bootstrap button.js v4.3.1 (https://getbootstrap.com/)
2019-01-04 11:29:45 -05:00
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2018-11-13 01:41:12 -05:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
2018-07-23 20:51:14 -04: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 11:31:34 -05:00
(global = global || self, global.Button = factory(global.Data, global.EventHandler, global.SelectorEngine));
}(this, function (Data, EventHandler, SelectorEngine) { 'use strict';
2018-07-23 20:51:14 -04:00
2019-03-01 11:31:34 -05: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-23 20:51:14 -04: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 00:05:12 -04:00
2018-07-23 20:51:14 -04:00
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
2016-10-09 20:26:51 -04:00
2019-03-01 11:31:34 -05:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2019-08-27 09:03:21 -04:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
return jQuery;
}
return null;
};
2019-03-01 11:31:34 -05:00
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'button';
2019-02-13 11:01:40 -05:00
var VERSION = '4.3.1';
2018-11-13 01:41:12 -05:00
var DATA_KEY = 'bs.button';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
var ClassName = {
ACTIVE: 'active',
BUTTON: 'btn',
FOCUS: 'focus'
};
var Selector = {
DATA_TOGGLE_CARROT: '[data-toggle^="button"]',
DATA_TOGGLE: '[data-toggle="buttons"]',
2018-12-15 18:13:22 -05:00
INPUT: 'input:not([type="hidden"])',
2018-11-13 01:41:12 -05:00
ACTIVE: '.active',
BUTTON: '.btn'
};
var Event = {
CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY,
2019-03-01 11:31:34 -05:00
FOCUS_DATA_API: "focus" + EVENT_KEY + DATA_API_KEY,
BLUR_DATA_API: "blur" + EVENT_KEY + DATA_API_KEY
2017-09-13 01:24:15 -04:00
/**
* ------------------------------------------------------------------------
2018-11-13 01:41:12 -05:00
* Class Definition
2017-09-13 01:24:15 -04:00
* ------------------------------------------------------------------------
*/
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
};
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
var Button =
/*#__PURE__*/
function () {
function Button(element) {
this._element = element;
2019-03-01 11:31:34 -05:00
Data.setData(element, DATA_KEY, this);
2018-11-13 01:41:12 -05:00
} // Getters
2015-08-13 00:12:03 -04:00
2017-09-06 00:05:12 -04:00
2018-11-13 01:41:12 -05:00
var _proto = Button.prototype;
2017-09-06 00:05:12 -04:00
2018-11-13 01:41:12 -05:00
// Public
_proto.toggle = function toggle() {
var triggerChangeEvent = true;
var addAriaPressed = true;
2019-03-01 11:31:34 -05:00
var rootElement = SelectorEngine.closest(this._element, Selector.DATA_TOGGLE);
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
if (rootElement) {
2019-03-01 11:31:34 -05:00
var input = SelectorEngine.findOne(Selector.INPUT, this._element);
2015-05-07 20:07:38 -04:00
2019-08-27 09:03:21 -04:00
if (input && input.type === 'radio') {
if (input.checked && this._element.classList.contains(ClassName.ACTIVE)) {
triggerChangeEvent = false;
} else {
var activeElement = SelectorEngine.findOne(Selector.ACTIVE, rootElement);
2015-05-07 20:07:38 -04:00
2019-08-27 09:03:21 -04:00
if (activeElement) {
activeElement.classList.remove(ClassName.ACTIVE);
2015-05-07 20:07:38 -04:00
}
2018-11-13 01:41:12 -05:00
}
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
if (triggerChangeEvent) {
if (input.hasAttribute('disabled') || rootElement.hasAttribute('disabled') || input.classList.contains('disabled') || rootElement.classList.contains('disabled')) {
return;
2017-05-16 03:59:44 -04:00
}
2017-09-06 00:05:12 -04:00
2018-11-13 01:41:12 -05:00
input.checked = !this._element.classList.contains(ClassName.ACTIVE);
2019-03-01 11:31:34 -05:00
EventHandler.trigger(input, 'change');
2015-05-07 20:07:38 -04:00
}
2018-11-13 01:41:12 -05:00
input.focus();
addAriaPressed = false;
2015-05-07 20:07:38 -04:00
}
2018-11-13 01:41:12 -05:00
}
2016-10-09 20:26:51 -04:00
2018-11-13 01:41:12 -05:00
if (addAriaPressed) {
this._element.setAttribute('aria-pressed', !this._element.classList.contains(ClassName.ACTIVE));
}
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (triggerChangeEvent) {
2019-03-01 11:31:34 -05:00
this._element.classList.toggle(ClassName.ACTIVE);
2018-11-13 01:41:12 -05:00
}
};
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
_proto.dispose = function dispose() {
2019-03-01 11:31:34 -05:00
Data.removeData(this._element, DATA_KEY);
2018-11-13 01:41:12 -05:00
this._element = null;
2019-01-04 11:29:45 -05:00
} // Static
;
2016-10-09 20:26:51 -04:00
2019-08-27 09:03:21 -04:00
Button.jQueryInterface = function jQueryInterface(config) {
2018-11-13 01:41:12 -05:00
return this.each(function () {
2019-03-01 11:31:34 -05:00
var data = Data.getData(this, DATA_KEY);
2016-10-09 20:26:51 -04:00
2018-11-13 01:41:12 -05:00
if (!data) {
data = new Button(this);
}
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (config === 'toggle') {
data[config]();
2017-09-30 17:28:03 -04:00
}
2018-11-13 01:41:12 -05:00
});
};
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
Button.getInstance = function getInstance(element) {
2019-03-01 11:31:34 -05:00
return Data.getData(element, DATA_KEY);
};
2018-11-13 01:41:12 -05:00
_createClass(Button, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}]);
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
return Button;
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 20:07:38 -04:00
2017-09-30 17:28:03 -04:00
2019-03-01 11:31:34 -05:00
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
2018-11-13 01:41:12 -05:00
event.preventDefault();
var button = event.target;
2015-05-07 20:07:38 -04:00
2019-03-01 11:31:34 -05:00
if (!button.classList.contains(ClassName.BUTTON)) {
button = SelectorEngine.closest(button, Selector.BUTTON);
2018-11-13 01:41:12 -05:00
}
2015-05-07 20:07:38 -04:00
2019-03-01 11:31:34 -05:00
var data = Data.getData(button, DATA_KEY);
if (!data) {
data = new Button(button);
}
data.toggle();
});
EventHandler.on(document, Event.FOCUS_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
var button = SelectorEngine.closest(event.target, Selector.BUTTON);
2019-04-18 07:47:52 -04:00
if (button) {
button.classList.add(ClassName.FOCUS);
}
2019-03-01 11:31:34 -05:00
});
EventHandler.on(document, Event.BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
var button = SelectorEngine.closest(event.target, Selector.BUTTON);
2019-04-18 07:47:52 -04:00
if (button) {
button.classList.remove(ClassName.FOCUS);
}
2018-11-13 01:41:12 -05:00
});
2019-08-27 09:03:21 -04:00
var $ = getjQuery();
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2019-03-01 11:31:34 -05:00
* add .button to jQuery only if jQuery is present
2018-11-13 01:41:12 -05:00
*/
2015-05-07 20:07:38 -04:00
/* istanbul ignore if */
2019-08-27 09:03:21 -04:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[NAME];
$.fn[NAME] = Button.jQueryInterface;
$.fn[NAME].Constructor = Button;
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Button.jQueryInterface;
2019-03-01 11:31:34 -05:00
};
}
2015-05-07 20:07:38 -04:00
return Button;
2018-07-23 20:51:14 -04:00
2019-01-04 11:29:45 -05:00
}));
2018-07-23 20:51:14 -04:00
//# sourceMappingURL=button.js.map