@vkontakte/router
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -15,2 +15,2 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("react"))&&"object"==typeof e&&"default"in e?e.default:e,r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)}; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function n(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var o=function(){return(o=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function a(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o<n.length;o++)t.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n[o])&&(r[n[o]]=e[n[o]])}return r}function i(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var n=Array(e),o=0;for(t=0;t<r;t++)for(var a=arguments[t],i=0,s=a.length;i<s;i++,o++)n[o]=a[i];return n}var s=function(e){var t=e.split(".");t.splice(-1,1);var r=t.length?".":"";return t.join(r)},u=function(e){return e.includes(".")},c=function(e,t){for(var r=function(e){var t=e.split("."),r="";return t.map((function(e,t){return r+=t?"."+e:e}))}(t),n=i(e),o=null;n.length&&r.length;){var a=n.shift(),s=r[0];if(a.name===s){if(r.length>1){n.push.apply(n,a.children),r.shift();continue}if(1===r.length){o=a;break}}}return o},f=function(e){var t=e.name,r=e.params,n=e.encodeParams,o=e.decodeParams,i=a(e,["name","params","encodeParams","decodeParams"]);this.name=t,this.params=r||[],this.parent=null,this.data=i,this.children=[],this.encodeParams=n,this.decodeParams=o},d=function(e){var t=this;this.errorLogger=function(e,t){return console.error(e,t)},this.contains=function(e){t.traverse(e)},this.getParentNode=function(e){var r=s(e),n=t.getByPath(r);if(n)return n},this.add=function(e,r){var n=e instanceof f?e:new f(e),o=null;t.contains((function(e){r&&e&&e.name===r.name?o=e:r||(o=t.root)})),o?(o.children=o.children||[],o.children.push(n),n.parent=o):t.errorLogger("Cannot add node to a non-existent parent")},this.getByPath=function(e){var r=c(t.root.children,e);return r||t.errorLogger("No such route in a tree"),r},this.findByName=function(e){var r;return t.contains((function(t){t.name===e&&(r=t)})),r||t.errorLogger("No such route in a tree"),r},this.remove=function(e){var r,n=null,o=null;return t.contains((function(t){t.name===e&&(n=t)})),n?void 0===(r=t.findIndex(n.children,e))?t.errorLogger("Node to remove does not exist"):o=n.children.splice(r,1):t.errorLogger("Parent does not exist"),o},this.traverse=function(e){!function t(r){for(var n=0,o=r.children?r.children.length:0;n<o;n++)t(r.children[n]);e(r)}(t.root)},this.findIndex=function(e,t){return e.findIndex((function(e){return e.name===t}))},this.getRouteNode=function(e){void 0===e&&(e="");var r=u(e)?e:"",n=r?t.getByPath(r):t.findByName(e);return n instanceof f||t.errorLogger("Route is not registered",r),n},e.errorLogger&&(this.errorLogger=e.errorLogger),this.root=new f({name:"",params:"",children:[]})},h=function(e){var t=[],r=function(e,n){if(Array.isArray(e))e.forEach((function(e){return r(e)}));else if(e){var i=function(e){e.path;var t=a(e,["path"]);return o({},t)}(e);if(n)n.children.push(i);else if(u(e.name)){var f=function(e,t){var r=s(t);return c(e,r)}(t,e.name);f&&(f.children=f.children||[],f.children.push(i))}else t.push(i);Array.isArray(e.children)&&e.children.forEach((function(t){return r(t,e)}))}};return r(e),t};function l(e,t){void 0===t&&(t={});var r=h(e),n=new d(t);return function e(t,r){if(Array.isArray(t))t.forEach((function(t){return e(t)}));else if(t){var o=new f(t);r?n.add(o,r):n.add(o),Array.isArray(t.children)&&t.children.forEach((function(t){return e(t,o)}))}}(r),n}var p=function(e){return("object"==typeof e||"function"==typeof e)&&null!==e},v=function(e,t){return void 0===t&&(t=""),e&&"object"==typeof e?Object.entries(e).filter((function(e){return!!e[1]})).map((function(e){var r=e[0],n=e[1];return"object"==typeof n?v(n,t+(r+".")):[t+r,n].map(escape).join("=")})).filter((function(e){return e})).join("&"):""},g=function(e,t,r){if(void 0===r&&(r=!1),!e||!t)return!1;for(var n=0,o=Object.keys(t);n<o.length;n++){var a=o[n],i=e[a],s=t[a],u=p(i)&&p(s);if(u&&!g(i,s)||!u&&(r?i!==s:i!=s))return!1}return!0},m=function(e,t,r){if(void 0===r&&(r=!0),!e||!t)return!1;var n=Object.keys(e),o=Object.keys(t);if(n.length!==o.length)return!1;for(var a=0,i=n;a<i.length;a++){var s=i[a],u=e[s],c=t[s],f=p(u)&&p(c);if(f&&!m(u,c)||!f&&(r?u!==c:u!=c))return!1}return!0},b=function(){return window.location.pathname},y=function(e,t){return function(r){if(1===function(e){return null===(e=e||window.event).which?e.button:e.which}(r)&&!(r.metaKey||r.ctrlKey||r.shiftKey||r.defaultPrevented)){for(var n,o,a=r.target;a&&"A"!==a.nodeName&&"BUTTON"!==a.nodeName;)a=a.parentNode;if(a&&"A"===a.nodeName)if(!a.hasAttribute("download")&&"external"!==a.getAttribute("rel")&&(n=a.href,(o=function(e){return e.replace("http://","").replace("https://","").split("/")[0]})(window.location.href)===o(n)))if(!a.target&&a.href){var i=e(a.href);if(i){r.preventDefault();var s=i.modal||i.page,u=i.params||{};t(s,u)}}}}},S=function(e,t,r){return window.history.pushState(e,t,r)},w=function(e,t,r){return window.history.replaceState(e,t,r)},P=function(e,t){var r=t.useHash&&!(-1===window.navigator.userAgent.indexOf("Trident"));return window.addEventListener("popstate",e),r&&window.addEventListener("hashchange",e),function(){window.removeEventListener("popstate",e),r&&window.removeEventListener("hashchange",e)}},L=function(e,t){var r=document.ontouchstart?"touchstart":"click",n=y(e,t);return document.addEventListener(r,n,!1),function(){window.removeEventListener(r,n)}},A=function(e,t){return(function(e){try{return encodeURI(decodeURI(e))}catch(t){return e}}((e.preservePath?b():"")+e.preserveHash?window.location.pathname.replace(new RegExp("^"),""):window.location.hash.replace(new RegExp("^#"),""))||"/")+(t||window.location.search)},N={defaultRoute:"default",base:"",subRouteKey:"subRoute",routeKey:"route",rootPage:void 0,preserveHash:!1,preservePath:!0},x=function(){function e(e,t){var r=this;this.history=[],this.routes=[],this.subscribers=[],this.routeHandlerCollection={},this.config=N,this.isStarted=!1,this.errorLogger=function(e){return console.log(e)},this.trees={},this.initialize=function(){var e=(r.routes[0]||{}).name,t=r.config.defaultRoute||e;r.defaultState={page:t,modal:null,params:{}};var n=r.buildState(A(r.config));r.browserSessionId=Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15),r.setState(n)},this.buildHistory=function(){var e=r.getState(),t=e.page,n=e.params,o=r.config.rootPage;if(t!==o){var a=r.makeState(o).newState;r.history.push(a),r.updateUrl(a)}for(var s=i(r.getActiveNodes(t));s.length;){var u=s.shift(),c={page:u.name,modal:null,params:r.getActiveParams([u],n)};r.history.push(c),r.updateUrl(c)}},this.onPopState=function(e){var t,n,a=null===(t=e.state)||void 0===t?void 0:t.counter,i=r.history[0],s=r.history[a],u=s||i,c=(null===(n=e.state)||void 0===n?void 0:n.browserSessionId)===r.browserSessionId;void 0!==a&&c?r.replaceState(u):c&&s||(r.replaceState(i),r.updateUrl(o(o({},i),{counter:0}),{replace:!0}))},this.broadCastState=function(){var e=r.getState(),t=r.getPrevState(),n=r.getHistory();r.subscribers.forEach((function(r){return r({toState:e,fromState:t,history:n})}))},this.done=function(e){e.redirect&&r.go(e.redirect.name,{},{replace:!0})},this.setActiveTree=function(e){r.trees[e]?r.tree=r.trees[e]:r.errorLogger("Tree does not exist")},this.addTree=function(e,t){if(r.trees[e])r.errorLogger("Tree with such name already exists");else{var n=l(t);r.trees[e]=n}},this.removeTree=function(e){r.trees[e]?r.tree=r.trees[e]:r.errorLogger("Tree does not exist")},this.replaceState=function(e){var t=o({},r.state),n=o({},e);if(!m(t,n)){var a=r.routeHandlerCollection[n.page];r.state=n,r.prevState=t,"function"==typeof a&&a(n,t,r.done)}r.broadCastState()},this.setState=function(e){var t=o({},r.state),n=o(o({},r.state),e);if(!m(t,n)){var a=r.routeHandlerCollection[n.page];r.state=n,r.prevState=t,"function"==typeof a&&a(n,t,r.done)}r.broadCastState()},this.buildState=function(e){var t=function(e){var t={},r=e&&e.includes("?")?e.slice(e.indexOf("?")+1):"";if(!r.length)return t;for(var n=0,o=r?r.split("&"):[];n<o.length;n++){var a=o[n].split("="),i=a[0],s=a[1];if(s=s||"",i.includes(".")){var u=i.split("."),c=u.pop(),f=u.join(".");t[f]=t[f]||{},t[f][c]=s}t[i]=s}return t}(function(e,t){var r=e.match(/^(?:http|https):\/\/(?:[0-9a-z_\-.:]+?)(?=\/)(.*)$/),n=(r?r[1]:e).match(/^(.+?)(#.+?)?(\?.+)?$/);if(!n)return"";var o=n[1],a=n[2]||"",i=n[3]||"";return(t.useHash?a.replace(new RegExp("^#"+t.hashPrefix),""):t.base?o.replace(new RegExp("^"+t.base),""):o)+i}(e,r.config)),n=t.p,o=t.m,i=void 0===o?null:o,s=a(t,["p","m"]),u=r.tree.getRouteNode(n),c=r.defaultState,f=s;return u&&(u.decodeParams&&(f=u.decodeParams(s)),c={page:n,modal:i,params:f}),c},this.subscribe=function(e){if(!r.subscribers.includes(e))return r.subscribers.push(e),r.broadCastState(),function(){return r.unsubscribe(e)};r.errorLogger("This listener is already subscribed")},this.unsubscribe=function(e){r.subscribers.includes(e)&&r.subscribers.slice(r.subscribers.indexOf(e),1)},this.removeAllSubscribers=function(){r.subscribers=[]},this.add=function(e){Array.isArray(e)?(r.routes=i(r.routes,e),e.forEach((function(e){r.tree.add(e)}))):e?r.tree.add(e):r.errorLogger("No such route in a tree")},this.remove=function(e){r.tree.remove(e)},this.buildSearch=function(e,t){void 0===t&&(t={});var n=r.makeState(e,t),a=n.newState,i=n.encodeParams,s=a.page,u=a.modal,c=a.params,f=c;i&&(f=i(c));var d=o({p:s,m:u},f),h=v(d),l=h.length?"?"+h:"";return""+r.config.base+l},this.buildUrl=function(e,t){return void 0===t&&(t={}),r.buildSearch(e,t)},this.buildPath=function(e,t){return void 0===t&&(t={}),r.buildSearch(e,t)},this.getActiveNodes=function(e){var t=[];if(e)for(var n=[r.tree.getRouteNode(e)];n.length;){var o=n.shift();t.push(o),o.parent&&o.parent.name&&n.push(o.parent)}return t.reverse()},this.getRequiredParams=function(e){var t=[];if(e)for(var r=[e];r.length;){var n=r.shift();n&&(n.params&&(t=t.concat(n.params)),n.parent&&r.push(n.parent))}return t},this.getActiveParams=function(e,t){for(var n={},o=i(e);o.length;){var a=o.shift();r.getRequiredParams(a).forEach((function(e){n[e]=t[e]}))}return n},this.makeState=function(e,t){var n,a;void 0===t&&(t={});var i=r.getState(),s=r.tree.getRouteNode(e),u=s.data,c=s.decodeParams,f=s.encodeParams,d=r.config.subRouteKey,h=r.getActiveNodes(e),l=o({},t);if(null===(n=null==s?void 0:s.parent)||void 0===n?void 0:n.name){var p=r.getActiveParams(h,i.params);l=o(o({},p),t)}var v={page:e,modal:null,params:l};return(null===(a=null==s?void 0:s.data)||void 0===a?void 0:a[d])&&(v={page:i.page,params:o(o({},i.params),t),modal:s.name}),{newState:v,routeData:u,activeNodes:h,encodeParams:f,decodeParams:c}},this.go=function(e,t,n,o){void 0===n&&(n={});var a=r.makeState(e,t),i=a.newState,s=a.routeData,u=a.encodeParams,c=a.decodeParams,f=r.history.length,d=r.history[f-2],h=m(r.state,i),l=m(d,i);if(h)r.broadCastState();else{l?r.history.pop():r.history.push(i),c&&(i.params=c(i.params)),r.setState(i);var p=r.getPrevState();u&&(i.params=u(i.params)),s&&!1!==s.updateUrl?r.updateUrl(i,n):r.updateUrl(p,{fakeEntry:!0}),o&&o(i)}},this.updateUrl=function(e,t,n){var a;void 0===t&&(t={}),void 0===n&&(n="");var i=r.history.length-1,s=o(o({},e),{browserSessionId:r.browserSessionId,counter:i});if(t.replace&&(s=o(o({},e),{counter:null!==(a=e.counter)&&void 0!==a?a:i,browserSessionId:r.browserSessionId})),t.fakeEntry){var u=A(r.config);S(s,n,u)}else{var c=o({p:e.page},e.params);e.modal&&(c.m=e.modal);var f=v(c),d=f.length?"?"+f:"",h=""+A(r.config,d)+r.config.base;t.replace?w(s,n,h):S(s,n,h)}},this.back=function(){window.history.back()},this.start=function(e,t,n){var o=r.getState(),a=r.config.defaultRoute,i=r.routes[0];if(r.isStarted=!0,r.removePopStateListener=P(r.onPopState,r.config),r.removeLinkPressListener=L(r.buildState,r.go),r.buildHistory(),o&&o.page){var s=o.modal||o.page,u=o.params;r.go(s,u,{firstLoad:!0})}else e?r.go(e,t,n):a?r.go(a):i&&i.name?r.go(i.name):r.errorLogger("No routes specified")},this.stop=function(){r.isStarted=!1,r.removeLinkPressListener(),r.removePopStateListener()},this.getState=function(){return r.state},this.getPrevState=function(){return r.prevState},this.isActive=function(e,t,n,o){void 0===n&&(n=!0),void 0===o&&(o=!1);var a=r.getState({withoutHistory:!0}),i=a.params,s=r.getActiveNodes(a.page),u=r.getActiveNodes(a.modal),c=s.concat(u).find((function(t){return t.name===e})),f=!!c,d=r.getRequiredParams(c),h=o?function(e,t){for(var r={};e.length;){var n=e.shift();r[n]=t[n]}return r}(d,t):t,l=g(i,h),p=r.makeState(e,h).newState,v=m(a,p);return n?v:t&&Object.keys(t).length?v||f&&l:v||f},this.canActivate=function(e,t){r.routeHandlerCollection[e]=t},this.routes=e||[],this.config=o(o({},N),t),this.errorLogger=this.config.errorLogger?this.config.errorLogger:this.errorLogger,this.tree=l(this.routes,{errorLogger:this.errorLogger}),this.initialize()}return e.prototype.getHistory=function(){return i(this.history)},e}(),O=function(e,t){return void 0===t&&(t=N),new x(e,t)},j=t.createContext({}),E=function(e){function r(t){var r=e.call(this,t)||this;r.onNavigatorChange=function(e){var t=e.toState;r.setState(o({},t))};var n=r.props,a=n.routes,i=n.config,s=O(a,i),u=s.getState();return s.subscribe(r.onNavigatorChange),s.start(),r.state=o({},u),r}return n(r,e),r.prototype.render=function(){return t.createElement(j.Provider,{value:this.state},this.props.children)},r}(t.PureComponent);exports.Navigator=x,exports.NavigatorProvider=E,exports.createNavigator=O,exports.withNavigator=function(e){var r;return(r=function(r){function a(){return null!==r&&r.apply(this,arguments)||this}return n(a,r),a.prototype.render=function(){var r=o(o({},this.props),this.context);return t.createElement(e,o({},r))},a}(t.PureComponent)).contextType=j,r}; | ||
***************************************************************************** */function n(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var o=function(){return(o=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function a(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o<n.length;o++)t.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n[o])&&(r[n[o]]=e[n[o]])}return r}function i(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var n=Array(e),o=0;for(t=0;t<r;t++)for(var a=arguments[t],i=0,s=a.length;i<s;i++,o++)n[o]=a[i];return n}var s=function(e){var t=e.split(".");t.splice(-1,1);var r=t.length?".":"";return t.join(r)},u=function(e){return e.includes(".")},c=function(e,t){for(var r=function(e){var t=e.split("."),r="";return t.map((function(e,t){return r+=t?"."+e:e}))}(t),n=i(e),o=null;n.length&&r.length;){var a=n.shift(),s=r[0];if(a.name===s){if(r.length>1){n.push.apply(n,a.children),r.shift();continue}if(1===r.length){o=a;break}}}return o},f=function(e){var t=e.name,r=e.params,n=e.encodeParams,o=e.decodeParams,i=a(e,["name","params","encodeParams","decodeParams"]);this.name=t,this.params=r||[],this.parent=null,this.data=i,this.children=[],this.encodeParams=n,this.decodeParams=o},d=function(e){var t=this;this.errorLogger=function(e,t){return console.error(e,t)},this.contains=function(e){t.traverse(e)},this.getParentNode=function(e){var r=s(e),n=t.getByPath(r);if(n)return n},this.add=function(e,r){var n=e instanceof f?e:new f(e),o=null;t.contains((function(e){r&&e&&e.name===r.name?o=e:r||(o=t.root)})),o?(o.children=o.children||[],o.children.push(n),n.parent=o):t.errorLogger("Cannot add node to a non-existent parent")},this.getByPath=function(e){var r=c(t.root.children,e);return r||t.errorLogger("No such route in a tree"),r},this.findByName=function(e){var r;return t.contains((function(t){t.name===e&&(r=t)})),r||t.errorLogger("No such route in a tree"),r},this.remove=function(e){var r,n=null,o=null;return t.contains((function(t){t.name===e&&(n=t)})),n?void 0===(r=t.findIndex(n.children,e))?t.errorLogger("Node to remove does not exist"):o=n.children.splice(r,1):t.errorLogger("Parent does not exist"),o},this.traverse=function(e){!function t(r){for(var n=0,o=r.children?r.children.length:0;n<o;n++)t(r.children[n]);e(r)}(t.root)},this.findIndex=function(e,t){return e.findIndex((function(e){return e.name===t}))},this.getRouteNode=function(e){void 0===e&&(e="");var r=u(e)?e:"",n=r?t.getByPath(r):t.findByName(e);return n instanceof f||t.errorLogger("Route is not registered",r),n},e.errorLogger&&(this.errorLogger=e.errorLogger),this.root=new f({name:"",params:"",children:[]})},h=function(e){var t=[],r=function(e,n){if(Array.isArray(e))e.forEach((function(e){return r(e)}));else if(e){var i=function(e){e.path;var t=a(e,["path"]);return o({},t)}(e);if(n)n.children.push(i);else if(u(e.name)){var f=function(e,t){var r=s(t);return c(e,r)}(t,e.name);f&&(f.children=f.children||[],f.children.push(i))}else t.push(i);Array.isArray(e.children)&&e.children.forEach((function(t){return r(t,e)}))}};return r(e),t};function l(e,t){void 0===t&&(t={});var r=h(e),n=new d(t);return function e(t,r){if(Array.isArray(t))t.forEach((function(t){return e(t)}));else if(t){var o=new f(t);r?n.add(o,r):n.add(o),Array.isArray(t.children)&&t.children.forEach((function(t){return e(t,o)}))}}(r),n}var p=function(e){return("object"==typeof e||"function"==typeof e)&&null!==e},v=function(e,t){return void 0===t&&(t=""),e&&"object"==typeof e?Object.entries(e).filter((function(e){return!!e[1]})).map((function(e){var r=e[0],n=e[1];return"object"==typeof n?v(n,t+(r+".")):[t+r,n].map(escape).join("=")})).filter((function(e){return e})).join("&"):""},g=function(e,t,r){if(void 0===r&&(r=!1),!e||!t)return!1;for(var n=0,o=Object.keys(t);n<o.length;n++){var a=o[n],i=e[a],s=t[a],u=p(i)&&p(s);if(u&&!g(i,s)||!u&&(r?i!==s:i!=s))return!1}return!0},m=function(e,t,r){if(void 0===r&&(r=!0),!e||!t)return!1;var n=Object.keys(e),o=Object.keys(t);if(n.length!==o.length)return!1;for(var a=0,i=n;a<i.length;a++){var s=i[a],u=e[s],c=t[s],f=p(u)&&p(c);if(f&&!m(u,c)||!f&&(r?u!==c:u!=c))return!1}return!0},b=function(){return window.location.pathname},y=function(e,t){return function(r){if(1===function(e){return null===(e=e||window.event).which?e.button:e.which}(r)&&!(r.metaKey||r.ctrlKey||r.shiftKey||r.defaultPrevented)){for(var n,o,a=r.target;a&&"A"!==a.nodeName&&"BUTTON"!==a.nodeName;)a=a.parentNode;if(a&&"A"===a.nodeName)if(!a.hasAttribute("download")&&"external"!==a.getAttribute("rel")&&(n=a.href,(o=function(e){return e.replace("http://","").replace("https://","").split("/")[0]})(window.location.href)===o(n)))if(!a.target&&a.href){var i=e(a.href);if(i){r.preventDefault();var s=i.modal||i.page,u=i.params||{};t(s,u)}}}}},S=function(e,t,r){return window.history.pushState(e,t,r)},w=function(e,t,r){return window.history.replaceState(e,t,r)},P=function(e,t){var r=t.useHash&&!(-1===window.navigator.userAgent.indexOf("Trident"));return window.addEventListener("popstate",e),r&&window.addEventListener("hashchange",e),function(){window.removeEventListener("popstate",e),r&&window.removeEventListener("hashchange",e)}},L=function(e,t){var r=document.ontouchstart?"touchstart":"click",n=y(e,t);return document.addEventListener(r,n,!1),function(){window.removeEventListener(r,n)}},A=function(e,t){return(function(e){try{return encodeURI(decodeURI(e))}catch(t){return e}}((e.preservePath?b():"")+e.preserveHash?window.location.pathname.replace(new RegExp("^"),""):window.location.hash.replace(new RegExp("^#"),""))||"/")+(t||window.location.search)},N={defaultRoute:"default",base:"",subRouteKey:"subRoute",routeKey:"route",rootPage:void 0,preserveHash:!1,preservePath:!0},x=function(){function e(e,t){var r=this;this.history=[],this.routes=[],this.subscribers=[],this.routeHandlerCollection={},this.config=N,this.isStarted=!1,this.errorLogger=function(e){return console.log(e)},this.trees={},this.initialize=function(){var e=(r.routes[0]||{}).name,t=r.config.defaultRoute||e;r.defaultState={page:t,modal:null,params:{}};var n=r.buildState(A(r.config));r.browserSessionId=Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15),r.setState(n)},this.buildHistory=function(){var e=r.getState(),t=e.page,n=e.params,o=r.config.rootPage;if(t!==o){var a=r.makeState(o).newState;r.history.push(a),r.updateUrl(a)}for(var s=i(r.getActiveNodes(t));s.length;){var u=s.shift(),c={page:u.name,modal:null,params:r.getActiveParams([u],n)};r.history.push(c),r.updateUrl(c)}},this.onPopState=function(e){var t,n,a=null===(t=e.state)||void 0===t?void 0:t.counter,i=r.history[0],s=r.history[a],u=s||i,c=(null===(n=e.state)||void 0===n?void 0:n.browserSessionId)===r.browserSessionId;void 0!==a&&c?r.replaceState(u):c&&s||(r.replaceState(i),r.updateUrl(o(o({},i),{counter:0}),{replace:!0}))},this.broadCastState=function(){var e=r.getState(),t=r.getPrevState(),n=r.getHistory();r.subscribers.forEach((function(r){return r({toState:e,fromState:t,history:n})}))},this.done=function(e){e.redirect&&r.go(e.redirect.name,{},{replace:!0})},this.setActiveTree=function(e){r.trees[e]?r.tree=r.trees[e]:r.errorLogger("Tree does not exist")},this.addTree=function(e,t){if(r.trees[e])r.errorLogger("Tree with such name already exists");else{var n=l(t);r.trees[e]=n}},this.removeTree=function(e){r.trees[e]?r.tree=r.trees[e]:r.errorLogger("Tree does not exist")},this.replaceState=function(e){var t=o({},r.state),n=o({},e);if(!m(t,n)){var a=r.routeHandlerCollection[n.page];r.state=n,r.prevState=t,"function"==typeof a&&a(n,t,r.done)}r.broadCastState()},this.setState=function(e){var t=o({},r.state),n=o(o({},r.state),e);if(!m(t,n)){var a=r.routeHandlerCollection[n.page];r.state=n,r.prevState=t,"function"==typeof a&&a(n,t,r.done)}r.broadCastState()},this.buildState=function(e){var t=function(e){var t={},r=e&&e.includes("?")?e.slice(e.indexOf("?")+1):"";if(!r.length)return t;for(var n=0,o=r?r.split("&"):[];n<o.length;n++){var a=o[n].split("="),i=a[0],s=a[1];if(s=s||"",i.includes(".")){var u=i.split("."),c=u.pop(),f=u.join(".");t[f]=t[f]||{},t[f][c]=s}t[i]=s}return t}(function(e,t){var r=e.match(/^(?:http|https):\/\/(?:[0-9a-z_\-.:]+?)(?=\/)(.*)$/),n=(r?r[1]:e).match(/^(.+?)(#.+?)?(\?.+)?$/);if(!n)return"";var o=n[1],a=n[2]||"",i=n[3]||"";return(t.useHash?a.replace(new RegExp("^#"+t.hashPrefix),""):t.base?o.replace(new RegExp("^"+t.base),""):o)+i}(e,r.config)),n=t.p,o=t.m,i=void 0===o?null:o,s=a(t,["p","m"]),u=r.tree.getRouteNode(n),c=r.defaultState,f=s;return u&&(u.decodeParams&&(f=u.decodeParams(s)),c={page:n,modal:i,params:f}),c},this.subscribe=function(e){if(!r.subscribers.includes(e))return r.subscribers.push(e),r.broadCastState(),function(){return r.unsubscribe(e)};r.errorLogger("This listener is already subscribed")},this.unsubscribe=function(e){r.subscribers.includes(e)&&r.subscribers.slice(r.subscribers.indexOf(e),1)},this.removeAllSubscribers=function(){r.subscribers=[]},this.add=function(e){Array.isArray(e)?(r.routes=i(r.routes,e),e.forEach((function(e){r.tree.add(e)}))):e?r.tree.add(e):r.errorLogger("No such route in a tree")},this.remove=function(e){r.tree.remove(e)},this.buildSearch=function(e,t){void 0===t&&(t={});var n=r.makeState(e,t),a=n.newState,i=n.encodeParams,s=a.page,u=a.modal,c=a.params,f=c;i&&(f=i(c));var d=o({p:s,m:u},f),h=v(d),l=h.length?"?"+h:"";return""+r.config.base+l},this.buildUrl=function(e,t){return void 0===t&&(t={}),r.buildSearch(e,t)},this.buildPath=function(e,t){return void 0===t&&(t={}),r.buildSearch(e,t)},this.getActiveNodes=function(e){var t=[];if(e)for(var n=[r.tree.getRouteNode(e)];n.length;){var o=n.shift();t.push(o),o.parent&&o.parent.name&&n.push(o.parent)}return t.reverse()},this.getRequiredParams=function(e){var t=[];if(e)for(var r=[e];r.length;){var n=r.shift();n&&(n.params&&(t=t.concat(n.params)),n.parent&&r.push(n.parent))}return t},this.getActiveParams=function(e,t){for(var n={},o=i(e);o.length;){var a=o.shift();r.getRequiredParams(a).forEach((function(e){n[e]=t[e]}))}return n},this.makeState=function(e,t){var n,a;void 0===t&&(t={});var i=r.getState(),s=r.tree.getRouteNode(e),u=s.data,c=s.decodeParams,f=s.encodeParams,d=r.config.subRouteKey,h=r.getActiveNodes(e),l=o({},t);if(null===(n=null==s?void 0:s.parent)||void 0===n?void 0:n.name){var p=r.getActiveParams(h,i.params);l=o(o({},p),t)}var v={page:e,modal:null,params:l};return(null===(a=null==s?void 0:s.data)||void 0===a?void 0:a[d])&&(v={page:i.page,params:o(o({},i.params),t),modal:s.name}),{newState:v,routeData:u,activeNodes:h,encodeParams:f,decodeParams:c}},this.go=function(e,t,n,o){void 0===n&&(n={});var a=r.makeState(e,t),i=a.newState,s=a.routeData,u=a.encodeParams,c=a.decodeParams,f=r.history.length,d=r.history[f-2],h=m(r.state,i),l=m(d,i);if(h)r.broadCastState();else{l?r.history.pop():r.history.push(i),c&&(i.params=c(i.params)),m(r.state.params,i.params)&&(i.params=r.state.params),r.setState(i);var p=r.getPrevState();u&&(i.params=u(i.params)),s&&!1!==s.updateUrl?r.updateUrl(i,n):r.updateUrl(p,{fakeEntry:!0}),o&&o(i)}},this.updateUrl=function(e,t,n){var a;void 0===t&&(t={}),void 0===n&&(n="");var i=r.history.length-1,s=o(o({},e),{browserSessionId:r.browserSessionId,counter:i});if(t.replace&&(s=o(o({},e),{counter:null!==(a=e.counter)&&void 0!==a?a:i,browserSessionId:r.browserSessionId})),t.fakeEntry){var u=A(r.config);S(s,n,u)}else{var c=o({p:e.page},e.params);e.modal&&(c.m=e.modal);var f=v(c),d=f.length?"?"+f:"",h=""+A(r.config,d)+r.config.base;t.replace?w(s,n,h):S(s,n,h)}},this.back=function(){window.history.back()},this.start=function(e,t,n){var o=r.getState(),a=r.config.defaultRoute,i=r.routes[0];if(r.isStarted=!0,r.removePopStateListener=P(r.onPopState,r.config),r.removeLinkPressListener=L(r.buildState,r.go),r.buildHistory(),o&&o.page){var s=o.modal||o.page,u=o.params;r.go(s,u,{firstLoad:!0})}else e?r.go(e,t,n):a?r.go(a):i&&i.name?r.go(i.name):r.errorLogger("No routes specified")},this.stop=function(){r.isStarted=!1,r.removeLinkPressListener(),r.removePopStateListener()},this.getState=function(){return r.state},this.getPrevState=function(){return r.prevState},this.isActive=function(e,t,n,o){void 0===n&&(n=!0),void 0===o&&(o=!1);var a=r.getState({withoutHistory:!0}),i=a.params,s=r.getActiveNodes(a.page),u=r.getActiveNodes(a.modal),c=s.concat(u).find((function(t){return t.name===e})),f=!!c,d=r.getRequiredParams(c),h=o?function(e,t){for(var r={};e.length;){var n=e.shift();r[n]=t[n]}return r}(d,t):t,l=g(i,h),p=r.makeState(e,h).newState,v=m(a,p);return n?v:t&&Object.keys(t).length?v||f&&l:v||f},this.canActivate=function(e,t){r.routeHandlerCollection[e]=t},this.routes=e||[],this.config=o(o({},N),t),this.errorLogger=this.config.errorLogger?this.config.errorLogger:this.errorLogger,this.tree=l(this.routes,{errorLogger:this.errorLogger}),this.initialize()}return e.prototype.getHistory=function(){return i(this.history)},e}(),O=function(e,t){return void 0===t&&(t=N),new x(e,t)},j=t.createContext({}),E=function(e){function r(t){var r=e.call(this,t)||this;r.onNavigatorChange=function(e){var t=e.toState;r.setState(o({},t))};var n=r.props,a=n.routes,i=n.config,s=O(a,i),u=s.getState();return s.subscribe(r.onNavigatorChange),s.start(),r.state=o({},u),r}return n(r,e),r.prototype.render=function(){return t.createElement(j.Provider,{value:this.state},this.props.children)},r}(t.PureComponent);exports.Navigator=x,exports.NavigatorProvider=E,exports.createNavigator=O,exports.withNavigator=function(e){var r;return(r=function(r){function a(){return null!==r&&r.apply(this,arguments)||this}return n(a,r),a.prototype.render=function(){var r=o(o({},this.props),this.context);return t.createElement(e,o({},r))},a}(t.PureComponent)).contextType=j,r}; |
{ | ||
"name": "@vkontakte/router", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "VK Web Router", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32056