button.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ========================================================================
  2. * Bootstrap: button.js v3.4.1
  3. * https://getbootstrap.com/docs/3.4/javascript/#buttons
  4. * ========================================================================
  5. * Copyright 2011-2019 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // BUTTON PUBLIC CLASS DEFINITION
  11. // ==============================
  12. var Button = function (element, options) {
  13. this.$element = $(element)
  14. this.options = $.extend({}, Button.DEFAULTS, options)
  15. this.isLoading = false
  16. }
  17. Button.VERSION = '3.4.1'
  18. Button.DEFAULTS = {
  19. loadingText: 'loading...'
  20. }
  21. Button.prototype.setState = function (state) {
  22. var d = 'disabled'
  23. var $el = this.$element
  24. var val = $el.is('input') ? 'val' : 'html'
  25. var data = $el.data()
  26. state += 'Text'
  27. if (data.resetText == null) $el.data('resetText', $el[val]())
  28. // push to event loop to allow forms to submit
  29. setTimeout($.proxy(function () {
  30. $el[val](data[state] == null ? this.options[state] : data[state])
  31. if (state == 'loadingText') {
  32. this.isLoading = true
  33. $el.addClass(d).attr(d, d).prop(d, true)
  34. } else if (this.isLoading) {
  35. this.isLoading = false
  36. $el.removeClass(d).removeAttr(d).prop(d, false)
  37. }
  38. }, this), 0)
  39. }
  40. Button.prototype.toggle = function () {
  41. var changed = true
  42. var $parent = this.$element.closest('[data-toggle="buttons"]')
  43. if ($parent.length) {
  44. var $input = this.$element.find('input')
  45. if ($input.prop('type') == 'radio') {
  46. if ($input.prop('checked')) changed = false
  47. $parent.find('.active').removeClass('active')
  48. this.$element.addClass('active')
  49. } else if ($input.prop('type') == 'checkbox') {
  50. if (($input.prop('checked')) !== this.$element.hasClass('active')) changed = false
  51. this.$element.toggleClass('active')
  52. }
  53. $input.prop('checked', this.$element.hasClass('active'))
  54. if (changed) $input.trigger('change')
  55. } else {
  56. this.$element.attr('aria-pressed', !this.$element.hasClass('active'))
  57. this.$element.toggleClass('active')
  58. }
  59. }
  60. // BUTTON PLUGIN DEFINITION
  61. // ========================
  62. function Plugin(option) {
  63. return this.each(function () {
  64. var $this = $(this)
  65. var data = $this.data('bs.button')
  66. var options = typeof option == 'object' && option
  67. if (!data) $this.data('bs.button', (data = new Button(this, options)))
  68. if (option == 'toggle') data.toggle()
  69. else if (option) data.setState(option)
  70. })
  71. }
  72. var old = $.fn.button
  73. $.fn.button = Plugin
  74. $.fn.button.Constructor = Button
  75. // BUTTON NO CONFLICT
  76. // ==================
  77. $.fn.button.noConflict = function () {
  78. $.fn.button = old
  79. return this
  80. }
  81. // BUTTON DATA-API
  82. // ===============
  83. $(document)
  84. .on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) {
  85. var $btn = $(e.target).closest('.btn')
  86. Plugin.call($btn, 'toggle')
  87. if (!($(e.target).is('input[type="radio"], input[type="checkbox"]'))) {
  88. // Prevent double click on radios, and the double selections (so cancellation) on checkboxes
  89. e.preventDefault()
  90. // The target component still receive the focus
  91. if ($btn.is('input,button')) $btn.trigger('focus')
  92. else $btn.find('input:visible,button:visible').first().trigger('focus')
  93. }
  94. })
  95. .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) {
  96. $(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type))
  97. })
  98. }(jQuery);