a02c5ae224c8ad4b44cb05dac1edcd0752322ca9.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v4.0.0): button.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. const Button = (($) => {
  8. /**
  9. * ------------------------------------------------------------------------
  10. * Constants
  11. * ------------------------------------------------------------------------
  12. */
  13. const NAME = 'button'
  14. const VERSION = '4.0.0'
  15. const DATA_KEY = 'bs.button'
  16. const EVENT_KEY = `.${DATA_KEY}`
  17. const DATA_API_KEY = '.data-api'
  18. const JQUERY_NO_CONFLICT = $.fn[NAME]
  19. const ClassName = {
  20. ACTIVE : 'active',
  21. BUTTON : 'btn',
  22. FOCUS : 'focus'
  23. }
  24. const Selector = {
  25. DATA_TOGGLE_CARROT : '[data-toggle^="button"]',
  26. DATA_TOGGLE : '[data-toggle="buttons"]',
  27. INPUT : 'input',
  28. ACTIVE : '.active',
  29. BUTTON : '.btn'
  30. }
  31. const Event = {
  32. CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`,
  33. FOCUS_BLUR_DATA_API : `focus${EVENT_KEY}${DATA_API_KEY} `
  34. + `blur${EVENT_KEY}${DATA_API_KEY}`
  35. }
  36. /**
  37. * ------------------------------------------------------------------------
  38. * Class Definition
  39. * ------------------------------------------------------------------------
  40. */
  41. class Button {
  42. constructor(element) {
  43. this._element = element
  44. }
  45. // getters
  46. static get VERSION() {
  47. return VERSION
  48. }
  49. // public
  50. toggle() {
  51. let triggerChangeEvent = true
  52. let rootElement = $(this._element).closest(
  53. Selector.DATA_TOGGLE
  54. )[0]
  55. if (rootElement) {
  56. let input = $(this._element).find(Selector.INPUT)[0]
  57. if (input) {
  58. if (input.type === 'radio') {
  59. if (input.checked &&
  60. $(this._element).hasClass(ClassName.ACTIVE)) {
  61. triggerChangeEvent = false
  62. } else {
  63. let activeElement = $(rootElement).find(Selector.ACTIVE)[0]
  64. if (activeElement) {
  65. $(activeElement).removeClass(ClassName.ACTIVE)
  66. }
  67. }
  68. }
  69. if (triggerChangeEvent) {
  70. input.checked = !$(this._element).hasClass(ClassName.ACTIVE)
  71. $(this._element).trigger('change')
  72. }
  73. }
  74. } else {
  75. this._element.setAttribute('aria-pressed',
  76. !$(this._element).hasClass(ClassName.ACTIVE))
  77. }
  78. if (triggerChangeEvent) {
  79. $(this._element).toggleClass(ClassName.ACTIVE)
  80. }
  81. }
  82. dispose() {
  83. $.removeData(this._element, DATA_KEY)
  84. this._element = null
  85. }
  86. // static
  87. static _jQueryInterface(config) {
  88. return this.each(function () {
  89. let data = $(this).data(DATA_KEY)
  90. if (!data) {
  91. data = new Button(this)
  92. $(this).data(DATA_KEY, data)
  93. }
  94. if (config === 'toggle') {
  95. data[config]()
  96. }
  97. })
  98. }
  99. }
  100. /**
  101. * ------------------------------------------------------------------------
  102. * Data Api implementation
  103. * ------------------------------------------------------------------------
  104. */
  105. $(document)
  106. .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
  107. event.preventDefault()
  108. let button = event.target
  109. if (!$(button).hasClass(ClassName.BUTTON)) {
  110. button = $(button).closest(Selector.BUTTON)
  111. }
  112. Button._jQueryInterface.call($(button), 'toggle')
  113. })
  114. .on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
  115. let button = $(event.target).closest(Selector.BUTTON)[0]
  116. $(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type))
  117. })
  118. /**
  119. * ------------------------------------------------------------------------
  120. * jQuery
  121. * ------------------------------------------------------------------------
  122. */
  123. $.fn[NAME] = Button._jQueryInterface
  124. $.fn[NAME].Constructor = Button
  125. $.fn[NAME].noConflict = function () {
  126. $.fn[NAME] = JQUERY_NO_CONFLICT
  127. return Button._jQueryInterface
  128. }
  129. return Button
  130. })(jQuery)
  131. export default Button