assets/javascripts/bootstrap/button.js
changeset 114 af15590802a4
equal deleted inserted replaced
113:d4ec02c51c91 114:af15590802a4
       
     1 /* ========================================================================
       
     2  * Bootstrap: button.js v3.3.5
       
     3  * http://getbootstrap.com/javascript/#buttons
       
     4  * ========================================================================
       
     5  * Copyright 2011-2015 Twitter, Inc.
       
     6  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
       
     7  * ======================================================================== */
       
     8 
       
     9 
       
    10 +function ($) {
       
    11   'use strict';
       
    12 
       
    13   // BUTTON PUBLIC CLASS DEFINITION
       
    14   // ==============================
       
    15 
       
    16   var Button = function (element, options) {
       
    17     this.$element  = $(element)
       
    18     this.options   = $.extend({}, Button.DEFAULTS, options)
       
    19     this.isLoading = false
       
    20   }
       
    21 
       
    22   Button.VERSION  = '3.3.5'
       
    23 
       
    24   Button.DEFAULTS = {
       
    25     loadingText: 'loading...'
       
    26   }
       
    27 
       
    28   Button.prototype.setState = function (state) {
       
    29     var d    = 'disabled'
       
    30     var $el  = this.$element
       
    31     var val  = $el.is('input') ? 'val' : 'html'
       
    32     var data = $el.data()
       
    33 
       
    34     state += 'Text'
       
    35 
       
    36     if (data.resetText == null) $el.data('resetText', $el[val]())
       
    37 
       
    38     // push to event loop to allow forms to submit
       
    39     setTimeout($.proxy(function () {
       
    40       $el[val](data[state] == null ? this.options[state] : data[state])
       
    41 
       
    42       if (state == 'loadingText') {
       
    43         this.isLoading = true
       
    44         $el.addClass(d).attr(d, d)
       
    45       } else if (this.isLoading) {
       
    46         this.isLoading = false
       
    47         $el.removeClass(d).removeAttr(d)
       
    48       }
       
    49     }, this), 0)
       
    50   }
       
    51 
       
    52   Button.prototype.toggle = function () {
       
    53     var changed = true
       
    54     var $parent = this.$element.closest('[data-toggle="buttons"]')
       
    55 
       
    56     if ($parent.length) {
       
    57       var $input = this.$element.find('input')
       
    58       if ($input.prop('type') == 'radio') {
       
    59         if ($input.prop('checked')) changed = false
       
    60         $parent.find('.active').removeClass('active')
       
    61         this.$element.addClass('active')
       
    62       } else if ($input.prop('type') == 'checkbox') {
       
    63         if (($input.prop('checked')) !== this.$element.hasClass('active')) changed = false
       
    64         this.$element.toggleClass('active')
       
    65       }
       
    66       $input.prop('checked', this.$element.hasClass('active'))
       
    67       if (changed) $input.trigger('change')
       
    68     } else {
       
    69       this.$element.attr('aria-pressed', !this.$element.hasClass('active'))
       
    70       this.$element.toggleClass('active')
       
    71     }
       
    72   }
       
    73 
       
    74 
       
    75   // BUTTON PLUGIN DEFINITION
       
    76   // ========================
       
    77 
       
    78   function Plugin(option) {
       
    79     return this.each(function () {
       
    80       var $this   = $(this)
       
    81       var data    = $this.data('bs.button')
       
    82       var options = typeof option == 'object' && option
       
    83 
       
    84       if (!data) $this.data('bs.button', (data = new Button(this, options)))
       
    85 
       
    86       if (option == 'toggle') data.toggle()
       
    87       else if (option) data.setState(option)
       
    88     })
       
    89   }
       
    90 
       
    91   var old = $.fn.button
       
    92 
       
    93   $.fn.button             = Plugin
       
    94   $.fn.button.Constructor = Button
       
    95 
       
    96 
       
    97   // BUTTON NO CONFLICT
       
    98   // ==================
       
    99 
       
   100   $.fn.button.noConflict = function () {
       
   101     $.fn.button = old
       
   102     return this
       
   103   }
       
   104 
       
   105 
       
   106   // BUTTON DATA-API
       
   107   // ===============
       
   108 
       
   109   $(document)
       
   110     .on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) {
       
   111       var $btn = $(e.target)
       
   112       if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
       
   113       Plugin.call($btn, 'toggle')
       
   114       if (!($(e.target).is('input[type="radio"]') || $(e.target).is('input[type="checkbox"]'))) e.preventDefault()
       
   115     })
       
   116     .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) {
       
   117       $(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type))
       
   118     })
       
   119 
       
   120 }(jQuery);