jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/pagejs/page.js

1 line
9.9 KiB
JavaScript
Raw Normal View History

2017-01-27 16:07:14 -07:00
define([],function(){"use strict";function page(path,fn){if("function"==typeof path)return page("*",path);if("function"==typeof fn)for(var route=new Route(path),i=1;i<arguments.length;++i)page.callbacks.push(route.middleware(arguments[i]));else"string"==typeof path?page["string"==typeof fn?"redirect":"show"](path,fn):page.start(path)}function unhandled(ctx){if(!ctx.handled){var current;current=hashbang?base+location.hash.replace("#!",""):location.pathname+location.search,current!==ctx.canonicalPath&&(page.stop(),ctx.handled=!1,location.href=ctx.canonicalPath)}}function decodeURLEncodedURIComponent(val){return"string"!=typeof val?val:decodeURLComponents?decodeURIComponent(val.replace(/\+/g," ")):val}function Context(path,state){"/"===path[0]&&0!==path.indexOf(base)&&(path=base+(hashbang?"#!":"")+path);var i=path.indexOf("?");if(this.canonicalPath=path,this.path=path.replace(base,"")||"/",hashbang&&(this.path=this.path.replace("#!","")||"/"),this.title=document.title,this.state=state||{},this.state.path=path,this.querystring=~i?decodeURLEncodedURIComponent(path.slice(i+1)):"",this.pathname=decodeURLEncodedURIComponent(~i?path.slice(0,i):path),this.params={},this.hash="",!hashbang){if(!~this.path.indexOf("#"))return;var parts=this.path.split("#");this.path=parts[0],this.hash=decodeURLEncodedURIComponent(parts[1])||"",this.querystring=this.querystring.split("#")[0]}}function Route(path,options){options=options||{},this.path="*"===path?"(.*)":path,this.method="GET",this.regexp=pathToRegexp(this.path,this.keys=[],options.sensitive,options.strict)}function ignorePopState(event){var state=event.state||{};return previousPopState.navigate===!1?(previousPopState=state,!0):(previousPopState=state,!1)}function onclick(e){if(1===which(e)&&!(e.metaKey||e.ctrlKey||e.shiftKey||e.defaultPrevented)){for(var el=e.target;el&&"A"!==el.nodeName;)el=el.parentNode;if(el&&"A"===el.nodeName&&!el.hasAttribute("download")&&"external"!==el.getAttribute("rel")){var link=el.getAttribute("href");if("#"===link)return void e.preventDefault();if((hashbang||el.pathname!==location.pathname||!el.hash&&"#"!==link)&&!(link&&link.indexOf("mailto:")>-1)&&!el.target&&sameOrigin(el.href)){var path=el.pathname+el.search+(el.hash||""),orig=path;0===path.indexOf(base)&&(path=path.substr(base.length)),hashbang&&(path=path.replace("#!","")),base&&orig===path||(e.preventDefault(),page.show(orig))}}}}function which(e){return e=e||window.event,null===e.which?e.button:e.which}function sameOrigin(href){var origin=location.protocol+"//"+location.hostname;return location.port&&(origin+=":"+location.port),href&&0===href.indexOf(origin)}function parse(str){for(var res,tokens=[],key=0,index=0,path="";null!=(res=PATH_REGEXP.exec(str));){var m=res[0],escaped=res[1],offset=res.index;if(path+=str.slice(index,offset),index=offset+m.length,escaped)path+=escaped[1];else{path&&(tokens.push(path),path="");var prefix=res[2],name=res[3],capture=res[4],group=res[5],suffix=res[6],asterisk=res[7],repeat="+"===suffix||"*"===suffix,optional="?"===suffix||"*"===suffix,delimiter=prefix||"/",pattern=capture||group||(asterisk?".*":"[^"+delimiter+"]+?");tokens.push({name:name||key++,prefix:prefix||"",delimiter:delimiter,optional:optional,repeat:repeat,pattern:escapeGroup(pattern)})}}return index<str.length&&(path+=str.substr(index)),path&&tokens.push(path),tokens}function escapeString(str){return str.replace(/([.+*?=^!:${}()[\]|\/])/g,"\\$1")}function escapeGroup(group){return group.replace(/([=!:$\/()])/g,"\\$1")}function attachKeys(re,keys){return re.keys=keys,re}function flags(options){return options.sensitive?"":"i"}function regexpToRegexp(path,keys){var groups=path.source.match(/\((?!\?)/g);if(groups)for(var i=0;i<groups.length;i++)keys.push({name:i,prefix:null,delimiter:null,optional:!1,repeat:!1,pattern:null});return attachKeys(path,keys)}function arrayToRegexp(path,keys,options){for(var parts=[],i=0;i<path.length;i++)parts.push(pathToRegexp(path[i],keys,options).source);var regexp=new RegExp("(?:"+parts.join("|")+")",flags(options));return attachKeys(regexp,keys)}function stringToRegexp(path