1 /*! ========================================================================
2 * Bootstrap Toggle: bootstrap-toggle.js v2.2.0
3 * http://www.bootstraptoggle.com
4 * ========================================================================
5 * Copyright 2014 Min Hur, The New York Times Company
7 * ======================================================================== */
13 // TOGGLE PUBLIC CLASS DEFINITION
14 // ==============================
16 var Toggle = function (element, options) {
17 this.$element = $(element)
18 this.options = $.extend({}, this.defaults(), options)
22 Toggle.VERSION = '2.2.0'
35 Toggle.prototype.defaults = function() {
37 on: this.$element.attr('data-on') || Toggle.DEFAULTS.on,
38 off: this.$element.attr('data-off') || Toggle.DEFAULTS.off,
39 onstyle: this.$element.attr('data-onstyle') || Toggle.DEFAULTS.onstyle,
40 offstyle: this.$element.attr('data-offstyle') || Toggle.DEFAULTS.offstyle,
41 size: this.$element.attr('data-size') || Toggle.DEFAULTS.size,
42 style: this.$element.attr('data-style') || Toggle.DEFAULTS.style,
43 width: this.$element.attr('data-width') || Toggle.DEFAULTS.width,
44 height: this.$element.attr('data-height') || Toggle.DEFAULTS.height
48 Toggle.prototype.render = function () {
49 this._onstyle = 'btn-' + this.options.onstyle
50 this._offstyle = 'btn-' + this.options.offstyle
51 var size = this.options.size === 'large' ? 'btn-lg'
52 : this.options.size === 'small' ? 'btn-sm'
53 : this.options.size === 'mini' ? 'btn-xs'
55 var $toggleOn = $('<label class="btn">').html(this.options.on)
56 .addClass(this._onstyle + ' ' + size)
57 var $toggleOff = $('<label class="btn">').html(this.options.off)
58 .addClass(this._offstyle + ' ' + size + ' active')
59 var $toggleHandle = $('<span class="toggle-handle btn btn-default">')
61 var $toggleGroup = $('<div class="toggle-group">')
62 .append($toggleOn, $toggleOff, $toggleHandle)
63 var $toggle = $('<div class="toggle btn" data-toggle="toggle">')
64 .addClass( this.$element.prop('checked') ? this._onstyle : this._offstyle+' off' )
65 .addClass(size).addClass(this.options.style)
67 this.$element.wrap($toggle)
69 $toggle: this.$element.parent(),
71 $toggleOff: $toggleOff,
72 $toggleGroup: $toggleGroup
74 this.$toggle.append($toggleGroup)
76 var width = this.options.width || Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth())+($toggleHandle.outerWidth()/2)
77 var height = this.options.height || Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight())
78 $toggleOn.addClass('toggle-on')
79 $toggleOff.addClass('toggle-off')
80 this.$toggle.css({ width: width, height: height })
81 if (this.options.height) {
82 $toggleOn.css('line-height', $toggleOn.height() + 'px')
83 $toggleOff.css('line-height', $toggleOff.height() + 'px')
89 Toggle.prototype.toggle = function () {
90 if (this.$element.prop('checked')) this.off()
94 Toggle.prototype.on = function (silent) {
95 if (this.$element.prop('disabled')) return false
96 this.$toggle.removeClass(this._offstyle + ' off').addClass(this._onstyle)
97 this.$element.prop('checked', true)
98 if (!silent) this.trigger()
101 Toggle.prototype.off = function (silent) {
102 if (this.$element.prop('disabled')) return false
103 this.$toggle.removeClass(this._onstyle).addClass(this._offstyle + ' off')
104 this.$element.prop('checked', false)
105 if (!silent) this.trigger()
108 Toggle.prototype.enable = function () {
109 this.$toggle.removeAttr('disabled')
110 this.$element.prop('disabled', false)
113 Toggle.prototype.disable = function () {
114 this.$toggle.attr('disabled', 'disabled')
115 this.$element.prop('disabled', true)
118 Toggle.prototype.update = function (silent) {
119 if (this.$element.prop('disabled')) this.disable()
121 if (this.$element.prop('checked')) this.on(silent)
122 else this.off(silent)
125 Toggle.prototype.trigger = function (silent) {
126 this.$element.off('change.bs.toggle')
127 if (!silent) this.$element.change()
128 this.$element.on('change.bs.toggle', $.proxy(function() {
133 Toggle.prototype.destroy = function() {
134 this.$element.off('change.bs.toggle')
135 this.$toggleGroup.remove()
136 this.$element.removeData('bs.toggle')
137 this.$element.unwrap()
140 // TOGGLE PLUGIN DEFINITION
141 // ========================
143 function Plugin(option) {
144 return this.each(function () {
146 var data = $this.data('bs.toggle')
147 var options = typeof option == 'object' && option
149 if (!data) $this.data('bs.toggle', (data = new Toggle(this, options)))
150 if (typeof option == 'string' && data[option]) data[option]()
154 var old = $.fn.bootstrapToggle
156 $.fn.bootstrapToggle = Plugin
157 $.fn.bootstrapToggle.Constructor = Toggle
159 // TOGGLE NO CONFLICT
160 // ==================
162 $.fn.toggle.noConflict = function () {
163 $.fn.bootstrapToggle = old
171 $('input[type=checkbox][data-toggle^=toggle]').bootstrapToggle()
174 $(document).on('click.bs.toggle', 'div[data-toggle^=toggle]', function(e) {
175 var $checkbox = $(this).find('input[type=checkbox]')
176 $checkbox.bootstrapToggle('toggle')