twbs--bootstrap/js/button.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-05-22 02:30:33 +00:00
/* ========================================================================
2013-05-16 18:06:30 +00:00
* Bootstrap: button.js v3.0.0
2013-07-25 19:24:13 +00:00
* http://twbs.github.com/bootstrap/javascript.html#buttons
2013-05-22 02:30:33 +00:00
* ========================================================================
2013-05-16 18:06:30 +00:00
* Copyright 2013 Twitter, Inc.
2011-11-21 02:19:50 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2013-05-22 02:30:33 +00:00
* ======================================================================== */
2011-11-21 02:19:50 +00:00
2013-05-22 02:30:33 +00:00
+function ($) { "use strict";
2013-05-16 18:06:30 +00:00
// BUTTON PUBLIC CLASS DEFINITION
// ==============================
var Button = function (element, options) {
this.$element = $(element)
2013-05-16 18:06:30 +00:00
this.options = $.extend({}, Button.DEFAULTS, options)
}
Button.DEFAULTS = {
loadingText: 'loading...'
}
Button.prototype.setState = function (state) {
2013-05-16 18:06:30 +00:00
var d = 'disabled'
var $el = this.$element
var val = $el.is('input') ? 'val' : 'html'
var data = $el.data()
2011-11-21 02:19:50 +00:00
state = state + 'Text'
2013-05-16 18:06:30 +00:00
if (!data.resetText) $el.data('resetText', $el[val]())
2011-11-21 02:19:50 +00:00
$el[val](data[state] || this.options[state])
// push to event loop to allow forms to submit
setTimeout(function () {
state == 'loadingText' ?
$el.addClass(d).attr(d, d) :
2013-05-16 18:06:30 +00:00
$el.removeClass(d).removeAttr(d);
}, 0)
}
2011-11-21 02:19:50 +00:00
Button.prototype.toggle = function () {
var $parent = this.$element.closest('[data-toggle="buttons"]')
2011-11-24 22:43:26 +00:00
if ($parent.length) {
2013-08-02 22:13:12 +00:00
var $input = this.$element.find('input')
.prop('checked', !this.$element.hasClass('active'))
.trigger('change')
if ($input.prop('type') === 'radio') $parent.find('.active').removeClass('active')
2013-05-16 18:06:30 +00:00
}
2011-11-24 22:43:26 +00:00
this.$element.toggleClass('active')
2011-11-21 02:19:50 +00:00
}
2013-05-16 18:06:30 +00:00
// BUTTON PLUGIN DEFINITION
// ========================
var old = $.fn.button
$.fn.button = function (option) {
2011-11-21 02:19:50 +00:00
return this.each(function () {
2013-05-16 18:06:30 +00:00
var $this = $(this)
2013-07-30 20:31:57 +00:00
var data = $this.data('bs.button')
2013-05-16 18:06:30 +00:00
var options = typeof option == 'object' && option
if (!data) $this.data('bs.button', (data = new Button(this, options)))
2013-05-16 18:06:30 +00:00
if (option == 'toggle') data.toggle()
else if (option) data.setState(option)
2011-11-21 02:19:50 +00:00
})
}
$.fn.button.Constructor = Button
2013-05-16 18:06:30 +00:00
// BUTTON NO CONFLICT
// ==================
$.fn.button.noConflict = function () {
$.fn.button = old
return this
}
2013-05-16 18:06:30 +00: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-21 02:19:50 +00:00
})
}(window.jQuery);