-
Notifications
You must be signed in to change notification settings - Fork 5
/
ractive-route.min.js
13 lines (13 loc) · 6.82 KB
/
ractive-route.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
* ractive-route 0.3.11
* https://github.com/MartinKolarik/ractive-route/
*
* Copyright (c) 2014 Martin Kolárik
* https://kolarik.sk
*
* Licensed under the MIT license
* http://www.opensource.org/licenses/MIT
*/
!function(a){"undefined"!=typeof module&&module.exports?module.exports=a(require("ractive")):"function"==typeof define&&define.amd?define(["ractive"],a):a(window.Ractive)}(function(a){function b(a,b,e,g){this.pattern=a,this.map=c(a),this.regExp=d(a),this.strictRegExp=f(a),this.isComponent=!!b.extend,this.Handler=b,this.observe=i({qs:[],hash:[],state:[]},e),this.allObserved=this.observe.qs.concat(this.observe.hash,this.observe.state),this.router=g||{},this.view=null}function c(a){return(a.match(/\/:\w+/g)||[]).map(function(a){return a.substr(2)})}function d(a){return new RegExp(e(a).replace(/^\^(\\\/)?/,"^\\/?").replace(/(\\\/)?\$$/,"\\/?$"),"i")}function e(a){return("^"+a+"$").replace(/\/:\w+(\([^)]+\))?/g,"(?:/([^/]+)$1)").replace(/\(\?:\/\(\[\^\/]\+\)\(/g,"(?:/(").replace(/\//g,"\\/")}function f(a){return new RegExp(e(a))}function g(a){this.globals=a.globals||[],this.basePath=a.basePath||"",this.el=a.el,this.data=a.data||function(){return{}},this.history=a.history||history,this.strictMode=!!a.strictMode,this.reloadOnClick=a.reloadOnClick,this.linksWatcher=null,this.stateWatcher=null,this.route=null,this.routes=[],this.uri={}}function h(a,b,c){return a.path!==b.path||a.qs!==b.qs||decodeURIComponent(a.hash)!==decodeURIComponent(b.hash)&&(!c||c.observe.hash.length)}function i(a,b){for(var c=1,d=arguments.length;c<d;c++)for(var e in arguments[c])arguments[c].hasOwnProperty(e)&&void 0!==arguments[c][e]&&(a[e]=arguments[c][e]);return a}function j(a){return s(a,function(a){return!l(a)})}function k(a){return a.replace(/\+/g," ")}function l(a){return a&&"object"==typeof a?!Object.keys(a).length:!a}function m(a){return Array.prototype.slice.call(arguments).join("/").replace(/\/+/g,"/")}function n(a,b){for(;a&&a.nodeName.toLowerCase()!==b;)a=a.parentNode;return a&&a.nodeName.toLowerCase()===b?a:null}function o(a,b){try{var c=j(JSON.parse(decodeURIComponent(a.substr(2))));return b?s(c,b):c}catch(d){return{}}}function p(a){try{return JSON.parse(a)}catch(b){return a||""}}function q(a){var b=a.match(/^(?:([\w+.-]+):\/\/([^/]+))?([^?#]*)?(\?[^#]*)?(#.*)?/);return{protocol:b[1]||"",host:b[2]||"",path:b[3]||"",qs:b[4]||"",hash:b[5]||""}}function r(a,b){var c=a.indexOf("?"),d={};if(-1!==c)for(var e=a.substr(c+1).split("&"),f=[],g=0,h=e.length;g<h;g++)f=e[g].split("="),l(f[1])||l(p(f[1]))||(d[k(decodeURIComponent(f[0]))]=p(k(decodeURIComponent(f[1]))));return b?s(d,b):d}function s(a,b){var c={};if("function"==typeof b)for(var d in a)a.hasOwnProperty(d)&&b(a[d],d)&&(c[d]=a[d]);else for(var e=0,f=b.length;e<f;e++)c[b[e]]=a[b[e]];return c}function t(a){var b=document.getElementById(a);b?window.scrollBy(0,b.getBoundingClientRect().top):window.scrollTo(0,0)}function u(a){return a&&"object"==typeof a?JSON.stringify(a):a}function v(a){return a=j(a),l(a)?"":"#!"+encodeURIComponent(u(a))}function w(a){var b="";for(var c in a)a.hasOwnProperty(c)&&!l(a[c])&&(b+="&"+encodeURIComponent(c)+"="+encodeURIComponent(u(a[c])));return b?"?"+b.substr(1):""}return b.prototype.destroy=function(){return this.view&&(this.view.teardown(),this.view=null),this},b.prototype.getState=function(){for(var a={},b=0,c=this.allObserved.length;b<c;b++)a[this.allObserved[b]]=this.view.get(this.allObserved[b]);return{qs:s(a,this.observe.qs),hash:s(a,this.observe.hash),state:s(a,this.observe.state)}},b.prototype.init=function(a,b){var c=this;return i(b,this.parsePath(a.path),r(a.qs,this.observe.qs),o(a.hash,this.observe.hash)),this.isComponent?(this.view=new this.Handler({el:this.router.el,data:b}),this.allObserved.length&&this.view.observe(this.allObserved.join(" "),function(){c.updating||c.router.update()},{init:!1}),this.view.set("__ready",!0)):this.Handler({el:this.router.el,data:b,uri:this.router.uri}),this},b.prototype.match=function(a,b){return b?this.strictRegExp.test(a):this.regExp.test(a)},b.prototype.parsePath=function(a){for(var b=a.match(this.regExp),c={},d=0,e=this.map.length;d<e;d++)l(b[d+1])||(c[this.map[d]]=p(b[d+1]));return c},g.prototype.addRoute=function(a,c,d){return this.routes.push(new b(a,c,d,this)),this},g.prototype.buildHash=function(a){var b=this.route.getState().hash;return l(b)&&a?a:v(b)},g.prototype.buildQS=function(a){return w(i.apply(null,[{}].concat(a,this.route.getState().qs)))},g.prototype.dispatch=function(a,b){b=b||{};var c=q(a),d=this.match(c.path),e=this.uri;if(!d)return this.redirect(a);if(b.reload||h(this.uri,c,d)){this.route&&this.route.view&&(b.state=b.state||{},this.globals.forEach(function(a){void 0===b.state[a]&&(b.state[a]=this.route.view.get(a))},this));var f="function"==typeof this.data?this.data():this.data,g=i(f,b.state,b.hash,b.qs);this.route&&this.route.destroy(),this.uri=c,this.route=d.init(c,g)}return b.noScroll||t(c.hash.substr(1)),this.update(!e.path||e.path!==c.path,!b.noHistory,c)},g.prototype.getUri=function(){return location.pathname.substr(this.basePath.length)+location.search+location.hash},g.prototype.init=function(a){return this.dispatch(this.getUri(),i({noHistory:!0},a))},g.prototype.match=function(a){for(var b=-1;this.routes[++b];)if(this.routes[b].match(a))return this.routes[b];return null},g.prototype.redirect=function(a){var b=q(a);return b.protocol||b.host?location.href=a:location.pathname=m(this.basePath,a),this},g.prototype.unwatchLinks=function(){return this.linksWatcher&&(document.body.removeEventListener("click",this.linksWatcher),this.linksWatcher=null),this},g.prototype.unwatchState=function(){return this.stateWatcher&&(window.removeEventListener("popstate",this.stateWatcher),this.stateWatcher=null),this},g.prototype.update=function(a,b,c){if(!this.route)return this;c=c||{qs:"",hash:""};var d=m(this.basePath,this.uri.path),e=this.buildQS([r(c.qs)].concat(a?[]:[r(location.search)])),f=c.hash||a||e!==location.search?this.buildHash(c.hash):location.hash,g=d+e+f,h=location.pathname+location.search+location.hash,i=this.route.getState().state;return this.uri.qs=e,this.uri.hash=f,!0===b?this.history.pushState(i,null,g):!1===b?this.history.replaceState(i,null,g):g!==h&&this.history.pushState(i,null,g),this},g.prototype.watchLinks=function(a){a=a||new RegExp("^((https?:)?\\/\\/"+location.hostname.replace(/\./g,"\\.")+".*|((?!\\/\\/)[^:]+))$");var b=this;return document.body.addEventListener("click",this.unwatchLinks().linksWatcher=function(c){var d=n(c.target,"a");if(d){var e=d.getAttribute("href")||d.getAttribute("data-href");e&&!d.classList.contains("router-ignore")&&a.test(e)&&(b.reloadOnClick?b.dispatch(e,{reload:!0}):b.dispatch(e),c.preventDefault())}}),this},g.prototype.watchState=function(){var a=this;return window.addEventListener("popstate",this.unwatchState().stateWatcher=function(b){b.state&&a.init({state:b.state})}),this},g.Route=b,a.Router=g});
//# sourceMappingURL=ractive-route.min.map