bootstrap-button.js 2.65 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ============================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-button.js v2.1.2
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * http://twitter.github.com/bootstrap/javascript.html#buttons
 * ============================================================
 * Copyright 2012 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================ */

20

21
22
23
!function ($) {

  "use strict"; // jshint ;_;
Jacob Thornton's avatar
Jacob Thornton committed
24
25
26
27
28


 /* BUTTON PUBLIC CLASS DEFINITION
  * ============================== */

29
  var Button = function (element, options) {
Jacob Thornton's avatar
Jacob Thornton committed
30
31
32
33
    this.$element = $(element)
    this.options = $.extend({}, $.fn.button.defaults, options)
  }

34
35
36
37
38
  Button.prototype.setState = function (state) {
    var d = 'disabled'
      , $el = this.$element
      , data = $el.data()
      , val = $el.is('input') ? 'val' : 'html'
Jacob Thornton's avatar
Jacob Thornton committed
39

40
41
    state = state + 'Text'
    data.resetText || $el.data('resetText', $el[val]())
Jacob Thornton's avatar
Jacob Thornton committed
42

43
    $el[val](data[state] || this.options[state])
Jacob Thornton's avatar
Jacob Thornton committed
44

45
46
47
48
49
50
51
    // push to event loop to allow forms to submit
    setTimeout(function () {
      state == 'loadingText' ?
        $el.addClass(d).attr(d, d) :
        $el.removeClass(d).removeAttr(d)
    }, 0)
  }
Jacob Thornton's avatar
Jacob Thornton committed
52

53
  Button.prototype.toggle = function () {
Jacob Thornton's avatar
Jacob Thornton committed
54
    var $parent = this.$element.closest('[data-toggle="buttons-radio"]')
Jacob Thornton's avatar
Jacob Thornton committed
55

56
57
58
    $parent && $parent
      .find('.active')
      .removeClass('active')
Jacob Thornton's avatar
Jacob Thornton committed
59

60
    this.$element.toggleClass('active')
Jacob Thornton's avatar
Jacob Thornton committed
61
62
63
64
65
66
  }


 /* BUTTON PLUGIN DEFINITION
  * ======================== */

67
  $.fn.button = function (option) {
Jacob Thornton's avatar
Jacob Thornton committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('button')
        , options = typeof option == 'object' && option
      if (!data) $this.data('button', (data = new Button(this, options)))
      if (option == 'toggle') data.toggle()
      else if (option) data.setState(option)
    })
  }

  $.fn.button.defaults = {
    loadingText: 'loading...'
  }

  $.fn.button.Constructor = Button


 /* BUTTON DATA-API
  * =============== */

  $(function () {
    $('body').on('click.button.data-api', '[data-toggle^=button]', function ( e ) {
90
91
92
      var $btn = $(e.target)
      if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
      $btn.button('toggle')
Jacob Thornton's avatar
Jacob Thornton committed
93
94
95
    })
  })

96
}(window.jQuery);