bootstrap-button.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* ============================================================
  2. * bootstrap-button.js v2.1.1
  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"; // jshint ;_;
  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.setState = function (state) {
  28. var d = 'disabled'
  29. , $el = this.$element
  30. , data = $el.data()
  31. , val = $el.is('input') ? 'val' : 'html'
  32. state = state + 'Text'
  33. data.resetText || $el.data('resetText', $el[val]())
  34. $el[val](data[state] || this.options[state])
  35. // push to event loop to allow forms to submit
  36. setTimeout(function () {
  37. state == 'loadingText' ?
  38. $el.addClass(d).attr(d, d) :
  39. $el.removeClass(d).removeAttr(d)
  40. }, 0)
  41. }
  42. Button.prototype.toggle = function () {
  43. var $parent = this.$element.closest('[data-toggle="buttons-radio"]')
  44. $parent && $parent
  45. .find('.active')
  46. .removeClass('active')
  47. this.$element.toggleClass('active')
  48. }
  49. /* BUTTON PLUGIN DEFINITION
  50. * ======================== */
  51. $.fn.button = function (option) {
  52. return this.each(function () {
  53. var $this = $(this)
  54. , data = $this.data('button')
  55. , options = typeof option == 'object' && option
  56. if (!data) $this.data('button', (data = new Button(this, options)))
  57. if (option == 'toggle') data.toggle()
  58. else if (option) data.setState(option)
  59. })
  60. }
  61. $.fn.button.defaults = {
  62. loadingText: 'loading...'
  63. }
  64. $.fn.button.Constructor = Button
  65. /* BUTTON DATA-API
  66. * =============== */
  67. $(function () {
  68. $('body').on('click.button.data-api', '[data-toggle^=button]', function ( e ) {
  69. var $btn = $(e.target)
  70. if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
  71. $btn.button('toggle')
  72. })
  73. })
  74. }(window.jQuery);