1
lorchess.ru/_assets/vendor/bootstrap/js/button.js

108 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-01-24 13:24:23 +04:00
/* ========================================================================
2014-04-17 22:36:17 +04:00
* Bootstrap: button.js v3.1.1
2014-01-24 13:24:23 +04:00
* http://getbootstrap.com/javascript/#buttons
* ========================================================================
2014-04-17 22:36:17 +04:00
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2014-01-24 13:24:23 +04:00
* ======================================================================== */
2014-04-17 22:36:17 +04:00
+function ($) {
'use strict';
2014-01-24 13:24:23 +04:00
// BUTTON PUBLIC CLASS DEFINITION
// ==============================
var Button = function (element, options) {
2014-04-17 22:36:17 +04:00
this.$element = $(element)
this.options = $.extend({}, Button.DEFAULTS, options)
this.isLoading = false
2014-01-24 13:24:23 +04:00
}
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 = state + 'Text'
if (!data.resetText) $el.data('resetText', $el[val]())
$el[val](data[state] || this.options[state])
// push to event loop to allow forms to submit
2014-04-17 22:36:17 +04:00
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)
2014-01-24 13:24:23 +04:00
}
Button.prototype.toggle = function () {
var changed = true
2014-04-17 22:36:17 +04:00
var $parent = this.$element.closest('[data-toggle="buttons"]')
2014-01-24 13:24:23 +04:00
if ($parent.length) {
var $input = this.$element.find('input')
2014-04-17 22:36:17 +04:00
if ($input.prop('type') == 'radio') {
if ($input.prop('checked') && this.$element.hasClass('active')) changed = false
else $parent.find('.active').removeClass('active')
2014-01-24 13:24:23 +04:00
}
if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
}
if (changed) this.$element.toggleClass('active')
}
// BUTTON PLUGIN DEFINITION
// ========================
var old = $.fn.button
$.fn.button = function (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 (option == 'toggle') data.toggle()
else if (option) data.setState(option)
})
}
$.fn.button.Constructor = Button
// BUTTON NO CONFLICT
// ==================
$.fn.button.noConflict = function () {
$.fn.button = old
return this
}
// 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')
$btn.button('toggle')
e.preventDefault()
})
}(jQuery);