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

168 lines
5.0 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; }; }();
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-01-06 17:38:04 +01:00
* Bootstrap (v4.0.0-alpha.6): button.js
2015-05-08 02:07:38 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2016-10-10 02:26:51 +02:00
var Button = function ($) {
2015-05-08 02:07:38 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'button';
2017-01-06 17:38:04 +01:00
var VERSION = '4.0.0-alpha.6';
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)
2015-05-08 02:07:38 +02:00
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
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
2016-10-10 02:26:51 +02:00
// public
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
Button.prototype.toggle = function toggle() {
var triggerChangeEvent = true;
var rootElement = $(this._element).closest(Selector.DATA_TOGGLE)[0];
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
if (rootElement) {
var input = $(this._element).find(Selector.INPUT)[0];
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +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
2016-10-10 02:26:51 +02:00
if (activeElement) {
$(activeElement).removeClass(ClassName.ACTIVE);
2015-05-08 02:07:38 +02:00
}
}
2016-10-10 02:26:51 +02:00
}
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
if (triggerChangeEvent) {
input.checked = !$(this._element).hasClass(ClassName.ACTIVE);
2016-11-26 20:16:53 +01:00
$(input).trigger('change');
2015-05-08 02:07:38 +02:00
}
2016-10-10 02:26:51 +02:00
input.focus();
2015-05-08 02:07:38 +02:00
}
}
2016-10-10 02:26:51 +02:00
2017-01-05 23:51:14 +01:00
this._element.setAttribute('aria-pressed', !$(this._element).hasClass(ClassName.ACTIVE));
2016-10-10 02:26:51 +02:00
if (triggerChangeEvent) {
$(this._element).toggleClass(ClassName.ACTIVE);
2015-05-13 21:48:34 +02:00
}
2016-10-10 02:26:51 +02:00
};
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
Button.prototype.dispose = function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
};
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
// static
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
Button._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var data = $(this).data(DATA_KEY);
2015-05-08 02:07:38 +02:00
2016-10-10 02:26:51 +02:00
if (!data) {
data = new Button(this);
$(this).data(DATA_KEY, data);
}
if (config === 'toggle') {
data[config]();
}
});
};
_createClass(Button, null, [{
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));
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Button._jQueryInterface;
$.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);
2015-08-13 06:12:03 +02:00
//# sourceMappingURL=button.js.map