button.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* ========================================================================
  2. * Bootstrap: button.js v3.0.0
  3. * http://twbs.github.com/bootstrap/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 ($) { "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. if ($parent.length) {
  47. var $input = this.$element.find('input')
  48. .prop('checked', !this.$element.hasClass('active'))
  49. .trigger('change')
  50. if ($input.prop('type') === 'radio') $parent.find('.active').removeClass('active')
  51. }
  52. this.$element.toggleClass('active')
  53. }
  54. // BUTTON PLUGIN DEFINITION
  55. // ========================
  56. var old = $.fn.button
  57. $.fn.button = function (option) {
  58. return this.each(function () {
  59. var $this = $(this)
  60. var data = $this.data('bs.button')
  61. var options = typeof option == 'object' && option
  62. if (!data) $this.data('bs.button', (data = new Button(this, options)))
  63. if (option == 'toggle') data.toggle()
  64. else if (option) data.setState(option)
  65. })
  66. }
  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. $btn.button('toggle')
  80. e.preventDefault()
  81. })
  82. }(window.jQuery);