|
21 | 21 | }
|
22 | 22 | })(function($) {
|
23 | 23 | $.create = function() {
|
24 |
| - return $($.map(arguments, function(tagName) { |
25 |
| - return document.createElement(tagName); |
26 |
| - })); |
| 24 | + return $($.map(arguments, $.proxy(document, 'createElement'))); |
27 | 25 | };
|
28 | 26 |
|
29 | 27 | function Checkboxpicker(element, options) {
|
30 | 28 | this.element = element;
|
31 | 29 | this.$element = $(element);
|
32 | 30 |
|
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); |
34 | 44 |
|
35 | 45 | if (this.$element.closest('label').length) {
|
36 | 46 | console.warn(this.options.warningMessage);
|
|
43 | 53 | // .btn-group-justified works with <a> elements as the <button> doesn't pick up the styles
|
44 | 54 | this.$buttons = $.create('a', 'a').addClass('btn');
|
45 | 55 |
|
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); |
51 | 58 |
|
52 | 59 | this.init();
|
53 | 60 | }
|
|
117 | 124 | this.$group.on('keydown', $.proxy(this, 'keydown'));
|
118 | 125 |
|
119 | 126 | // 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')); |
121 | 128 |
|
122 | 129 | this.$element.on('change', $.proxy(this, 'toggleChecked'));
|
123 | 130 | $(this.element.labels).on('click', $.proxy(this, 'focus'));
|
|
166 | 173 | // Original behavior
|
167 | 174 | this.$group.trigger('focus');
|
168 | 175 | },
|
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 | + } |
171 | 182 | },
|
172 | 183 | change: function(value) {
|
173 | 184 | // Fix #12
|
|
0 commit comments