button.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* ========================================================================
  2. * Bootstrap: button.js v3.1.1
  3. * http://getbootstrap.com/javascript/#buttons
  4. * ========================================================================
  5. * Copyright 2011-2014 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.DEFAULTS = {
  18. loadingText: 'loading...'
  19. }
  20. Button.prototype.setState = function (state) {
  21. var d = 'disabled'
  22. var $el = this.$element
  23. var val = $el.is('input') ? 'val' : 'html'
  24. var data = $el.data()
  25. state = state + 'Text'
  26. if (!data.resetText) $el.data('resetText', $el[val]())
  27. $el[val](data[state] || this.options[state])
  28. // push to event loop to allow forms to submit
  29. setTimeout($.proxy(function () {
  30. if (state == 'loadingText') {
  31. this.isLoading = true
  32. $el.addClass(d).attr(d, d)
  33. } else if (this.isLoading) {
  34. this.isLoading = false
  35. $el.removeClass(d).removeAttr(d)
  36. }
  37. }, this), 0)
  38. }
  39. Button.prototype.toggle = function () {
  40. var changed = true
  41. var $parent = this.$element.closest('[data-toggle="buttons"]')
  42. if ($parent.length) {
  43. var $input = this.$element.find('input')
  44. if ($input.prop('type') == 'radio') {
  45. if ($input.prop('checked') && this.$element.hasClass('active')) changed = false
  46. else $parent.find('.active').removeClass('active')
  47. }
  48. if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
  49. }
  50. if (changed) this.$element.toggleClass('active')
  51. }
  52. // BUTTON PLUGIN DEFINITION
  53. // ========================
  54. var old = $.fn.button
  55. $.fn.button = function (option) {
  56. return this.each(function () {
  57. var $this = $(this)
  58. var data = $this.data('bs.button')
  59. var options = typeof option == 'object' && option
  60. if (!data) $this.data('bs.button', (data = new Button(this, options)))
  61. if (option == 'toggle') data.toggle()
  62. else if (option) data.setState(option)
  63. })
  64. }
  65. $.fn.button.Constructor = Button
  66. // BUTTON NO CONFLICT
  67. // ==================
  68. $.fn.button.noConflict = function () {
  69. $.fn.button = old
  70. return this
  71. }
  72. // BUTTON DATA-API
  73. // ===============
  74. $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
  75. var $btn = $(e.target)
  76. if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
  77. $btn.button('toggle')
  78. e.preventDefault()
  79. })
  80. }(jQuery);