bootstrap-button.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* ============================================================
  2. * bootstrap-button.js v2.0.2
  3. * http://twitter.github.com/bootstrap/javascript.html#buttons
  4. * ============================================================
  5. * Copyright 2012 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( $ ){
  20. "use strict"
  21. /* BUTTON PUBLIC CLASS DEFINITION
  22. * ============================== */
  23. var Button = function ( element, options ) {
  24. this.$element = $(element)
  25. this.options = $.extend({}, $.fn.button.defaults, options)
  26. }
  27. Button.prototype = {
  28. constructor: Button
  29. , setState: function ( state ) {
  30. var d = 'disabled'
  31. , $el = this.$element
  32. , data = $el.data()
  33. , val = $el.is('input') ? 'val' : 'html'
  34. state = state + 'Text'
  35. 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. , toggle: function () {
  45. var $parent = this.$element.parent('[data-toggle="buttons-radio"]')
  46. $parent && $parent
  47. .find('.active')
  48. .removeClass('active')
  49. this.$element.toggleClass('active')
  50. }
  51. }
  52. /* BUTTON PLUGIN DEFINITION
  53. * ======================== */
  54. $.fn.button = function ( option ) {
  55. return this.each(function () {
  56. var $this = $(this)
  57. , data = $this.data('button')
  58. , options = typeof option == 'object' && option
  59. if (!data) $this.data('button', (data = new Button(this, options)))
  60. if (option == 'toggle') data.toggle()
  61. else if (option) data.setState(option)
  62. })
  63. }
  64. $.fn.button.defaults = {
  65. loadingText: 'loading...'
  66. }
  67. $.fn.button.Constructor = Button
  68. /* BUTTON DATA-API
  69. * =============== */
  70. $(function () {
  71. $('body').on('click.button.data-api', '[data-toggle^=button]', function ( e ) {
  72. var $btn = $(e.target)
  73. if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
  74. $btn.button('toggle')
  75. })
  76. })
  77. }( window.jQuery );