3.0.2 -> 3.0.3
[bootswatch] / bower_components / bootstrap / js / button.js
1 /* ========================================================================
2  * Bootstrap: button.js v3.0.3
3  * http://getbootstrap.com/javascript/#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
20
21 +function ($) { "use strict";
22
23   // BUTTON PUBLIC CLASS DEFINITION
24   // ==============================
25
26   var Button = function (element, options) {
27     this.$element = $(element)
28     this.options  = $.extend({}, Button.DEFAULTS, options)
29   }
30
31   Button.DEFAULTS = {
32     loadingText: 'loading...'
33   }
34
35   Button.prototype.setState = function (state) {
36     var d    = 'disabled'
37     var $el  = this.$element
38     var val  = $el.is('input') ? 'val' : 'html'
39     var data = $el.data()
40
41     state = state + 'Text'
42
43     if (!data.resetText) $el.data('resetText', $el[val]())
44
45     $el[val](data[state] || this.options[state])
46
47     // push to event loop to allow forms to submit
48     setTimeout(function () {
49       state == 'loadingText' ?
50         $el.addClass(d).attr(d, d) :
51         $el.removeClass(d).removeAttr(d);
52     }, 0)
53   }
54
55   Button.prototype.toggle = function () {
56     var $parent = this.$element.closest('[data-toggle="buttons"]')
57     var changed = true
58
59     if ($parent.length) {
60       var $input = this.$element.find('input')
61       if ($input.prop('type') === 'radio') {
62         // see if clicking on current one
63         if ($input.prop('checked') && this.$element.hasClass('active'))
64           changed = false
65         else
66           $parent.find('.active').removeClass('active')
67       }
68       if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
69     }
70
71     if (changed) this.$element.toggleClass('active')
72   }
73
74
75   // BUTTON PLUGIN DEFINITION
76   // ========================
77
78   var old = $.fn.button
79
80   $.fn.button = function (option) {
81     return this.each(function () {
82       var $this   = $(this)
83       var data    = $this.data('bs.button')
84       var options = typeof option == 'object' && option
85
86       if (!data) $this.data('bs.button', (data = new Button(this, options)))
87
88       if (option == 'toggle') data.toggle()
89       else if (option) data.setState(option)
90     })
91   }
92
93   $.fn.button.Constructor = Button
94
95
96   // BUTTON NO CONFLICT
97   // ==================
98
99   $.fn.button.noConflict = function () {
100     $.fn.button = old
101     return this
102   }
103
104
105   // BUTTON DATA-API
106   // ===============
107
108   $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
109     var $btn = $(e.target)
110     if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
111     $btn.button('toggle')
112     e.preventDefault()
113   })
114
115 }(jQuery);