tab-exp-view.tsx 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
import { IModal, ViewMode } from '@ibiz-template/runtime';
import {
  getViewName,
  useNamespace,
  useRoute,
  useRouter,
  useTabExpViewController,
} from '@ibiz-template/vue-util';
import {
  computed,
  defineComponent,
  getCurrentInstance,
  PropType,
14
  h,
15 16 17 18 19 20 21 22 23 24
  ref,
} from 'vue';
import './tab-exp-view.scss';

export const TabExpView = defineComponent({
  props: {
    context: Object as PropType<IContext>,
    params: { type: Object as PropType<IParams> },
    modelPath: { type: String, required: true },
    modal: { type: Object as PropType<IModal> },
25
    noLoadDefault: { type: Boolean, required: false },
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  },
  setup(props) {
    const { proxy } = getCurrentInstance()!;
    const ns = useNamespace('view-detabexpview');
    const c = useTabExpViewController(proxy, props.modelPath);
    const lazyList = ref<string[]>([]);
    const activeTab = ref('');
    // 使分页都不激活
    const deactivateAll = ref(false);
    const router = useRouter(proxy);

    const tabExpPagesHistory: {
      [page: string]: string;
    } = {};

    // 点击回调
    const onTabClick = async (name: string) => {
      const route = useRoute(proxy);
      // 标签切换前缓存当前路径
      const fullPath = route.fullPath;
      tabExpPagesHistory[activeTab.value] = fullPath;

      // 找到对应分页模型并初始化分页视图模型
      const page = c.model.tabExpPanel.tabExpPages.find(
        item => item.name === name,
      );
      // 分页面板视图延迟初始化
      if (page?.embedView.initialized === false) {
        await page.embedView.init();
      }

      if (c.context.isRouter) {
        // 先让已有的分页变为不活跃
        deactivateAll.value = true;
        // 路由模式下变更路由
        if (tabExpPagesHistory[name]) {
          // 如果缓存过了路径直接跳路径
          router.push(tabExpPagesHistory[name]);
        } else {
          const tempContext = Object.assign(c.context.clone(), {
            toRouteLevel: c.modal.level! + 1,
          });
          ibiz.openView.root(page!.embedView.source, tempContext, c.params);
        }
        setTimeout(() => {
          deactivateAll.value = false;
          activeTab.value = name;
        }, 0);
      } else if (!lazyList.value.includes(name)) {
        lazyList.value.push(name);
        activeTab.value = name;
      }
    };

    // 处理默认展示分页,预置是第一个分页
    c.nerve.self.evt.on('created', () => {
      const route = useRoute(proxy);
      const { tabExpPages } = c.model.tabExpPanel;
      // 路由呈现下,处理默认打开的标签页
      if (c.context.isRouter) {
        const tabLevel = props.modal!.level! + 1;
        const tabViewName = route.params[`view${tabLevel}`];
        if (tabViewName) {
          const page = tabExpPages.find(item => {
            return getViewName(item.embedView.source) === tabViewName;
          });
          if (page) {
            activeTab.value = page.name;
          }
        }
      }

      // 路由没有打开的分页时,默认打开第一个分页
      if (tabExpPages.length && !activeTab.value) {
        const defaultTab = tabExpPages[0].name;
        onTabClick(defaultTab);
      }
    });

    const keyHistory = computed(() => {
      return Object.values(c.tabExpPages).map(item => item.key);
    });

109 110
    const embedViewModal = { mode: ViewMode.EMBED };

111 112 113 114 115 116 117 118
    return {
      c,
      ns,
      onTabClick,
      lazyList,
      deactivateAll,
      activeTab,
      keyHistory,
119
      embedViewModal,
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
    };
  },
  render() {
    const isRouter = this.c.context.isRouter === true;
    return (
      <view-base
        class={[this.ns.b(), isRouter ? this.ns.m('route') : '']}
        controller={this.c}
      >
        {this.c.complete && [
          <i-tabs
            class={this.ns.e('tab')}
            name={this.c.model.codeName}
            model-value={this.activeTab}
            on-on-click={this.onTabClick}
          >
            {this.c.model.tabExpPanel.tabExpPages.map(page => {
              const tabExpPage = this.c.tabExpPages[page.name];
              if (!tabExpPage) {
                return;
              }
              return (
                <i-tab-pane
                  class={this.ns.e('tab-item')}
                  tab={this.c.model.codeName}
                  label={page.source.caption}
                  name={page.name}
                >
                  {this.lazyList.includes(page.name) &&
                    h('ViewShell', {
150
                      attrs: {
151 152
                        context: this.c.context,
                        params: this.c.params,
153
                        modal: this.embedViewModal,
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                        modelPath: page.embedView.source.modelPath,
                      },
                      on: {
                        neuronInit: this.c.nerve.onNeuronInit(page.name),
                      },
                      key: tabExpPage.key,
                    })}
                </i-tab-pane>
              );
            })}
          </i-tabs>,
          isRouter && this.activeTab && (
            <div class={this.ns.e('route-content')}>
              <router-view
                // manualKey={this.c.tabExpPages[this.activeTab].key}
                key={this.c.tabExpPages[this.activeTab].key}
                on-neuron-init={this.c.nerve.onNeuronInit(this.activeTab)}
              >
                {({ Component }: { Component: string }) => {
                  return (
                    Component && (
                      <appKeepAlive keyList={this.keyHistory}>
                        <Component />
                      </appKeepAlive>
                    )
                  );
                }}
              </router-view>
            </div>
          ),
        ]}
      </view-base>
    );
  },
});