diff options
| author | fat <[email protected]> | 2014-06-12 11:11:04 -0700 |
|---|---|---|
| committer | fat <[email protected]> | 2014-06-12 11:11:04 -0700 |
| commit | 2b302f69eea416bc85e7827b7d7a74d49f879662 (patch) | |
| tree | 6cdc496ae2264e96ee9e1bfa0b48b0d4cbd6253e /js/button.js | |
| parent | 696632d7dac91dba3f83c056263b527c56b63991 (diff) | |
| download | bootstrap-2b302f69eea416bc85e7827b7d7a74d49f879662.tar.xz bootstrap-2b302f69eea416bc85e7827b7d7a74d49f879662.zip | |
some changes from #13801 - add strict mode back and ==
Diffstat (limited to 'js/button.js')
| -rw-r--r-- | js/button.js | 156 |
1 files changed, 79 insertions, 77 deletions
diff --git a/js/button.js b/js/button.js index c05c5e3dd..e96788c05 100644 --- a/js/button.js +++ b/js/button.js @@ -7,108 +7,110 @@ * ======================================================================== */ -(function (o_o) { - typeof define === 'function' && define.amd ? define(['jquery'], o_o) : - typeof exports === 'object' ? o_o(require('jquery')) : o_o(jQuery) -})(function ($) { ++function () { 'use strict'; - 'use strict'; + (function (o_o) { + typeof define == 'function' && define.amd ? define(['jquery'], o_o) : + typeof exports == 'object' ? o_o(require('jquery')) : o_o(jQuery) + })(function ($) { - // BUTTON PUBLIC CLASS DEFINITION - // ============================== + // BUTTON PUBLIC CLASS DEFINITION + // ============================== - var Button = function (element, options) { - this.$element = $(element) - this.options = $.extend({}, Button.DEFAULTS, options) - this.isLoading = false - } + var Button = function (element, options) { + this.$element = $(element) + this.options = $.extend({}, Button.DEFAULTS, options) + this.isLoading = false + } - Button.VERSION = '3.1.1' + Button.VERSION = '3.1.1' - Button.DEFAULTS = { - loadingText: 'loading...' - } + Button.DEFAULTS = { + loadingText: 'loading...' + } - Button.prototype.setState = function (state) { - var d = 'disabled' - var $el = this.$element - var val = $el.is('input') ? 'val' : 'html' - var data = $el.data() + Button.prototype.setState = function (state) { + var d = 'disabled' + var $el = this.$element + var val = $el.is('input') ? 'val' : 'html' + var data = $el.data() - state = state + 'Text' + state = state + 'Text' - if (data.resetText == null) $el.data('resetText', $el[val]()) + if (data.resetText == null) $el.data('resetText', $el[val]()) - $el[val](data[state] == null ? this.options[state] : data[state]) + $el[val](data[state] == null ? this.options[state] : data[state]) - // push to event loop to allow forms to submit - setTimeout($.proxy(function () { - if (state == 'loadingText') { - this.isLoading = true - $el.addClass(d).attr(d, d) - } else if (this.isLoading) { - this.isLoading = false - $el.removeClass(d).removeAttr(d) - } - }, this), 0) - } - - Button.prototype.toggle = function () { - var changed = true - var $parent = this.$element.closest('[data-toggle="buttons"]') - - if ($parent.length) { - var $input = this.$element.find('input') - if ($input.prop('type') == 'radio') { - if ($input.prop('checked') && this.$element.hasClass('active')) changed = false - else $parent.find('.active').removeClass('active') + // push to event loop to allow forms to submit + setTimeout($.proxy(function () { + if (state == 'loadingText') { + this.isLoading = true + $el.addClass(d).attr(d, d) + } else if (this.isLoading) { + this.isLoading = false + $el.removeClass(d).removeAttr(d) + } + }, this), 0) + } + + Button.prototype.toggle = function () { + var changed = true + var $parent = this.$element.closest('[data-toggle="buttons"]') + + if ($parent.length) { + var $input = this.$element.find('input') + if ($input.prop('type') == 'radio') { + if ($input.prop('checked') && this.$element.hasClass('active')) changed = false + else $parent.find('.active').removeClass('active') + } + if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change') } - if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change') + + if (changed) this.$element.toggleClass('active') } - if (changed) this.$element.toggleClass('active') - } + // BUTTON PLUGIN DEFINITION + // ======================== - // BUTTON PLUGIN DEFINITION - // ======================== + function Plugin(option) { + return this.each(function () { + var $this = $(this) + var data = $this.data('bs.button') + var options = typeof option == 'object' && option - function Plugin(option) { - return this.each(function () { - var $this = $(this) - var data = $this.data('bs.button') - var options = typeof option == 'object' && option + if (!data) $this.data('bs.button', (data = new Button(this, options))) - if (!data) $this.data('bs.button', (data = new Button(this, options))) + if (option == 'toggle') data.toggle() + else if (option) data.setState(option) + }) + } - if (option == 'toggle') data.toggle() - else if (option) data.setState(option) - }) - } + var old = $.fn.button - var old = $.fn.button + $.fn.button = Plugin + $.fn.button.Constructor = Button - $.fn.button = Plugin - $.fn.button.Constructor = Button + // BUTTON NO CONFLICT + // ================== - // BUTTON NO CONFLICT - // ================== + $.fn.button.noConflict = function () { + $.fn.button = old + return this + } - $.fn.button.noConflict = function () { - $.fn.button = old - return this - } + // BUTTON DATA-API + // =============== - // BUTTON DATA-API - // =============== + $(document).on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) { + var $btn = $(e.target) + if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn') + Plugin.call($btn, 'toggle') + e.preventDefault() + }) - $(document).on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) { - var $btn = $(e.target) - if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn') - Plugin.call($btn, 'toggle') - e.preventDefault() }) -}); +}(); |
