button.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* ========================================================================
  2. * Bootstrap: button.js v3.2.0
  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.VERSION = '3.2.0'
  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 = state + 'Text'
  27. if (data.resetText == null) $el.data('resetText', $el[val]())
  28. $el[val](data[state] == null ? this.options[state] : data[state])
  29. // push to event loop to allow forms to submit
  30. setTimeout($.proxy(function () {
  31. if (state == 'loadingText') {
  32. this.isLoading = true
  33. $el.addClass(d).attr(d, d)
  34. } else if (this.isLoading) {
  35. this.isLoading = false
  36. $el.removeClass(d).removeAttr(d)
  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') && this.$element.hasClass('active')) changed = false
  47. else $parent.find('.active').removeClass('active')
  48. }
  49. if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
  50. }
  51. if (changed) this.$element.toggleClass('active')
  52. }
  53. // BUTTON PLUGIN DEFINITION
  54. // ========================
  55. function Plugin(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. var old = $.fn.button
  66. $.fn.button = Plugin
  67. $.fn.button.Constructor = Button
  68. // BUTTON NO CONFLICT
  69. // ==================
  70. $.fn.button.noConflict = function () {
  71. $.fn.button = old
  72. return this
  73. }
  74. // BUTTON DATA-API
  75. // ===============
  76. $(document).on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) {
  77. var $btn = $(e.target)
  78. if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
  79. Plugin.call($btn, 'toggle')
  80. e.preventDefault()
  81. })
  82. }(jQuery);