button.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*!
  2. * Bootstrap button.js v4.3.1 (https://getbootstrap.com/)
  3. * Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
  8. typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  9. (global = global || self, global.Button = factory(global.jQuery));
  10. }(this, function ($) { 'use strict';
  11. $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  12. function _defineProperties(target, props) {
  13. for (var i = 0; i < props.length; i++) {
  14. var descriptor = props[i];
  15. descriptor.enumerable = descriptor.enumerable || false;
  16. descriptor.configurable = true;
  17. if ("value" in descriptor) descriptor.writable = true;
  18. Object.defineProperty(target, descriptor.key, descriptor);
  19. }
  20. }
  21. function _createClass(Constructor, protoProps, staticProps) {
  22. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  23. if (staticProps) _defineProperties(Constructor, staticProps);
  24. return Constructor;
  25. }
  26. /**
  27. * ------------------------------------------------------------------------
  28. * Constants
  29. * ------------------------------------------------------------------------
  30. */
  31. var NAME = 'button';
  32. var VERSION = '4.3.1';
  33. var DATA_KEY = 'bs.button';
  34. var EVENT_KEY = "." + DATA_KEY;
  35. var DATA_API_KEY = '.data-api';
  36. var JQUERY_NO_CONFLICT = $.fn[NAME];
  37. var ClassName = {
  38. ACTIVE: 'active',
  39. BUTTON: 'btn',
  40. FOCUS: 'focus'
  41. };
  42. var Selector = {
  43. DATA_TOGGLE_CARROT: '[data-toggle^="button"]',
  44. DATA_TOGGLE: '[data-toggle="buttons"]',
  45. INPUT: 'input:not([type="hidden"])',
  46. ACTIVE: '.active',
  47. BUTTON: '.btn'
  48. };
  49. var Event = {
  50. CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY,
  51. FOCUS_BLUR_DATA_API: "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY)
  52. /**
  53. * ------------------------------------------------------------------------
  54. * Class Definition
  55. * ------------------------------------------------------------------------
  56. */
  57. };
  58. var Button =
  59. /*#__PURE__*/
  60. function () {
  61. function Button(element) {
  62. this._element = element;
  63. } // Getters
  64. var _proto = Button.prototype;
  65. // Public
  66. _proto.toggle = function toggle() {
  67. var triggerChangeEvent = true;
  68. var addAriaPressed = true;
  69. var rootElement = $(this._element).closest(Selector.DATA_TOGGLE)[0];
  70. if (rootElement) {
  71. var input = this._element.querySelector(Selector.INPUT);
  72. if (input) {
  73. if (input.type === 'radio') {
  74. if (input.checked && this._element.classList.contains(ClassName.ACTIVE)) {
  75. triggerChangeEvent = false;
  76. } else {
  77. var activeElement = rootElement.querySelector(Selector.ACTIVE);
  78. if (activeElement) {
  79. $(activeElement).removeClass(ClassName.ACTIVE);
  80. }
  81. }
  82. }
  83. if (triggerChangeEvent) {
  84. if (input.hasAttribute('disabled') || rootElement.hasAttribute('disabled') || input.classList.contains('disabled') || rootElement.classList.contains('disabled')) {
  85. return;
  86. }
  87. input.checked = !this._element.classList.contains(ClassName.ACTIVE);
  88. $(input).trigger('change');
  89. }
  90. input.focus();
  91. addAriaPressed = false;
  92. }
  93. }
  94. if (addAriaPressed) {
  95. this._element.setAttribute('aria-pressed', !this._element.classList.contains(ClassName.ACTIVE));
  96. }
  97. if (triggerChangeEvent) {
  98. $(this._element).toggleClass(ClassName.ACTIVE);
  99. }
  100. };
  101. _proto.dispose = function dispose() {
  102. $.removeData(this._element, DATA_KEY);
  103. this._element = null;
  104. } // Static
  105. ;
  106. Button._jQueryInterface = function _jQueryInterface(config) {
  107. return this.each(function () {
  108. var data = $(this).data(DATA_KEY);
  109. if (!data) {
  110. data = new Button(this);
  111. $(this).data(DATA_KEY, data);
  112. }
  113. if (config === 'toggle') {
  114. data[config]();
  115. }
  116. });
  117. };
  118. _createClass(Button, null, [{
  119. key: "VERSION",
  120. get: function get() {
  121. return VERSION;
  122. }
  123. }]);
  124. return Button;
  125. }();
  126. /**
  127. * ------------------------------------------------------------------------
  128. * Data Api implementation
  129. * ------------------------------------------------------------------------
  130. */
  131. $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
  132. event.preventDefault();
  133. var button = event.target;
  134. if (!$(button).hasClass(ClassName.BUTTON)) {
  135. button = $(button).closest(Selector.BUTTON);
  136. }
  137. Button._jQueryInterface.call($(button), 'toggle');
  138. }).on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
  139. var button = $(event.target).closest(Selector.BUTTON)[0];
  140. $(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type));
  141. });
  142. /**
  143. * ------------------------------------------------------------------------
  144. * jQuery
  145. * ------------------------------------------------------------------------
  146. */
  147. $.fn[NAME] = Button._jQueryInterface;
  148. $.fn[NAME].Constructor = Button;
  149. $.fn[NAME].noConflict = function () {
  150. $.fn[NAME] = JQUERY_NO_CONFLICT;
  151. return Button._jQueryInterface;
  152. };
  153. return Button;
  154. }));
  155. //# sourceMappingURL=button.js.map