js/bootstrap-button.js
author indvd00m (gotoindvdum[at]gmail[dot]com)
Fri, 04 Jul 2014 16:42:41 +0400
changeset 0 ba8ab09f730e
permissions -rw-r--r--
First home page
     1 /* ============================================================
     2  * bootstrap-button.js v2.3.1
     3  * http://twitter.github.com/bootstrap/javascript.html#buttons
     4  * ============================================================
     5  * Copyright 2012 Twitter, Inc.
     6  *
     7  * Licensed under the Apache License, Version 2.0 (the "License");
     8  * you may not use this file except in compliance with the License.
     9  * You may obtain a copy of the License at
    10  *
    11  * http://www.apache.org/licenses/LICENSE-2.0
    12  *
    13  * Unless required by applicable law or agreed to in writing, software
    14  * distributed under the License is distributed on an "AS IS" BASIS,
    15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  * See the License for the specific language governing permissions and
    17  * limitations under the License.
    18  * ============================================================ */
    19 
    20 
    21 !function ($) {
    22 
    23   "use strict"; // jshint ;_;
    24 
    25 
    26  /* BUTTON PUBLIC CLASS DEFINITION
    27   * ============================== */
    28 
    29   var Button = function (element, options) {
    30     this.$element = $(element)
    31     this.options = $.extend({}, $.fn.button.defaults, options)
    32   }
    33 
    34   Button.prototype.setState = function (state) {
    35     var d = 'disabled'
    36       , $el = this.$element
    37       , data = $el.data()
    38       , val = $el.is('input') ? 'val' : 'html'
    39 
    40     state = state + 'Text'
    41     data.resetText || $el.data('resetText', $el[val]())
    42 
    43     $el[val](data[state] || this.options[state])
    44 
    45     // push to event loop to allow forms to submit
    46     setTimeout(function () {
    47       state == 'loadingText' ?
    48         $el.addClass(d).attr(d, d) :
    49         $el.removeClass(d).removeAttr(d)
    50     }, 0)
    51   }
    52 
    53   Button.prototype.toggle = function () {
    54     var $parent = this.$element.closest('[data-toggle="buttons-radio"]')
    55 
    56     $parent && $parent
    57       .find('.active')
    58       .removeClass('active')
    59 
    60     this.$element.toggleClass('active')
    61   }
    62 
    63 
    64  /* BUTTON PLUGIN DEFINITION
    65   * ======================== */
    66 
    67   var old = $.fn.button
    68 
    69   $.fn.button = function (option) {
    70     return this.each(function () {
    71       var $this = $(this)
    72         , data = $this.data('button')
    73         , options = typeof option == 'object' && option
    74       if (!data) $this.data('button', (data = new Button(this, options)))
    75       if (option == 'toggle') data.toggle()
    76       else if (option) data.setState(option)
    77     })
    78   }
    79 
    80   $.fn.button.defaults = {
    81     loadingText: 'loading...'
    82   }
    83 
    84   $.fn.button.Constructor = Button
    85 
    86 
    87  /* BUTTON NO CONFLICT
    88   * ================== */
    89 
    90   $.fn.button.noConflict = function () {
    91     $.fn.button = old
    92     return this
    93   }
    94 
    95 
    96  /* BUTTON DATA-API
    97   * =============== */
    98 
    99   $(document).on('click.button.data-api', '[data-toggle^=button]', function (e) {
   100     var $btn = $(e.target)
   101     if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
   102     $btn.button('toggle')
   103   })
   104 
   105 }(window.jQuery);