File indexing completed on 2024-05-19 06:00:18
0001 /* ============================================================= 0002 * flatui-radio.js v0.0.2 0003 * ============================================================ */ 0004 0005 !function ($) { 0006 0007 /* RADIO PUBLIC CLASS DEFINITION 0008 * ============================== */ 0009 0010 var Radio = function (element, options) { 0011 this.init(element, options); 0012 } 0013 0014 Radio.prototype = { 0015 0016 constructor: Radio 0017 0018 , init: function (element, options) { 0019 var $el = this.$element = $(element) 0020 0021 this.options = $.extend({}, $.fn.radio.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('.radio'); 0029 0030 $el.prop('disabled') && $parent.addClass('disabled'); 0031 $el.prop('checked') && $parent.addClass('checked'); 0032 } 0033 0034 , toggle: function () { 0035 var d = 'disabled' 0036 , ch = 'checked' 0037 , $el = this.$element 0038 , checked = $el.prop(ch) 0039 , $parent = $el.closest('.radio') 0040 , $parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body') 0041 , $elemGroup = $parentWrap.find(':radio[name="' + $el.prop('name') + '"]') 0042 , e = $.Event('toggle') 0043 0044 $elemGroup.not($el).each(function () { 0045 var $el = $(this) 0046 , $parent = $(this).closest('.radio'); 0047 0048 if ($el.prop(d) == false) { 0049 $parent.removeClass(ch) && $el.prop(ch, false).trigger('change'); 0050 } 0051 }); 0052 0053 if ($el.prop(d) == false) { 0054 if (checked == false) $parent.addClass(ch) && $el.prop(ch, true); 0055 $el.trigger(e); 0056 0057 if (checked !== $el.prop(ch)) { 0058 $el.trigger('change'); 0059 } 0060 } 0061 } 0062 0063 , setCheck: function (option) { 0064 var ch = 'checked' 0065 , $el = this.$element 0066 , $parent = $el.closest('.radio') 0067 , checkAction = option == 'check' ? true : false 0068 , checked = $el.prop(ch) 0069 , $parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body') 0070 , $elemGroup = $parentWrap.find(':radio[name="' + $el['attr']('name') + '"]') 0071 , e = $.Event(option) 0072 0073 $elemGroup.not($el).each(function () { 0074 var $el = $(this) 0075 , $parent = $(this).closest('.radio'); 0076 0077 $parent.removeClass(ch) && $el.removeAttr(ch); 0078 }); 0079 0080 $parent[checkAction ? 'addClass' : 'removeClass'](ch) && checkAction ? $el.prop(ch, true) : $el.removeAttr(ch); 0081 $el.trigger(e); 0082 0083 if (checked !== $el.prop(ch)) { 0084 $el.trigger('change'); 0085 } 0086 } 0087 0088 } 0089 0090 0091 /* RADIO PLUGIN DEFINITION 0092 * ======================== */ 0093 0094 var old = $.fn.radio 0095 0096 $.fn.radio = function (option) { 0097 return this.each(function () { 0098 var $this = $(this) 0099 , data = $this.data('radio') 0100 , options = $.extend({}, $.fn.radio.defaults, $this.data(), typeof option == 'object' && option); 0101 if (!data) $this.data('radio', (data = new Radio(this, options))); 0102 if (option == 'toggle') data.toggle() 0103 if (option == 'check' || option == 'uncheck') data.setCheck(option) 0104 else if (option) data.setState(); 0105 }); 0106 } 0107 0108 $.fn.radio.defaults = { 0109 template: '<span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span>' 0110 } 0111 0112 0113 /* RADIO NO CONFLICT 0114 * ================== */ 0115 0116 $.fn.radio.noConflict = function () { 0117 $.fn.radio = old; 0118 return this; 0119 } 0120 0121 0122 /* RADIO DATA-API 0123 * =============== */ 0124 0125 $(document).on('click.radio.data-api', '[data-toggle^=radio], .radio', function (e) { 0126 var $radio = $(e.target); 0127 if (e.target.tagName != "A") { 0128 e && e.preventDefault() && e.stopPropagation(); 0129 if (!$radio.hasClass('radio')) $radio = $radio.closest('.radio'); 0130 $radio.find(':radio').radio('toggle'); 0131 } 0132 }); 0133 0134 $(window).on('load', function () { 0135 $('[data-toggle="radio"]').each(function () { 0136 var $radio = $(this); 0137 $radio.radio(); 0138 }); 0139 }); 0140 0141 }(window.jQuery);