-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajsf.min.js
6 lines (5 loc) · 8.55 KB
/
ajsf.min.js
1
2
3
4
5
6
/**
ajsf 0.0.1-20230822
https://github.com/tvrzna/ajsf
**/
Ajsf={init:function(a,s,t){for(var e in s.find("[ajsf-bind]").each(function(t,e){var n,r=$(e),i=r.attr("ajsf-bind");if(void 0===Ajsf.digObject(a,i)&&Ajsf.digObject(a,i,""),"INPUT"===r.prop("tagName")||"TEXTAREA"===r.prop("tagName")||"SELECT"===r.prop("tagName")){if("TEXTAREA"===r.prop("tagName"))n="input";else switch(r.prop("type")){case"text":case"password":case"email":case"url":case"search":case"number":n="input";break;case"submit":break;default:n="change"}void 0!==n&&r.on(n,function(){Ajsf.digObject(a,i,Ajsf.getVal(e)),Ajsf.refresh(a,s,i)})}}),createEvent=function(r){s.find("["+r+"]").each(function(t,e){var e=$(e),n=e.attr(r);e.on(Ajsf.basicEvents[r],function(t){var e=Ajsf.digObject(a,n,void 0,!0);"function"==typeof e[0]&&e[0](t,...e[1],$(this))})})},Ajsf.basicEvents)createEvent(e);Ajsf.refresh(a,s,void 0,t)},basicEvents:{"ajsf-blur":"blur","ajsf-change":"change","ajsf-click":"click","ajsf-keydown":"keydown","ajsf-keyup":"keyup","ajsf-mousedown":"mousedown","ajsf-mouseup":"mouseup","ajsf-submit":"submit"},convertToObject:function(str){return JSON.parse(JSON.stringify(eval("("+str+")")))},digObject:function(t,e,n,r){var i,a=e.match(/(([^\|]+)|([^\'\|]*\'[^\']*\'[^\'\|]+))/g),s=!1,o=[];if((u=a[0].trim()).startsWith("'")&&u.endsWith("'"))i=u.substring(1,u.length-1);else if(u.startsWith("!")&&(u=u.substring(1).trim(),s=!0),"false"===u||"true"===u)i="true"===u;else if("undefined"===u)i=void 0;else if(isNaN(u)){var c=t.context;if(u.startsWith("root()."))for(u=u.substring(7).trim();"function"==typeof c.parent;)c=c.parent();for(;u.startsWith("parent().");)u=u.substring(9).trim(),"function"==typeof c.parent&&(c=c.parent());var f=/(([^'\.\(\)\[\]]+)\((([^\)]*\'[^\']*\'[^\)]*)|([^\']*))\))/g.exec(u);if(null!==f&&void 0!==f[0])for(var d=f[3],u=f[2],v=/\s?([^',]+|([^']+\(.*\))|\'([^']*)\')\s?,?\s?/g;null!==(l=v.exec(d));){var l=Ajsf.digObject(t,l[1],n,!0);o.push("function"==typeof l[0]?l[0](...l[1]):l[0])}for(var j=/\[([^\]]*\]*)\]/g,p=u.split("."),h=0;h<p.length-1;h++){var b=p[h].substring(0,p[h].indexOf("["));j.lastIndex=0;var g=j.exec(p[h]);null!==g&&void 0!==g[1]?(g=Ajsf.digObject(t,g[1]),c=c[b][g]):void 0!==c[p[h]]&&(c=c[p[h]])}j.lastIndex=0;e=j.exec(p[h]);if(null!==e&&void 0!==e[1]){f=p[h].substring(0,p[h].indexOf("[")),e=Ajsf.digObject(t,e[1]);if(void 0!==n)return void(c[f][e]=n);i=c[f][e]}else{if(void 0!==n)return void(c[p[h]]=n);i=c[p[h]]}}else i=Number(u);if("function"!=typeof i||r||(i=i(...o)),s&&(i=!i),r)return[i,o];for(var x=1;x<a.length;x++)void 0!==a[x]&&(i=Ajsf.filter(t,a[x].trim(),i));return i},filter:function(t,e,n){if(null==n)return n;var r,i,a=(e=e.replace(/\s\s+/g," ")).split(" ")[0];for(i in 0<e.indexOf(" ")&&void 0!==(r=e.substring(e.indexOf(" ")).trim())&&(r=r.startsWith("'")&&r.endsWith("'")?r.substring(1,r.length-1):r.startsWith("{")&&r.endsWith("}")?Ajsf.convertToObject(r):isNaN(r)?Ajsf.digObject(t,r):Number(r)),t.filters)if(i.toLowerCase()===a.toLowerCase()){var s=void 0!==t.context.parent?t.context.parent():t.context;return t.filters[i].definition(s,n,r)}switch(a.toLowerCase()){case"gt":return r<n;case"gte":return r<=n;case"lt":return n<r;case"lte":return n<=r;case"eq":return n==r;case"ne":return n!=r;case"limit":return n.slice(0,Number(r));case"and":return n&&r;case"or":return n||r;case"prefix":return r+n;case"suffix":return n+r;case"then":return n?r:n;case"else":return n?n:r;case"filter":if("object"==typeof n&&0<(""+n.constructor).toString().indexOf("Array")){var o,c=[];for(o in n){var f=n[o];if("string"==typeof f)f===r&&c.push(f);else if("object"==typeof f)for(var d in Object.keys(r).length||c.push(f),r)void 0!==r[d]&&f[d]!==r[d]||c.push(f)}return c}return n;default:return console.warn("Filter "+a+" is not implemented yet."),n}},randomString:function(e){var n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r="";for(let t=0;t<e;t++)r+=n.charAt(Math.floor(Math.random()*n.length));return r},refresh:function(d,t,r,e){var n=":not([ajsf-repeated]) > ";t.is("[ajsf-repeated]")&&(n="",void 0!==e&&(n='[ajsf-repeated="'+e+'"]',t=t.parent()));var i,u="";for(i in void 0!==r&&void 0!==Ajsf.digObject(d,r)&&(u='="'+r+'"'),t.find(n+"[ajsf-bind"+u+"]").each(function(t,e){var n=r;""===u&&(n=$(e).attr("ajsf-bind")),Ajsf.setVal(e,Ajsf.digObject(d,n))}),t.find(n+"[ajsf-hide"+u+"]").each(function(t,e){var n=$(e),e=r;""===u&&(e=n.attr("ajsf-hide")),Ajsf.digObject(d,e)?n.hide():n.show()}),t.find(n+"[ajsf-show"+u+"]").each(function(t,e){var n=$(e),e=r;""===u&&(e=n.attr("ajsf-show")),Ajsf.digObject(d,e)?n.show():n.hide()}),t.find(n+"[ajsf-class]").each(function(t,e){var n,r=$(e),i=Ajsf.convertToObject(r.attr("ajsf-class"));for(n in i)Ajsf.digObject(d,i[n])?r.hasClass(n)||r.addClass(n):r.removeClass(n)}),t.find(n+"[ajsf-style]").each(function(t,e){var n,r=$(e),i=Ajsf.convertToObject(r.attr("ajsf-style"));for(n in i){var a=Ajsf.digObject(d,i[n]);r.each(function(t,e){e.style[n]=a||""})}}),void 0===e&&t.find(n+"[ajsf-repeated"+u+"]").each(function(t,e){$(e).remove()}),t.find(n+"[ajsf-title"+u+"]").each(function(t,e){var n=$(e),e=r;""===u&&(e=n.attr("ajsf-title")),n.attr("title",Ajsf.digObject(d,e))}),t.find(n+"[ajsf-text"+u+"]").each(function(t,e){var n=$(e),e=r;""===u&&(e=n.attr("ajsf-text")),n.text(Ajsf.digObject(d,e))}),t.find(n+"[ajsf-repeat"+u+"]").each(function(t,e){var n=$(e),r=void 0;n.hide(),""===u&&(r=n.attr("ajsf-repeat"));var i=Ajsf.digObject(d,r),a="OPTION"===n.prop("tagName");if(void 0!==i){for(var s in i){var o=Ajsf.randomString(6),c=n.clone().removeAttr("ajsf-repeat").attr("ajsf-repeated",a?o:"true").show().insertBefore(n),f={context:{parent:function(){return d.context},root:function(){for(var t=f.context;"function"==typeof t.parent;)t=t.parent();return t},item:i[s]},directives:d.directives,filters:d.filters,attributes:d.attributes};"object"==typeof f.context.item&&(f.context.item.index=s),Ajsf.init(f,c,a?o:void 0)}!a||(e=n.parent()).attr("ajsf-bind")&&(r=e.attr("ajsf-bind"),Ajsf.setVal(e[0],Ajsf.digObject(d,r)))}}),d.attributes)t.find(n+"["+i+u+"]").each(function(t,e){var n=r;""===u&&(n=$(e).attr(i)),d.attributes[i].definition(e,Ajsf.digObject(d,n),d.context)});for(i in d.directives){void 0===d.context.directiveInstances&&(d.context.directiveInstances=[]);function a(t){var e=$(t),n=e.attr("ajsf-model"),r=void 0;null!==n&&(r=Ajsf.digObject(d,n)),e.html(s.template);var i={context:{model:r,parent:function(){return d.context},root:function(){for(var t=i.context;"function"==typeof t.parent;)t=t.parent();return t},refresh:function(t){t||"function"!=typeof i.context.onRefresh||i.context.onRefresh(),Ajsf.refresh(i,e)}},directives:d.directives,filters:d.filters,attributes:d.attributes,element:t};d.context.directiveInstances.push(i),void 0!==s.definition&&(i.context=Object.assign(i.context,s.definition(i.context,t))),Ajsf.init(i,e)}var s=d.directives[i];t.prop("tagName")!==i.toUpperCase()||null===t.attr("ajsf-repeat")&&null===t.attr("ajsf-repeated")?t.find(i).each(function(t,e){var n=$(e);""===n.html()&&null===n.attr("ajsf-repeat")&&null===n.attr("ajsf-repeated")&&a(e)}):""===t.html()&&a(t[0])}if(void 0!==d.context.directiveInstances&&0<d.context.directiveInstances.length){for(var o=d.context.directiveInstances.length-1;0<=o;o--)void 0!==d.context.directiveInstances[o]&&(void 0===d.context.directiveInstances[o].element||document.body.contains(d.context.directiveInstances[o].element))||d.context.directiveInstances.splice(o,1);for(o in d.context.directiveInstances)void 0!==d.context.directiveInstances[o]&&void 0!==d.context.directiveInstances[o].context&&d.context.directiveInstances[o].context.refresh()}},getVal:function(t){var e=$(t);switch(e.prop("type")){case"checkbox":return e.prop("checked");case"radio":for(var n=e.attr("ajsf-bind"),r=$("input[ajsf-bind="+n+"][type=radio]"),i=0;i<=r.length;i++)if((e=$(r[i])).prop("checked"))return e.val();return null;default:return e.val()}},setVal:function(t,e){var n=$(t);if("INPUT"===n.prop("tagName"))switch(n.prop("type")){case"checkbox":n.prop("checked",e);break;case"radio":n.prop("checked",t.value==e);break;default:n.val(e)}else"TEXTAREA"===n.prop("tagName")||"SELECT"===n.prop("tagName")?n.val(e):("OPTION"===n.prop("tagName")&&n.attr("value",e),n.text(e))}},window.ajsf=function(t,e){var n=$('[ajsf="'+t+'"]'),r={context:{rootElement:rootElement=n[0]},directives:{},directive:function(t,e,n){this.directives[t]={name:t,template:e,definition:n}},filters:{},filter:function(t,e){this.filters[t]={name:t,definition:e}},attributes:{},attribute:function(t,e){this.attributes[t]={name:t,definition:e}}};return r.context.refresh=function(){Ajsf.refresh(r,n)},r.context=Object.assign(r.context,e(r.context,n[0])),r.context.rootElement=n[0],$(document).ready(function(){Ajsf.init(r,n)}),r};