diff options
| author | fat <[email protected]> | 2015-05-06 13:34:14 -0700 |
|---|---|---|
| committer | fat <[email protected]> | 2015-05-06 13:34:14 -0700 |
| commit | d1fbe200f46002431cdeebf965c4b789ef7ed267 (patch) | |
| tree | 43a7cc7667492e519b906f8a428935da2972ac14 /js/button.js | |
| parent | 09fb80568a52af6c440db971cdc6fd88eab8f8b5 (diff) | |
| download | bootstrap-d1fbe200f46002431cdeebf965c4b789ef7ed267.tar.xz bootstrap-d1fbe200f46002431cdeebf965c4b789ef7ed267.zip | |
remove closureness from plugins
Diffstat (limited to 'js/button.js')
| -rw-r--r-- | js/button.js | 279 |
1 files changed, 96 insertions, 183 deletions
diff --git a/js/button.js b/js/button.js index 8ee2d6b08..3e2e34512 100644 --- a/js/button.js +++ b/js/button.js @@ -1,207 +1,120 @@ -/** ======================================================================= - * Bootstrap: button.js v4.0.0 +/* ======================================================================== + * Bootstrap: button.js v3.3.4 * http://getbootstrap.com/javascript/#buttons * ======================================================================== * Copyright 2011-2015 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) - * ======================================================================== - * @fileoverview - Bootstrap's generic button component. - * - * Note (@fat): Deprecated "setState" – imo, better solutions for managing a - * buttons state should exist outside this plugin. - * - * Public Methods & Properties: - * - * + $.button - * + $.button.noConflict - * + $.button.Constructor - * + $.button.Constructor.VERSION - * + $.button.Constructor.prototype.toggle - * - * ======================================================================== - */ - -'use strict'; - - -/** - * Our Button class. - * @param {Element!} element - * @constructor - */ -var Button = function (element) { - - /** @private {Element} */ - this._element = element - -} - - -/** - * @const - * @type {string} - */ -Button['VERSION'] = '4.0.0' - - -/** - * @const - * @type {string} - * @private - */ -Button._NAME = 'button' - - -/** - * @const - * @type {string} - * @private - */ -Button._DATA_KEY = 'bs.button' - - -/** - * @const - * @type {Function} - * @private - */ -Button._JQUERY_NO_CONFLICT = $.fn[Button._NAME] - - -/** - * @const - * @enum {string} - * @private - */ -Button._ClassName = { - ACTIVE : 'active', - BUTTON : 'btn', - FOCUS : 'focus' -} - - -/** - * @const - * @enum {string} - * @private - */ -Button._Selector = { - DATA_TOGGLE_CARROT : '[data-toggle^="button"]', - DATA_TOGGLE : '[data-toggle="buttons"]', - INPUT : 'input', - ACTIVE : '.active', - BUTTON : '.btn' -} - - -/** - * Provides the jQuery Interface for the Button component. - * @param {string=} opt_config - * @this {jQuery} - * @return {jQuery} - * @private - */ -Button._jQueryInterface = function (opt_config) { - return this.each(function () { - var data = $(this).data(Button._DATA_KEY) - - if (!data) { - data = new Button(this) - $(this).data(Button._DATA_KEY, data) - } + * ======================================================================== */ - if (opt_config === 'toggle') { - data[opt_config]() - } - }) -} - - -/** - * Toggle's the button active state - */ -Button.prototype['toggle'] = function () { - var triggerChangeEvent = true - var rootElement = $(this._element).closest(Button._Selector.DATA_TOGGLE)[0] - - if (rootElement) { - var input = $(this._element).find(Button._Selector.INPUT)[0] - if (input) { - if (input.type == 'radio') { - if (input.checked && $(this._element).hasClass(Button._ClassName.ACTIVE)) { - triggerChangeEvent = false - } else { - var activeElement = $(rootElement).find(Button._Selector.ACTIVE)[0] - if (activeElement) { - $(activeElement).removeClass(Button._ClassName.ACTIVE) - } - } + ++function ($) { + 'use strict'; + + // BUTTON PUBLIC CLASS DEFINITION + // ============================== + + var Button = function (element, options) { + this.$element = $(element) + this.options = $.extend({}, Button.DEFAULTS, options) + this.isLoading = false + } + + Button.VERSION = '3.3.4' + + 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() + + state += 'Text' + + if (data.resetText == null) $el.data('resetText', $el[val]()) + + // push to event loop to allow forms to submit + setTimeout($.proxy(function () { + $el[val](data[state] == null ? this.options[state] : data[state]) + + 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) + } - if (triggerChangeEvent) { - input.checked = !$(this._element).hasClass(Button._ClassName.ACTIVE) - $(this._element).trigger('change') + 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')) changed = false + $parent.find('.active').removeClass('active') + this.$element.addClass('active') + } else if ($input.prop('type') == 'checkbox') { + if (($input.prop('checked')) !== this.$element.hasClass('active')) changed = false + this.$element.toggleClass('active') } + $input.prop('checked', this.$element.hasClass('active')) + if (changed) $input.trigger('change') + } else { + this.$element.attr('aria-pressed', !this.$element.hasClass('active')) + this.$element.toggleClass('active') } - } else { - this._element.setAttribute('aria-pressed', !$(this._element).hasClass(Button._ClassName.ACTIVE)) } - if (triggerChangeEvent) { - $(this._element).toggleClass(Button._ClassName.ACTIVE) - } -} + // BUTTON PLUGIN DEFINITION + // ======================== -/** - * ------------------------------------------------------------------------ - * jQuery Interface + noConflict implementaiton - * ------------------------------------------------------------------------ - */ + function Plugin(option) { + return this.each(function () { + var $this = $(this) + var data = $this.data('bs.button') + var options = typeof option == 'object' && option -/** - * @const - * @type {Function} - */ -$.fn[Button._NAME] = Button._jQueryInterface + if (!data) $this.data('bs.button', (data = new Button(this, options))) + if (option == 'toggle') data.toggle() + else if (option) data.setState(option) + }) + } -/** - * @const - * @type {Function} - */ -$.fn[Button._NAME]['Constructor'] = Button + var old = $.fn.button + $.fn.button = Plugin + $.fn.button.Constructor = Button -/** - * @const - * @type {Function} - */ -$.fn[Button._NAME]['noConflict'] = function () { - $.fn[Button._NAME] = Button._JQUERY_NO_CONFLICT - return this -} + // BUTTON NO CONFLICT + // ================== -/** - * ------------------------------------------------------------------------ - * Data Api implementation - * ------------------------------------------------------------------------ - */ + $.fn.button.noConflict = function () { + $.fn.button = old + return this + } -$(document) - .on('click.bs.button.data-api', Button._Selector.DATA_TOGGLE_CARROT, function (event) { - event.preventDefault() - var button = event.target + // BUTTON DATA-API + // =============== - if (!$(button).hasClass(Button._ClassName.BUTTON)) { - button = $(button).closest(Button._Selector.BUTTON) - } + $(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') + if (!($(e.target).is('input[type="radio"]') || $(e.target).is('input[type="checkbox"]'))) e.preventDefault() + }) + .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) { + $(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type)) + }) - Button._jQueryInterface.call($(button), 'toggle') - }) - .on('focus.bs.button.data-api blur.bs.button.data-api', Button._Selector.DATA_TOGGLE_CARROT, function (event) { - var button = $(event.target).closest(Button._Selector.BUTTON)[0] - $(button).toggleClass(Button._ClassName.FOCUS, /^focus(in)?$/.test(event.type)) - }) +}(jQuery); |
