forked from lazojs/hermes
/
hermes.amd.min.js
7 lines (7 loc) · 4.3 KB
/
hermes.amd.min.js
1
2
3
4
5
6
7
// HERMES, sweet llamas of the bahamas
// ----------------------------------
// v1.0.2
//
// Copyright (c)2015 Jason Strimpel
// Distributed under MIT license
define(function(){"use strict";function a(a,b,c){for(var d in b)c&&c[d]||(a[d]=b[d]);return a}function b(a){return"[object RegExp]"===Object.prototype.toString.call(a)}function c(a){return"function"==typeof a}var d,e=/\((.*?)\)/g,f=/(\(\?)?:\w+/g,g=/\*\w+/g,h=/[\-{}\[\]+?.,\\\^$|#\s]/g,i=/^\/+|\/+$/g,j=/\/$/;return d={start:function(b){var c=this;return this._started||!window.history.pushState?this:(b=b||{},this._handlers=this._handlers||[],this._cache=this._cache||{},b.routeOnHashChange=b.routeOnHashChange||!1,a(this,b,{state:!0,title:!0}),this.root=this.root?("/"+this.root+"/").replace(i,"/"):void 0,b.routeOnHashChange=b.routeOnHashChange||!1,b.state=b.state||{},b.state.title=b.title=b.title||document.title,window.history.replaceState(b.state||{},b.title,this._getUrl()),document.title=b.title,this._setCacheItem(b.title,b.state),this._currentUrl={pathname:window.location.pathname,search:window.location.search},c._backboneEvents(),c._bindRoutes(),setTimeout(function(){c._bindPopState()},0),this._started=!0,this)},getPreviousUrl:function(){return this._lastUrl?this._lastUrl.pathname+this._lastUrl.search:""},stop:function(){window.removeEventListener("popstate",this._popstateListener),this._started=!1},route:function(a,d,e){var f,g=this,h=a.match(/\*\w+/g)||[],i=(a.match(/:\w*/g)||[]).concat(h);return a=b(a)?a:this._routeToRegExp(a),c(d)&&(e=d,d=""),this._handlers=this._handlers||[],this._handlers.unshift({route:a,callback:function(b){f=g._getParams(a,i),c(e)&&e.apply(g,[window.location.pathname,f,b]),g._backboneEventsAugmented&&(g.trigger.apply(g,["route:"+d].concat(f,b)),g.trigger("route",d,f,b))}}),this},destroyRoutes:function(){this._handlers=[]},navigate:function(a,b){return window.history.pushState?(b=b||{},b.state=b.state||{},b.state.title=b.title=b.title||document.title,window.history[b.replace?"replaceState":"pushState"](b.state,b.title,a),document.title=b.title,this._setCacheItem(b.title,b.state),void((void 0===b.trigger||b.trigger)&&this._loadUrl(b.state))):void(window.location=a)},getItem:function(a){return this._cache[a||this._getUrl()]},clearCache:function(){return this._cache={},this},updateState:function(a,b){var c=this.getItem(b),d=c?c.title:a.title;this._setCacheItem(d,a,b),window.history.replaceState(a,d,b||this._getUrl())},routeNotMatched:function(a){},_setCacheItem:function(a,b,c){var d=this._cache[c||this._getUrl()]={title:a};return this.cache&&(d.state=b),this},_getUrl:function(){return window.location.pathname+window.location.search},_getParams:function(a,b){var c,d,e=/\+/g,f=/([^&=]+)=?([^&]*)/g,g=function(a){return decodeURIComponent(a.replace(e," "))},h=window.location.search.substring(1),i={};d=a.exec(this._stripRoot(window.location.pathname)).slice(1);for(var j=0;j<b.length;j++)i[b[j].substring(1)]=d[j]||null;for(;c=f.exec(h);)i[g(c[1])]=g(c[2]);return i},_routeToRegExp:function(a){return a=a.replace(h,"\\$&").replace(e,"(?:$1)?").replace(f,function(a,b){return b?a:"([^/]+)"}).replace(g,"(.*?)"),new RegExp("^"+a+"$")},_stripRoot:function(a){var b;return a="/"===a.substring(0,1)?a.substring(1):a,this.root?(b=this.root.replace(j,""),a=0===a.indexOf(b)?a.substr(b.length):void 0):a},_backboneEvents:function(){return!this.Backbone||this._backboneEventsAugmented?this:(a(this,Backbone.Events),this._backboneEventsAugmented=!0,this)},_bindPopState:function(){var a=this;return this._popstateListener=window.addEventListener("popstate",function(b){var c=a.getItem();a._loadUrl(b.state),document.title=c?c.title:document.title}),this},_bindRoutes:function(){var a;if(!this.routes||this._routesBound)return this;a=c(this.routes)?this.routes():this.routes;for(var b in this.routes)this.route(b,this.routes[b]);return this._routesBound=!0,this},_loadUrl:function(a){var b=this._handlers,c=this._stripRoot(window.location.pathname),d=b.length;if(!this.routeOnHashChange&&this._currentUrl.pathname===window.location.pathname&&this._currentUrl.search===window.location.search)return this;a=a||{};for(var e=0;d>e;e++)if(b[e].route.test(c))return b[e].callback(a),this._lastUrl={pathname:this._currentUrl.pathname,search:this._currentUrl.search},this._currentUrl={pathname:window.location.pathname,search:window.location.search},this;return this.routeNotMatched(c),this}}});