Skip to content

Commit 9eb69e1

Browse files
committed
Add switchAlways option
1 parent 1ee73a7 commit 9eb69e1

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Diff for: js/bootstrap-checkbox.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,26 @@
2121
}
2222
})(function($) {
2323
$.create = function() {
24-
return $($.map(arguments, function(tagName) {
25-
return document.createElement(tagName);
26-
}));
24+
return $($.map(arguments, $.proxy(document, 'createElement')));
2725
};
2826

2927
function Checkboxpicker(element, options) {
3028
this.element = element;
3129
this.$element = $(element);
3230

33-
this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, this.$element.data());
31+
var data = this.$element.data();
32+
33+
// === '': <... data-reverse>
34+
if (data.reverse === '') {
35+
data.reverse = true;
36+
}
37+
38+
// === '': <... data-switch-always>
39+
if (data.switchAlways === '') {
40+
data.switchAlways = true;
41+
}
42+
43+
this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, data);
3444

3545
if (this.$element.closest('label').length) {
3646
console.warn(this.options.warningMessage);
@@ -43,11 +53,8 @@
4353
// .btn-group-justified works with <a> elements as the <button> doesn't pick up the styles
4454
this.$buttons = $.create('a', 'a').addClass('btn');
4555

46-
// === '': <... data-reverse>
47-
var reverse = this.options.reverse || this.options.reverse === '';
48-
49-
this.$off = this.$buttons.eq(reverse ? 1 : 0);
50-
this.$on = this.$buttons.eq(reverse ? 0 : 1);
56+
this.$off = this.$buttons.eq(this.options.reverse ? 1 : 0);
57+
this.$on = this.$buttons.eq(this.options.reverse ? 0 : 1);
5158

5259
this.init();
5360
}
@@ -117,7 +124,7 @@
117124
this.$group.on('keydown', $.proxy(this, 'keydown'));
118125

119126
// Don't trigger if <a> element has .disabled class, fine!
120-
this.$group.on('click', 'a:not(.active)', $.proxy(this, 'click'));
127+
this.$buttons.on('click', $.proxy(this, 'click'));
121128

122129
this.$element.on('change', $.proxy(this, 'toggleChecked'));
123130
$(this.element.labels).on('click', $.proxy(this, 'focus'));
@@ -166,8 +173,12 @@
166173
// Original behavior
167174
this.$group.trigger('focus');
168175
},
169-
click: function() {
170-
this.change(!this.element.checked);
176+
click: function(event) {
177+
var $button = $(event.target);
178+
179+
if (!$button.hasClass('active') || this.options.switchAlways) {
180+
this.change(!this.element.checked);
181+
}
171182
},
172183
change: function(value) {
173184
// Fix #12

0 commit comments

Comments
 (0)