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