-
Notifications
You must be signed in to change notification settings - Fork 0
/
DJsonLoader.min.js
7 lines (7 loc) · 2.9 KB
/
DJsonLoader.min.js
1
2
3
4
5
6
7
/*
*DJsonLoader v1.2.0
* Copyright (C) 2023 David Esneyder Jerez Garnica
* esneyderg357@gmail.com
* https://github.com/esneyderg357/DJsonLoader.git
*/
if("undefined"==typeof jQuery)throw new Error("DJsonLoader requires jQuery 1.11.1 or higher.");!function(e){var a={reset:!1,imgPrefix:"",resetString:"",slabel:"",svalue:"",sselected:"",sempty:!1,selabel:"select an option...",sevalue:"",Adata:[],ajax:!1,url:"",data:{},method:"post",onLoad:function(){},onReset:function(){},onError:function(){}};function t(e,a,t){switch(Array.isArray(a)||(a=String(a)),e.prop("tagName").toLowerCase()){case"input":switch(e.attr("type").toLowerCase()){case"radio":e.val()==a&&e.prop("checked","checked");break;case"checkbox":1==a||"true"==a?e.prop("checked","checked"):e.removeAttr("checked");break;default:e.val(a)}break;case"textarea":case"select":e.val(a);break;case"a":e.attr("href",a);break;case"img":e.attr("src",t.imgPrefix+a);break;case"iframe":e.attr("src",a);break;case"ul":case"ol":var r="";if(Array.isArray(a)&&a.length>0)for(var o=0;o<a.length;o++)r+="<li>"+a[o]+"</li>";else r+="<li>"+a+"</li>";e.html(r);break;default:e.html(a)}}function r(a,o,n,s){null!=o&&null!=o&&e.each(o,(function(o,i){var l=typeof i,c=Array.isArray(i);if("object"!=l||c)for(var d=a.find("[name='"+n+o+"']").add(a.find("."+n+o)).add(a.find("[data-djload='"+n+o+"']")),f=0;f<d.length;f++)t(e(d[f]),i,s);else r(a,i,o+".",s)}))}function o(a,t,r){if("select"==t)return a.html(""),void r.onReset(a);"form"==t&&a[0].reset();for(var o=a.find("[data-djload]"),n=0;n<o.length;n++){var s=e(o[n]);switch((t=s.prop("tagName")).toLowerCase()){case"input":case"textarea":case"select":s.val("");break;case"a":s.attr("href","#");break;case"img":case"iframe":s.attr("src","");break;default:s.html(r.resetString)}}r.onReset(a)}function n(a,t,n){var s=e(t),i=s.prop("tagName").toLowerCase();"string"==typeof n&&(n=JSON.parse(n)),1==a.reset&&o(s,i,a),"select"==i?function(e,a,t){if(!Array.isArray(a))throw new Error("Data required for load select must be array.");var r="";if(1==t.sempty&&(r+='<option value="'+t.sevalue+'" selected>'+t.selabel+"</option>"),a.length>0)if("object"==typeof a[0])for(var o=""!=t.sselected,n=0;n<a.length;n++){for(var s=o&&a[n][t.sselected]?" selected ":"",i="",l=0;l<t.Adata.length;l++)i+=" data-"+t.Adata[l]+'="'+a[n][t.Adata[l]]+'" ';r+='<option value="'+a[n][t.svalue]+'" '+i+s+" >"+a[n][t.slabel]+"</option>"}else for(n=0;n<a.length;n++)r+='<option value="'+a[n]+'">'+a[n]+"</option>";e.html(r)}(s,n,a):r(s,n,"",a),a.onLoad(s)}e.fn.djload=function(t,r){var s=e.extend({},a,r);return"reset"==t?this.each((function(a,t){var r=e(t).prop("tagName").toLowerCase();o(e(t),r,s)})):"options"!=t?this.each((function(r,o){s.ajax?function(t,r){var o=e.extend({},a,t);e.ajax({url:o.url,type:o.method,dataType:"json",data:o.data,cache:!1}).done((function(e){n(o,r,e.data)})).fail((function(e){console.log("ajax load error."),o.onError(e)}))}(s,o):n(s,o,t)})):void e.extend(a,r)}}(jQuery);