button.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*!
  2. * Bootstrap button.js v4.4.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.4.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_TOGGLES: '[data-toggle="buttons"]',
  45. DATA_TOGGLE: '[data-toggle="button"]',
  46. DATA_TOGGLES_BUTTONS: '[data-toggle="buttons"] .btn',
  47. INPUT: 'input:not([type="hidden"])',
  48. ACTIVE: '.active',
  49. BUTTON: '.btn'
  50. };
  51. var Event = {
  52. CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY,
  53. FOCUS_BLUR_DATA_API: "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY),
  54. LOAD_DATA_API: "load" + EVENT_KEY + DATA_API_KEY
  55. };
  56. /**
  57. * ------------------------------------------------------------------------
  58. * Class Definition
  59. * ------------------------------------------------------------------------
  60. */
  61. var Button =
  62. /*#__PURE__*/
  63. function () {
  64. function Button(element) {
  65. this._element = element;
  66. } // Getters
  67. var _proto = Button.prototype;
  68. // Public
  69. _proto.toggle = function toggle() {
  70. var triggerChangeEvent = true;
  71. var addAriaPressed = true;
  72. var rootElement = $(this._element).closest(Selector.DATA_TOGGLES)[0];
  73. if (rootElement) {
  74. var input = this._element.querySelector(Selector.INPUT);
  75. if (input) {
  76. if (input.type === 'radio') {
  77. if (input.checked && this._element.classList.contains(ClassName.ACTIVE)) {
  78. triggerChangeEvent = false;
  79. } else {
  80. var activeElement = rootElement.querySelector(Selector.ACTIVE);
  81. if (activeElement) {
  82. $(activeElement).removeClass(ClassName.ACTIVE);
  83. }
  84. }
  85. } else if (input.type === 'checkbox') {
  86. if (this._element.tagName === 'LABEL' && input.checked === this._element.classList.contains(ClassName.ACTIVE)) {
  87. triggerChangeEvent = false;
  88. }
  89. } else {
  90. // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
  91. triggerChangeEvent = false;
  92. }
  93. if (triggerChangeEvent) {
  94. input.checked = !this._element.classList.contains(ClassName.ACTIVE);
  95. $(input).trigger('change');
  96. }
  97. input.focus();
  98. addAriaPressed = false;
  99. }
  100. }
  101. if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
  102. if (addAriaPressed) {
  103. this._element.setAttribute('aria-pressed', !this._element.classList.contains(ClassName.ACTIVE));
  104. }
  105. if (triggerChangeEvent) {
  106. $(this._element).toggleClass(ClassName.ACTIVE);
  107. }
  108. }
  109. };
  110. _proto.dispose = function dispose() {
  111. $.removeData(this._element, DATA_KEY);
  112. this._element = null;
  113. } // Static
  114. ;
  115. Button._jQueryInterface = function _jQueryInterface(config) {
  116. return this.each(function () {
  117. var data = $(this).data(DATA_KEY);
  118. if (!data) {
  119. data = new Button(this);
  120. $(this).data(DATA_KEY, data);
  121. }
  122. if (config === 'toggle') {
  123. data[config]();
  124. }
  125. });
  126. };
  127. _createClass(Button, null, [{
  128. key: "VERSION",
  129. get: function get() {
  130. return VERSION;
  131. }
  132. }]);
  133. return Button;
  134. }();
  135. /**
  136. * ------------------------------------------------------------------------
  137. * Data Api implementation
  138. * ------------------------------------------------------------------------
  139. */
  140. $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
  141. var button = event.target;
  142. if (!$(button).hasClass(ClassName.BUTTON)) {
  143. button = $(button).closest(Selector.BUTTON)[0];
  144. }
  145. if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
  146. event.preventDefault(); // work around Firefox bug #1540995
  147. } else {
  148. var inputBtn = button.querySelector(Selector.INPUT);
  149. if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
  150. event.preventDefault(); // work around Firefox bug #1540995
  151. return;
  152. }
  153. Button._jQueryInterface.call($(button), 'toggle');
  154. }
  155. }).on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, function (event) {
  156. var button = $(event.target).closest(Selector.BUTTON)[0];
  157. $(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type));
  158. });
  159. $(window).on(Event.LOAD_DATA_API, function () {
  160. // ensure correct active class is set to match the controls' actual values/states
  161. // find all checkboxes/readio buttons inside data-toggle groups
  162. var buttons = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLES_BUTTONS));
  163. for (var i = 0, len = buttons.length; i < len; i++) {
  164. var button = buttons[i];
  165. var input = button.querySelector(Selector.INPUT);
  166. if (input.checked || input.hasAttribute('checked')) {
  167. button.classList.add(ClassName.ACTIVE);
  168. } else {
  169. button.classList.remove(ClassName.ACTIVE);
  170. }
  171. } // find all button toggles
  172. buttons = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE));
  173. for (var _i = 0, _len = buttons.length; _i < _len; _i++) {
  174. var _button = buttons[_i];
  175. if (_button.getAttribute('aria-pressed') === 'true') {
  176. _button.classList.add(ClassName.ACTIVE);
  177. } else {
  178. _button.classList.remove(ClassName.ACTIVE);
  179. }
  180. }
  181. });
  182. /**
  183. * ------------------------------------------------------------------------
  184. * jQuery
  185. * ------------------------------------------------------------------------
  186. */
  187. $.fn[NAME] = Button._jQueryInterface;
  188. $.fn[NAME].Constructor = Button;
  189. $.fn[NAME].noConflict = function () {
  190. $.fn[NAME] = JQUERY_NO_CONFLICT;
  191. return Button._jQueryInterface;
  192. };
  193. return Button;
  194. })));
  195. //# sourceMappingURL=button.js.map