vue-navigation
Version:
A page navigation library, built with vue2 and vue-router2.
6 lines • 4.71 kB
JavaScript
/**
* vue-navigation v1.1.4
* https://github.com/zack24q/vue-navigation
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.VueNavigation=t()}(this,function(){"use strict";function e(){return"xxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}function t(e,t){return(e.name||e.path)+"?"+e.query[t]}function n(e,t){return Array.isArray(e)?e.indexOf(t)>-1:"string"==typeof e?e.split(",").indexOf(t)>-1:!!isRegExp(e)&&e.test(t)}function o(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,i=!1,a=void 0;try{for(var c,u=n[Symbol.iterator]();!(r=(c=u.next()).done);r=!0){var s=c.value;if(e[s]!==t[s])return!1}}catch(e){i=!0,a=e}finally{try{!r&&u.return&&u.return()}finally{if(i)throw a}}return!0}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var i=[];window.sessionStorage.VUE_NAVIGATION&&(i=JSON.parse(window.sessionStorage.VUE_NAVIGATION));var a=i,c=function(e,n,o,r){n&&n.registerModule(o,{state:{routes:a},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(a.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 i=function(t,r,i){var c={route:r},u={route:i},s=n?n.state[o].routes:a;u.name=s[s.length-1]||null,c.name=t,n?n.commit("navigation/FORWARD",{to:c,from:u,name:t}):s.push(t),window.sessionStorage.VUE_NAVIGATION=JSON.stringify(s),e.$emit("forward",c,u)},c=function(t,r,i){var c={route:r},u={route:i},s=n?n.state[o].routes:a;u.name=s[s.length-1],c.name=s[s.length-1-t],n?n.commit("navigation/BACK",{to:c,from:u,count:t}):s.splice(a.length-t,t),window.sessionStorage.VUE_NAVIGATION=JSON.stringify(s),e.$emit("back",c,u)},u=function(t,r,i){var c={route:r},u={route:i},s=n?n.state[o].routes:a;u.name=s[s.length-1]||null,c.name=t,n?n.commit("navigation/REPLACE",{to:c,from:u,name:t}):s.splice(a.length-1,1,t),window.sessionStorage.VUE_NAVIGATION=JSON.stringify(s),e.$emit("replace",c,u)},s=function(t,r){var i={route:t},c={route:r},u=n?n.state[o].routes:a;i.name=c.name=u[u.length-1],n&&n.commit("navigation/REFRESH",{to:i,from:c}),e.$emit("refresh",i,c)};return{record:function(e,n,o){var f=t(e,r);if(o)u(f,e,n);else{var l=a.lastIndexOf(f);-1===l?i(f,e,n):l===a.length-1?s(e,n):c(a.length-1-l,e,n)}},reset:function(){n?n.commit("navigation/RESET"):a.splice(0,a.length),window.sessionStorage.VUE_NAVIGATION=JSON.stringify([]),e.$emit("reset")}}},u=function(e){return{name:"navigation",abstract:!0,props:{},data:function(){return{routes:a}},computed:{},watch:{routes:function(e){for(var t in this.cache)if(!n(e,t)){var o=this.cache[t];o&&o.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 n=this.$slots.default?this.$slots.default[0]:null;if(n){n.key=n.key||(n.isComment?"comment":n.tag);var o=t(this.$route,e);-1===n.key.indexOf(o)&&(n.key="__navigation-"+o+"-"+n.key),this.cache[o]?n.key===this.cache[o].key?n.componentInstance=this.cache[o].componentInstance:(this.cache[o].componentInstance.$destroy(),this.cache[o]=n):this.cache[o]=n,n.data.keepAlive=!0}return n}}},s=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};return{install:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=n.router,f=n.store,l=n.moduleName,m=void 0===l?"navigation":l,h=n.keyName,g=void 0===h?"VNK":h;if(i){var v=new t,p=c(v,f,m,g),d=i.replace.bind(i),y=!1;i.replace=function(e,t,n){y=!0,d(e,t,n)},i.beforeEach(function(t,n,i){if(t.query[g])i();else{var a=s({},t.query);t.path===n.path&&o(s({},t.query,r({},g,null)),s({},n.query,r({},g,null)))&&n.query[g]?a[g]=n.query[g]:a[g]=e(),i({name:t.name,params:t.params,query:a,replace:y||!n.query[g]})}}),i.afterEach(function(e,t){p.record(e,t,y),y=!1}),t.component("navigation",u(g)),t.navigation=t.prototype.$navigation={on:function(e,t){v.$on(e,t)},once:function(e,t){v.$once(e,t)},off:function(e,t){v.$off(e,t)},getRoutes:function(){return a.slice()},cleanRoutes:function(){return p.reset()}}}else console.error("vue-navigation need options: router")}}});