-
Notifications
You must be signed in to change notification settings - Fork 1
/
beth.min.js
1 lines (1 loc) · 9.74 KB
/
beth.min.js
1
$(document,window,void 0).ready(function(){Beth._refreshInterval=70,Beth._init()});class Beth{constructor(){this._init()}static _init(){Beth._initialized=!1,Beth.loops={},Beth._initDynamicHtml(),Beth._bindDirectives(),Beth._initInputValues(),Beth._bindVariables(),Beth._initialized=!0}static _initDynamicHtml(){Beth.binds=[];var e=document.documentElement.innerHTML,t=e,a=e.match(/\{\{.*\}\}/g);null==a&&(a=[]);for(var i=0,r=a.length;i<r;i++){var n,h=a[i],s=$.trim(Beth._extractTextBetween(h,"{{","}}"));t=e,void 0!==(n=Beth._parseValue(s))?(Beth.binds.push(s),t=e.replace(h,'<beth data-bind="'+(Beth.binds.length-1)+'">'+Beth._unescapeReal(n)+"</beth>")):$('[beth-bind="'+s+'"][beth-default]').length>0?(Beth._declareVar(s,$('[beth-bind="'+s+'"][beth-default]').first().attr("beth-default")),Beth.binds.push(s),t=e.replace(h,'<beth data-bind="'+(Beth.binds.length-1)+'">'+Beth._unescapeReal($('[beth-bind="'+s+'"][beth-default]').first().attr("beth-default"))+"</beth>")):t=e.replace(h,""),e=t}document.documentElement.innerHTML=t}static _bindVariables(){$("textarea[beth-bind], input[beth-bind][type!='radio'][type!='checkbox'], select[beth-bind]").each(function(){var bethvar_name=$(this).attr("beth-bind");if(Beth._isVarDefined(bethvar_name))try{var bethvar_value=eval(bethvar_name);bethvar_value!=$(this).val()&&(eval(bethvar_name+" = $(this).val()"),Beth._triggerEvent($(this),"change"))}catch(e){console.warn("Beth: Variable not defined",e)}}),$("input[beth-bind][type='radio']").each(function(){var bethvar_name=$(this).attr("beth-bind");if($(this).is(":checked")&&Beth._isVarDefined(bethvar_name))try{var bethvar_value=eval(bethvar_name);bethvar_value!=$(this).val()&&(eval(bethvar_name+" = $(this).val()"),Beth._triggerEvent($(this),"change"))}catch(e){console.warn("Beth: Variable not defined",e)}}),$("input[beth-bind][type='checkbox']").each(function(){var bethvar_name=$(this).attr("beth-bind");if(Beth._isVarDefined(bethvar_name))try{var bethvar_value=eval(bethvar_name);bethvar_value!=$(this).is(":checked")&&(eval(bethvar_name+" = $(this).is(':checked')"),Beth._triggerEvent($(this),"change"))}catch(e){console.warn("Beth: Variable not defined",e)}}),$("beth").each(function(){try{var newHtml=eval(Beth.binds[parseInt($(this).data("bind"))])}catch(e){var newHtml=""}$(this).html()!=newHtml&&$(this).html(Beth._unescapeReal(newHtml))}),setTimeout(function(){Beth._bindVariables()},Beth._refreshInterval)}static _initInputValues(){$("textarea[beth-default]:not([beth-bind]), input[beth-default]:not([beth-bind]), select[beth-default]:not([beth-bind])").each(function(){try{var bethvar_name=$(this).val(eval($(this).attr("beth-default")))}catch(e){var bethvar_name=$(this).val($(this).attr("beth-default"))}}),$("textarea[beth-bind], input[beth-bind][type!='radio'][type!='checkbox'], select[beth-bind]").each(function(){try{var bethvar_name=$(this).attr("beth-bind");Beth._declareVar(bethvar_name,$(this).attr("beth-default")),$(this).val(Beth._unescapeReal(eval($(this).attr("beth-bind"))))}catch(e){$(this).removeAttr("beth-bind"),console.warn("Beth: Variable not defined",e)}}),$("input[beth-bind][type='radio']").each(function(){try{var bethvar_name=$(this).attr("beth-bind");Beth._declareVar(bethvar_name,$(this).attr("beth-default")),$(this).prop("checked",Beth._unescapeReal(eval($(this).attr("beth-bind")))==$(this).val())}catch(e){$(this).removeAttr("beth-bind"),console.warn("Beth: Variable not defined",e)}}),$("input[beth-bind][type='checkbox']").each(function(){try{var bethvar_name=$(this).attr("beth-bind");Beth._declareVar(bethvar_name,$(this).attr("beth-default")),$(this).prop("checked",!!eval($(this).attr("beth-bind")))}catch(e){$(this).removeAttr("beth-bind"),console.warn("Beth: Variable not defined",e)}}),$("beth-include, beth-include-async").each(function(){try{var e=$(this),t=e.attr("url"),a="beth-include-async"===e.prop("tagName");$.get({async:a,url:t,error:function(e,t,a){console.warn("Beth: Error downloading include data: Server responded with status code ["+e.status+"] "+e.statusText)},success:function(t,a,i){e.replaceWith(i.responseText)}})}catch(e){$(this).removeAttr("beth-bind"),console.warn("Beth: Error downloading include data",e)}})}static _bindDirectives(){$("[beth-hide]").each(function(){try{var hide=eval($(this).attr("beth-hide"));1==hide?$(this).hide():$(this).show()}catch(e){$(this).removeAttr("beth-hide"),console.warn("Beth: Invalid expression inside 'Hide' directive",e)}}),$("[beth-show]").each(function(){try{var show=eval($(this).attr("beth-show"));1==show?$(this).show():$(this).hide()}catch(e){$(this).removeAttr("beth-show"),console.warn("Beth: Invalid expression inside 'Show' directive",e)}}),$("[beth-disable]").each(function(){try{var disable=eval($(this).attr("beth-disable"));1==disable?$(this).prop("disabled",!0):$(this).prop("disabled",!1)}catch(e){$(this).removeAttr("beth-disable"),console.warn("Beth: Invalid expression inside 'Disable' directive",e)}}),$("[beth-if]").each(function(){try{var result=eval($(this).attr("beth-if"));1!=result&&$(this).remove()}catch(e){console.warn("Beth: Invalid expression inside 'If' directive",e)}$(this).removeAttr("beth-if")}),$("[beth-click]").each(function(){$(this).off("click"),$(this).on("click",function(){Beth._triggerEvent($(this),"click")})}),$("[beth-inside]").each(function(){let e="";try{e=Beth._parseValue($(this).attr("beth-inside"))}catch(e){console.warn("Beth: Invalid expression inside 'Inside' directive",e)}$(this).text()!==e&&$(this).text(e)}),$("[beth-html-inside]").each(function(){let e="";try{e=Beth._parseValue($(this).attr("beth-html-inside"))}catch(e){console.warn("Beth: Invalid expression inside 'Html-inside' directive",e)}$(this).html()!==e&&$(this).html(e)}),$("[beth-class]").each(function(){try{var added_classes=$(this).attr("beth-added-classes"),new_classes=eval($(this).attr("beth-class"));added_classes!==new_classes&&($(this).removeClass(added_classes),$(this).addClass(new_classes),$(this).attr("beth-added-classes",new_classes))}catch(e){console.warn("Beth: Invalid expression inside 'Class' directive",e)}}),$("[beth-each]").each(function(){var newLoop=!1;if(void 0===$(this).attr("beth-id")){do{var uniqueId=Beth._makeId()}while($("[beth-id="+uniqueId+"]").length>0);$(this).attr("beth-id",uniqueId);var newLoop=!0}try{var uniqueId=$(this).attr("beth-id"),expression=$(this).attr("beth-each"),exploded=expression.split("=>"),loopVarName=exploded[1].trim(),source=eval(exploded[0].trim()),match=!1;if(newLoop?Beth.loops[uniqueId]=jQuery.extend(!0,[],source):JSON.stringify(Beth.loops[uniqueId])===JSON.stringify(source)?match=!0:Beth.loops[uniqueId]=jQuery.extend(!0,[],source),!match){"BETH-IT"!=$(this).parent().prop("tagName")&&$(this).wrap("<beth-it></beth-it>"),$("[beth-loop-id='"+uniqueId+"']").remove(),void 0===source&&(source=[]),0===source.length&&($(this).parent().hide(),console.log("hey"));for(let index=0;index<source.length;index++){const element=source[index];var clone=$(this).clone().removeAttr("beth-each").removeAttr("beth-id").attr("beth-loop-id",uniqueId).unwrap(),cloneHTML=clone.prop("outerHTML"),newHtml=cloneHTML.replace(/\{(.*?)\$index(.*?)\}/g,index),matches=newHtml.match(eval("/{(.*?)\\"+loopVarName+"(.*?)}/g"));null===matches&&(matches=[]);for(let j=0;j<matches.length;j++){const curr_match=matches[j];var realVar=Beth._extractTextBetween(curr_match,"{","}");realVar=realVar.replace(loopVarName,"element"),newHtml=newHtml.replace(curr_match,eval(realVar))}clone.remove(),$(this).parent().hide().before(newHtml)}}}catch(e){$(this).remove(),console.warn("Beth: Invalid expression inside 'Each' directive",e)}}),setTimeout(function(){Beth._bindDirectives()},Beth._refreshInterval)}static _makeId(){for(var e="",t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",a=0;a<5;a++)e+=t.charAt(Math.floor(Math.random()*t.length));return e}static _watchVar(varname){if(varname.indexOf(".")>-1)var varname_arr=varname.split("."),prop=varname_arr.pop(),obj=varname_arr.join("."),realObj=eval(obj);else var prop=varname,realObj=window;realObj.watch(prop,function(e,t,a){realObj.unwatch(e),$("[beth-bind='"+varname+"']").each(function(){"radio"==$(this).prop("type")?$(this).prop("checked",a==$(this).val()):"checkbox"==$(this).prop("type")?$(this).prop("checked",a):$(this).val(Beth._unescapeReal(a)).change()}),realObj[prop]!=a&&(realObj[prop]=a),setTimeout(function(){Beth._watchVar(varname)},Beth._refreshInterval)})}static _triggerEvent(selector,event){var callback=selector.attr("beth-"+event);if(void 0!==callback&&Beth._initialized)try{eval(callback)}catch(e){console.error("Beth: Invalid Statement",e)}}static _unescapeReal(e){for(var t=e;t!==unescape(t);)t=unescape(t);return t}static _declareVar(varname,varvalue=null){if(Beth._watchVar(varname),Beth._isVarDefined(varname))return!1;varvalue=Beth._parseValue(varvalue);try{return eval("window."+varname+'="'+escape(varvalue)+'";'),"null"!=eval("window."+varname)&&"undefined"!=eval("window."+varname)||eval("window."+varname+'="";'),!0}catch(e){return!1}}static _isVarDefined(varname){var valid=!0;try{if(valid=eval("typeof "+varname+" !== 'undefined'"),valid){var varvalue=eval(varname);valid=void 0!==varvalue||-1==Beth.binds.indexOf(varname)}}catch(e){valid=!1}return valid}static _parseValue(varvalue){if(void 0!==varvalue)try{varvalue=eval(varvalue)}catch(e){varvalue=String(varvalue)}else varvalue="";return varvalue}static _extractTextBetween(e,t,a){var i=e;try{return e.split(t)[1].split(a)[0]}catch(e){return i}}}Object.prototype.watch||Object.defineProperty(Object.prototype,"watch",{enumerable:!1,configurable:!0,writable:!1,value:function(e,t){var a=this[e],i=a;delete this[e]&&Object.defineProperty(this,e,{get:function(){return i},set:function(r){return a=i,i=t.call(this,e,a,r)},enumerable:!0,configurable:!0})}}),Object.prototype.unwatch||Object.defineProperty(Object.prototype,"unwatch",{enumerable:!1,configurable:!0,writable:!1,value:function(e){var t=this[e];delete this[e],this[e]=t}});