0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/bootstrap-buttons.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2011-11-21 03:19:50 +01:00
/* ============================================================
2011-11-21 06:13:28 +01:00
* bootstrap-buttons.js v2.0.0
2011-11-21 03:19:50 +01:00
* http://twitter.github.com/bootstrap/javascript.html#buttons
* ============================================================
* Copyright 2011 Twitter, Inc.
*
* 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.
* ============================================================ */
!function( $ ){
"use strict"
/* BUTTON PUBLIC CLASS DEFINITION
* ============================== */
var Button
2011-11-21 03:19:50 +01:00
function setState(el, state) {
var d = 'disabled'
, $el = $(el)
, data = $el.data()
2011-11-24 23:43:26 +01:00
, val = $el.is('input') ? 'val' : 'html'
2011-11-21 03:19:50 +01:00
state = state + 'Text'
2011-11-24 23:43:26 +01:00
data.resetText || $el.data('resetText', $el[val]())
2011-11-21 03:19:50 +01:00
2011-11-24 23:43:26 +01:00
$el[val]( data[state] || $.fn.button.defaults[state] )
2011-11-21 03:19:50 +01:00
2011-11-24 23:43:26 +01:00
setTimeout(function () {
state == 'loadingText' ?
$el.addClass(d).attr(d, d) :
$el.removeClass(d).removeAttr(d)
}, 0)
2011-11-21 03:19:50 +01:00
}
function toggle(el) {
2011-11-24 23:43:26 +01:00
var $el = $(el)
, $parent = $el.parent('[data-toggle="buttons-radio"]')
$parent && $parent
.find('.active')
.removeClass('active')
2011-11-24 23:43:26 +01:00
$el.toggleClass('active')
2011-11-21 03:19:50 +01:00
}
/* BUTTON PLUGIN DEFINITION
* ======================== */
2011-11-21 03:19:50 +01:00
$.fn.button = function(options) {
return this.each(function () {
2011-11-24 23:43:26 +01:00
if (options == 'toggle') return toggle(this)
2011-11-21 03:19:50 +01:00
options && setState(this, options)
})
}
$.fn.button.defaults = {
loadingText: 'loading...'
}
$.fn.button.Button = Button
/* BUTTON DATA-API
* =============== */
2011-11-21 03:19:50 +01:00
$(function () {
$('body').delegate('[data-toggle^=button]', 'click.button.data-api', function (e) {
2011-11-24 23:43:26 +01:00
$(e.srcElement).button('toggle')
2011-11-21 03:19:50 +01:00
})
})
}( window.jQuery || window.ender );