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