1 /* ========================================================================
2 * Bootstrap: button.js v3.3.7
3 * http://getbootstrap.com/javascript/#buttons
4 * ========================================================================
5 * Copyright 2011-2016 Twitter, Inc.
6 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7 * ======================================================================== */
13 // BUTTON PUBLIC CLASS DEFINITION
14 // ==============================
16 var Button = function (element, options) {
17 this.$element = $(element)
18 this.options = $.extend({}, Button.DEFAULTS, options)
19 this.isLoading = false
22 Button.VERSION = '3.3.7'
25 loadingText: 'loading...'
28 Button.prototype.setState = function (state) {
30 var $el = this.$element
31 var val = $el.is('input') ? 'val' : 'html'
36 if (data.resetText == null) $el.data('resetText', $el[val]())
38 // push to event loop to allow forms to submit
39 setTimeout($.proxy(function () {
40 $el[val](data[state] == null ? this.options[state] : data[state])
42 if (state == 'loadingText') {
44 $el.addClass(d).attr(d, d).prop(d, true)
45 } else if (this.isLoading) {
46 this.isLoading = false
47 $el.removeClass(d).removeAttr(d).prop(d, false)
52 Button.prototype.toggle = function () {
54 var $parent = this.$element.closest('[data-toggle="buttons"]')
57 var $input = this.$element.find('input')
58 if ($input.prop('type') == 'radio') {
59 if ($input.prop('checked')) changed = false
60 $parent.find('.active').removeClass('active')
61 this.$element.addClass('active')
62 } else if ($input.prop('type') == 'checkbox') {
63 if (($input.prop('checked')) !== this.$element.hasClass('active')) changed = false
64 this.$element.toggleClass('active')
66 $input.prop('checked', this.$element.hasClass('active'))
67 if (changed) $input.trigger('change')
69 this.$element.attr('aria-pressed', !this.$element.hasClass('active'))
70 this.$element.toggleClass('active')
75 // BUTTON PLUGIN DEFINITION
76 // ========================
78 function Plugin(option) {
79 return this.each(function () {
81 var data = $this.data('bs.button')
82 var options = typeof option == 'object' && option
84 if (!data) $this.data('bs.button', (data = new Button(this, options)))
86 if (option == 'toggle') data.toggle()
87 else if (option) data.setState(option)
94 $.fn.button.Constructor = Button
100 $.fn.button.noConflict = function () {
110 .on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) {
111 var $btn = $(e.target).closest('.btn')
112 Plugin.call($btn, 'toggle')
113 if (!($(e.target).is('input[type="radio"], input[type="checkbox"]'))) {
114 // Prevent double click on radios, and the double selections (so cancellation) on checkboxes
116 // The target component still receive the focus
117 if ($btn.is('input,button')) $btn.trigger('focus')
118 else $btn.find('input:visible,button:visible').first().trigger('focus')
121 .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) {
122 $(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type))