rstacruz/repage.js

View on GitHub
repage.js

Summary

Maintainability
A
0 mins
Test Coverage
/* repage.js - https://github.com/rstacruz/repage.js
 * includes page.js - https://github.com/visionmedia/page.js
 * @license MIT */
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;"undefined"!=typeof window?t=window:"undefined"!=typeof global?t=global:"undefined"!=typeof self&&(t=self),t.page=e()}}(function(){var e,t,n;return function e(t,n,r){function i(o,f){if(!n[o]){if(!t[o]){var s=typeof require=="function"&&require;if(!f&&s)return s(o,!0);if(a)return a(o,!0);var u=new Error("Cannot find module '"+o+"'");throw u.code="MODULE_NOT_FOUND",u}var c=n[o]={exports:{}};t[o][0].call(c.exports,function(e){var n=t[o][1][e];return i(n?n:e)},c,c.exports,e,t,n,r)}return n[o].exports}var a=typeof require=="function"&&require;for(var o=0;o<r.length;o++)i(r[o]);return i}({1:[function(e,t,n){var r=e("page");var i=this.setImmediate?this.setImmediate:function(e){window.setTimeout(e,0)};function a(e,t){if("function"===typeof e){return a("*",e)}if("function"===typeof t){r(e,t)}else if("string"===typeof e){a.show(e,t)}else{a.start(e)}}a.start=r.start;a.stop=r.stop;a.dispatch=r.dispatch;a.base=r.base;a.sameOrigin=r.sameOrigin;a.show=function(e,t,n){var i=a.uri(e,t);r.show(i,{},n)};a.replace=function(e,t,n,i){var o=a.uri(e,t);r.replace(o,{},n,i)};a.len=0;a.uri=function(e,t){var n=e;if(t){n=e.replace(/:([A-Za-z_]+)/g,function(e,n){var r=t[n];delete t[n];return r})}if(t&&Object.keys(t).length>0){n+="?"+a.querystring(t)}return n};a.querystring=function(e,t){var n=[],r;if(Array.isArray(e)){for(var i=0,o=e.length;i<o;i++){r=e[i];n.push(a.querystring({"":r},t))}}else if(typeof e==="object"){for(var f in e){if(!e.hasOwnProperty(f))continue;r=e[f];if(typeof r==="undefined")continue;if(t)f=t+"["+f+"]";if(r===null){n.push(f+"=")}else if(typeof r==="object"){n.push(a.querystring(r,f))}else{n.push([f,encodeURIComponent(r)].join("="))}}}return n.join("&")};a.back=function(e,t){if(a.len>0){history.back()}else if(e){a(e,t)}};a.redirect=function(e,t){i(function(){a.replace(e,t)})};a.teardown=function(){a.stop();a.len=0;r.callbacks=[]};var o=r.Context.prototype.pushState;r.Context.prototype.pushState=function(){a.len++;o.apply(this,arguments)};a.page=r;t.exports=a},{page:3}],2:[function(e,t,n){t.exports=Array.isArray||function(e){return Object.prototype.toString.call(e)=="[object Array]"}},{}],3:[function(e,t,n){(function(n){"use strict";var r=e("path-to-regexp");t.exports=h;var i="undefined"!==typeof document&&document.ontouchstart?"touchstart":"click";var a="undefined"!==typeof window&&(window.history.location||window.location);var o=true;var f=true;var s="";var u;var c=false;var p;function h(e,t){if("function"===typeof e){return h("*",e)}if("function"===typeof t){var n=new w(e);for(var r=1;r<arguments.length;++r){h.callbacks.push(n.middleware(arguments[r]))}}else if("string"===typeof e){h["string"===typeof t?"redirect":"show"](e,t)}else{h.start(e)}}h.callbacks=[];h.exits=[];h.current="";h.len=0;h.base=function(e){if(0===arguments.length)return s;s=e};h.start=function(e){e=e||{};if(u)return;u=true;if(false===e.dispatch)o=false;if(false===e.decodeURLComponents)f=false;if(false!==e.popstate)window.addEventListener("popstate",m,false);if(false!==e.click){document.addEventListener(i,y,false)}if(true===e.hashbang)c=true;if(!o)return;var t=c&&~a.hash.indexOf("#!")?a.hash.substr(2)+a.search:a.pathname+a.search+a.hash;h.replace(t,null,true,o)};h.stop=function(){if(!u)return;h.current="";h.len=0;u=false;document.removeEventListener(i,y,false);window.removeEventListener("popstate",m,false)};h.show=function(e,t,n,r){var i=new v(e,t);h.current=i.path;if(false!==n)h.dispatch(i);if(false!==i.handled&&false!==r)i.pushState();return i};h.back=function(e,t){if(h.len>0){history.back();h.len--}else if(e){setTimeout(function(){h.show(e,t)})}else{setTimeout(function(){h.show(s,t)})}};h.redirect=function(e,t){if("string"===typeof e&&"string"===typeof t){h(e,function(e){setTimeout(function(){h.replace(t)},0)})}if("string"===typeof e&&"undefined"===typeof t){setTimeout(function(){h.replace(e)},0)}};h.replace=function(e,t,n,r){var i=new v(e,t);h.current=i.path;i.init=n;i.save();if(false!==r)h.dispatch(i);return i};h.dispatch=function(e){var t=p,n=0,r=0;p=e;function i(){var e=h.exits[r++];if(!e)return a();e(t,i)}function a(){var t=h.callbacks[n++];if(e.path!==h.current){e.handled=false;return}if(!t)return l(e);t(e,a)}if(t){i()}else{a()}};function l(e){if(e.handled)return;var t;if(c){t=s+a.hash.replace("#!","")}else{t=a.pathname+a.search}if(t===e.canonicalPath)return;h.stop();e.handled=false;a.href=e.canonicalPath}h.exit=function(e,t){if(typeof e==="function"){return h.exit("*",e)}var n=new w(e);for(var r=1;r<arguments.length;++r){h.exits.push(n.middleware(arguments[r]))}};function d(e){if(typeof e!=="string"){return e}return f?decodeURIComponent(e.replace(/\+/g," ")):e}function v(e,t){if("/"===e[0]&&0!==e.indexOf(s))e=s+(c?"#!":"")+e;var n=e.indexOf("?");this.canonicalPath=e;this.path=e.replace(s,"")||"/";if(c)this.path=this.path.replace("#!","")||"/";this.title=document.title;this.state=t||{};this.state.path=e;this.querystring=~n?d(e.slice(n+1)):"";this.pathname=d(~n?e.slice(0,n):e);this.params={};this.hash="";if(!c){if(!~this.path.indexOf("#"))return;var r=this.path.split("#");this.path=r[0];this.hash=d(r[1])||"";this.querystring=this.querystring.split("#")[0]}}h.Context=v;v.prototype.pushState=function(){h.len++;history.pushState(this.state,this.title,c&&this.path!=="/"?"#!"+this.path:this.canonicalPath)};v.prototype.save=function(){history.replaceState(this.state,this.title,c&&this.path!=="/"?"#!"+this.path:this.canonicalPath)};function w(e,t){t=t||{};this.path=e==="*"?"(.*)":e;this.method="GET";this.regexp=r(this.path,this.keys=[],t)}h.Route=w;w.prototype.middleware=function(e){var t=this;return function(n,r){if(t.match(n.path,n.params))return e(n,r);r()}};w.prototype.match=function(e,t){var n=this.keys,r=e.indexOf("?"),i=~r?e.slice(0,r):e,a=this.regexp.exec(decodeURIComponent(i));if(!a)return false;for(var o=1,f=a.length;o<f;++o){var s=n[o-1];var u=d(a[o]);if(u!==undefined||!hasOwnProperty.call(t,s.name)){t[s.name]=u}}return true};var m=function(){var e=false;if("undefined"===typeof window){return}if(document.readyState==="complete"){e=true}else{window.addEventListener("load",function(){setTimeout(function(){e=true},0)})}return function t(n){if(!e)return;if(n.state){var r=n.state.path;h.replace(r,n.state)}else{h.show(a.pathname+a.hash,undefined,undefined,false)}}}();function y(e){if(1!==g(e))return;if(e.metaKey||e.ctrlKey||e.shiftKey)return;if(e.defaultPrevented)return;var t=e.path?e.path[0]:e.target;while(t&&"A"!==t.nodeName)t=t.parentNode;if(!t||"A"!==t.nodeName)return;if(t.hasAttribute("download")||t.getAttribute("rel")==="external")return;var r=t.getAttribute("href");if(!c&&t.pathname===a.pathname&&(t.hash||"#"===r))return;if(r&&r.indexOf("mailto:")>-1)return;if(t.target)return;if(!x(t.href))return;var i=t.pathname+t.search+(t.hash||"");if(typeof n!=="undefined"&&i.match(/^\/[a-zA-Z]:\//)){i=i.replace(/^\/[a-zA-Z]:\//,"/")}var o=i;if(i.indexOf(s)===0){i=i.substr(s.length)}if(c)i=i.replace("#!","");if(s&&o===i)return;e.preventDefault();h.show(o)}function g(e){e=e||window.event;return null===e.which?e.button:e.which}function x(e){var t=a.protocol+"//"+a.hostname;if(a.port)t+=":"+a.port;return e&&0===e.indexOf(t)}h.sameOrigin=x}).call(this,e("_process"))},{_process:5,"path-to-regexp":4}],4:[function(e,t,n){var r=e("isarray");t.exports=w;t.exports.parse=a;t.exports.compile=o;t.exports.tokensToFunction=f;t.exports.tokensToRegExp=v;var i=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^()])+)\\))?|\\(((?:\\\\.|[^()])+)\\))([+*?])?|(\\*))"].join("|"),"g");function a(e){var t=[];var n=0;var r=0;var a="";var o;while((o=i.exec(e))!=null){var f=o[0];var s=o[1];var c=o.index;a+=e.slice(r,c);r=c+f.length;if(s){a+=s[1];continue}if(a){t.push(a);a=""}var p=o[2];var h=o[3];var l=o[4];var d=o[5];var v=o[6];var w=o[7];var m=v==="+"||v==="*";var y=v==="?"||v==="*";var g=p||"/";var x=l||d||(w?".*":"[^"+g+"]+?");t.push({name:h||n++,prefix:p||"",delimiter:g,optional:y,repeat:m,pattern:u(x)})}if(r<e.length){a+=e.substr(r)}if(a){t.push(a)}return t}function o(e){return f(a(e))}function f(e){var t=new Array(e.length);for(var n=0;n<e.length;n++){if(typeof e[n]==="object"){t[n]=new RegExp("^"+e[n].pattern+"$")}}return function(n){var i="";var a=n||{};for(var o=0;o<e.length;o++){var f=e[o];if(typeof f==="string"){i+=f;continue}var s=a[f.name];var u;if(s==null){if(f.optional){continue}else{throw new TypeError('Expected "'+f.name+'" to be defined')}}if(r(s)){if(!f.repeat){throw new TypeError('Expected "'+f.name+'" to not repeat, but received "'+s+'"')}if(s.length===0){if(f.optional){continue}else{throw new TypeError('Expected "'+f.name+'" to not be empty')}}for(var c=0;c<s.length;c++){u=encodeURIComponent(s[c]);if(!t[o].test(u)){throw new TypeError('Expected all "'+f.name+'" to match "'+f.pattern+'", but received "'+u+'"')}i+=(c===0?f.prefix:f.delimiter)+u}continue}u=encodeURIComponent(s);if(!t[o].test(u)){throw new TypeError('Expected "'+f.name+'" to match "'+f.pattern+'", but received "'+u+'"')}i+=f.prefix+u}return i}}function s(e){return e.replace(/([.+*?=^!:${}()[\]|\/])/g,"\\$1")}function u(e){return e.replace(/([=!:$\/()])/g,"\\$1")}function c(e,t){e.keys=t;return e}function p(e){return e.sensitive?"":"i"}function h(e,t){var n=e.source.match(/\((?!\?)/g);if(n){for(var r=0;r<n.length;r++){t.push({name:r,prefix:null,delimiter:null,optional:false,repeat:false,pattern:null})}}return c(e,t)}function l(e,t,n){var r=[];for(var i=0;i<e.length;i++){r.push(w(e[i],t,n).source)}var a=new RegExp("(?:"+r.join("|")+")",p(n));return c(a,t)}function d(e,t,n){var r=a(e);var i=v(r,n);for(var o=0;o<r.length;o++){if(typeof r[o]!=="string"){t.push(r[o])}}return c(i,t)}function v(e,t){t=t||{};var n=t.strict;var r=t.end!==false;var i="";var a=e[e.length-1];var o=typeof a==="string"&&/\/$/.test(a);for(var f=0;f<e.length;f++){var u=e[f];if(typeof u==="string"){i+=s(u)}else{var c=s(u.prefix);var h=u.pattern;if(u.repeat){h+="(?:"+c+h+")*"}if(u.optional){if(c){h="(?:"+c+"("+h+"))?"}else{h="("+h+")?"}}else{h=c+"("+h+")"}i+=h}}if(!n){i=(o?i.slice(0,-2):i)+"(?:\\/(?=$))?"}if(r){i+="$"}else{i+=n&&o?"":"(?=\\/|$)"}return new RegExp("^"+i,p(t))}function w(e,t,n){t=t||[];if(!r(t)){n=t;t=[]}else if(!n){n={}}if(e instanceof RegExp){return h(e,t,n)}if(r(e)){return l(e,t,n)}return d(e,t,n)}},{isarray:2}],5:[function(e,t,n){var r=t.exports={};r.nextTick=function(){var e=typeof window!=="undefined"&&window.setImmediate;var t=typeof window!=="undefined"&&window.MutationObserver;var n=typeof window!=="undefined"&&window.postMessage&&window.addEventListener;if(e){return function(e){return window.setImmediate(e)}}var r=[];if(t){var i=document.createElement("div");var a=new MutationObserver(function(){var e=r.slice();r.length=0;e.forEach(function(e){e()})});a.observe(i,{attributes:true});return function e(t){if(!r.length){i.setAttribute("yes","no")}r.push(t)}}if(n){window.addEventListener("message",function(e){var t=e.source;if((t===window||t===null)&&e.data==="process-tick"){e.stopPropagation();if(r.length>0){var n=r.shift();n()}}},true);return function e(t){r.push(t);window.postMessage("process-tick","*")}}return function e(t){setTimeout(t,0)}}();r.title="browser";r.browser=true;r.env={};r.argv=[];function i(){}r.on=i;r.addListener=i;r.once=i;r.off=i;r.removeListener=i;r.removeAllListeners=i;r.emit=i;r.binding=function(e){throw new Error("process.binding is not supported")};r.cwd=function(){return"/"};r.chdir=function(e){throw new Error("process.chdir is not supported")}},{}]},{},[1])(1)});