forked from BytesAngels/jqMVC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq.mvc.min.js
13 lines (13 loc) · 6.05 KB
/
jq.mvc.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
(function(c){c.mvc={};c.mvc.app=function(){return{_loadTimer:null,_modelsReady:!1,_controllersReady:!1,_loadedListeners:[],_modelsLoaded:0,_totalModels:0,_controllersDir:"controllers/",_modelsDir:"models/",_baseDir:"",setBaseDir:function(a){a[0]=="/"&&(a=a.substr(1));a[a.length-1]=="/"&&(a=a.slice(0,-1));e+="/"+a},listenHashChange:function(){window.addEventListener("hashchange",function(a){var b=document.location.hash.replace("#","/");c.mvc.route(b,a)})},controllersDir:function(a){this._controllersDir=
a},modelsDir:function(a){this._modelsDir=a},setViewType:function(a){c.mvc._app._templateType=a},ready:function(a){if(this.loaded)a();else c(document).one("jqmvc:loaded",a)},loadControllers:function(a){var b=this;c(document).ready(function(){b._loadTimer=setTimeout(function(){b._modelsReady=!0;b._controllersReady&&c(document).trigger("jqmvc:loaded")},1500);typeof a==="string"&&(a=[a]);for(var d=0;d<a.length;d++){var g=document.createElement("script");g.src=b._controllersDir+a[d]+".js";g.onerror=function(a){console.log("error ",
a)};c("head").append(g);b._loadedListeners[a[d]]=1;b._loadedListeners.length++;c(document).one(a[d]+":ready",function(a){delete b._loadedListeners[a.data.name];b._loadedListeners.length--;if(b._loadedListeners.length==0)b._controllersReady=!0,b._modelsReady&&c(document).trigger("jqmvc:loaded")});delete g}})},loadModels:function(a){var b=this;clearTimeout(this._loadTimer);c(document).ready(function(){typeof a==="string"&&(a=[a]);b._totalModels=a.length;for(var d=0;d<a.length;d++){var g=document.createElement("script");
g.src=b._modelsDir+a[d]+".js";g.onload=function(){b._modelsLoaded++;if(b._modelsLoaded>=b._totalModels)b._modelsReady=!0,b._controllersReady&&c(document).trigger("jqmvc:loaded")};g.onerror=function(a){console.log("error ",a)};c("head").append(g);delete g}})}}};c.mvc._app={_templateType:"text/html"};var e=document.location.protocol+"//"+document.location.host,i=[],h={},b={},d={};c.mvc.controller={};c.mvc.controller.create=function(a,f){var j=!0;c.mvc.controller[a]=f;h[a]=0;b[a]=0;f.hasOwnProperty("init")&&
(d[a]=f);if(f.hasOwnProperty("views")&&(f.views.length>0||Object.keys(f.views).length)>0)for(var g in j=!1,h[a]=f.views.length||Object.keys(f.views).length,f.views){var e=c.isArray(f.views)?f.views[g]:g;!i[e]&&jq("#"+e).length==0&&(c.mvc.controller.addView(f.views[g],a,e),i[e]=1)}j&&(c(document).trigger(a+":ready",{name:a}),d[a]&&d[a].init.apply(d[a]));return c.mvc.controller[a]};c.mvc.route=function(a,b){if(typeof a!=="string"&&a.nodeName&&a.nodeName.toLowerCase()=="a")a=a.href;var d,g;a.indexOf(e)===
0&&(a=a.substring(e.length,a.length));a[0]=="/"&&(a=a.substr(1));a[a.length-1]=="/"&&(a=a.slice(0,-1));a=a.split("/");a.length>1?(d=a.splice(0,1),g=a.splice(0,1)):(d=a[0],g="default");if(c.mvc.controller[d]&&c.mvc.controller[d].hasOwnProperty(g))return b&&b.preventDefault(),c.mvc.controller[d][g].apply(c.mvc.controller[d],a),!0;return!1};c.mvc.addRoute=function(a,b){a.indexOf(e)===0&&(a=a.substring(e.length,a.length));a[0]=="/"&&(a=a.substr(1));a=a.split("/");if(a.length>1)var d=a.splice(0,1),g=a.splice(0,
1);else d=a[0],g="default";c.mvc.controller[d]||(c.mvc.controller[d]={});c.mvc.controller[d][g]=b};c.mvc.controller.addView=function(a,f,e){c.get(a,function(a){c(document.body).append(c("<script type='"+c.mvc._app._templateType+"' id='"+e+"'>"+a+"<\/script>"));b[f]++;b[f]==h[f]&&(c(document).trigger(f+":ready",{name:f}),d[f]&&d[f].init.apply(d[f]))})};if(c.ui)c.ui.customClickHandler=c.mvc.route;else c(document).on("click","a",function(a){c.mvc.route(a.target.href,a)})})(jq);
(function(c){var e={},i={};c.mvc.model=function(b,d){d&&d.modelName&&delete d.modelName;d&&d.id&&delete d.id;i[b]||(i[b]=d);c.extend(this,d);this.modelName=b};c.mvc.model.prototype={get:function(b,d){var a=this,f=new c.mvc.model(this.modelName,i[this.modelName]);e[this.modelName].get(b,function(e){f=c.extend(f,e);f.modelName=a.modelName;f.id=b;if(d)return d(f);return f})},getAll:function(b){return e[this.modelName].getAll(this.modelName,b)},save:function(b){return e[this.modelName].save(this,b)},
remove:function(b){return e[this.modelName].remove(this,b)},set:function(b,d){if(c.isObject(b))for(var a in b&&b.modelName&&delete b.modelName,b&&b.id&&delete b.id,b)this.hasOwnProperty(a)&&(this[a]=b[a]);else b.toLowerCase()!="id"&&b.toLowerCase()!="modelname"&&(this[b]=d)},getStorageAdapter:function(){return e[this.modelName]},getBaseOptions:function(){return i[this.modelName]}};c.mvc.model.extend=function(b,d,a){e[b]=a?a:(h.linkerCache[b]={},h);return function(){return new c.mvc.model(b,d)}};var h=
{linkerCache:{},save:function(b,d){if(!b.id)b.id=c.uuid();window.localStorage.setItem(b.id,JSON.stringify(b));this.linkerCache[b.modelName][b.id]=1;window.localStorage.setItem(b.modelName+"_linker",JSON.stringify(this.linkerCache[b.modelName]));c(document).trigger(b.modelName+":save",b);if(d)return d(b)},get:function(b,d){var a=window.localStorage.getItem(b);try{a=JSON.parse(a)}catch(c){a={}}return d(a)},getAll:function(b,d){var a=JSON.parse(window.localStorage.getItem(b+"_linker")),c=[],e;for(e in a)if(localStorage[e]){var g=
JSON.parse(localStorage[e]);g.modelName=b;g.id=e;c.push(g)}else delete a[e];this.linkerCache[b]=a?a:{};window.localStorage.setItem(b+"_linker",JSON.stringify(this.linkerCache[b]));return d(c)},remove:function(b,d){window.localStorage.removeItem(b.id);delete this.linkerCache[b.modelName][b.id];window.localStorage.setItem(b.modelName+"_linker",JSON.stringify(this.linkerCache[b.modelName]));c(document).trigger(b.modelName+":remove",b.id);if(d)return d(b)}}})(jq);
(function(c){c.template=function(c,h){return e(c,h)};c.tmpl=function(i,h){return c(e(i,h))};var e=function(c,e){e||(e={});return tmpl(c,e)};(function(){var c={};this.tmpl=function b(d,a){var e=!/\W/.test(d)||/.js$/.test(d)?c[d]=c[d]||b(document.getElementById(d).innerHTML):new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+d.replace(/[\r\t\n]/g," ").replace(/'(?=[^%]*%>)/g,"\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g,"',$1,'").split("<%").join("');").split("%>").join("p.push('")+
"');}return p.join('');");return a?e(a):e}})()})(jq);