index-view.vue 6.2 KB
Newer Older
1 2
<script setup lang="ts">
import { ViewNeuron } from '@ibiz-template/controller';
3
import { useIndexViewController, useNamespace } 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
import '@ibiz-template/theme/style/components/views/index-view/index-view.scss';
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

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

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

const { proxy } = getCurrentInstance()!;

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

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

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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;
  }[]
>;
52 53
const appKeepAliveKey = ref(createUUID());

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 101
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 || '';
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
} 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 || '';
148
  });
149
}
150 151 152 153 154 155 156 157 158

onMounted(() => {
  setTimeout(() => {
    const el = document.querySelector('.app-loading-x') as HTMLDivElement;
    if (el) {
      el.style.display = 'none';
    }
  }, 300);
});
159
const ns = useNamespace('index-view');
160 161 162 163 164 165 166
// 菜单收缩变化
const collapseChange = (collapse: boolean) => {
  c.collapseChange = collapse;
};
</script>

<template>
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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
  <div :class="ns.b()">
    <AppLayout
      v-if="c.model?.source?.mainMenuAlign !== 'CENTER'"
      :is-complete="c.complete"
      :model="c.model"
      :class="c.model?.source?.mainMenuAlign?.toLowerCase()"
      :is-show-tab-page-exp="isShowTabPageExp"
      @onCollapseChange="collapseChange"
    >
      <template v-if="c.complete">
        <app-menu
          v-if="c.complete"
          slot="menu"
          :current-path="currentPath"
          :model-data="c.model.appMenu"
          :context="c.context"
          :collapse-change="c.collapseChange"
          :menu-model="c.model?.source"
          @menuRouteChange="onMenuRouteChange"
        ></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">
          <router-view
            :key="currentKey"
            @neuronInit="onCreated"
            @viewFound="onViewFound"
          />
        </AppKeepAlive>
      </template>
    </AppLayout>
    <div v-else>
218
      <app-menu
219 220 221 222 223
        v-if="c.complete"
        :current-path="currentPath"
        :model-data="c.model.appMenu"
        :context="c.context"
        :collapse-change="c.collapseChange"
224
        :menu-model="c.model?.source"
225
        @menuRouteChange="onMenuRouteChange"
226
      ></app-menu>
227 228
    </div>
  </div>
229
</template>