CardIndex.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div>
  3. <router-view v-slot="{ Component }">
  4. <keep-alive>
  5. <component :is="Component" />
  6. </keep-alive>
  7. </router-view>
  8. </div>
  9. </template>
  10. <script>
  11. //-----------------------------------------------------------------------------
  12. import vueComponent from '../vueComponent.js';
  13. import _ from 'lodash';
  14. const selfRoute = '/cardindex';
  15. const tab2Route = [
  16. '/cardindex/search',
  17. '/cardindex/card',
  18. '/cardindex/book',
  19. '/cardindex/history',
  20. ];
  21. let lastActiveTab = null;
  22. const componentOptions = {
  23. watch: {
  24. selectedTab: function(newValue) {
  25. lastActiveTab = newValue;
  26. this.setRouteByTab(newValue);
  27. },
  28. curRoute: function(newValue) {
  29. this.setTabByRoute(newValue);
  30. },
  31. },
  32. };
  33. class CardIndex {
  34. _options = componentOptions;
  35. selectedTab = null;
  36. created() {
  37. this.$watch(
  38. () => this.$route.path,
  39. (newValue) => {
  40. if (newValue == '/cardindex' && this.isReader) {
  41. this.$router.replace({ path: '/reader' });
  42. }
  43. }
  44. )
  45. }
  46. mounted() {
  47. this.setTabByRoute(this.curRoute);
  48. }
  49. setTabByRoute(route) {
  50. const t = _.indexOf(tab2Route, route);
  51. if (t >= 0) {
  52. if (t !== this.selectedTab)
  53. this.selectedTab = t.toString();
  54. } else {
  55. if (route == selfRoute && lastActiveTab !== null)
  56. this.setRouteByTab(lastActiveTab);
  57. }
  58. }
  59. setRouteByTab(tab) {
  60. const t = Number(tab);
  61. if (tab2Route[t] !== this.curRoute) {
  62. this.$router.replace(tab2Route[t]);
  63. }
  64. }
  65. get mode() {
  66. return this.$store.state.config.mode;
  67. }
  68. get curRoute() {
  69. const m = this.$route.path.match(/^(\/[^/]*\/[^/]*).*$/i);
  70. return (m ? m[1] : this.$route.path);
  71. }
  72. get isReader() {
  73. return (this.mode !== null && (this.mode == 'reader' || this.mode == 'omnireader' || this.mode == 'liberama'));
  74. }
  75. }
  76. export default vueComponent(CardIndex);
  77. //-----------------------------------------------------------------------------
  78. </script>
  79. <style scoped>
  80. </style>