MainLayout.vue 6.19 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<template>
  <q-layout view="lHh Lpr lFf">
    <q-header elevated>
      <q-toolbar>
        <q-btn flat dense round @click="toggleLeftDrawer">
          <q-avatar>
            <img
              :src="
                leftDrawerOpen
                  ? require('./menuListIcons/toggle-left-drawer-show.svg')
                  : require('./menuListIcons/toggle-left-drawer-hide.svg')
              "
            />
          </q-avatar>
        </q-btn>

        <q-toolbar-title> Quasar App </q-toolbar-title>
        <q-tabs
          v-model="defaultRouter"
          no-caps
          shrink
          active-color="white"
          active-bg-color="headeractive"
        >
          <q-tab
            class="my-tab"
            v-for="page in tabsList"
            :key="page.link"
            :name="page.link"
            :label="page.title"
            @click.stop="clickTab(page)"
          >
            <q-btn
              v-show="tabsList.length > 1"
              dense
              flat
              round
              size="sm"
              icon="close"
              @mousedown.stop="doNothing"
              @click.stop="closeTab(page)"
            >
            </q-btn>
          </q-tab>
        </q-tabs>

        <q-space />
hucy's avatar
hucy committed
48
        <language-select />
hucy's avatar
hucy committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        <div>Quasar v{{ $q.version }}</div>
      </q-toolbar>
    </q-header>

    <q-drawer v-model="leftDrawerOpen" show-if-above bordered>
      <q-list>
        <q-item-label header style="padding: 0">
          <TopLeftLoading />
        </q-item-label>

        <EssentialLink
          v-for="link in essentialLinks"
          :key="link.title"
          v-bind="link"
        />
      </q-list>
    </q-drawer>

    <q-page-container>
      <router-view />
    </q-page-container>
  </q-layout>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, reactive, toRefs } from 'vue';
import { onBeforeRouteUpdate, useRouter } from 'vue-router';
import { usePageStore } from 'src/common/hooks';
import EssentialLink from 'components/EssentialLink.vue';
hucy's avatar
hucy committed
78 79
// import TopLeftLoading from './TopLeftLoading.vue';
import TopLeftLoading from './TopLeftText.vue';
hucy's avatar
hucy committed
80
import LanguageSelect from './element/LanguageSelect.vue';
hucy's avatar
hucy committed
81 82 83 84 85 86 87

export default defineComponent({
  name: 'MainLayout',

  components: {
    EssentialLink,
    TopLeftLoading,
hucy's avatar
hucy committed
88
    LanguageSelect,
hucy's avatar
hucy committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  },

  setup() {
    const pageStore = usePageStore();
    const router = useRouter();
    const leftDrawerOpen = ref(false);
    const defaultRouter = ref('/page1');
    const linksList = ref<any>([]);
    const state = reactive({
      tabsList: [] as any,
    });

    onBeforeRouteUpdate((to, from, next) => {
      defaultRouter.value = to.path;
      if (to.matched.length == 3) {
        pageStore.addRoute(to as any);
        getTabsList();
      }
      next();
    });
    onMounted(() => {
      getLinksList();
      const path = router.currentRoute.value.path;
      defaultRouter.value = path;
      pageStore.addRoute(router.currentRoute.value as any);
      getTabsList();
    });

    const getLinksList = () => {
      const lists = [
        {
          title: '一',
          caption: '日历',
          icon: require('./menuListIcons/page1.svg'),
          link: '/page1',
          active: true,
        },
        {
          title: '贰',
          caption: '2',
          icon: require('./menuListIcons/page2.svg'),
          link: '/page2',
          active: false,
        },
        {
          title: '叁',
          caption: '3',
          icon: require('./menuListIcons/page3.svg'),
          link: '/page3',
          active: false,
        },
        {
          title: '四',
          caption: '4',
          icon: require('./menuListIcons/page4.svg'),
          link: '/page4',
          active: false,
        },
        {
          title: '五',
          caption: '5',
          icon: require('./menuListIcons/page5.svg'),
          link: '/page5',
          active: false,
        },
        {
          title: '六',
          caption: '一些js练习',
          icon: require('./menuListIcons/page6.png'),
          link: '/page6',
          active: false,
        },
hucy's avatar
hucy committed
161 162 163 164 165 166 167
        {
          title: '七',
          caption: '疫情防控',
          icon: require('./menuListIcons/page7.svg'),
          link: '/page7',
          active: false,
        },
hucy's avatar
hucy committed
168 169 170 171 172 173 174
        {
          title: '八',
          caption: '动画',
          icon: require('./menuListIcons/page8.svg'),
          link: '/page8',
          active: false,
        },
hucy's avatar
hucy committed
175 176
        {
          title: '九',
hucy's avatar
hucy committed
177
          caption: '动画2&表格',
hcyhuchaoyue's avatar
hcyhuchaoyue committed
178
          icon: require('./menuListIcons/page9.svg'),
hucy's avatar
hucy committed
179 180 181
          link: '/page9',
          active: false,
        },
hcyhuchaoyue's avatar
hcyhuchaoyue committed
182 183 184 185 186 187 188
        {
          title: '十',
          caption: '设计',
          icon: require('./menuListIcons/page10.svg'),
          link: '/page10',
          active: false,
        },
hucy's avatar
hucy committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
      ];

      for (const item of lists) {
        if (item.link === defaultRouter.value) {
          item.active = true;
        } else {
          item.active = false;
        }
      }

      linksList.value = lists;
    };

    const getTabsList = () => {
      state.tabsList = pageStore.tabRouterList.map((item: any) => {
        return {
          title: item.meta?.title,
          link: item.path,
          keepalive: item.meta?.keepalive,
          permission: item.meta?.permission,
        };
      });
    };

    const clickTab = (page: any) => {
      router.push(page.link);
    };

    const closeTab = (page: any) => {
      pageStore.removePage(page.link);
      getTabsList();
      router.push(pageStore.activeRouter.path);
    };

    const doNothing = () => {
      // to prevent tab close button ripple effect
    };

    return {
      ...toRefs(state),
      essentialLinks: linksList,
      leftDrawerOpen,
      toggleLeftDrawer() {
        leftDrawerOpen.value = !leftDrawerOpen.value;
      },
      linksList,
      pageStore,
      defaultRouter,
      clickTab,
      closeTab,
      doNothing,
    };
  },
});
</script>
<style lang="scss" scoped>
.my-tab {
  :deep(.q-tab__content) {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    > button {
      color: transparent;
    }
    &:hover > button {
      color: white;
    }
    .q-tab__label {
      flex: 1;
    }
  }
}
</style>