2 * bootstrap-toggle.js v1.0
3 * http://github.com/Nijikokun/bootstrap-toggle/
5 * http://twitter.com/nijikokun
6 * Copyright 2012 Nijiko Yonskai, Goodybag Inc
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 var Toggle = function (element, options) {
27 this.$element = $(element)
28 this.$checkbox = this.$element.children('.checkbox')
29 this.options = $.extend({}, $.fn.toggle.defaults, options)
31 // Initial Setup from options
32 if(this.options.text.enabled) this.$element.attr('data-enabled', this.options.text.enabled)
33 if(this.options.text.disabled) this.$element.attr('data-disabled', this.options.text.disabled)
36 this.setState(this.$checkbox.is(':checked'))
39 this.$element.click(function (e) {
40 if(self.options.onClick) self.options.onClick(e, self.$checkbox.is(':checked'))
45 Toggle.prototype.setState = function (state) {
46 // change checkbox state
47 this.$checkbox.attr('checked', state)
50 this.$element.removeClass('disabled')
51 if(this.options.style.disabled)
52 this.$element.removeClass('disabled-' + this.options.style.disabled)
53 if(this.options.style.enabled)
54 this.$element.addClass(this.options.style.enabled)
56 this.$element.addClass('disabled')
57 if(this.options.style.enabled)
58 this.$element.removeClass(this.options.style.enabled)
59 if(this.options.style.disabled)
60 this.$element.addClass('disabled-' + this.options.style.disabled)
64 Toggle.prototype.on = function () {
68 Toggle.prototype.off = function () {
72 Toggle.prototype.toggle = function () {
73 var status = this.$checkbox.is(':checked')
76 this.setState(!status)
82 $.fn.toggle = function (options) {
83 return new Toggle(this, typeof options == 'object' ? options : {})
86 $.fn.toggle.defaults = {
87 onClick: function () {},
98 $.fn.toggle.Constructor = Toggle