UNPKG

alejs

Version:

a Flexible and fast JavaScript view framework.

8 lines (7 loc) 13.2 kB
/*! Ale.js JavaScript Framework * Version: 1.2.1 * (c) 2018 Dong Yingxuan (Bill Dong) * Released under the MIT License */ (function(E,h){var I="1.2.1",F=0,u=[],D=[],H=[],b=[],m=[],A=[],l=0,p=false,j=null,a={html:false,diff:true,async:true,configLock:false};E.Ale=function(O,N){if(typeof O!=="string"||!w(N)){if(!w(O)){throw"Ale.js: Wrong type of parameters was used when creating component objects!"}else{var M=v(O);if(M!==undefined){return new x(M)}}}if(u[O]!==undefined){throw"Ale.js: This component has been created!"}var M=v(N);if(M!==undefined){u[O]=M}};Ale.render=function(N,M){var O=s(u[N]);return K(O,M)};Ale.config=function(N,O){if(a["configLock"]){throw"Ale.js: The configuration function is locked, no changes are allowed!"}if(w(N)){for(var M in N){a[M]=N[M]}}else{if(typeof N==="string"&&typeof O==="boolean"){a[N]=O}else{throw"Ale.js: The parameter type is wrong when calling the config function!"}}};Ale.isHasThisComponent=function(M){if(u[M]===undefined){return false}return true};Ale.use=function(M){if(typeof M.name!=="string"||typeof M.install!=="function"){throw"Ale.js: Some fields were not added when the plugin was registered!"}if(D[M.name]!==undefined){throw"Ale.js: This plugin has been registered!"}D[M.name]=M.name;M.install()};Ale.filter=function(M){if(typeof M!=="function"&&!Array.isArray(M)){throw"Ale.js: You need to pass in a function or array when adding a filter!"}if(typeof M==="function"){H.push(M)}else{if(Array.isArray(M)){H=H.concat(M)}}};Ale.nextTick=function(M){if(typeof M==="function"){j=M}else{throw"Ale.js: You need to pass in a function when adding a tick callback function!"}};Ale.plugin={inject:function(M){if(typeof M!=="function"){throw"Ale: The wrong data type for plug-in injection!"}b.push(M)},mixin:function(M){if(typeof M!=="function"){throw"Ale: The wrong data type for plug-in injection!"}m.push(M)}};Ale.version=I;function x(M){this.config=M;this.render=function(N){return K(s(this.config),N)}}function t(P,O){if(typeof O!=="undefined"){if(typeof O==="function"){if(!(P instanceof O)){throw"Ale.js: Incorrect component data type!"}}else{if(Array.isArray(O)){var N=false;for(var M=0;M<O.length;M++){if(typeof O[M]==="function"){if(P instanceof O[M]){N=true;break}}else{if(typeof P===O[M]){N=true;break}}}if(!N){throw"Ale.js: Incorrect component data type!"}}else{if(typeof P!==O){throw"Ale.js: Incorrect component data type!"}}}}}function G(P,M){this.id="i"+F++;this.el=P;this.nodeList=h.querySelectorAll(this.el);this.config=M;this.template=M.template;this.data=M.data;this.staticData=M.staticData;this.dataType=M.dataType;this.methods=w(M.methods)?M.methods:{};this.life=M.life;this.type=M.type;this.imports=M.imports;this.proxy=w(M.proxy)?M.proxy:{};this.watch=w(M.watch)?M.watch:{};this.name=typeof M.name==="string"?M.name:"unknow";if(!w(this.dataType.data)){this.dataType.data={}}if(!w(this.dataType.staticData)){this.dataType.staticData={}}for(var N in this.data){t(this.data[N],this.dataType.data[N]);this.data[N]=C(this.data[N])}for(var N in this.staticData){t(this.staticData[N],this.dataType.staticData[N]);this.staticData[N]=C(this.staticData[N])}this.life=c(this.life,this);this.methods=B(this.methods,this);this.proxy=y(this.proxy,this);var O=typeof this.data==="function"?this.data():s(this.data);this.data=typeof this.data==="function"?this.data():this.data;var Q=typeof this.staticData==="function"?this.staticData():s(this.staticData);this.staticData=typeof this.staticData==="function"?this.staticData():this.staticData;(function(T,U,V,S,R){Object.defineProperty(T,"staticData",{set:function(Y){if(typeof U.staticDataUpdating==="function"){U.staticDataUpdating()}var X=n("static",S.setter,T,Y);Y=X.val;for(var W in Y){t(Y[W],R.staticData[W]);Q[W]=C(Y[W])}},get:function(){return n("static",S.getter,T,V).val}})})(this,this.life,this.staticData,this.proxy,this.dataType);for(var N in this.staticData){(function(T,U,V,S,R){Object.defineProperty(V,T,{set:function(X){t(X,R.staticData[T]);if(typeof U.staticDataUpdating==="function"){U.staticDataUpdating()}var W=n("static",S.setter,T,X);X=W.val;Q[T]=C(X)},get:function(){return n("static",S.getter,T,Q[T]).val}})})(N,this.life,this.staticData,this.proxy,this.dataType)}(function(X,S,W,U,ad,V,ac,aa,R,Z,T,ab,Y){Object.defineProperty(X,"data",{set:function(ag){var af=n("normal",Z.setter,X,ag);ag=af.val;for(var ae in ag){t(ag[ae],ab.data[ae]);O[ae]=C(ag[ae])}if(af.needBreak){return}if(typeof ac.updating==="function"){ac.updating()}o(X,W,U,{id:S,type:aa,nodeList:V,newData:O,template:ad,imports:R,life:ac,staticData:T})},get:function(){return n("normal",Z.getter,X,W).val}})})(this,this.id,this.data,this.methods,this.template,this.nodeList,this.life,this.type,this.imports,this.proxy,this.staticData,this.dataType,this.watch);for(var N in this.data){(function(Y,T,S,X,V,ad,W,ac,aa,R,Z,U,ab){Object.defineProperty(X,Y,{set:function(af){t(af,ab.data[Y]);var ae=n("normal",Z.setter,Y,af);af=ae.val;O[Y]=C(af);if(ae.needBreak){return}if(typeof ac.updating==="function"){ac.updating()}o(T,X,V,{id:S,type:aa,nodeList:W,newData:O,template:ad,imports:R,life:ac,staticData:U})},get:function(){return n("normal",Z.getter,Y,O[Y]).val}})})(N,this,this.id,this.data,this.methods,this.template,this.nodeList,this.life,this.type,this.imports,this.proxy,this.staticData,this.dataType)}d(this,this.data,this.methods,{id:this.id,type:this.type,nodeList:this.nodeList,template:this.template,imports:this.imports,life:this.life,staticData:this.staticData});if(typeof this.life.mounting==="function"){this.life.mounting()}}G.prototype.destroy=function(){for(var M in this.data){(function(O,P){Object.defineProperty(P,O,{set:function(){},get:function(){return undefined}})})(M,this.data)}for(var M=0;M<this.nodeList.length;M++){this.nodeList[M].innerHTML=""}if(typeof this.life.unmounting==="function"){this.life.unmounting()}var N=this.life.destroy;for(var M in this){delete this[M]}if(typeof N==="function"){N()}};function K(O,M){if(O===undefined){throw"Ale.js: This component has not been created yet!"}if(typeof M.el!=="string"){throw"Ale.js: The type of the el should be a string!"}if(typeof O.life.using==="function"){O.life.using()}if(w(M.proxy)){O.proxy=M.proxy}O.data=typeof O.data==="function"?O.data():O.data;if(!w(O.data)){O.data={}}for(var N in M.data){O.data[N]=M.data[N]}O.staticData=typeof O.staticData==="function"?O.staticData():O.staticData;if(!w(O.staticData)){O.staticData={}}for(var N in M.staticData){O.staticData[N]=M.staticData[N]}for(var N in M.methods){O.methods[N]=M.methods[N]}return new G(M.el,O)}function v(P){for(var U=0;U<m.length;U++){P=m[U](P)}if(P.template===undefined){throw"Ale.js: No template is added when creating a component object!"}P.life=w(P.life)?P.life:{};P.dataType=w(P.dataType)?P.dataType:{};if(!Array.isArray(P.imports)){P.type=1}else{P.type=2;P.template=[P.template];var Y,W,O,Z={},Q={},R={},X={},N={},M={},T={},S=[];for(var U=0;U<P.imports.length;U++){if(typeof P.imports[U]==="string"){Y=u[P.imports[U]]}else{if(typeof P.imports[U]==="object"){Y=P.imports[U].config}}if(Y===undefined){throw"Ale.js: The imported component is not created!"}if(Y.type===1){P.template.push(Y.template)}else{P.template=P.template.concat(Y.template);S=S.concat(Y.imports)}W=typeof Y.data==="function"?Y.data():Y.data;O=typeof Y.staticData==="function"?Y.staticData():Y.staticData;if(w(W)){for(var V in W){Z[V]=W[V]}}if(w(Y.methods)){for(var V in Y.methods){Q[V]=Y.methods[V]}}if(w(Y.life)){for(var V in Y.life){R[V]=Y.life[V]}}if(w(Y.proxy)){for(var V in Y.proxy){X[V]=Y.proxy[V]}}if(w(Y.dataType.data)){for(var V in Y.dataType.data){M[V]=Y.dataType.data[V]}}if(w(Y.dataType.staticData)){for(var V in Y.dataType.staticData){T[V]=Y.dataType.staticData[V]}}if(w(O)){for(var V in O){N[V]=O[V]}}}P.imports=P.imports.concat(S);P.data=typeof P.data==="function"?P.data():P.data;P.staticData=typeof P.staticData==="function"?P.staticData():P.staticData;if(w(P.data)){for(var U in P.data){Z[U]=P.data[U]}}if(w(P.methods)){for(var U in P.methods){Q[U]=P.methods[U]}}if(w(P.life)){for(var U in P.life){R[U]=P.life[U]}}if(w(P.proxy)){for(var U in P.proxy){X[U]=P.proxy[U]}}if(w(P.dataType.data)){for(var U in P.dataType.data){M[U]=P.dataType.data[U]}}if(w(P.dataType.staticData)){for(var U in P.dataType.staticData){T[U]=P.dataType.staticData[U]}}if(w(P.staticData)){for(var U in P.staticData){N[U]=P.staticData[U]}}P.data=Z;P.methods=Q;P.life=R;P.proxy=X;P.dataType.data=M;P.dataType.staticData=T;P.staticData=N}return P}function n(Q,M,P,T){if(typeof M==="function"){var S=false;function R(){S=true}var N={type:Q,key:P,val:T,preventUpdating:R},O=M(N);return{val:O,needBreak:S}}return{val:T,needBreak:false}}function f(M){return/native code/.test(M.toString())&&typeof M!=="undefined"}function z(M){for(var O in M){for(var N=0;N<M[O].nodeList.length;N++){if(M[O].diff){g(M[O].renderingResultObj.newEl.childNodes,M[O].nodeList[N].childNodes,M[O].nodeList[N],M[O].renderingResultObj.newTem)}else{M[O].nodeList[N].innerHTML=M[O].renderingResultObj.newTem}k(M[O].nodeList[N],M[O].data,M[O].methods)}}}function L(){if(typeof j==="function"){var M=j;j=null;M()}if(l===0){p=false}else{J(A);A=[];l=0}}function J(M){p=true;(function(N){if(typeof Promise!=="undefined"&&f(Promise)){new Promise(function(P,O){P()}).then(function(){z(N);L()})}else{setTimeout(function(){z(N);L()},0)}})(M)}function r(O,N,P){if(a.async){P.data=O;P.methods=N;A[P.id.toString()]=P;l++;if(!p){J(A);A=[];l=0}}else{for(var M=0;M<P.nodeList.length;M++){if(P.diff){g(P.renderingResultObj.newEl.childNodes,P.nodeList[M].childNodes,P.nodeList[M],P.renderingResultObj.newTem)}else{P.nodeList[M].innerHTML=P.renderingResultObj.newTem}k(P.nodeList[M],O,N)}}}function e(Q){var N={data:Q.data,methods:Q.methods,staticData:Q.staticData};if(Q.type===1){var P=typeof Q.template==="function"?i(Q.template,N)():Q.template,R=h.createElement("div");for(var M=0;M<H.length;M++){P=H[M](P)}R.innerHTML=P}else{var P=Q.template[0],O,R=h.createElement("div");if(typeof P==="function"){P=i(P,N)()}for(var M=1;M<Q.template.length;M++){O=new RegExp("<"+(typeof Q.imports[M-1]==="string"?Q.imports[M-1]:Q.imports[M-1].config.name)+"/>","g"); if(typeof Q.template[M]==="function"){P=P.replace(O,i(Q.template[M],N)())}else{P=P.replace(O,Q.template[M])}}for(var M=0;M<H.length;M++){P=H[M](P)}R.innerHTML=P}if(typeof Q.life.rendering==="function"){Q.life.rendering()}return({newEl:R,newTem:P})}function q(N){for(var M=0;M<b.length;M++){b[M](N)}}function d(N,P,M,Q){Q.data=P;Q.methods=M;var O=e(Q);r(P,M,{id:Q.id,nodeList:Q.nodeList,renderingResultObj:O,diff:false});q(N)}function o(N,P,M,Q){Q.data=Q.newData;Q.methods=M;var O=e(Q);r(P,M,{id:Q.id,nodeList:Q.nodeList,renderingResultObj:O,newData:Q.newData,diff:true});q(N)}function g(M,N,V,U){if(!a.diff){V.innerHTML=U}if(M.length===0){V.innerHTML="";return}var S=0;for(var O=0;O<M.length;O++){while(M[O+1]===undefined&&N[O+1]!==undefined){N[O+1].remove()}if(N[O]===undefined){if(V.nodeName==="#text"){if(M[O].nodeName==="#text"){V.nodeValue=M[O].nodeValue}else{V.parentNode.replaceChild(M[O],V)}}else{V.appendChild(M[O].cloneNode(true))}}else{if(M[O].nodeName==="#text"){if(N[O].nodeName==="#text"){if(M[O].nodeValue!==N[O].nodeValue){N[O].nodeValue=M[O].nodeValue}}else{if(N[O].innerHTML!==M[O].nodeValue){N[O].innerHTML=M[O].nodeValue}}}else{if(N[O].nodeName!=="#text"){var W=(N[O].tagName!==M[O].tagName);if(W){V.replaceChild(M[O].cloneNode(true),N[O]);continue}else{var P=M[O].attributes,Q=s(N[O].attributes),T;if(P.length>Q.length){for(var R=0;R<P.length;R++){if(Q[R]===undefined||Q[R].name!==P[R].name||Q[R].value!==P[R].value){N[O].removeAttribute(Q[R].name);N[O].setAttribute(P[R].name,P[R].value)}}}else{for(var R=0;R<Q.length;R++){if(P[R]===undefined){N[O].removeAttribute(Q[R].name)}else{if(P[R].name!==Q[R].name||P[R].value!==Q[R].value){N[O].removeAttribute(Q[R].name);N[O].setAttribute(P[R].name,P[R].value)}}}}}if(M[O].childNodes.length>0){g(M[O].childNodes,N[O].childNodes,V.childNodes[O])}else{if(N[O].childNodes.length>0){N[O].innerHTML=""}}}else{V.replaceChild(M[O],N[O])}}}}}function C(M){if(typeof M==="string"&&!a.html){M=M.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}return M}function y(N,O){for(var M in N){N[M]=i(N[M],O)}return N}function B(M,O){for(var N in M){M[N]=i(M[N],O)}return M}function c(O,N){for(var M in O){O[M]=i(O[M],N)}return O}function k(O,P,M){O.data=P;O.methods=M;if(O.children.length>0){for(var N=0;N<O.children.length;N++){k(O.children[N],P,M)}}}function s(O){var M=Array.isArray(O)?[]:{};if(Array.isArray(O)){for(var N=0;N<O.length;N++){if(w(O[N])||Array.isArray(O[N])){M.push(s(O[N]))}else{M.push(O[N])}}}else{for(var N in O){if(w(O[N])||Array.isArray(O[N])){M[N]=s(O[N])}else{M[N]=O[N]}}}return M}function w(P){var O,N,M=function(){return{}};if(!P||M().toString.call(P)!=="[object Object]"){return false}O=Object.getPrototypeOf(P);if(!O){return true}N=M.hasOwnProperty.call(O,"constructor")&&O.constructor;return typeof N==="function"&&M.hasOwnProperty.toString(N)===M.hasOwnProperty.toString(Object)}function i(O,M){function N(Q){var P=arguments.length;return P?P>1?O.apply(M,arguments):O.call(M,Q):O.call(M)}N._length=O.length;return N}})(window,document);