-
Notifications
You must be signed in to change notification settings - Fork 184
/
vue-navigation.esm.min.js
6 lines (6 loc) · 4.69 KB
/
vue-navigation.esm.min.js
1
2
3
4
5
6
/**
* vue-navigation v1.1.4
* https://github.com/zack24q/vue-navigation
* Released under the MIT License.
*/
function genKey(){return"xxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}function getKey(e,t){return(e.name||e.path)+"?"+e.query[t]}function matches(e,t){return Array.isArray(e)?e.indexOf(t)>-1:"string"==typeof e?e.split(",").indexOf(t)>-1:!!isRegExp(e)&&e.test(t)}function isObjEqual(e,t){if(e===t)return!0;var n=Object.getOwnPropertyNames(e),o=Object.getOwnPropertyNames(t);if(n.length!==o.length)return!1;var r=!0,a=!1,i=void 0;try{for(var s,u=n[Symbol.iterator]();!(r=(s=u.next()).done);r=!0){var c=s.value;if(e[c]!==t[c])return!1}}catch(e){a=!0,i=e}finally{try{!r&&u.return&&u.return()}finally{if(a)throw i}}return!0}function _defineProperty(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var routes=[];window.sessionStorage.VUE_NAVIGATION&&(routes=JSON.parse(window.sessionStorage.VUE_NAVIGATION));var Routes=routes,Navigator=function(e,t,n,o){t&&t.registerModule(n,{state:{routes:Routes},mutations:{"navigation/FORWARD":function(e,t){t.to,t.from;var n=t.name;e.routes.push(n)},"navigation/BACK":function(e,t){t.to,t.from;var n=t.count;e.routes.splice(e.routes.length-n,n)},"navigation/REPLACE":function(e,t){t.to,t.from;var n=t.name;e.routes.splice(Routes.length-1,1,n)},"navigation/REFRESH":function(e,t){t.to,t.from},"navigation/RESET":function(e){e.routes.splice(0,e.routes.length)}}});var r=function(o,r,a){var i={route:r},s={route:a},u=t?t.state[n].routes:Routes;s.name=u[u.length-1]||null,i.name=o,t?t.commit("navigation/FORWARD",{to:i,from:s,name:o}):u.push(o),window.sessionStorage.VUE_NAVIGATION=JSON.stringify(u),e.$emit("forward",i,s)},a=function(o,r,a){var i={route:r},s={route:a},u=t?t.state[n].routes:Routes;s.name=u[u.length-1],i.name=u[u.length-1-o],t?t.commit("navigation/BACK",{to:i,from:s,count:o}):u.splice(Routes.length-o,o),window.sessionStorage.VUE_NAVIGATION=JSON.stringify(u),e.$emit("back",i,s)},i=function(o,r,a){var i={route:r},s={route:a},u=t?t.state[n].routes:Routes;s.name=u[u.length-1]||null,i.name=o,t?t.commit("navigation/REPLACE",{to:i,from:s,name:o}):u.splice(Routes.length-1,1,o),window.sessionStorage.VUE_NAVIGATION=JSON.stringify(u),e.$emit("replace",i,s)},s=function(o,r){var a={route:o},i={route:r},s=t?t.state[n].routes:Routes;a.name=i.name=s[s.length-1],t&&t.commit("navigation/REFRESH",{to:a,from:i}),e.$emit("refresh",a,i)};return{record:function(e,t,n){var u=getKey(e,o);if(n)i(u,e,t);else{var c=Routes.lastIndexOf(u);-1===c?r(u,e,t):c===Routes.length-1?s(e,t):a(Routes.length-1-c,e,t)}},reset:function(){t?t.commit("navigation/RESET"):Routes.splice(0,Routes.length),window.sessionStorage.VUE_NAVIGATION=JSON.stringify([]),e.$emit("reset")}}},NavComponent=function(e){return{name:"navigation",abstract:!0,props:{},data:function(){return{routes:Routes}},computed:{},watch:{routes:function(e){for(var t in this.cache)if(!matches(e,t)){var n=this.cache[t];n&&n.componentInstance.$destroy(),delete this.cache[t]}}},created:function(){this.cache={}},destroyed:function(){for(var e in this.cache){var t=this.cache[e];t&&t.componentInstance.$destroy()}},render:function(){var t=this.$slots.default?this.$slots.default[0]:null;if(t){t.key=t.key||(t.isComment?"comment":t.tag);var n=getKey(this.$route,e);-1===t.key.indexOf(n)&&(t.key="__navigation-"+n+"-"+t.key),this.cache[n]?t.key===this.cache[n].key?t.componentInstance=this.cache[n].componentInstance:(this.cache[n].componentInstance.$destroy(),this.cache[n]=t):this.cache[n]=t,t.data.keepAlive=!0}return t}}},_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},index={install:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.router,o=t.store,r=t.moduleName,a=void 0===r?"navigation":r,i=t.keyName,s=void 0===i?"VNK":i;if(n){var u=new e,c=Navigator(u,o,a,s),f=n.replace.bind(n),l=!1;n.replace=function(e,t,n){l=!0,f(e,t,n)},n.beforeEach(function(e,t,n){if(e.query[s])n();else{var o=_extends({},e.query);e.path===t.path&&isObjEqual(_extends({},e.query,_defineProperty({},s,null)),_extends({},t.query,_defineProperty({},s,null)))&&t.query[s]?o[s]=t.query[s]:o[s]=genKey(),n({name:e.name,params:e.params,query:o,replace:l||!t.query[s]})}}),n.afterEach(function(e,t){c.record(e,t,l),l=!1}),e.component("navigation",NavComponent(s)),e.navigation=e.prototype.$navigation={on:function(e,t){u.$on(e,t)},once:function(e,t){u.$once(e,t)},off:function(e,t){u.$off(e,t)},getRoutes:function(){return Routes.slice()},cleanRoutes:function(){return c.reset()}}}else console.error("vue-navigation need options: router")}};export default index;