forked from BytesAngels/jqMVC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaf.mvc.min.js
More file actions
22 lines (22 loc) · 10.2 KB
/
af.mvc.min.js
File metadata and controls
22 lines (22 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(function(c){var h=document.location.protocol+"//"+document.location.host,k=location.href,b="state"in window.history;c.mvc={};c.mvc={};c.mvc._app=null;c.mvc.app=function(){c.mvc._app=this};c.mvc.app.prototype={_loadTimer:null,_modelsReady:!1,_controllersReady:!1,_loadedListeners:[],_modelsLoaded:0,_totalModels:0,_controllersDir:"controllers/",_modelsDir:"models/",_templateType:"text/x-dot-template",_hasModels:!0,_useHistory:!1,_html5Pop:function(a){var f=!b&&location.href!=k;b=!0;f||c.mvc.route(document.location.href,
a,!0)},useHTML5History:function(a){!0==a?(this._useHistory=!0,window.addEventListener("popstate",this._html5Pop)):(this._useHistory=!1,window.removeEventListener("popstate",this._html5Pop))},setBaseDir:function(a){"/"==a[0]&&(a=a.substr(1));"/"==a[a.length-1]&&(a=a.slice(0,-1));h+="/"+a},listenHashChange:function(a){window.addEventListener("hashchange",function(a){var b=document.location.hash.replace("#","/");c.mvc.route(b,a,!0)})},controllersDir:function(a){this._controllersDir=a},modelsDir:function(a){this._modelsDir=
a},setViewType:function(a){this._templateType=a},ready:function(a){if(this.loaded)a();else c(document).one("afmvc:loaded",a)},loadControllers:function(a){var b=this;c(document).ready(function(){"string"===typeof a&&(a=[a]);for(var d=0;d<a.length;d++){var e=document.createElement("script");e.src=b._controllersDir+a[d]+".js";e.onerror=function(a){console.log("error ",a)};c("head").append(e);b._loadedListeners[a[d]]=1;b._loadedListeners.length++;c(document).one(a[d]+":ready",function(a,d){d=d||a.data;
delete b._loadedListeners[d.name];b._loadedListeners.length--;0==b._loadedListeners.length&&(b._controllersReady=!0,b._modelsReady||!b._hasModels?c(document).trigger("afmvc:loaded"):b._loadTimer=setTimeout(function(){b._modelsReady=!0;b._controllersReady&&c(document).trigger("afmvc:loaded")},1500))});delete e}})},loadModels:function(a){var b=this;clearTimeout(this._loadTimer);c(document).ready(function(){"string"===typeof a&&(a=[a]);b._totalModels=a.length;for(var d=0;d<a.length;d++){var e=document.createElement("script");
e.src=b._modelsDir+a[d]+".js";e.onload=function(){b._modelsLoaded++;b._modelsLoaded>=b._totalModels&&(b._modelsReady=!0,b._controllersReady&&c(document).trigger("afmvc:loaded"))};e.onerror=function(a){console.log("error ",a)};c("head").append(e);delete e}})}};c.mvc.route=function(a,b,d){"string"!==typeof a&&(a.nodeName&&"a"==a.nodeName.toLowerCase())&&(a=a.href);if("string"!==typeof a)throw"Invalid route parameter. String or <a> expected";var e,l,g=a;0===a.indexOf(h)&&(a=a.substring(h.length,a.length));
"/"==a[0]&&(a=a.substr(1));"/"==a[a.length-1]&&(a=a.slice(0,-1));a=a.split("/");1<a.length?(e=a.splice(0,1),l=a.splice(0,1)):(e=a[0],l="default");if(c.mvc.controller[e]&&c.mvc.controller[e].hasOwnProperty(l)){b&&b.preventDefault();try{c.mvc.controller[e][l].apply(c.mvc.controller[e],a)}catch(q){console.log(q.message)}c.mvc._app._useHistory&&!0!==d&&window.history.pushState(g,g,g);return!0}return!1};c.mvc.addRoute=function(a,b){0===a.indexOf(h)&&(a=a.substring(h.length,a.length));"/"==a[0]&&(a=a.substr(1));
a=a.split("/");if(1<a.length)var d=a.splice(0,1),e=a.splice(0,1);else d=a[0],e="default";c.mvc.controller[d]||(c.mvc.controller[d]={});c.mvc.controller[d][e]=b};if(c.ui)c.ui.customClickHandler=c.mvc.route;else c(document).on("click","a",function(a){c.mvc.route(a.target,a)})})(af);
(function(c){var h=[],k={},b={},a={};c.mvc.controller={};c.mvc.controller.create=function(f,d){var e=!0;c.mvc.controller[f]=d;k[f]=0;b[f]=0;d.hasOwnProperty("init")&&(a[f]=d);if(d.hasOwnProperty("views")&&0<(0<d.views.length||Object.keys(d.views).length)){e=!1;k[f]=d.views.length||Object.keys(d.views).length;for(var l in d.views){var g=c.isArray(d.views)?d.views[l]:l;!h[g]&&0==af("#"+g).length&&(c.mvc.controller.addView(d.views[l],f,g),h[g]=1)}}e&&(c(document).trigger(f+":ready",{name:f}),a[f]&&a[f].init.apply(a[f]));
return c.mvc.controller[f]};c.mvc.controller.addView=function(f,d,e){c.get(f,function(f){c(document.body).append(c("<script type='"+c.mvc._app._templateType+"' id='"+e+"'>"+f+"\x3c/script>"));b[d]++;b[d]==k[d]&&(c(document).trigger(d+":ready",{name:d}),a[d]&&a[d].init.apply(a[d]))})}})(af);
(function(c){var h={};c.mvc.model=function(b,a,f){var d;if(this.__proto__)d={},f=f||this.__proto__,d.__proto__=f;else{f=f||this;for(d in f);d=Object.create(Object.getPrototypeOf(this))}a&&a.modelName&&delete a.modelName;a&&a.id&&delete a.id;c.extend(d,a);d.modelName=b;return d};c.mvc.model.clone=function(b,a,c){if(c)if(a.__proto__)b.__proto__=a.__proto__;else{for(var d in a.prototype);b=Object.create(Object.getPrototypeOf(a))}for(d in a)a.hasOwnProperty(d)&&(b[d]=a[d]);return b};c.mvc.model.prototype=
{fetch:function(b,a){"string"==typeof b&&(this.id=b);var c=this;this.storageAdapter().fetch(c,function(b){b=c._prepareModel(b);return a?a(b):b},this.modelName)},fetchAll:function(b){var a=this;this.storageAdapter().fetchAll(a,function(c){var d=[];c.forEach(function(b){d.push(a._prepareModel(b))});return b?b(d):d})},save:function(b){return this.storageAdapter().save(this,b)},remove:function(b){return this.storageAdapter().remove(this,b)},get:function(b){if(this.hasOwnProperty(b))return this[b]},set:function(b,
a,f){var d=c.mvc.model.clone({},this);if(c.isObject(b)){b&&b.modelName&&delete b.modelName;b&&b.id&&delete b.id;for(var e in b)this.hasOwnProperty(e)&&(this[e]=b[e]);return!this._validate(f)?(c.mvc.model.clone(this,d),!1):!0}"id"!=b.toLowerCase()&&"modelname"!=b.toLowerCase()&&(this[b]=a);return!this._validate(f)?(c.mvc.model.clone(this,d),!1):!0},storageAdapter:function(){return h[this.modelName]},valid:function(b){return!0===this.validate(b)},validate:function(b){return!0},_prepareModel:function(b){var a=
c.mvc.model.clone({},this,!0);return a=c.mvc.model.clone(a,b)},_validate:function(b){if(b&&b.silent)return!0;var a=this.validate(b);if(!0===a)return!0;b&&b.error&&b.error(this,a,b);return!1}};c.mvc.model.extend=function(b,a,f,d){h[b]=f?f:(k.linkerCache[b]={},k);return function(){return new c.mvc.model(b,a)}};var k={linkerCache:{},save:function(b,a){b.id||(b.id=c.uuid());window.localStorage[b.id]=JSON.stringify(b);this.linkerCache[b.modelName][b.id]=1;window.localStorage[b.modelName+"_linker"]=JSON.stringify(this.linkerCache[b.modelName]);
c(document).trigger(b.modelName+":save",b);if(a)return a(b)},fetch:function(b,a){var c=window.localStorage.getItem(b.id);try{c=JSON.parse(c)}catch(d){c={}}return a(c)},fetchAll:function(b,a){var c=b.modelName,d=JSON.parse(window.localStorage.getItem(c+"_linker")),e=[],l;for(l in d)if(localStorage[l]){var g=JSON.parse(localStorage[l]);g.id=l;e.push(g)}else delete d[l];this.linkerCache[c]=d?d:{};window.localStorage[c+"_linker"]=JSON.stringify(this.linkerCache[c]);return a(e)},remove:function(b,a){window.localStorage.removeItem(b.id);
delete this.linkerCache[b.modelName][b.id];window.localStorage[b.modelName+"_linker"]=JSON.stringify(this.linkerCache[b.modelName]);c(document).trigger(b.modelName+":remove",b.id);if(a)return a(b)}}})(af);
(function(){function c(){var a={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"},b=/&(?!#?\w+;)|<|>|"|'|\//g;return function(){return this?this.replace(b,function(b){return a[b]||b}):this}}function h(a,b,c){return("string"===typeof b?b:b.toString()).replace(a.define||d,function(b,d,e,f){0===d.indexOf("def.")&&(d=d.substring(4));d in c||(":"===e?(a.defineParams&&f.replace(a.defineParams,function(a,b,f){c[d]={arg:b,text:f}}),d in c||(c[d]=f)):(new Function("def","def['"+d+"']="+
f))(c));return""}).replace(a.use||d,function(b,d){a.useParams&&(d=d.replace(a.useParams,function(a,b,d,f){if(c[d]&&c[d].arg&&f)return a=(d+":"+f).replace(/'|\\/g,"_"),c.__exp=c.__exp||{},c.__exp[a]=c[d].text.replace(RegExp("(^|[^\\w$])"+c[d].arg+"([^\\w$])","g"),"$1"+f+"$2"),b+"def.__exp['"+a+"']"}));var f=(new Function("def","return "+d))(c);return f?h(a,f,c):f})}function k(a){return a.replace(/\\('|\\)/g,"$1").replace(/[\r\t\n]/g," ")}var b={version:"1.0.0",templateSettings:{evaluate:/\{\{([\s\S]+?\}?)\}\}/g,
interpolate:/\{\{=([\s\S]+?)\}\}/g,encode:/\{\{!([\s\S]+?)\}\}/g,use:/\{\{#([\s\S]+?)\}\}/g,useParams:/(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,defineParams:/^\s*([\w$]+):([\s\S]+)/,conditional:/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,iterate:/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,varname:"it",strip:!0,append:!0,selfcontained:!1},template:void 0,compile:void 0};
"undefined"!==typeof module&&module.exports?module.exports=b:"function"===typeof define&&define.amd?define(function(){return b}):function(){return this||(0,eval)("this")}().doT=b;String.prototype.encodeHTML=c();var a={start:"'+(",end:")+'",endencode:"||'').toString().encodeHTML()+'"},f={start:"';out+=(",end:");out+='",endencode:"||'').toString().encodeHTML();out+='"},d=/$^/;b.template=function(e,g,q){g=g||b.templateSettings;var n=g.append?a:f,r,m=0,p;e=g.use||g.define?h(g,e,q||{}):e;e=("var out='"+
(g.strip?e.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ").replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""):e).replace(/'|\\/g,"\\$&").replace(g.interpolate||d,function(a,b){return n.start+k(b)+n.end}).replace(g.encode||d,function(a,b){r=!0;return n.start+k(b)+n.endencode}).replace(g.conditional||d,function(a,b,c){return b?c?"';}else if("+k(c)+"){out+='":"';}else{out+='":c?"';if("+k(c)+"){out+='":"';}out+='"}).replace(g.iterate||d,function(a,b,c,d){if(!b)return"';} } out+='";m+=1;p=d||"i"+m;b=k(b);return"';var arr"+
m+"="+b+";if(arr"+m+"){var "+c+","+p+"=-1,l"+m+"=arr"+m+".length-1;while("+p+"<l"+m+"){"+c+"=arr"+m+"["+p+"+=1];out+='"}).replace(g.evaluate||d,function(a,b){return"';"+k(b)+"out+='"})+"';return out;").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r").replace(/(\s|;|\}|^|\{)out\+='';/g,"$1").replace(/\+''/g,"").replace(/(\s|;|\}|^|\{)out\+=''\+/g,"$1out+=");r&&g.selfcontained&&(e="String.prototype.encodeHTML=("+c.toString()+"());"+e);try{return new Function(g.varname,e)}catch(s){throw"undefined"!==
typeof console&&console.log("Could not create a template function: "+e),s;}};b.compile=function(a,c){return b.template(a,null,c)};var e={};$.template=function(a,c,d){return(e[a]=e[a]||b.template(document.getElementById(a).text,void 0,d))(c)};$.tmpl=function(a,b,c){return $(template(a,b,c))}})(jq);