github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/assets/bootstrap-3.1.1/js/button.js (about) 1 /* ======================================================================== 2 * Bootstrap: button.js v3.1.1 3 * http://getbootstrap.com/javascript/#buttons 4 * ======================================================================== 5 * Copyright 2011-2014 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.DEFAULTS = { 23 loadingText: 'loading...' 24 } 25 26 Button.prototype.setState = function (state) { 27 var d = 'disabled' 28 var $el = this.$element 29 var val = $el.is('input') ? 'val' : 'html' 30 var data = $el.data() 31 32 state = state + 'Text' 33 34 if (!data.resetText) $el.data('resetText', $el[val]()) 35 36 $el[val](data[state] || this.options[state]) 37 38 // push to event loop to allow forms to submit 39 setTimeout($.proxy(function () { 40 if (state == 'loadingText') { 41 this.isLoading = true 42 $el.addClass(d).attr(d, d) 43 } else if (this.isLoading) { 44 this.isLoading = false 45 $el.removeClass(d).removeAttr(d) 46 } 47 }, this), 0) 48 } 49 50 Button.prototype.toggle = function () { 51 var changed = true 52 var $parent = this.$element.closest('[data-toggle="buttons"]') 53 54 if ($parent.length) { 55 var $input = this.$element.find('input') 56 if ($input.prop('type') == 'radio') { 57 if ($input.prop('checked') && this.$element.hasClass('active')) changed = false 58 else $parent.find('.active').removeClass('active') 59 } 60 if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change') 61 } 62 63 if (changed) this.$element.toggleClass('active') 64 } 65 66 67 // BUTTON PLUGIN DEFINITION 68 // ======================== 69 70 var old = $.fn.button 71 72 $.fn.button = function (option) { 73 return this.each(function () { 74 var $this = $(this) 75 var data = $this.data('bs.button') 76 var options = typeof option == 'object' && option 77 78 if (!data) $this.data('bs.button', (data = new Button(this, options))) 79 80 if (option == 'toggle') data.toggle() 81 else if (option) data.setState(option) 82 }) 83 } 84 85 $.fn.button.Constructor = Button 86 87 88 // BUTTON NO CONFLICT 89 // ================== 90 91 $.fn.button.noConflict = function () { 92 $.fn.button = old 93 return this 94 } 95 96 97 // BUTTON DATA-API 98 // =============== 99 100 $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) { 101 var $btn = $(e.target) 102 if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn') 103 $btn.button('toggle') 104 e.preventDefault() 105 }) 106 107 }(jQuery);