bootstrap-button.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* ============================================================
  2. * bootstrap-button.js v2.3.2
  3. * http://getbootstrap.com/2.3.2/javascript.html#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 ($) {
  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. var old = $.fn.button
  52. $.fn.button = function (option) {
  53. return this.each(function () {
  54. var $this = $(this)
  55. , data = $this.data('button')
  56. , options = typeof option == 'object' && option
  57. if (!data) $this.data('button', (data = new Button(this, options)))
  58. if (option == 'toggle') data.toggle()
  59. else if (option) data.setState(option)
  60. })
  61. }
  62. $.fn.button.defaults = {
  63. loadingText: 'loading...'
  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.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. })
  79. }(window.jQuery);