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