File indexing completed on 2024-05-19 06:00:18
0001 /* ============================================================= 0002 * flatui-checkbox.js v0.0.2 0003 * ============================================================ */ 0004 0005 !function ($) { 0006 0007 /* CHECKBOX PUBLIC CLASS DEFINITION 0008 * ============================== */ 0009 0010 var Checkbox = function (element, options) { 0011 this.init(element, options); 0012 } 0013 0014 Checkbox.prototype = { 0015 0016 constructor: Checkbox 0017 0018 , init: function (element, options) { 0019 var $el = this.$element = $(element) 0020 0021 this.options = $.extend({}, $.fn.checkbox.defaults, options); 0022 $el.before(this.options.template); 0023 this.setState(); 0024 } 0025 0026 , setState: function () { 0027 var $el = this.$element 0028 , $parent = $el.closest('.checkbox'); 0029 0030 $el.prop('disabled') && $parent.addClass('disabled'); 0031 $el.prop('checked') && $parent.addClass('checked'); 0032 } 0033 0034 , toggle: function () { 0035 var ch = 'checked' 0036 , $el = this.$element 0037 , $parent = $el.closest('.checkbox') 0038 , checked = $el.prop(ch) 0039 , e = $.Event('toggle') 0040 0041 if ($el.prop('disabled') == false) { 0042 $parent.toggleClass(ch) && checked ? $el.removeAttr(ch) : $el.attr(ch, true); 0043 $el.trigger(e).trigger('change'); 0044 } 0045 } 0046 0047 , setCheck: function (option) { 0048 var d = 'disabled' 0049 , ch = 'checked' 0050 , $el = this.$element 0051 , $parent = $el.closest('.checkbox') 0052 , checkAction = option == 'check' ? true : false 0053 , e = $.Event(option) 0054 0055 $parent[checkAction ? 'addClass' : 'removeClass' ](ch) && checkAction ? $el.attr(ch, true) : $el.removeAttr(ch); 0056 $el.trigger(e).trigger('change'); 0057 } 0058 0059 } 0060 0061 0062 /* CHECKBOX PLUGIN DEFINITION 0063 * ======================== */ 0064 0065 var old = $.fn.checkbox 0066 0067 $.fn.checkbox = function (option) { 0068 return this.each(function () { 0069 var $this = $(this) 0070 , data = $this.data('checkbox') 0071 , options = $.extend({}, $.fn.checkbox.defaults, $this.data(), typeof option == 'object' && option); 0072 if (!data) $this.data('checkbox', (data = new Checkbox(this, options))); 0073 if (option == 'toggle') data.toggle() 0074 if (option == 'check' || option == 'uncheck') data.setCheck(option) 0075 else if (option) data.setState(); 0076 }); 0077 } 0078 0079 $.fn.checkbox.defaults = { 0080 template: '<span class="icons"><span class="first-icon fui-checkbox-unchecked"></span><span class="second-icon fui-checkbox-checked"></span></span>' 0081 } 0082 0083 0084 /* CHECKBOX NO CONFLICT 0085 * ================== */ 0086 0087 $.fn.checkbox.noConflict = function () { 0088 $.fn.checkbox = old; 0089 return this; 0090 } 0091 0092 0093 /* CHECKBOX DATA-API 0094 * =============== */ 0095 0096 $(document).on('click.checkbox.data-api', '[data-toggle^=checkbox], .checkbox', function (e) { 0097 var $checkbox = $(e.target); 0098 if (e.target.tagName != "A") { 0099 e && e.preventDefault() && e.stopPropagation(); 0100 if (!$checkbox.hasClass('checkbox')) $checkbox = $checkbox.closest('.checkbox'); 0101 $checkbox.find(':checkbox').checkbox('toggle'); 0102 } 0103 }); 0104 0105 $(window).on('load', function () { 0106 $('[data-toggle="checkbox"]').each(function () { 0107 var $checkbox = $(this); 0108 $checkbox.checkbox(); 0109 }); 0110 }); 0111 0112 }(window.jQuery);