|
| 1 | +/*! |
| 2 | + * Bootstrap-checkbox v1.4.0 (https://vsn4ik.github.io/bootstrap-checkbox/) |
| 3 | + * Copyright 2013-2018 Vasilii A. (https://github.com/vsn4ik) |
| 4 | + * Licensed under the MIT license |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * $.inArray: friends with IE8. Use Array.prototype.indexOf in future. |
| 9 | + * Use this.element.hidden in future. |
| 10 | + * $.proxy: friends with IE8. Use Function.prototype.bind in future. |
| 11 | + */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +(function(factory) { |
| 16 | + if (typeof define === 'function' && define.amd) { |
| 17 | + // AMD. Register as an anonymous module |
| 18 | + define(['jquery'], factory); |
| 19 | + } else if (typeof exports === 'object') { |
| 20 | + // Node/CommonJS |
| 21 | + module.exports = factory(require('jquery')); |
| 22 | + } else { |
| 23 | + // Browser globals |
| 24 | + factory(jQuery); |
| 25 | + } |
| 26 | +})(function($) { |
| 27 | + function create() { |
| 28 | + return $($.map(arguments, $.proxy(document, 'createElement'))); |
| 29 | + } |
| 30 | + |
| 31 | + function Checkboxpicker(element, options) { |
| 32 | + this.element = element; |
| 33 | + this.$element = $(element); |
| 34 | + |
| 35 | + var data = this.$element.data(); |
| 36 | + |
| 37 | + // <... data-reverse> |
| 38 | + if (data.reverse === '') { |
| 39 | + data.reverse = true; |
| 40 | + } |
| 41 | + |
| 42 | + // <... data-switch-always> |
| 43 | + if (data.switchAlways === '') { |
| 44 | + data.switchAlways = true; |
| 45 | + } |
| 46 | + |
| 47 | + // <... data-html> |
| 48 | + if (data.html === '') { |
| 49 | + data.html = true; |
| 50 | + } |
| 51 | + |
| 52 | + this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, data); |
| 53 | + |
| 54 | + if (this.$element.closest('label').length) { |
| 55 | + console.warn(this.options.warningMessage); |
| 56 | + |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + this.$group = create('div'); |
| 61 | + |
| 62 | + // .btn-group-justified works with <a> elements as the <button> doesn't pick up the styles |
| 63 | + this.$buttons = create('a', 'a'); |
| 64 | + |
| 65 | + this.$off = this.$buttons.eq(this.options.reverse ? 1 : 0); |
| 66 | + this.$on = this.$buttons.eq(this.options.reverse ? 0 : 1); |
| 67 | + |
| 68 | + this.init(); |
| 69 | + } |
| 70 | + |
| 71 | + Checkboxpicker.prototype = { |
| 72 | + init: function() { |
| 73 | + var fn = this.options.html ? 'html' : 'text'; |
| 74 | + |
| 75 | + this.$element.addClass('hidden'); |
| 76 | + this.$group.addClass(this.options.baseGroupCls).addClass(this.options.groupCls); |
| 77 | + this.$buttons.addClass(this.options.baseCls).addClass(this.options.cls); |
| 78 | + |
| 79 | + if (this.options.offLabel) { |
| 80 | + this.$off[fn](this.options.offLabel); |
| 81 | + } |
| 82 | + |
| 83 | + if (this.options.onLabel) { |
| 84 | + this.$on[fn](this.options.onLabel); |
| 85 | + } |
| 86 | + |
| 87 | + if (this.options.offIconCls) { |
| 88 | + if (this.options.offLabel) { |
| 89 | + // -- whitespace (or wrap into span) |
| 90 | + this.$off.prepend(' '); |
| 91 | + } |
| 92 | + |
| 93 | + // $.addClass for XSS check |
| 94 | + create('span').addClass(this.options.iconCls).addClass(this.options.offIconCls).prependTo(this.$off); |
| 95 | + } |
| 96 | + |
| 97 | + if (this.options.onIconCls) { |
| 98 | + if (this.options.onLabel) { |
| 99 | + // -- whitespace (or wrap into span) |
| 100 | + this.$on.prepend(' '); |
| 101 | + } |
| 102 | + |
| 103 | + // $.addClass for XSS check |
| 104 | + create('span').addClass(this.options.iconCls).addClass(this.options.onIconCls).prependTo(this.$on); |
| 105 | + } |
| 106 | + |
| 107 | + if (this.element.checked) { |
| 108 | + this.$on.addClass('active'); |
| 109 | + this.$on.addClass(this.options.onActiveCls); |
| 110 | + this.$off.addClass(this.options.offCls); |
| 111 | + } else { |
| 112 | + this.$off.addClass('active'); |
| 113 | + this.$off.addClass(this.options.offActiveCls); |
| 114 | + this.$on.addClass(this.options.onCls); |
| 115 | + } |
| 116 | + |
| 117 | + if (this.element.title) { |
| 118 | + this.$group.attr('title', this.element.title); |
| 119 | + } else { |
| 120 | + // Attribute title (offTitle, onTitle) on this.$buttons not work (native) if this.element.disabled, fine! |
| 121 | + if (this.options.offTitle) { |
| 122 | + this.$off.attr('title', this.options.offTitle); |
| 123 | + } |
| 124 | + |
| 125 | + if (this.options.onTitle) { |
| 126 | + this.$on.attr('title', this.options.onTitle); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Keydown event only trigger if set tabindex, fine! |
| 131 | + this.$group.on('keydown', $.proxy(this, 'keydown')); |
| 132 | + |
| 133 | + // Don't trigger if <a> element has .disabled class, fine! |
| 134 | + this.$buttons.on('click', $.proxy(this, 'click')); |
| 135 | + |
| 136 | + this.$element.on('change', $.proxy(this, 'toggleChecked')); |
| 137 | + $(this.element.labels).on('click', $.proxy(this, 'focus')); |
| 138 | + $(this.element.form).on('reset', $.proxy(this, 'reset')); |
| 139 | + |
| 140 | + this.$group.append(this.$buttons).insertAfter(this.element); |
| 141 | + |
| 142 | + // Necessarily after this.$group.append() (autofocus) |
| 143 | + if (this.element.disabled) { |
| 144 | + this.$buttons.addClass('disabled'); |
| 145 | + |
| 146 | + if (this.options.disabledCursor) { |
| 147 | + this.$group.css('cursor', this.options.disabledCursor); |
| 148 | + } |
| 149 | + } else { |
| 150 | + this.$group.attr('tabindex', this.element.tabIndex); |
| 151 | + |
| 152 | + if (this.element.autofocus) { |
| 153 | + this.focus(); |
| 154 | + } |
| 155 | + } |
| 156 | + }, |
| 157 | + toggleChecked: function() { |
| 158 | + // this.$group not focus (incorrect on form reset) |
| 159 | + this.$buttons.toggleClass('active'); |
| 160 | + |
| 161 | + this.$off.toggleClass(this.options.offCls); |
| 162 | + this.$off.toggleClass(this.options.offActiveCls); |
| 163 | + this.$on.toggleClass(this.options.onCls); |
| 164 | + this.$on.toggleClass(this.options.onActiveCls); |
| 165 | + }, |
| 166 | + toggleDisabled: function() { |
| 167 | + this.$buttons.toggleClass('disabled'); |
| 168 | + |
| 169 | + if (this.element.disabled) { |
| 170 | + this.$group.attr('tabindex', this.element.tabIndex); |
| 171 | + this.$group.css('cursor', ''); |
| 172 | + } else { |
| 173 | + this.$group.removeAttr('tabindex'); |
| 174 | + |
| 175 | + if (this.options.disabledCursor) { |
| 176 | + this.$group.css('cursor', this.options.disabledCursor); |
| 177 | + } |
| 178 | + } |
| 179 | + }, |
| 180 | + focus: function() { |
| 181 | + // Original behavior |
| 182 | + this.$group.trigger('focus'); |
| 183 | + }, |
| 184 | + click: function(event) { |
| 185 | + // Strictly event.currentTarget. Fix #19 |
| 186 | + var $button = $(event.currentTarget); |
| 187 | + |
| 188 | + if (!$button.hasClass('active') || this.options.switchAlways) { |
| 189 | + this.change(); |
| 190 | + } |
| 191 | + }, |
| 192 | + change: function() { |
| 193 | + this.set(!this.element.checked); |
| 194 | + }, |
| 195 | + set: function(value) { |
| 196 | + // Fix #12 |
| 197 | + this.element.checked = value; |
| 198 | + |
| 199 | + this.$element.trigger('change'); |
| 200 | + }, |
| 201 | + keydown: function(event) { |
| 202 | + if ($.inArray(event.keyCode, this.options.toggleKeyCodes) !== -1) { |
| 203 | + // Off vertical scrolling on Spacebar |
| 204 | + event.preventDefault(); |
| 205 | + |
| 206 | + this.change(); |
| 207 | + } else if (event.keyCode === 13) { |
| 208 | + $(this.element.form).trigger('submit'); |
| 209 | + } |
| 210 | + }, |
| 211 | + reset: function() { |
| 212 | + // this.element.checked not used (incorect on large number of form elements) |
| 213 | + if ((this.element.defaultChecked && this.$off.hasClass('active')) || (!this.element.defaultChecked && this.$on.hasClass('active'))) { |
| 214 | + this.set(this.element.defaultChecked); |
| 215 | + } |
| 216 | + } |
| 217 | + }; |
| 218 | + |
| 219 | + // Be hooks friendly |
| 220 | + var oldPropHooks = $.extend({}, $.propHooks); |
| 221 | + |
| 222 | + // Support $.fn.prop setter (checked, disabled) |
| 223 | + $.extend($.propHooks, { |
| 224 | + checked: { |
| 225 | + set: function(element, value) { |
| 226 | + var data = $.data(element, 'bs.checkbox'); |
| 227 | + |
| 228 | + if (data && element.checked !== value) { |
| 229 | + data.change(value); |
| 230 | + } |
| 231 | + |
| 232 | + if (oldPropHooks.checked && oldPropHooks.checked.set) { |
| 233 | + oldPropHooks.checked.set(element, value); |
| 234 | + } |
| 235 | + } |
| 236 | + }, |
| 237 | + disabled: { |
| 238 | + set: function(element, value) { |
| 239 | + var data = $.data(element, 'bs.checkbox'); |
| 240 | + |
| 241 | + if (data && element.disabled !== value) { |
| 242 | + data.toggleDisabled(); |
| 243 | + } |
| 244 | + |
| 245 | + if (oldPropHooks.disabled && oldPropHooks.disabled.set) { |
| 246 | + oldPropHooks.disabled.set(element, value); |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + }); |
| 251 | + |
| 252 | + var old = $.fn.checkboxpicker; |
| 253 | + |
| 254 | + // For AMD/Node/CommonJS used elements (optional) |
| 255 | + // http://learn.jquery.com/jquery-ui/environments/amd/ |
| 256 | + $.fn.checkboxpicker = function(options, elements) { |
| 257 | + var $elements; |
| 258 | + |
| 259 | + if (this instanceof $) { |
| 260 | + $elements = this; |
| 261 | + } else if (typeof options === 'string') { |
| 262 | + $elements = $(options); |
| 263 | + } else { |
| 264 | + $elements = $(elements); |
| 265 | + } |
| 266 | + |
| 267 | + return $elements.each(function() { |
| 268 | + var data = $.data(this, 'bs.checkbox'); |
| 269 | + |
| 270 | + if (!data) { |
| 271 | + data = new Checkboxpicker(this, options); |
| 272 | + |
| 273 | + $.data(this, 'bs.checkbox', data); |
| 274 | + } |
| 275 | + }); |
| 276 | + }; |
| 277 | + |
| 278 | + // HTML5 data-*. |
| 279 | + // <input data-on-label="43"> --> $('input').data('onLabel') === '43'. |
| 280 | + $.fn.checkboxpicker.defaults = { |
| 281 | + baseGroupCls: 'btn-group', |
| 282 | + baseCls: 'btn', |
| 283 | + groupCls: null, |
| 284 | + cls: null, |
| 285 | + offCls: 'btn-default', |
| 286 | + onCls: 'btn-default', |
| 287 | + offActiveCls: 'btn-danger', |
| 288 | + onActiveCls: 'btn-success', |
| 289 | + offLabel: 'No', |
| 290 | + onLabel: 'Yes', |
| 291 | + offTitle: false, |
| 292 | + onTitle: false, |
| 293 | + iconCls: 'glyphicon', |
| 294 | + |
| 295 | + disabledCursor: 'not-allowed', |
| 296 | + |
| 297 | + // Event key codes: |
| 298 | + // 13: Return |
| 299 | + // 32: Spacebar |
| 300 | + toggleKeyCodes: [13, 32], |
| 301 | + |
| 302 | + warningMessage: 'Please do not use Bootstrap-checkbox element in label element.' |
| 303 | + }; |
| 304 | + |
| 305 | + $.fn.checkboxpicker.Constructor = Checkboxpicker; |
| 306 | + $.fn.checkboxpicker.noConflict = function() { |
| 307 | + $.fn.checkboxpicker = old; |
| 308 | + return this; |
| 309 | + }; |
| 310 | + |
| 311 | + return $.fn.checkboxpicker; |
| 312 | +}); |
0 commit comments