bootstrap-checkbox.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. !function ($) {
  2. /* CHECKBOX PUBLIC CLASS DEFINITION
  3. * ============================== */
  4. var Checkbox = function (element, options) {
  5. this.init(element, options);
  6. }
  7. Checkbox.prototype = {
  8. constructor: Checkbox
  9. , init: function (element, options) {
  10. var $el = this.$element = $(element)
  11. this.options = $.extend({}, $.fn.checkbox.defaults, options);
  12. $el.before(this.options.template);
  13. this.setState();
  14. }
  15. , setState: function () {
  16. var $el = this.$element
  17. , $parent = $el.closest('.checkbox');
  18. $el.prop('disabled') && $parent.addClass('disabled');
  19. $el.prop('checked') && $parent.addClass('checked');
  20. }
  21. , toggle: function () {
  22. var ch = 'checked'
  23. , $el = this.$element
  24. , $parent = $el.closest('.checkbox')
  25. , checked = $el.prop(ch)
  26. , e = $.Event('toggle')
  27. if ($el.prop('disabled') == false) {
  28. $parent.toggleClass(ch) && checked ? $el.removeAttr(ch) : $el.prop(ch, ch);
  29. $el.trigger(e).trigger('change');
  30. }
  31. }
  32. , setCheck: function (option) {
  33. var d = 'disabled'
  34. , ch = 'checked'
  35. , $el = this.$element
  36. , $parent = $el.closest('.checkbox')
  37. , checkAction = option == 'check' ? true : false
  38. , e = $.Event(option)
  39. $parent[checkAction ? 'addClass' : 'removeClass' ](ch) && checkAction ? $el.prop(ch, ch) : $el.removeAttr(ch);
  40. $el.trigger(e).trigger('change');
  41. }
  42. }
  43. /* CHECKBOX PLUGIN DEFINITION
  44. * ======================== */
  45. var old = $.fn.checkbox
  46. $.fn.checkbox = function (option) {
  47. return this.each(function () {
  48. var $this = $(this)
  49. , data = $this.data('checkbox')
  50. , options = $.extend({}, $.fn.checkbox.defaults, $this.data(), typeof option == 'object' && option);
  51. if (!data)
  52. $this.data('checkbox', (data = new Checkbox(this, options)));
  53. if (option == 'toggle')
  54. data.toggle()
  55. if (option == 'check' || option == 'uncheck')
  56. data.setCheck(option)
  57. else if (option)
  58. data.setState();
  59. });
  60. }
  61. $.fn.checkbox.defaults = {
  62. template: '<span class="icons"><span class="first-icon fa fa-square-o"></span><span class="second-icon fa fa-check-square-o"></span></span>'
  63. }
  64. /* CHECKBOX NO CONFLICT
  65. * ================== */
  66. $.fn.checkbox.noConflict = function () {
  67. $.fn.checkbox = old;
  68. return this;
  69. }
  70. /* CHECKBOX DATA-API
  71. * =============== */
  72. $(document).on('click.checkbox.data-api', '[data-toggle^=checkbox], .checkbox', function (e) {
  73. var $checkbox = $(e.target);
  74. if (e.target.tagName != "A") {
  75. e && e.preventDefault() && e.stopPropagation();
  76. if (!$checkbox.hasClass('checkbox'))
  77. $checkbox = $checkbox.closest('.checkbox');
  78. $checkbox.find(':checkbox').checkbox('toggle');
  79. }
  80. });
  81. $(function () {
  82. $('[data-toggle="checkbox"]').each(function () {
  83. var $checkbox = $(this);
  84. $checkbox.checkbox();
  85. });
  86. });
  87. }(window.jQuery);