button.js 2.98 KB
Newer Older
1
/* ========================================================================
2
 * Bootstrap: button.js v3.0.1
3
 * http://getbootstrap.com/javascript/#buttons
4
 * ========================================================================
fat's avatar
fat committed
5
 * Copyright 2013 Twitter, Inc.
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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.
18
 * ======================================================================== */
19

20

21
+function ($) { "use strict";
22

fat's avatar
fat committed
23
24
  // BUTTON PUBLIC CLASS DEFINITION
  // ==============================
25

26
  var Button = function (element, options) {
27
    this.$element = $(element)
fat's avatar
fat committed
28
29
30
31
32
    this.options  = $.extend({}, Button.DEFAULTS, options)
  }

  Button.DEFAULTS = {
    loadingText: 'loading...'
33
  }
34

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

41
    state = state + 'Text'
fat's avatar
fat committed
42
43

    if (!data.resetText) $el.data('resetText', $el[val]())
44

45
    $el[val](data[state] || this.options[state])
46

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

55
  Button.prototype.toggle = function () {
56
    var $parent = this.$element.closest('[data-toggle="buttons"]')
57

58
    if ($parent.length) {
fat's avatar
fat committed
59
60
61
      var $input = this.$element.find('input')
        .prop('checked', !this.$element.hasClass('active'))
        .trigger('change')
62
      if ($input.prop('type') === 'radio') $parent.find('.active').removeClass('active')
fat's avatar
fat committed
63
    }
64

65
    this.$element.toggleClass('active')
66
67
  }

68

fat's avatar
fat committed
69
70
  // BUTTON PLUGIN DEFINITION
  // ========================
71

72
73
  var old = $.fn.button

74
  $.fn.button = function (option) {
75
    return this.each(function () {
fat's avatar
fat committed
76
      var $this   = $(this)
fat's avatar
fat committed
77
      var data    = $this.data('bs.button')
fat's avatar
fat committed
78
79
      var options = typeof option == 'object' && option

80
      if (!data) $this.data('bs.button', (data = new Button(this, options)))
fat's avatar
fat committed
81

82
83
      if (option == 'toggle') data.toggle()
      else if (option) data.setState(option)
84
85
86
    })
  }

87
  $.fn.button.Constructor = Button
88
89


fat's avatar
fat committed
90
91
  // BUTTON NO CONFLICT
  // ==================
92
93
94
95
96
97
98

  $.fn.button.noConflict = function () {
    $.fn.button = old
    return this
  }


fat's avatar
fat committed
99
100
  // BUTTON DATA-API
  // ===============
101

102
  $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
103
104
105
    var $btn = $(e.target)
    if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
    $btn.button('toggle')
106
    e.preventDefault()
107
108
  })

109
}(jQuery);