index-view.vue 5.6 KB
Newer Older
1 2 3
<script setup lang="ts">
import { ViewNeuron } from '@ibiz-template/controller';
import { useIndexViewController } from '@ibiz-template/vue-util';
4 5 6 7 8 9 10 11
import {
  computed,
  ComputedRef,
  getCurrentInstance,
  onMounted,
  Ref,
  ref,
} from 'vue';
12
import { createUUID } from 'qx-util';
13 14
import { useIndexRouteManage } from './index-view-default';
import { useIndexExpRouteManage } from './index-view-exp';
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

interface IndexViewProps {
  context: IContext;
  params?: IParams;
  modelPath: string;
}

const props = withDefaults(defineProps<IndexViewProps>(), {
  params: () => ({}),
});

const { proxy } = getCurrentInstance()!;

const c = useIndexViewController(proxy, props.modelPath);

30
const isShowTabPageExp = ibiz.config.isShowTabPageExp; // to define
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
let onCreated: ((_neuron: ViewNeuron) => void) | null = null;
let onViewFound: ((_opts: IData) => void) | null = null;
let onMenuRouteChange: () => void = () => {};
let deleteRouteCache: ((_keys: string[]) => void) | null = null;
let handleTabClick: ((_index: number) => void) | null = null;
let handleTabDelete: ((_name: number) => void) | null = null;
let handleCloseAll: (() => void) | null = null;
let handleCloseOther: (() => void) | null = null;
let currentPath: ComputedRef<string> | undefined;
let currentKey: Ref<string>;
let keyHistory: Ref<string[]>;
let routeMsgs: Ref<
  {
    key: string;
    fullPath: string;
    modelPath?: string | undefined;
    caption?: string | undefined;
  }[]
>;
51 52
const appKeepAliveKey = ref(createUUID());

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
if (isShowTabPageExp) {
  const {
    currentKey: _currentKey,
    keyHistory: _keyHistory,
    routeMsgs: _routeMsgs,
    updateRouteMsg,
    closeView,
    deleteRouteCache: _deleteRouteCache,
    handleTabClick: _handleTabClick,
    handleTabDelete: _handleTabDelete,
    handleCloseAll: _handleCloseAll,
    handleCloseOther: _handleCloseOther,
  } = useIndexExpRouteManage(proxy, c);

  currentKey = _currentKey;
  keyHistory = _keyHistory;
  routeMsgs = _routeMsgs;
  deleteRouteCache = _deleteRouteCache;
  handleTabClick = _handleTabClick;
  handleTabDelete = _handleTabDelete;
  handleCloseAll = _handleCloseAll;
  handleCloseOther = _handleCloseOther;

  // 视图初始化,监听事件
  onCreated = (neuron: ViewNeuron) => {
    const key = currentKey.value;
    neuron.evt.on('closeView', () => {
      closeView(key);
    });
    neuron.evt.on('setTitle', (title: string) => {
      updateRouteMsg(key, { caption: title });
    });
  };

  onViewFound = (opts: IData) => {
    updateRouteMsg(currentKey.value, opts);
  };

  // 后退按钮触发事件,删除上一个路由的缓存
  window.onpopstate = () => {
    deleteRouteCache!([keyHistory.value[1]]);
  };

  currentPath = computed(() => {
    const routeMsg = routeMsgs.value.find(
      (item: { key: string }) => item.key === currentKey.value,
    );
    return routeMsg?.modelPath || '';
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
} else {
  const {
    currentKey: _currentKey,
    keyHistory: _keyHistory,
    routeMsgs: _routeMsgs,
    updateRouteMsg,
    closeView,
    deleteRouteCache: _deleteRouteCache,
  } = useIndexRouteManage(proxy, c);

  currentKey = _currentKey;
  keyHistory = _keyHistory;
  routeMsgs = _routeMsgs;
  deleteRouteCache = _deleteRouteCache;

  // 视图初始化,监听事件
  onCreated = (neuron: ViewNeuron) => {
    const key = currentKey.value;
    neuron.evt.on('closeView', () => {
      closeView(key);
    });
    neuron.evt.on('setTitle', (title: string) => {
      updateRouteMsg(key, { caption: title });
    });
  };

  onViewFound = (opts: IData) => {
    updateRouteMsg(currentKey.value, opts);
  };

  onMenuRouteChange = () => {
    deleteRouteCache!(keyHistory.value.slice(1));
    appKeepAliveKey.value = createUUID();
  };

  // 后退按钮触发事件,删除上一个路由的缓存
  window.onpopstate = () => {
    deleteRouteCache!([keyHistory.value[1]]);
  };

  currentPath = computed(() => {
    const routeMsg = routeMsgs.value.find(
      (item: { key: string }) => item.key === currentKey.value,
    );
    return routeMsg?.modelPath || '';
147
  });
148
}
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

onMounted(() => {
  setTimeout(() => {
    const el = document.querySelector('.app-loading-x') as HTMLDivElement;
    if (el) {
      el.style.display = 'none';
    }
  }, 300);
});

// 菜单收缩变化
const collapseChange = (collapse: boolean) => {
  c.collapseChange = collapse;
};
</script>

<template>
  <AppLayout
    :is-complete="c.complete"
    :model="c.model"
169
    :class="c.model?.source?.mainMenuAlign?.toLowerCase()"
170
    :is-show-tab-page-exp="isShowTabPageExp"
171 172 173
    @onCollapseChange="collapseChange"
  >
    <template v-if="c.complete">
174
      <app-menu
175 176 177 178 179 180
        v-if="c.complete"
        slot="menu"
        :current-path="currentPath"
        :model-data="c.model.appMenu"
        :context="c.context"
        :collapse-change="c.collapseChange"
181
        :menu-model="c.model?.source"
182
        @menuRouteChange="onMenuRouteChange"
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
      ></app-menu>
      <tab-page-exp
        v-if="isShowTabPageExp"
        slot="tabPageExp"
        :route-msgs="routeMsgs"
        :current-key="currentKey"
        @tab-click="handleTabClick"
        @tab-delete="handleTabDelete"
        @close-all="handleCloseAll"
        @close-other="handleCloseOther"
      ></tab-page-exp>
      <AppKeepAlive
        v-if="!isShowTabPageExp"
        :key="appKeepAliveKey"
        :key-list="keyHistory"
      >
        <router-view
          :key="currentKey"
          @neuronInit="onCreated"
          @viewFound="onViewFound"
        />
      </AppKeepAlive>
      <AppKeepAlive v-else :key-list="keyHistory">
206 207 208 209 210 211 212 213 214
        <router-view
          :key="currentKey"
          @neuronInit="onCreated"
          @viewFound="onViewFound"
        />
      </AppKeepAlive>
    </template>
  </AppLayout>
</template>