button.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* ========================================================================
  2. * Bootstrap: button.js v3.0.3
  3. * http://getbootstrap.com/javascript/#buttons
  4. * ========================================================================
  5. * Copyright 2013 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. +function ($) { "use strict";
  20. // BUTTON PUBLIC CLASS DEFINITION
  21. // ==============================
  22. var Button = function (element, options) {
  23. this.$element = $(element)
  24. this.options = $.extend({}, Button.DEFAULTS, options)
  25. }
  26. Button.DEFAULTS = {
  27. loadingText: 'loading...'
  28. }
  29. Button.prototype.setState = function (state) {
  30. var d = 'disabled'
  31. var $el = this.$element
  32. var val = $el.is('input') ? 'val' : 'html'
  33. var data = $el.data()
  34. state = state + 'Text'
  35. if (!data.resetText) $el.data('resetText', $el[val]())
  36. $el[val](data[state] || this.options[state])
  37. // push to event loop to allow forms to submit
  38. setTimeout(function () {
  39. state == 'loadingText' ?
  40. $el.addClass(d).attr(d, d) :
  41. $el.removeClass(d).removeAttr(d);
  42. }, 0)
  43. }
  44. Button.prototype.toggle = function () {
  45. var $parent = this.$element.closest('[data-toggle="buttons"]')
  46. var changed = true
  47. if ($parent.length) {
  48. var $input = this.$element.find('input')
  49. if ($input.prop('type') === 'radio') {
  50. // see if clicking on current one
  51. if ($input.prop('checked') && this.$element.hasClass('active'))
  52. changed = false
  53. else
  54. $parent.find('.active').removeClass('active')
  55. }
  56. if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
  57. }
  58. if (changed) this.$element.toggleClass('active')
  59. }
  60. // BUTTON PLUGIN DEFINITION
  61. // ========================
  62. var old = $.fn.button
  63. $.fn.button = function (option) {
  64. return this.each(function () {
  65. var $this = $(this)
  66. var data = $this.data('bs.button')
  67. var options = typeof option == 'object' && option
  68. if (!data) $this.data('bs.button', (data = new Button(this, options)))
  69. if (option == 'toggle') data.toggle()
  70. else if (option) data.setState(option)
  71. })
  72. }
  73. $.fn.button.Constructor = Button
  74. // BUTTON NO CONFLICT
  75. // ==================
  76. $.fn.button.noConflict = function () {
  77. $.fn.button = old
  78. return this
  79. }
  80. // BUTTON DATA-API
  81. // ===============
  82. $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
  83. var $btn = $(e.target)
  84. if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
  85. $btn.button('toggle')
  86. e.preventDefault()
  87. })
  88. }(jQuery);