1 /*! ========================================================================
2 * Bootstrap Toggle: bootstrap-toggle.js v2.0.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 = '3.0.0'
33 Toggle.prototype.defaults = function() {
35 on: this.$element.attr('data-on') || Toggle.DEFAULTS.on,
36 off: this.$element.attr('data-off') || Toggle.DEFAULTS.off,
37 onstyle: this.$element.attr('data-onstyle') || Toggle.DEFAULTS.onstyle,
38 offstyle: this.$element.attr('data-offstyle') || Toggle.DEFAULTS.offstyle,
39 size: this.$element.attr('data-size') || Toggle.DEFAULTS.size,
40 style: this.$element.attr('data-style') || Toggle.DEFAULTS.style
44 Toggle.prototype.render = function () {
45 this._onstyle = 'btn-' + this.options.onstyle
46 this._offstyle = 'btn-' + this.options.offstyle
47 var size = this.options.size === 'large' ? 'btn-lg'
48 : this.options.size === 'small' ? 'btn-sm'
49 : this.options.size === 'mini' ? 'btn-xs'
51 var $toggleOn = $('<label class="btn">').html(this.options.on)
52 .addClass(this._onstyle + ' ' + size)
53 var $toggleOff = $('<label class="btn">').html(this.options.off)
54 .addClass(this._offstyle + ' ' + size + ' active')
55 var $toggleHandle = $('<span class="toggle-handle btn btn-default">')
57 var $toggleGroup = $('<div class="toggle-group">')
58 .append($toggleOn, $toggleOff, $toggleHandle)
59 var $toggle = $('<div class="toggle btn" data-toggle="toggle">')
60 .addClass( this.$element.prop('checked') ? this._onstyle : this._offstyle+' off' )
61 .addClass(size).addClass(this.options.style)
63 this.$element.wrap($toggle)
65 $toggle: this.$element.parent(),
67 $toggleOff: $toggleOff,
68 $toggleGroup: $toggleGroup
70 this.$toggle.append($toggleGroup)
72 var width = Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth())+($toggleHandle.outerWidth()/2)
73 var height = Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight())
74 $toggleOn.addClass('toggle-on')
75 $toggleOff.addClass('toggle-off')
76 this.$toggle.css({ width: width, height: height })
81 Toggle.prototype.toggle = function () {
82 if (this.$element.prop('checked')) this.off()
86 Toggle.prototype.on = function () {
87 if (this.$element.prop('disabled')) return false
88 this.$toggle.removeClass(this._offstyle + ' off').addClass(this._onstyle)
89 this.$element.prop('checked', true)
93 Toggle.prototype.off = function () {
94 if (this.$element.prop('disabled')) return false
95 this.$toggle.removeClass(this._onstyle).addClass(this._offstyle + ' off')
96 this.$element.prop('checked', false)
100 Toggle.prototype.enable = function () {
101 this.$toggle.removeAttr('disabled')
102 this.$element.prop('disabled', false)
105 Toggle.prototype.disable = function () {
106 this.$toggle.attr('disabled', 'disabled')
107 this.$element.prop('disabled', true)
110 Toggle.prototype.update = function () {
111 if (this.$element.prop('disabled')) this.disable()
113 if (this.$element.prop('checked')) this.on()
117 Toggle.prototype.trigger = function (silent) {
118 this.$element.off('change.bs.toggle')
119 if (!silent) this.$element.change()
120 this.$element.on('change.bs.toggle', $.proxy(function() {
125 Toggle.prototype.destroy = function() {
126 this.$element.off('change.bs.toggle')
127 this.$toggleGroup.remove()
128 this.$element.removeData('bs.toggle')
129 this.$element.unwrap()
132 // TOGGLE PLUGIN DEFINITION
133 // ========================
135 function Plugin(option) {
136 return this.each(function () {
138 var data = $this.data('bs.toggle')
139 var options = typeof option == 'object' && option
141 if (!data) $this.data('bs.toggle', (data = new Toggle(this, options)))
142 if (typeof option == 'string' && data[option]) data[option]()
146 var old = $.fn.bootstrapToggle
148 $.fn.bootstrapToggle = Plugin
149 $.fn.bootstrapToggle.Constructor = Toggle
151 // TOGGLE NO CONFLICT
152 // ==================
154 $.fn.toggle.noConflict = function () {
155 $.fn.bootstrapToggle = old
163 $('input[type=checkbox][data-toggle^=toggle]').bootstrapToggle()
166 $(document).on('click.bs.toggle', 'div[data-toggle^=toggle]', function(e) {
167 var $checkbox = $(this).find('input[type=checkbox]')
168 $checkbox.bootstrapToggle('toggle')