ion.checkRadio.min.js 3.77 KB
// Ion.CheckRadio | version 2.0.0 | https://github.com/IonDen/ion.CheckRadio
;(function(f,h){if(!f.fn.ionCheckRadio){String.prototype.trim||function(){var a=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;String.prototype.trim=function(){return this.replace(a,"")}}();var k={},l={},n=function(a){this.group=a.content;this.type=a.type;this.$group=f(this.group);this.observer=this.old=null;this.init()};n.prototype={init:function(){this.$group.eq(0).hasClass("icr-input")?this.prepare():this.createHTML()},prepare:function(){var a=this,e,b;for(b=0;b<this.group.length;b++)e=f(this.group[b]),e=
e.parent().parent(),f.data(this.group[b],"icr-parent",e),this.presetChecked(this.group[b]),this.presetDisabled(this.group[b]);this.$group.on("change",function(){a.change(this)});this.$group.on("focus",function(){a.focus(this)});this.$group.on("blur",function(){a.blur(this)});h.MutationObserver&&this.setUpObserver()},setUpObserver:function(){var a=this,e,b;this.observer=new MutationObserver(function(b){b.forEach(function(b){e=b.target;"disabled"===b.attributeName&&a.toggle(a.getParent(e),e.disabled,
"disabled")})});for(b=0;b<this.group.length;b++)this.observer.observe(this.group[b],{attributes:!0})},destroy:function(){this.$group.off();this.observer&&(this.observer.disconnect(),this.observer=null)},presetChecked:function(a){a.checked&&(this.toggle(this.getParent(a),!0,"checked"),"radio"===this.type&&(this.old=a))},presetDisabled:function(a){a.disabled&&this.toggle(this.getParent(a),!0,"disabled")},change:function(a){this.toggle(this.getParent(a),a.checked,"checked");"radio"===this.type&&this.old&&
this.old!==a&&this.toggle(this.getParent(this.old),this.old.checked,"checked");this.old=a},focus:function(a){this.toggle(this.getParent(a),!0,"focused")},blur:function(a){this.toggle(this.getParent(a),!1,"focused")},toggle:function(a,e,b){e?a.addClass(b):a.removeClass(b)},getParent:function(a){return f.data(a,"icr-parent")},createHTML:function(){var a=[],e=[],b=[],q=[],h=[],l=[],n=[],r=[],p=this,u=function(a){var b=[];a=a[0].childNodes;var d,c;for(c=0;c<a.length;c++)b.push(a[c]);for(;b.length;){d=
b[0];if(3===d.nodeType){if(a=d.nodeValue.trim())break}else if(1===d.nodeType)for(a=d.childNodes,c=0;c<a.length;c++)b.push(a[c]);Array.prototype.splice.call(b,0,1)}b=d.parentNode.innerHTML;0<=b.indexOf("<input")&&(d=b.indexOf("<input"),b=b.slice(d),d=b.indexOf(">"),b=b.slice(d+1).trim());return b};this.$group.each(function(g){var m,d=f(this);m=d.prop("className");var c=d.prop("type"),k=d.prop("name"),p=d.prop("value"),v=d.prop("checked"),w=d.prop("disabled"),t=d.prop("id");a.push(m);e.push(c);b.push(k);
q.push(p);l.push(v);n.push(w);m=t?f("label[for='"+t+"']"):d.closest("label");h.push(u(m));c='<label class="icr-label">   <span class="icr-item type_{type}"></span>   <span class="icr-hidden"><input class="icr-input {class_list}" type="{type}" name="{name}" value="{value}" {disabled} {checked} /></span>   <span class="icr-text">{text}</span></label>'.replace(/\{class_list\}/,a[g]);c=c.replace(/\{type\}/g,e[g]);c=c.replace(/\{name\}/,b[g]);c=c.replace(/\{value\}/,q[g]);c=c.replace(/\{text\}/,h[g]);
c=n[g]?c.replace(/\{disabled\}/,"disabled"):c.replace(/\{disabled\}/,"");c=l[g]?c.replace(/\{checked\}/,"checked"):c.replace(/\{checked\}/,"");m.after(c);g=m.next();r.push(g[0]);d.remove();m.remove()});this.$group=f(r).find("input");this.$group.each(function(a){p.group[a]=this;k[b[0]][a]=this});this.prepare()}};f.fn.ionCheckRadio=function(){var a,e=[],b,f;for(a=0;a<this.length;a++)b=this[a],f=b.name,"radio"!==b.type&&"checkbox"!==b.type||!f?h.console&&h.console.warn&&h.console.warn("Ion.CheckRadio: Some inputs have wrong type or absent name attribute!"):
(k[f]={type:b.type,content:[]},e.push(b));for(a=0;a<e.length;a++)b=e[a],f=b.name,k[f].content.push(b);for(a in k)l[a]&&(l[a].destroy(),l[a]=null),l[a]=new n(k[a])}}})(jQuery,window);