0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-10 22:24:19 +01:00
Bootstrap/js/button.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-05-22 04:30:33 +02:00
/* ========================================================================
2015-01-19 17:32:13 +01:00
* Bootstrap: button.js v3.3.2
* http://getbootstrap.com/javascript/#buttons
2013-05-22 04:30:33 +02:00
* ========================================================================
2015-01-01 01:23:48 +01:00
* Copyright 2011-2015 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2013-05-22 04:30:33 +02:00
* ======================================================================== */
2011-11-21 03:19:50 +01:00
+function ($) {
'use strict';
2014-06-10 07:18:05 +02:00
// BUTTON PUBLIC CLASS DEFINITION
// ==============================
var Button = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, Button.DEFAULTS, options)
this.isLoading = false
}
2015-01-19 17:32:13 +01:00
Button.VERSION = '3.3.2'
Button.DEFAULTS = {
loadingText: 'loading...'
}
2011-11-21 03:19:50 +01:00
Button.prototype.setState = function (state) {
var d = 'disabled'
var $el = this.$element
var val = $el.is('input') ? 'val' : 'html'
var data = $el.data()
2013-05-16 20:06:30 +02:00
state = state + 'Text'
2011-11-21 03:19:50 +01:00
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)
}
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')
} else {
this.$element.attr('aria-pressed', !this.$element.hasClass('active'))
2013-05-16 20:06:30 +02:00
}
2011-11-24 23:43:26 +01:00
if (changed) this.$element.toggleClass('active')
}
2011-11-21 03:19:50 +01:00
// 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
2013-05-16 20:06:30 +02:00
if (!data) $this.data('bs.button', (data = new Button(this, options)))
2013-05-16 20:06:30 +02:00
if (option == 'toggle') data.toggle()
else if (option) data.setState(option)
})
}
2011-11-21 03:19:50 +01:00
var old = $.fn.button
$.fn.button = Plugin
$.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')
Plugin.call($btn, 'toggle')
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))
})
2011-11-21 03:19:50 +01:00
}(jQuery);