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

173 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-05-08 02:07:38 +02:00
/**
* --------------------------------------------------------------------------
2015-12-05 11:59:13 +01:00
* Bootstrap (v4.0.0-alpha): button.js
2015-05-08 02:07:38 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2015-08-13 06:12:03 +02:00
'use strict';
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
var Button = (function ($) {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'button';
2015-12-05 11:59:13 +01:00
var VERSION = '4.0.0-alpha';
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
* ------------------------------------------------------------------------
*/
var Button = (function () {
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
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
// getters
2015-05-08 02:07:38 +02:00
_createClass(Button, [{
key: 'toggle',
// public
value: function toggle() {
var triggerChangeEvent = true;
2015-05-08 07:26:40 +02:00
var rootElement = $(this._element).closest(Selector.DATA_TOGGLE)[0];
2015-05-08 02:07:38 +02:00
if (rootElement) {
2015-05-08 07:26:40 +02:00
var input = $(this._element).find(Selector.INPUT)[0];
2015-05-08 02:07:38 +02:00
if (input) {
if (input.type === 'radio') {
2015-05-08 07:26:40 +02:00
if (input.checked && $(this._element).hasClass(ClassName.ACTIVE)) {
2015-05-08 02:07:38 +02:00
triggerChangeEvent = false;
} else {
var activeElement = $(rootElement).find(Selector.ACTIVE)[0];
if (activeElement) {
$(activeElement).removeClass(ClassName.ACTIVE);
}
}
}
if (triggerChangeEvent) {
2015-05-08 07:26:40 +02:00
input.checked = !$(this._element).hasClass(ClassName.ACTIVE);
$(this._element).trigger('change');
2015-05-08 02:07:38 +02:00
}
}
} else {
2015-05-08 07:26:40 +02:00
this._element.setAttribute('aria-pressed', !$(this._element).hasClass(ClassName.ACTIVE));
2015-05-08 02:07:38 +02:00
}
if (triggerChangeEvent) {
2015-05-08 07:26:40 +02:00
$(this._element).toggleClass(ClassName.ACTIVE);
2015-05-08 02:07:38 +02:00
}
}
2015-05-13 21:48:34 +02:00
}, {
key: 'dispose',
value: function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
}
2015-05-08 02:07:38 +02:00
// static
2015-08-13 06:12:03 +02:00
}], [{
key: '_jQueryInterface',
2015-05-08 02:07:38 +02:00
value: function _jQueryInterface(config) {
return this.each(function () {
var data = $(this).data(DATA_KEY);
if (!data) {
data = new Button(this);
$(this).data(DATA_KEY, data);
}
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;
})();
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;
})(jQuery);
2015-08-13 06:12:03 +02:00
//# sourceMappingURL=button.js.map