forked from olivernn/davis.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
davis.min.js
1 lines (1 loc) · 8.09 KB
/
davis.min.js
1
define(["libs/underscore"],function(a){var b={};return b=function(a){var c=new b.App;return a&&a.call(c),b.$(function(){c.start()}),c},window.jQuery?b.$=jQuery:b.$=null,b.supported=function(){return typeof window.history.pushState=="function"},b.noop=function(){},b.extend=function(a){a(b)},b.version="0.8.0",b.listener=function(){var a={A:function(a){return a.hostname!==window.location.host||a.protocol!==window.location.protocol},FORM:function(a){var b=document.createElement("a");return b.href=a.action,this.A(b)}},c=function(b){return a[b.nodeName.toUpperCase()]?a[b.nodeName.toUpperCase()](b):!0},d=function(a){return function(d){if(c(this))return!0;var e=new b.Request(a.call(b.$(this)));return b.location.assign(e),!1}},e=d(function(){var a=this;return{method:"get",fullPath:this.attr("href"),title:this.attr("title"),delegateToServer:function(){window.location.pathname=a.attr("href")}}}),f=d(function(){var a=this;return{method:this.attr("method"),fullPath:decodeURI(this.serialize()?[this.attr("action"),this.serialize()].join("?"):this.attr("action")),title:this.attr("title"),delegateToServer:function(){a.submit()}}});this.listen=function(){b.$(document).delegate(this.settings.formSelector,"submit",f),b.$(document).delegate(this.settings.linkSelector,"click",e)},this.unlisten=function(){b.$(document).undelegate(this.settings.linkSelector,"click",e),b.$(document).undelegate(this.settings.formSelector,"submit",f)}},b.event=function(){var b={};this.bind=function(a,c){return(b[a]=b[a]||[]).push(c),this},this.trigger=function(c){var d=a.toArray(arguments).slice(1),e=b[c];if(!e)return this;for(var f=0,g=e.length;f<g;++f)e[f].apply(this,d);return this}},b.logger=function(){function b(){return"["+Date()+"]"}function c(c){var d=a.toArray(c);return d.unshift(b()),d.join(" ")}var d=function(a){return function(){window.console&&console[a](c(arguments))}},e=d("error"),f=d("info"),g=d("warn");this.logger={error:e,info:f,warn:g}},b.Route=function(){var a=/:([\w\d]+)/g,b="([^/]+)",c=function(c,d,e){var f=function(){if(d instanceof RegExp)return d;var c=d.replace(a,b);return d.lastIndex=0,new RegExp("^"+c+"$","gi")},g=function(){return c instanceof RegExp?c:new RegExp("^"+c+"$","i")},h=function(){var b=[],c;while(c=a.exec(d))b.push(c[1]);return b};this.paramNames=h(),this.path=f(),this.method=g(),this.callback=e};return c.prototype.match=function(a,b){return this.reset(),this.method.test(a)&&this.path.test(b)},c.prototype.reset=function(){this.method.lastIndex=0,this.path.lastIndex=0},c.prototype.run=function(a){this.reset();var b=this.path.exec(a.path);if(b){b.shift();for(var c=0;c<b.length;c++)a.params[this.paramNames[c]]=b[c]}return this.callback.call(a,a)},c.prototype.toString=function(){return[this.method,this.path].join(" ")},c}(),b.router=function(){this.route=function(a,d,e){var f=function(d,e){var f=new b.Route(a,d,e);return c.push(f),f};return arguments.length==1?f:f(d,e)},this.get=this.route("get"),this.post=this.route("post"),this.put=this.route("put"),this.del=this.route("delete"),this.state=this.route("state"),this.trans=function(a,c){if(c)var d=[a,decodeURIComponent(b.$.param(c))].join("?");else var d=a;var e=new b.Request({method:"state",fullPath:d,title:""});b.location.assign(e)},this.filter=function(a){return function(){var c=/.+/;if(arguments.length==1)var e=/.+/,f=arguments[0];else if(arguments.length==2)var e=arguments[0],f=arguments[1];var g=new b.Route(c,e,f);return d[a].push(g),g}},this.lookupFilter=function(b){return function(c,e){return a.filter(d[b],function(a){return a.match(c,e)})}},this.before=this.filter("before"),this.after=this.filter("after"),this.lookupBeforeFilter=this.lookupFilter("before"),this.lookupAfterFilter=this.lookupFilter("after");var c=[],d={before:[],after:[]};this.lookupRoute=function(b,d){return a.filter(c,function(a){return a.match(b,d)})[0]}},b.history=function(){function e(){return"state"in window.history?!0:d}function f(a){c.push(a)}function g(a){window.addEventListener("popstate",a,!0)}function h(a){return function(c){c.state&&c.state._davis?a(new b.Request(c.state._davis)):(e()&&a(b.Request.forPageLoad()),d=!0)}}function i(a){return{_davis:a}}function j(a){f(a),g(h(a))}function k(b){history.pushState(i(b.toJSON()),b.title,b.location()),a.forEach(c,function(a){a(b)})}function l(b){history.replaceState(i(b.toJSON()),b.title,b.location()),a.forEach(c,function(a){a(b)})}function m(){return window.location.pathname+(window.location.search?window.location.search:"")}var c=[],d=!1;return{onChange:j,current:m,assign:k,replace:l}}(),b.location=function(){function c(b){a=b}function d(){return a.current()}function e(b){a.assign(b)}function f(b){a.replace(b)}function g(b){a.onChange(b)}var a=b.history;return{setLocationDelegate:c,current:d,assign:e,replace:f,onChange:g}}(),b.Request=function(){var c=function(d){var e=b.$.extend({},{title:"",fullPath:"",method:"get"},d),f=this;this.raw=e,this.params={},this.title=e.title,this.queryString=e.fullPath.split("?")[1],this._staleCallback=function(){},this.queryString&&a.forEach(this.queryString.split("&"),function(a){var b=a.split("=")[0],c=a.split("=")[1],d=/^(\w+)\[(\w+)?\](\[\])?/,e;if(e=d.exec(b)){var g=e[1],b=e[2],h=!!e[3],i=f.params[g]||{};h?(i[b]=i[b]||[],i[b].push(c),f.params[g]=i):!b&&!h?(i=f.params[g]||[],i.push(c),f.params[g]=i):(i[b]=c,f.params[g]=i)}else f.params[b]=c}),e.fullPath=e.fullPath.replace(/^https?:\/\/.+?\//,"/"),this.method=(this.params._method||e.method).toLowerCase(),this.path=e.fullPath.replace(/\?.+$/,"").replace(/^https?:\/\/[^\/]+/,""),this.fullPath=e.fullPath,this.delegateToServer=e.delegateToServer||b.noop,this.isForPageLoad=e.forPageLoad||!1,c.prev&&c.prev.makeStale(this),c.prev=this};return c.prototype.redirect=function(a){b.location.replace(new c({method:"get",fullPath:a,title:this.title}))},c.prototype.whenStale=function(a){this._staleCallback=a},c.prototype.makeStale=function(a){this._staleCallback.call(a,a)},c.prototype.location=function(){return this.method==="get"?this.fullPath:""},c.prototype.toString=function(){return[this.method.toUpperCase(),this.path].join(" ")},c.prototype.toJSON=function(){return{title:this.raw.title,fullPath:this.raw.fullPath,method:this.raw.method}},c.forPageLoad=function(){return new this({method:"get",fullPath:b.location.current(),title:document.title,forPageLoad:!0})},c.prev=null,c}(),b.App=function(){function c(){this.running=!1,this.boundToInternalEvents=!1,this.use(b.listener),this.use(b.event),this.use(b.router),this.use(b.logger)}return c.prototype.configure=function(a){a.call(this.settings,this.settings)},c.prototype.use=function(b){b.apply(this,a.toArray(arguments).slice(1))},c.prototype.helpers=function(a){for(property in a)a.hasOwnProperty(property)&&(b.Request.prototype[property]=a[property])},c.prototype.settings={linkSelector:"a",formSelector:"form",throwErrors:!0,handleRouteNotFound:!1,generateRequestOnPageLoad:!1},c.prototype.start=function(){var c=this;if(this.running)return;if(!b.supported()){this.trigger("unsupported");return}var d=function(a){return function(b){var c=b.run(a,a);return typeof c=="undefined"||c}},e=function(b){return a.every(c.lookupBeforeFilter(b.method,b.path),d(b))},f=function(b){if(e(b)){c.trigger("lookupRoute",b);var f=c.lookupRoute(b.method,b.path);if(f){c.trigger("runRoute",b,f);try{f.run(b),c.trigger("routeComplete",b,f)}catch(g){c.trigger("routeError",b,f,g)}a.every(c.lookupAfterFilter(b.method,b.path),d(b))}else c.trigger("routeNotFound",b)}else c.trigger("requestHalted",b)},g=function(){c.bind("runRoute",function(a){c.logger.info("runRoute: "+a.toString())}).bind("routeNotFound",function(a){!c.settings.handleRouteNotFound&&!a.isForPageLoad&&(c.stop(),a.delegateToServer()),c.logger.warn("routeNotFound: "+a.toString())}).bind("start",function(){c.logger.info("application started")}).bind("stop",function(){c.logger.info("application stopped")}).bind("routeError",function(a,b,d){if(c.settings.throwErrors)throw d;c.logger.error(d.message,d.stack)}),b.location.onChange(function(a){f(a)}),c.boundToInternalEvents=!0};this.boundToInternalEvents||g(),this.listen(),this.trigger("start"),this.running=!0,this.settings.generateRequestOnPageLoad&&f(b.Request.forPageLoad())},c.prototype.stop=function(){this.unlisten(),this.trigger("stop"),this.running=!1},c}(),b});