3.0.0 -> 3.0.1
[bootswatch] / bower_components / bootstrap / js / button.js
1 /* ========================================================================
2  * Bootstrap: button.js v3.0.0
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
58     if ($parent.length) {
59       var $input = this.$element.find('input')
60         .prop('checked', !this.$element.hasClass('active'))
61         .trigger('change')
62       if ($input.prop('type') === 'radio') $parent.find('.active').removeClass('active')
63     }
64
65     this.$element.toggleClass('active')
66   }
67
68
69   // BUTTON PLUGIN DEFINITION
70   // ========================
71
72   var old = $.fn.button
73
74   $.fn.button = function (option) {
75     return this.each(function () {
76       var $this   = $(this)
77       var data    = $this.data('bs.button')
78       var options = typeof option == 'object' && option
79
80       if (!data) $this.data('bs.button', (data = new Button(this, options)))
81
82       if (option == 'toggle') data.toggle()
83       else if (option) data.setState(option)
84     })
85   }
86
87   $.fn.button.Constructor = Button
88
89
90   // BUTTON NO CONFLICT
91   // ==================
92
93   $.fn.button.noConflict = function () {
94     $.fn.button = old
95     return this
96   }
97
98
99   // BUTTON DATA-API
100   // ===============
101
102   $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
103     var $btn = $(e.target)
104     if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
105     $btn.button('toggle')
106     e.preventDefault()
107   })
108
109 }(window.jQuery);