├── logo
├── logo.png
├── vertical.png
├── horizontal.png
└── logo.svg
├── dist
└── js
│ ├── i18n
│ ├── bg.js
│ ├── es.js
│ ├── pl.js
│ ├── ru.js
│ ├── tr.js
│ └── pt.js
│ ├── bootstrap-checkbox.min.js
│ └── bootstrap-checkbox.js
├── js
├── i18n
│ ├── bg.js
│ ├── es.js
│ ├── pl.js
│ ├── pt.js
│ ├── ru.js
│ └── tr.js
└── bootstrap-checkbox.js
└── LICENSE
/logo/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vsn4ik/bootstrap-checkbox/HEAD/logo/logo.png
--------------------------------------------------------------------------------
/logo/vertical.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vsn4ik/bootstrap-checkbox/HEAD/logo/vertical.png
--------------------------------------------------------------------------------
/logo/horizontal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vsn4ik/bootstrap-checkbox/HEAD/logo/horizontal.png
--------------------------------------------------------------------------------
/dist/js/i18n/bg.js:
--------------------------------------------------------------------------------
1 | (function ($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Не',
4 | onLabel: 'Да',
5 | warningMessage: 'Bootstrap-checkbox не поддържа checkbox в label елемент.'
6 | });
7 | })(jQuery);
--------------------------------------------------------------------------------
/js/i18n/bg.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Не',
4 | onLabel: 'Да',
5 | warningMessage: 'Bootstrap-checkbox не поддържа checkbox в label елемент.'
6 | });
7 | })(jQuery);
8 |
--------------------------------------------------------------------------------
/dist/js/i18n/es.js:
--------------------------------------------------------------------------------
1 | (function ($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'No',
4 | onLabel: 'Si',
5 | warningMessage: 'Por favor, no utilice Bootstrap-checkbox para elementos tipo label.'
6 | });
7 | })(jQuery);
--------------------------------------------------------------------------------
/dist/js/i18n/pl.js:
--------------------------------------------------------------------------------
1 | (function ($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Nie',
4 | onLabel: 'Tak',
5 | warningMessage: 'Nie używaj elementu Bootstrap-checkbox wewnątrz elementu label.'
6 | });
7 | })(jQuery);
--------------------------------------------------------------------------------
/js/i18n/es.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'No',
4 | onLabel: 'Si',
5 | warningMessage: 'Por favor, no utilice Bootstrap-checkbox para elementos tipo label.'
6 | });
7 | })(jQuery);
8 |
--------------------------------------------------------------------------------
/js/i18n/pl.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Nie',
4 | onLabel: 'Tak',
5 | warningMessage: 'Nie używaj elementu Bootstrap-checkbox wewnątrz elementu label.'
6 | });
7 | })(jQuery);
8 |
--------------------------------------------------------------------------------
/dist/js/i18n/ru.js:
--------------------------------------------------------------------------------
1 | (function ($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Нет',
4 | onLabel: 'Да',
5 | warningMessage: 'Bootstrap-checkbox не поддерживает использование внутри label элемента.'
6 | });
7 | })(jQuery);
--------------------------------------------------------------------------------
/dist/js/i18n/tr.js:
--------------------------------------------------------------------------------
1 | (function ($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Pasif',
4 | onLabel: 'Aktif',
5 | warningMessage: 'Lütfen label elementi içerisinde Bootstrap-checkbox kullanmayınız.'
6 | });
7 | })(jQuery);
--------------------------------------------------------------------------------
/js/i18n/pt.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Não',
4 | onLabel: 'Sim',
5 | warningMessage: 'Elementos do tipo label não são suportados dentro do Bootstrap-checkbox.'
6 | });
7 | })(jQuery);
8 |
--------------------------------------------------------------------------------
/js/i18n/ru.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Нет',
4 | onLabel: 'Да',
5 | warningMessage: 'Bootstrap-checkbox не поддерживает использование внутри label элемента.'
6 | });
7 | })(jQuery);
8 |
--------------------------------------------------------------------------------
/js/i18n/tr.js:
--------------------------------------------------------------------------------
1 | (function($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Pasif',
4 | onLabel: 'Aktif',
5 | warningMessage: 'Lütfen label elementi içerisinde Bootstrap-checkbox kullanmayınız.'
6 | });
7 | })(jQuery);
8 |
--------------------------------------------------------------------------------
/dist/js/i18n/pt.js:
--------------------------------------------------------------------------------
1 | (function ($) {
2 | Object.assign($.fn.checkboxpicker.defaults, {
3 | offLabel: 'Não',
4 | onLabel: 'Sim',
5 | warningMessage: 'Elementos do tipo label não são suportados dentro do Bootstrap-checkbox.'
6 | });
7 | })(jQuery);
--------------------------------------------------------------------------------
/logo/logo.svg:
--------------------------------------------------------------------------------
1 |
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright Vasilii A., 2013-2018
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/dist/js/bootstrap-checkbox.min.js:
--------------------------------------------------------------------------------
1 | !function(factory){"function"==typeof define&&define.amd?define(["jquery"],factory):"object"==typeof exports?module.exports=factory(require("jquery")):factory(jQuery)}((function($){function create(){return $($.map(arguments,$.proxy(document,"createElement")))}var Checkboxpicker=function(){function Checkboxpicker(element,options){this.element=element,this.$element=$(element);var data=this.$element.data();""===data.reverse&&(data.reverse=!0),""===data.switchAlways&&(data.switchAlways=!0),""===data.html&&(data.html=!0),this.options=$.extend({},$.fn.checkboxpicker.defaults,options,data),this.$element.closest("label").length?console.warn(this.options.warningMessage):(this.$group=$(document.createElement("div")),this.$buttons=create("button","button").attr("type","button").attr("tabindex",-1),this.$off=this.$buttons.eq(this.options.reverse?1:0),this.$on=this.$buttons.eq(this.options.reverse?0:1),this.init())}var _proto=Checkboxpicker.prototype;return _proto.init=function(){var fn=this.options.html?"html":"text";this.element.hidden=!0,this.$group.addClass(this.options.baseGroupCls).addClass(this.options.groupCls),this.$buttons.addClass(this.options.baseCls).addClass(this.options.cls),this.options.offLabel&&this.$off[fn](this.options.offLabel),this.options.onLabel&&this.$on[fn](this.options.onLabel),this.options.offIconCls&&(this.options.offLabel&&this.$off.prepend(" "),create("span").addClass(this.options.iconCls).addClass(this.options.offIconCls).prependTo(this.$off)),this.options.onIconCls&&(this.options.onLabel&&this.$on.prepend(" "),create("span").addClass(this.options.iconCls).addClass(this.options.onIconCls).prependTo(this.$on)),this.element.checked?(this.$on.addClass("active"),this.$on.addClass(this.options.onActiveCls),this.$off.addClass(this.options.offCls)):(this.$off.addClass("active"),this.$off.addClass(this.options.offActiveCls),this.$on.addClass(this.options.onCls)),this.element.title?this.$group.attr("title",this.element.title):(this.options.offTitle&&this.$off.attr("title",this.options.offTitle),this.options.onTitle&&this.$on.attr("title",this.options.onTitle)),this.$group.on("keydown",$.proxy(this,"keydown")),this.$buttons.on("click",$.proxy(this,"click")),this.$element.on("change",$.proxy(this,"toggleChecked")),$(this.element.labels).on("click",$.proxy(this,"focus")),$(this.element.form).on("reset",$.proxy(this,"reset")),this.$group.append(this.$buttons).insertAfter(this.element),this.element.readOnly||this.element.disabled?this.$buttons.prop("disabled",!0):(this.$group.attr("tabindex",this.element.tabIndex),this.element.autofocus&&this.focus())},_proto.toggleChecked=function(){this.$buttons.toggleClass("active"),this.$off.toggleClass(this.options.offCls),this.$off.toggleClass(this.options.offActiveCls),this.$on.toggleClass(this.options.onCls),this.$on.toggleClass(this.options.onActiveCls)},_proto.toggleDisabled=function(){this.$buttons.toggleClass("disabled"),this.element.disabled?this.$group.attr("tabindex",this.element.tabIndex):this.$group.removeAttr("tabindex")},_proto.focus=function(){this.$group.trigger("focus")},_proto.click=function(event){$(event.currentTarget).hasClass("active")&&!this.options.switchAlways||this.change()},_proto.change=function(){this.set(!this.element.checked)},_proto.set=function(value){this.element.checked=value,this.$element.trigger("change")},_proto.keydown=function(event){-1!==$.inArray(event.keyCode,this.options.toggleKeyCodes)?(event.preventDefault(),this.change()):13===event.keyCode&&$(this.element.form).trigger("submit")},_proto.reset=function(){(this.element.defaultChecked&&this.$off.hasClass("active")||!this.element.defaultChecked&&this.$on.hasClass("active"))&&this.set(this.element.defaultChecked)},Checkboxpicker}(),oldPropHooks=$.extend({},$.propHooks);return $.extend($.propHooks,{checked:{set:function(element,value){var data=$.data(element,"bs.checkbox");data&&element.checked!==value&&data.change(value),oldPropHooks.checked&&oldPropHooks.checked.set&&oldPropHooks.checked.set(element,value)}},disabled:{set:function(element,value){var data=$.data(element,"bs.checkbox");data&&element.disabled!==value&&data.toggleDisabled(),oldPropHooks.disabled&&oldPropHooks.disabled.set&&oldPropHooks.disabled.set(element,value)}}}),$.fn.checkboxpicker=function(options,elements){return(this instanceof $?this:$("string"==typeof options?options:elements)).each((function(){var data=$.data(this,"bs.checkbox");data||(data=new Checkboxpicker(this,options),$.data(this,"bs.checkbox",data))}))},$.fn.checkboxpicker.defaults={baseGroupCls:"btn-group",baseCls:"btn",groupCls:null,cls:null,offCls:"btn-default",onCls:"btn-default",offActiveCls:"btn-danger",onActiveCls:"btn-success",offLabel:"No",onLabel:"Yes",offTitle:!1,onTitle:!1,iconCls:"glyphicon",toggleKeyCodes:[13,32],warningMessage:"Please do not use Bootstrap-checkbox element in label element."},Checkboxpicker}));
--------------------------------------------------------------------------------
/js/bootstrap-checkbox.js:
--------------------------------------------------------------------------------
1 | (function(factory) {
2 | if (typeof define === 'function' && define.amd) {
3 | // AMD. Register as an anonymous module
4 | define(['jquery'], factory);
5 | } else if (typeof exports === 'object') {
6 | // Node/CommonJS
7 | module.exports = factory(require('jquery'));
8 | } else {
9 | // Browser globals
10 | factory(jQuery);
11 | }
12 | })(function($) {
13 | function create() {
14 | return $($.map(arguments, $.proxy(document, 'createElement')));
15 | }
16 |
17 | class Checkboxpicker {
18 | constructor(element, options) {
19 | this.element = element;
20 | this.$element = $(element);
21 |
22 | const data = this.$element.data();
23 |
24 | // <... data-reverse>
25 | if (data.reverse === '') {
26 | data.reverse = true;
27 | }
28 |
29 | // <... data-switch-always>
30 | if (data.switchAlways === '') {
31 | data.switchAlways = true;
32 | }
33 |
34 | // <... data-html>
35 | if (data.html === '') {
36 | data.html = true;
37 | }
38 |
39 | this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, data);
40 |
41 | if (this.$element.closest('label').length) {
42 | console.warn(this.options.warningMessage);
43 |
44 | return;
45 | }
46 |
47 | this.$group = $(document.createElement('div'));
48 | this.$buttons = create('button', 'button').attr('type', 'button').attr('tabindex', -1);
49 |
50 | this.$off = this.$buttons.eq(this.options.reverse ? 1 : 0);
51 | this.$on = this.$buttons.eq(this.options.reverse ? 0 : 1);
52 |
53 | this.init();
54 | }
55 |
56 | init() {
57 | const fn = this.options.html ? 'html' : 'text';
58 |
59 | this.element.hidden = true;
60 | this.$group.addClass(this.options.baseGroupCls).addClass(this.options.groupCls);
61 | this.$buttons.addClass(this.options.baseCls).addClass(this.options.cls);
62 |
63 | if (this.options.offLabel) {
64 | this.$off[fn](this.options.offLabel);
65 | }
66 |
67 | if (this.options.onLabel) {
68 | this.$on[fn](this.options.onLabel);
69 | }
70 |
71 | if (this.options.offIconCls) {
72 | if (this.options.offLabel) {
73 | // -- whitespace (or wrap into span)
74 | this.$off.prepend(' ');
75 | }
76 |
77 | // $.addClass for XSS check
78 | create('span').addClass(this.options.iconCls).addClass(this.options.offIconCls).prependTo(this.$off);
79 | }
80 |
81 | if (this.options.onIconCls) {
82 | if (this.options.onLabel) {
83 | // -- whitespace (or wrap into span)
84 | this.$on.prepend(' ');
85 | }
86 |
87 | // $.addClass for XSS check
88 | create('span').addClass(this.options.iconCls).addClass(this.options.onIconCls).prependTo(this.$on);
89 | }
90 |
91 | if (this.element.checked) {
92 | this.$on.addClass('active');
93 | this.$on.addClass(this.options.onActiveCls);
94 | this.$off.addClass(this.options.offCls);
95 | } else {
96 | this.$off.addClass('active');
97 | this.$off.addClass(this.options.offActiveCls);
98 | this.$on.addClass(this.options.onCls);
99 | }
100 |
101 | if (this.element.title) {
102 | this.$group.attr('title', this.element.title);
103 | } else {
104 | // Attribute title (offTitle, onTitle) on this.$buttons not work (native) if this.element.disabled, fine!
105 | if (this.options.offTitle) {
106 | this.$off.attr('title', this.options.offTitle);
107 | }
108 |
109 | if (this.options.onTitle) {
110 | this.$on.attr('title', this.options.onTitle);
111 | }
112 | }
113 |
114 | // Keydown event only trigger if set tabindex, fine!
115 | this.$group.on('keydown', $.proxy(this, 'keydown'));
116 |
117 | // Don't trigger if element has .disabled class, fine!
118 | this.$buttons.on('click', $.proxy(this, 'click'));
119 |
120 | this.$element.on('change', $.proxy(this, 'toggleChecked'));
121 | $(this.element.labels).on('click', $.proxy(this, 'focus'));
122 | $(this.element.form).on('reset', $.proxy(this, 'reset'));
123 |
124 | this.$group.append(this.$buttons).insertAfter(this.element);
125 |
126 | // Necessarily after this.$group.append() (autofocus)
127 | if (this.element.readOnly || this.element.disabled) {
128 | this.$buttons.prop('disabled', true);
129 | } else {
130 | this.$group.attr('tabindex', this.element.tabIndex);
131 |
132 | if (this.element.autofocus) {
133 | this.focus();
134 | }
135 | }
136 | }
137 |
138 | toggleChecked() {
139 | // this.$group not focus (incorrect on form reset)
140 | this.$buttons.toggleClass('active');
141 |
142 | this.$off.toggleClass(this.options.offCls);
143 | this.$off.toggleClass(this.options.offActiveCls);
144 | this.$on.toggleClass(this.options.onCls);
145 | this.$on.toggleClass(this.options.onActiveCls);
146 | }
147 |
148 | toggleDisabled() {
149 | this.$buttons.toggleClass('disabled');
150 |
151 | if (this.element.disabled) {
152 | this.$group.attr('tabindex', this.element.tabIndex);
153 | } else {
154 | this.$group.removeAttr('tabindex');
155 | }
156 | }
157 |
158 | focus() {
159 | // Original behavior
160 | this.$group.trigger('focus');
161 | }
162 |
163 | click(event) {
164 | // Strictly event.currentTarget. Fix #19
165 | const $button = $(event.currentTarget);
166 |
167 | if (!$button.hasClass('active') || this.options.switchAlways) {
168 | this.change();
169 | }
170 | }
171 |
172 | change() {
173 | this.set(!this.element.checked);
174 | }
175 |
176 | set(value) {
177 | // Fix #12
178 | this.element.checked = value;
179 |
180 | this.$element.trigger('change');
181 | }
182 |
183 | keydown(event) {
184 | if ($.inArray(event.keyCode, this.options.toggleKeyCodes) !== -1) {
185 | // Off vertical scrolling on Spacebar
186 | event.preventDefault();
187 |
188 | this.change();
189 | } else if (event.keyCode === 13) {
190 | $(this.element.form).trigger('submit');
191 | }
192 | }
193 |
194 | reset() {
195 | // this.element.checked not used (incorect on large number of form elements)
196 | if ((this.element.defaultChecked && this.$off.hasClass('active')) || (!this.element.defaultChecked && this.$on.hasClass('active'))) {
197 | this.set(this.element.defaultChecked);
198 | }
199 | }
200 | }
201 |
202 | // Be hooks friendly
203 | const oldPropHooks = $.extend({}, $.propHooks);
204 |
205 | // Support $.fn.prop setter (checked, disabled)
206 | $.extend($.propHooks, {
207 | checked: {
208 | set: function(element, value) {
209 | const data = $.data(element, 'bs.checkbox');
210 |
211 | if (data && element.checked !== value) {
212 | data.change(value);
213 | }
214 |
215 | if (oldPropHooks.checked && oldPropHooks.checked.set) {
216 | oldPropHooks.checked.set(element, value);
217 | }
218 | }
219 | },
220 | disabled: {
221 | set: function(element, value) {
222 | const data = $.data(element, 'bs.checkbox');
223 |
224 | if (data && element.disabled !== value) {
225 | data.toggleDisabled();
226 | }
227 |
228 | if (oldPropHooks.disabled && oldPropHooks.disabled.set) {
229 | oldPropHooks.disabled.set(element, value);
230 | }
231 | }
232 | }
233 | });
234 |
235 | // For AMD/Node/CommonJS used elements (optional)
236 | // http://learn.jquery.com/jquery-ui/environments/amd/
237 | $.fn.checkboxpicker = function(options, elements) {
238 | let $elements;
239 |
240 | if (this instanceof $) {
241 | $elements = this;
242 | } else if (typeof options === 'string') {
243 | $elements = $(options);
244 | } else {
245 | $elements = $(elements);
246 | }
247 |
248 | return $elements.each(function() {
249 | let data = $.data(this, 'bs.checkbox');
250 |
251 | if (!data) {
252 | data = new Checkboxpicker(this, options);
253 |
254 | $.data(this, 'bs.checkbox', data);
255 | }
256 | });
257 | };
258 |
259 | // HTML5 data-*.
260 | // --> $('input').data('onLabel') === '43'.
261 | $.fn.checkboxpicker.defaults = {
262 | baseGroupCls: 'btn-group',
263 | baseCls: 'btn',
264 | groupCls: null,
265 | cls: null,
266 | offCls: 'btn-default',
267 | onCls: 'btn-default',
268 | offActiveCls: 'btn-danger',
269 | onActiveCls: 'btn-success',
270 | offLabel: 'No',
271 | onLabel: 'Yes',
272 | offTitle: false,
273 | onTitle: false,
274 | iconCls: 'glyphicon',
275 |
276 | // Event key codes:
277 | // 13: Return
278 | // 32: Spacebar
279 | toggleKeyCodes: [13, 32],
280 |
281 | warningMessage: 'Please do not use Bootstrap-checkbox element in label element.'
282 | };
283 |
284 | return Checkboxpicker;
285 | });
286 |
--------------------------------------------------------------------------------
/dist/js/bootstrap-checkbox.js:
--------------------------------------------------------------------------------
1 | (function (factory) {
2 | if (typeof define === 'function' && define.amd) {
3 | // AMD. Register as an anonymous module
4 | define(['jquery'], factory);
5 | } else if (typeof exports === 'object') {
6 | // Node/CommonJS
7 | module.exports = factory(require('jquery'));
8 | } else {
9 | // Browser globals
10 | factory(jQuery);
11 | }
12 | })(function ($) {
13 | function create() {
14 | return $($.map(arguments, $.proxy(document, 'createElement')));
15 | }
16 | var Checkboxpicker = /*#__PURE__*/function () {
17 | function Checkboxpicker(element, options) {
18 | this.element = element;
19 | this.$element = $(element);
20 | var data = this.$element.data();
21 |
22 | // <... data-reverse>
23 | if (data.reverse === '') {
24 | data.reverse = true;
25 | }
26 |
27 | // <... data-switch-always>
28 | if (data.switchAlways === '') {
29 | data.switchAlways = true;
30 | }
31 |
32 | // <... data-html>
33 | if (data.html === '') {
34 | data.html = true;
35 | }
36 | this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, data);
37 | if (this.$element.closest('label').length) {
38 | console.warn(this.options.warningMessage);
39 | return;
40 | }
41 | this.$group = $(document.createElement('div'));
42 | this.$buttons = create('button', 'button').attr('type', 'button').attr('tabindex', -1);
43 | this.$off = this.$buttons.eq(this.options.reverse ? 1 : 0);
44 | this.$on = this.$buttons.eq(this.options.reverse ? 0 : 1);
45 | this.init();
46 | }
47 | var _proto = Checkboxpicker.prototype;
48 | _proto.init = function init() {
49 | var fn = this.options.html ? 'html' : 'text';
50 | this.element.hidden = true;
51 | this.$group.addClass(this.options.baseGroupCls).addClass(this.options.groupCls);
52 | this.$buttons.addClass(this.options.baseCls).addClass(this.options.cls);
53 | if (this.options.offLabel) {
54 | this.$off[fn](this.options.offLabel);
55 | }
56 | if (this.options.onLabel) {
57 | this.$on[fn](this.options.onLabel);
58 | }
59 | if (this.options.offIconCls) {
60 | if (this.options.offLabel) {
61 | // -- whitespace (or wrap into span)
62 | this.$off.prepend(' ');
63 | }
64 |
65 | // $.addClass for XSS check
66 | create('span').addClass(this.options.iconCls).addClass(this.options.offIconCls).prependTo(this.$off);
67 | }
68 | if (this.options.onIconCls) {
69 | if (this.options.onLabel) {
70 | // -- whitespace (or wrap into span)
71 | this.$on.prepend(' ');
72 | }
73 |
74 | // $.addClass for XSS check
75 | create('span').addClass(this.options.iconCls).addClass(this.options.onIconCls).prependTo(this.$on);
76 | }
77 | if (this.element.checked) {
78 | this.$on.addClass('active');
79 | this.$on.addClass(this.options.onActiveCls);
80 | this.$off.addClass(this.options.offCls);
81 | } else {
82 | this.$off.addClass('active');
83 | this.$off.addClass(this.options.offActiveCls);
84 | this.$on.addClass(this.options.onCls);
85 | }
86 | if (this.element.title) {
87 | this.$group.attr('title', this.element.title);
88 | } else {
89 | // Attribute title (offTitle, onTitle) on this.$buttons not work (native) if this.element.disabled, fine!
90 | if (this.options.offTitle) {
91 | this.$off.attr('title', this.options.offTitle);
92 | }
93 | if (this.options.onTitle) {
94 | this.$on.attr('title', this.options.onTitle);
95 | }
96 | }
97 |
98 | // Keydown event only trigger if set tabindex, fine!
99 | this.$group.on('keydown', $.proxy(this, 'keydown'));
100 |
101 | // Don't trigger if element has .disabled class, fine!
102 | this.$buttons.on('click', $.proxy(this, 'click'));
103 | this.$element.on('change', $.proxy(this, 'toggleChecked'));
104 | $(this.element.labels).on('click', $.proxy(this, 'focus'));
105 | $(this.element.form).on('reset', $.proxy(this, 'reset'));
106 | this.$group.append(this.$buttons).insertAfter(this.element);
107 |
108 | // Necessarily after this.$group.append() (autofocus)
109 | if (this.element.readOnly || this.element.disabled) {
110 | this.$buttons.prop('disabled', true);
111 | } else {
112 | this.$group.attr('tabindex', this.element.tabIndex);
113 | if (this.element.autofocus) {
114 | this.focus();
115 | }
116 | }
117 | };
118 | _proto.toggleChecked = function toggleChecked() {
119 | // this.$group not focus (incorrect on form reset)
120 | this.$buttons.toggleClass('active');
121 | this.$off.toggleClass(this.options.offCls);
122 | this.$off.toggleClass(this.options.offActiveCls);
123 | this.$on.toggleClass(this.options.onCls);
124 | this.$on.toggleClass(this.options.onActiveCls);
125 | };
126 | _proto.toggleDisabled = function toggleDisabled() {
127 | this.$buttons.toggleClass('disabled');
128 | if (this.element.disabled) {
129 | this.$group.attr('tabindex', this.element.tabIndex);
130 | } else {
131 | this.$group.removeAttr('tabindex');
132 | }
133 | };
134 | _proto.focus = function focus() {
135 | // Original behavior
136 | this.$group.trigger('focus');
137 | };
138 | _proto.click = function click(event) {
139 | // Strictly event.currentTarget. Fix #19
140 | var $button = $(event.currentTarget);
141 | if (!$button.hasClass('active') || this.options.switchAlways) {
142 | this.change();
143 | }
144 | };
145 | _proto.change = function change() {
146 | this.set(!this.element.checked);
147 | };
148 | _proto.set = function set(value) {
149 | // Fix #12
150 | this.element.checked = value;
151 | this.$element.trigger('change');
152 | };
153 | _proto.keydown = function keydown(event) {
154 | if ($.inArray(event.keyCode, this.options.toggleKeyCodes) !== -1) {
155 | // Off vertical scrolling on Spacebar
156 | event.preventDefault();
157 | this.change();
158 | } else if (event.keyCode === 13) {
159 | $(this.element.form).trigger('submit');
160 | }
161 | };
162 | _proto.reset = function reset() {
163 | // this.element.checked not used (incorect on large number of form elements)
164 | if (this.element.defaultChecked && this.$off.hasClass('active') || !this.element.defaultChecked && this.$on.hasClass('active')) {
165 | this.set(this.element.defaultChecked);
166 | }
167 | };
168 | return Checkboxpicker;
169 | }(); // Be hooks friendly
170 | var oldPropHooks = $.extend({}, $.propHooks);
171 |
172 | // Support $.fn.prop setter (checked, disabled)
173 | $.extend($.propHooks, {
174 | checked: {
175 | set: function set(element, value) {
176 | var data = $.data(element, 'bs.checkbox');
177 | if (data && element.checked !== value) {
178 | data.change(value);
179 | }
180 | if (oldPropHooks.checked && oldPropHooks.checked.set) {
181 | oldPropHooks.checked.set(element, value);
182 | }
183 | }
184 | },
185 | disabled: {
186 | set: function set(element, value) {
187 | var data = $.data(element, 'bs.checkbox');
188 | if (data && element.disabled !== value) {
189 | data.toggleDisabled();
190 | }
191 | if (oldPropHooks.disabled && oldPropHooks.disabled.set) {
192 | oldPropHooks.disabled.set(element, value);
193 | }
194 | }
195 | }
196 | });
197 |
198 | // For AMD/Node/CommonJS used elements (optional)
199 | // http://learn.jquery.com/jquery-ui/environments/amd/
200 | $.fn.checkboxpicker = function (options, elements) {
201 | var $elements;
202 | if (this instanceof $) {
203 | $elements = this;
204 | } else if (typeof options === 'string') {
205 | $elements = $(options);
206 | } else {
207 | $elements = $(elements);
208 | }
209 | return $elements.each(function () {
210 | var data = $.data(this, 'bs.checkbox');
211 | if (!data) {
212 | data = new Checkboxpicker(this, options);
213 | $.data(this, 'bs.checkbox', data);
214 | }
215 | });
216 | };
217 |
218 | // HTML5 data-*.
219 | // --> $('input').data('onLabel') === '43'.
220 | $.fn.checkboxpicker.defaults = {
221 | baseGroupCls: 'btn-group',
222 | baseCls: 'btn',
223 | groupCls: null,
224 | cls: null,
225 | offCls: 'btn-default',
226 | onCls: 'btn-default',
227 | offActiveCls: 'btn-danger',
228 | onActiveCls: 'btn-success',
229 | offLabel: 'No',
230 | onLabel: 'Yes',
231 | offTitle: false,
232 | onTitle: false,
233 | iconCls: 'glyphicon',
234 | // Event key codes:
235 | // 13: Return
236 | // 32: Spacebar
237 | toggleKeyCodes: [13, 32],
238 | warningMessage: 'Please do not use Bootstrap-checkbox element in label element.'
239 | };
240 | return Checkboxpicker;
241 | });
--------------------------------------------------------------------------------