ep-tarantino
Version:
A client Side/Server Side Router - A Fork of Director
2 lines (1 loc) • 10.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("tarantino",["exports"],e):e(t.tarantino=t.tarantino||{})}(this,function(t){"use strict";function e(){return""===o.hash||"#"===o.hash}function r(t){if(!(this instanceof r))return new r(t);Object.assign(this,n.prototype),this.params={},this.routes={},this.methods=["on","once","after","before"],this.scope=[],this._methods={},this._insert=this.insert,this.insert=this.insertEx,this.historySupport=null!=(null!=window.history?window.history.pushState:null),this.configure(),this.mount(t||{})}var i=function(t,e){return e={exports:{}},t(e,e.exports),e.exports}(function(t,e){function r(t){for(var e=[],r=0,i=t.length;r<i;r++)e=e.concat(t[r]);return e}function i(t,e){for(var r=0;r<t.length;r+=1)if(!1===e(t[r],r,t))return}function n(t,e,r){if(!t.length)return r();var i=0;!function n(){e(t[i],function(e){e||!1===e?(r(e),r=function(){}):(i+=1,i===t.length?r():n())})}()}function o(t,e,r){r=t;for(var i in e)if(e.hasOwnProperty(i)&&(r=e[i](t))!==t)break;return r===t?"([._a-zA-Z0-9-%()@+]+)":r}function s(t,e){for(var r,i=0,n="";r=t.substr(i).match(/[^\w\d\- %@&]*\*[^\w\d\- %@&]*/);)i=r.index+r[0].length,r[0]=r[0].replace(/^\*/,"([_.()!\\ %@&a-zA-Z0-9-]+)"),n+=t.substr(0,r.index)+r[0];t=n+=t.substr(i);var s,h,a=t.match(/:([^\/]+)/gi);if(a){h=a.length;for(var c=0;c<h;c++)s=a[c],t="::"===s.slice(0,2)?s.slice(1):t.replace(s,o(s,e))}return t}function h(t,e,r,i){var n,o=0,s=0,h=0,r=(r||"(").toString(),i=(i||")").toString();for(n=0;n<t.length;n++){var a=t[n];if(a.indexOf(r,o)>a.indexOf(i,o)||~a.indexOf(r,o)&&!~a.indexOf(i,o)||!~a.indexOf(r,o)&&~a.indexOf(i,o)){if(s=a.indexOf(r,o),h=a.indexOf(i,o),~s&&!~h||!~s&&~h){t=[t.slice(0,(n||1)+1).join(e)].concat(t.slice((n||1)+1))}o=(h>s?h:s)+1,n=0}else o=0}return t}var a=/\?.*/,c=e.Router=function(t){this.params={},this.routes={},this.methods=["on","after","before"],this.scope=[],this._methods={},this.configure(),this.mount(t||{})};c.prototype.configure=function(t){t=t||{};for(var e=0;e<this.methods.length;e++)this._methods[this.methods[e]]=!0;return this.recurse=void 0===t.recurse?this.recurse||!1:t.recurse,this.async=t.async||!1,this.delimiter=t.delimiter||"/",this.strict=void 0===t.strict||t.strict,this.notfound=t.notfound,this.resource=t.resource,this.history=(!0===t.html5history||void 0===t.html5history)&&this.historySupport||!1,this.run_in_init=!0===this.history&&!1!==t.run_handler_in_init,this.convert_hash_in_init=!0===this.history&&!1!==t.convert_hash_in_init,this.every={after:t.after||null,before:t.before||null,on:t.on||null},this},c.prototype.param=function(t,e){":"!==t[0]&&(t=":"+t);var r=new RegExp(t,"g");return this.params[t]=function(t){return t.replace(r,e.source||e)},this},c.prototype.on=c.prototype.route=function(t,e,r){var i=this;return r||"function"!=typeof e||(r=e,e=t,t="on"),Array.isArray(e)?e.forEach(function(e){i.on(t,e,r)}):(e.source&&(e=e.source.replace(/\\\//gi,"/")),Array.isArray(t)?t.forEach(function(t){i.on(t.toLowerCase(),e,r)}):(e=e.split(new RegExp(this.delimiter)),e=h(e,this.delimiter),void this.insert(t,this.scope.concat(e),r)))},c.prototype.path=function(t,e){var r=this.scope.length;t.source&&(t=t.source.replace(/\\\//gi,"/")),t=t.split(new RegExp(this.delimiter)),t=h(t,this.delimiter),this.scope=this.scope.concat(t),e.call(this,this),this.scope.splice(r,t.length)},c.prototype.dispatch=function(t,e,r){function i(){o.last=s.after,o.invoke(o.runlist(s),o,r)}var n,o=this,s=this.traverse(t,e.replace(a,""),this.routes,""),h=this._invoked;return this._invoked=!0,s&&0!==s.length?("forward"===this.recurse&&(s=s.reverse()),(n=this.every&&this.every.after?[this.every.after].concat(this.last):[this.last])&&n.length>0&&h?(this.async?this.invoke(n,this,i):(this.invoke(n,this),i()),!0):(i(),!0)):(this.last=[],"function"==typeof this.notfound&&this.invoke([this.notfound],{method:t,path:e},r),!1)},c.prototype.runlist=function(t){var e=this.every&&this.every.before?[this.every.before].concat(r(t)):r(t);return this.every&&this.every.on&&e.push(this.every.on),e.captures=t.captures,e.source=t.source,e},c.prototype.invoke=function(t,e,r){var o,s=this;this.async?(o=function(r,i){if(Array.isArray(r))return n(r,o,i);"function"==typeof r&&r.apply(e,(t.captures||[]).concat(i))},n(t,o,function(){r&&r.apply(e,arguments)})):(o=function(r){return Array.isArray(r)?i(r,o):"function"==typeof r?r.apply(e,t.captures||[]):void("string"==typeof r&&s.resource&&s.resource[r].apply(e,t.captures||[]))},i(t,o))},c.prototype.traverse=function(t,e,r,i,n){function o(t){function e(t){for(var r=[],i=0;i<t.length;i++)r[i]=Array.isArray(t[i])?e(t[i]):t[i];return r}function r(t){for(var e=t.length-1;e>=0;e--)Array.isArray(t[e])?(r(t[e]),0===t[e].length&&t.splice(e,1)):n(t[e])||t.splice(e,1)}if(!n)return t;var i=e(t);return i.matched=t.matched,i.captures=t.captures,i.after=t.after.filter(n),r(i),i}var s,h,a,c,u=[];if(e===this.delimiter&&r[t])return c=[[r.before,r[t]].filter(Boolean)],c.after=[r.after].filter(Boolean),c.matched=!0,c.captures=[],o(c);for(var f in r)if(r.hasOwnProperty(f)&&(!this._methods[f]||this._methods[f]&&"object"==typeof r[f]&&!Array.isArray(r[f]))){if(s=h=i+this.delimiter+f,this.strict||(h+="["+this.delimiter+"]?"),!(a=e.match(new RegExp("^"+h))))continue;if(a[0]&&a[0]==e&&r[f][t])return c=[[r[f].before,r[f][t]].filter(Boolean)],c.after=[r[f].after].filter(Boolean),c.matched=!0,c.captures=a.slice(1),this.recurse&&r===this.routes&&(c.push([r.before,r.on].filter(Boolean)),c.after=c.after.concat([r.after].filter(Boolean))),o(c);if(c=this.traverse(t,e,r[f],s),c.matched)return c.length>0&&(u=u.concat(c)),this.recurse&&(u.push([r[f].before,r[f][t]].filter(Boolean)),c.after=c.after.concat([r[f].after].filter(Boolean)),r===this.routes&&(u.push([r.before,r.on].filter(Boolean)),c.after=c.after.concat([r.after].filter(Boolean)))),u.matched=!0,u.captures=c.captures,u.after=c.after,o(u)}return!1},c.prototype.insert=function(t,e,r,i){var n,o,h,a;if(e=e.filter(function(t){return t&&t.length>0}),i=i||this.routes,a=e.shift(),/\:|\*/.test(a)&&!/\\d|\\w/.test(a)&&(a=s(a,this.params)),e.length>0)return i[a]=i[a]||{},this.insert(t,e,r,i[a]);if(a||e.length||i!==this.routes){if(n=typeof i[a],o=Array.isArray(i[a]),i[a]&&!o&&"object"==n)switch(typeof i[a][t]){case"function":return void(i[a][t]=[i[a][t],r]);case"object":return void i[a][t].push(r);case"undefined":return void(i[a][t]=r)}else if("undefined"==n)return h={},h[t]=r,void(i[a]=h);throw new Error("Invalid route context: "+n)}switch(typeof i[t]){case"function":return void(i[t]=[i[t],r]);case"object":return void i[t].push(r);case"undefined":return void(i[t]=r)}},c.prototype.extend=function(t){var e,r=this,i=t.length;for(e=0;e<i;e++)!function(t){r._methods[t]=!0,r[t]=function(){var e=1===arguments.length?[t,""]:[t];r.on.apply(r,e.concat(Array.prototype.slice.call(arguments)))}}(t[e])},c.prototype.mount=function(t,e){if(t&&"object"==typeof t&&!Array.isArray(t)){var r=this;e=e||[],Array.isArray(e)||(e=e.split(r.delimiter));for(var i in t)t.hasOwnProperty(i)&&function(e,i){var n=e,o=e.split(/\/(?!\!)/g),s=typeof t[e],a=""===o[0]||!r._methods[o[0]],c=a?"on":n;if(a&&(n=n.slice((n.match(new RegExp("^"+r.delimiter))||[""])[0].length),o.shift()),a&&"object"===s&&!Array.isArray(t[e]))return i=i.concat(o),void r.mount(t[e],i);a&&(i=i.concat(n.split(r.delimiter)),i=h(i,r.delimiter)),r.insert(c,i,t[e])}(i,e.slice(0))}}}),n=i.Router,o=document.location,s={mode:"modern",hash:o.hash,history:!1,check:function(){var t=o.hash;t!=this.hash&&(this.hash=t,this.onHashChanged())},fire:function(){"modern"===this.mode?!0===this.history?window.onpopstate():window.onhashchange():this.onHashChanged()},init:function(t,e){function i(t){for(var e=0,i=r.listeners.length;e<i;e++)r.listeners[e](t)}var n=this;if(this.history=e,r.listeners||(r.listeners=[]),"onhashchange"in window&&(void 0===document.documentMode||document.documentMode>7))!0===this.history?window.onpopstate=i:window.onhashchange=i,this.mode="modern";else{var o=document.createElement("iframe");o.id="state-frame",o.style.display="none",document.body.appendChild(o),this.writeFrame(""),"onpropertychange"in document&&"attachEvent"in document&&document.attachEvent("onpropertychange",function(){"location"===event.propertyName&&n.check()}),window.setInterval(function(){n.check()},50),this.onHashChanged=i,this.mode="legacy"}return r.listeners.push(t),this.mode},destroy:function(t){if(r&&r.listeners)for(var e=r.listeners,i=e.length-1;i>=0;i--)e[i]===t&&e.splice(i,1)},setHash:function(t){return"legacy"===this.mode&&this.writeFrame(t),!0===this.history?(t=0===t.indexOf("/")?t:"/"+t,window.history.pushState({},document.title,t),this.fire()):o.hash="/"===t[0]?t:"/"+t,this},writeFrame:function(t){var e=document.getElementById("state-frame"),r=e.contentDocument||e.contentWindow.document;r.open(),r.write("<script>_hash = '"+t+"'; onload = parent.listener.syncHash;<script>"),r.close()},syncHash:function(){var t=this._hash;return t!=o.hash&&(o.hash=t),this},onHashChanged:function(){}};r.prototype.init=function(t){var r,i=this;return this.handler=function(t){var e=t&&t.newURL||window.location.hash,r=!0===i.history?i.getPath():e.replace(/.*#/,"");i.dispatch("on","/"===r.charAt(0)?r:"/"+r)},s.init(this.handler,this.history),!1===this.history?e()&&t?o.hash=t:e()||i.dispatch("on","/"+o.hash.replace(/^(#\/|#|\/)/,"")):(this.convert_hash_in_init?(r=e()&&t?t:e()?null:o.hash.replace(/^#/,""))&&window.history.replaceState({},document.title,r):r=this.getPath(),(r||!0===this.run_in_init)&&this.handler()),this},r.prototype.explode=function(){var t=!0===this.history?this.getPath():o.hash;return"/"===t.charAt(1)&&(t=t.slice(1)),t.slice(1,t.length).split("/")},r.prototype.setRoute=function(t,e,r){var i=this.explode();return"number"==typeof t&&"number"==typeof e&&"string"==typeof r?t<i.length&&i.splice(t,e,r):"number"==typeof t&&"number"==typeof e?t<i.length&&i.splice(t,e):"number"==typeof t&&"string"==typeof e?i[t]=e:i=[t],s.setHash(i.join("/")),i},r.prototype.insertEx=function(t,e,r,i){return"once"===t&&(t="on",r=function(t){var e=!1;return function(){if(!e)return e=!0,t.apply(this,arguments)}}(r)),this._insert(t,e,r,i)},r.prototype.getRoute=function(t){var e=t;if("number"==typeof t)e=this.explode()[t];else if("string"==typeof t){var r=this.explode();e=r.indexOf(t)}else e=this.explode();return e},r.prototype.destroy=function(){return s.destroy(this.handler),this},r.prototype.getPath=function(){var t=window.location.pathname;return"/"!==t.substr(0,1)&&(t="/"+t),t},t.Router=r,Object.defineProperty(t,"__esModule",{value:!0})});