1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/button.js

108 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-05-21 22:30:33 -04:00
/* ========================================================================
2014-02-13 04:12:26 -05:00
* Bootstrap: button.js v3.1.1
* http://getbootstrap.com/javascript/#buttons
2013-05-21 22:30:33 -04:00
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2013-05-21 22:30:33 -04:00
* ======================================================================== */
2011-11-20 21:19:50 -05:00
2014-01-01 09:42:41 -05:00
+function ($) {
'use strict';
2013-05-16 14:06:30 -04:00
// BUTTON PUBLIC CLASS DEFINITION
// ==============================
var Button = function (element, options) {
2013-12-26 22:40:18 -05:00
this.$element = $(element)
this.options = $.extend({}, Button.DEFAULTS, options)
this.isLoading = false
2013-05-16 14:06:30 -04:00
}
Button.DEFAULTS = {
loadingText: 'loading...'
}
Button.prototype.setState = function (state) {
2013-05-16 14:06:30 -04:00
var d = 'disabled'
var $el = this.$element
var val = $el.is('input') ? 'val' : 'html'
var data = $el.data()
2011-11-20 21:19:50 -05:00
state = state + 'Text'
2013-05-16 14:06:30 -04:00
if (!data.resetText) $el.data('resetText', $el[val]())
2011-11-20 21:19:50 -05:00
$el[val](data[state] || this.options[state])
// push to event loop to allow forms to submit
2013-12-26 22:40:18 -05: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)
}
2011-11-20 21:19:50 -05:00
Button.prototype.toggle = function () {
var changed = true
2013-12-26 22:40:18 -05:00
var $parent = this.$element.closest('[data-toggle="buttons"]')
2011-11-24 17:43:26 -05:00
if ($parent.length) {
2013-08-02 18:13:12 -04:00
var $input = this.$element.find('input')
2013-12-26 22:40:18 -05:00
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')
2013-05-16 14:06:30 -04:00
}
2011-11-24 17:43:26 -05:00
if (changed) this.$element.toggleClass('active')
2011-11-20 21:19:50 -05:00
}
2013-05-16 14:06:30 -04:00
// BUTTON PLUGIN DEFINITION
// ========================
var old = $.fn.button
$.fn.button = function (option) {
2011-11-20 21:19:50 -05:00
return this.each(function () {
2013-05-16 14:06:30 -04:00
var $this = $(this)
2013-07-30 16:31:57 -04:00
var data = $this.data('bs.button')
2013-05-16 14:06:30 -04:00
var options = typeof option == 'object' && option
if (!data) $this.data('bs.button', (data = new Button(this, options)))
2013-05-16 14:06:30 -04:00
if (option == 'toggle') data.toggle()
else if (option) data.setState(option)
2011-11-20 21:19:50 -05:00
})
}
$.fn.button.Constructor = Button
2013-05-16 14:06:30 -04:00
// BUTTON NO CONFLICT
// ==================
$.fn.button.noConflict = function () {
$.fn.button = old
return this
}
2013-05-16 14:06:30 -04:00
// 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()
2011-11-20 21:19:50 -05:00
})
}(jQuery);