ffffng/app/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/button.js

126 lines
3.7 KiB
JavaScript
Raw Normal View History

2014-05-12 20:08:19 +02:00
/* ========================================================================
2019-03-29 22:00:08 +01:00
* Bootstrap: button.js v3.4.1
* https://getbootstrap.com/docs/3.4/javascript/#buttons
2014-05-12 20:08:19 +02:00
* ========================================================================
2019-03-29 22:00:08 +01:00
* Copyright 2011-2019 Twitter, Inc.
2014-05-12 20:08:19 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// BUTTON PUBLIC CLASS DEFINITION
// ==============================
var Button = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, Button.DEFAULTS, options)
this.isLoading = false
}
2019-03-29 22:00:08 +01:00
Button.VERSION = '3.4.1'
2014-08-10 12:02:48 +02:00
2014-05-12 20:08:19 +02: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()
2016-05-16 13:33:49 +02:00
state += 'Text'
2014-05-12 20:08:19 +02:00
2014-08-10 12:02:48 +02:00
if (data.resetText == null) $el.data('resetText', $el[val]())
2014-05-12 20:08:19 +02:00
// push to event loop to allow forms to submit
setTimeout($.proxy(function () {
2016-05-16 13:33:49 +02:00
$el[val](data[state] == null ? this.options[state] : data[state])
2014-05-12 20:08:19 +02:00
if (state == 'loadingText') {
this.isLoading = true
2017-05-13 13:25:33 +02:00
$el.addClass(d).attr(d, d).prop(d, true)
2014-05-12 20:08:19 +02:00
} else if (this.isLoading) {
this.isLoading = false
2017-05-13 13:25:33 +02:00
$el.removeClass(d).removeAttr(d).prop(d, false)
2014-05-12 20:08:19 +02:00
}
}, 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') {
2016-05-16 13:33:49 +02:00
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')
2014-05-12 20:08:19 +02:00
}
2016-05-16 13:33:49 +02:00
$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')
2014-05-12 20:08:19 +02:00
}
}
// BUTTON PLUGIN DEFINITION
// ========================
2014-08-10 12:02:48 +02:00
function Plugin(option) {
2014-05-12 20:08:19 +02:00
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)
})
}
2014-08-10 12:02:48 +02:00
var old = $.fn.button
$.fn.button = Plugin
2014-05-12 20:08:19 +02:00
$.fn.button.Constructor = Button
// BUTTON NO CONFLICT
// ==================
$.fn.button.noConflict = function () {
$.fn.button = old
return this
}
// BUTTON DATA-API
// ===============
2016-05-16 13:33:49 +02:00
$(document)
.on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) {
2017-05-13 13:25:33 +02:00
var $btn = $(e.target).closest('.btn')
2016-05-16 13:33:49 +02:00
Plugin.call($btn, 'toggle')
2017-05-13 13:25:33 +02:00
if (!($(e.target).is('input[type="radio"], input[type="checkbox"]'))) {
// Prevent double click on radios, and the double selections (so cancellation) on checkboxes
e.preventDefault()
// The target component still receive the focus
if ($btn.is('input,button')) $btn.trigger('focus')
else $btn.find('input:visible,button:visible').first().trigger('focus')
}
2016-05-16 13:33:49 +02:00
})
.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))
})
2014-05-12 20:08:19 +02:00
}(jQuery);