index.jsx 4.9 KB
Newer Older
wuhao's avatar
wuhao committed
1 2
/* eslint-disable react/jsx-key */
import React, { useState, useEffect, useRef, memo } from 'react';
wuhao's avatar
wuhao committed
3 4 5 6 7
import { RouteContext } from '@ant-design/pro-layout';
import { history } from '@umijs/max';
import Tags from './Tags';
import styles from './index.less';
import { Scrollbars } from 'react-custom-scrollbars';
wuhao's avatar
wuhao committed
8 9 10 11 12 13 14
// tree遍历
function treeForeach(tree, func) {
  tree.forEach((data) => {
    func(data);
    data.children && treeForeach(data.children, func); // 遍历子树
  });
}
wuhao's avatar
wuhao committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

const TagView = ({ children, home }) => {
  const [tagList, setTagList] = useState([]);

  const [currentPath, setCurrentPath] = useState();
  const routeContextRef = useRef();

  useEffect(() => {
    if (routeContextRef.current) {
      handleOnChange(routeContextRef.current);
    }
  }, [routeContextRef.current]);

  // 初始化 visitedViews,设置首页
  const initTags = (routeContext) => {
    const { menuData } = routeContext;
    if (tagList.length === 0 && menuData) {
wuhao's avatar
wuhao committed
32 33 34 35 36
      const allarr = [];
      treeForeach(menuData, (node) => {
        allarr.push(node);
      });
      const firstTag = allarr.filter((el) => el.path === location?.hash?.replace('#', ''))[0];
wuhao's avatar
wuhao committed
37
      if (firstTag) {
wuhao's avatar
wuhao committed
38
        const title = firstTag.name;
wuhao's avatar
wuhao committed
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
        const path = firstTag.path;
        history.push({ pathname: firstTag.path, query: firstTag.query });
        setTagList([
          {
            title,
            path,
            children: firstTag.element,
            refresh: 0,
            active: true,
            key: firstTag.key,
          },
        ]);
      }
    }
  };

  // 监听路由改变
  const handleOnChange = (routeContext) => {
    const { currentMenu } = routeContext;
    // tags初始化
    if (tagList.length === 0) {
      return initTags(routeContext);
    }
    // 判断是否已打开过该页面
    let hasOpen = false;
    if (currentMenu.path) {
      const tagsCopy = tagList.map((item) => {
        if (currentMenu?.path === item.path) {
TZW's avatar
TZW committed
67
          //console.log(item);
wuhao's avatar
wuhao committed
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
          hasOpen = true;
          // 刷新浏览器时,重新覆盖当前 path 的 children
          return { ...item, active: true, children: item.children };
        } else {
          return { ...item, active: false };
        }
      });

      // 没有该tag时追加一个,并打开这个tag页面
      if (!hasOpen) {
        const title = routeContext.title || '';
        const path = currentMenu?.path;
        tagsCopy.push({
          title,
          path,
          children,
          refresh: 0,
          active: true,
        });
      }
      return setTagList(tagsCopy);
    }
  };

  // 关闭标签
  const handleCloseTag = (tag) => {
    const tagsCopy = tagList.map((el, i) => ({ ...el }));

    // 判断关闭标签是否处于打开状态
wuhao's avatar
wuhao committed
97
    tagList.forEach?.((el, i) => {
wuhao's avatar
wuhao committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      if (el.path === tag.path && tag.active) {
        const next = tagList[i - 1];
        next.active = true;
        history.push({ pathname: next?.path, query: next?.query });
      }
    });

    setTagList(tagsCopy.filter((el) => el.path !== tag?.path));
  };

  // 关闭所有标签
  const handleCloseAll = () => {
    const tagsCopy = tagList.filter((el) => el.path === home);
    history.push(home);
    setTagList(tagsCopy);
  };

  // 关闭其他标签
  const handleCloseOther = (tag) => {
    const tagsCopy = tagList.filter((el) => el.path === home || el.path === tag.path);
    history.push({ pathname: tag?.path, query: tag?.query });
    setTagList(tagsCopy);
  };

wuhao's avatar
wuhao committed
122
  const [refresh, setrefresh] = useState(true);
wuhao's avatar
wuhao committed
123
  // 刷新选择的标签
wuhao's avatar
wuhao committed
124 125 126 127 128 129 130 131 132 133 134
  const handleRefreshTag = async (tag) => {
    // const tagsCopy = tagList.map((item) => {
    //   if (item.path === tag.path) {
    //     history.push({ pathname: tag?.path, query: tag?.query });
    //     return { ...item, refresh: item.refresh + 1, active: true };
    //   }
    //   return { ...item, active: false };
    // });
    // setTagList(tagsCopy);
    await setrefresh(false);
    setrefresh(true);
wuhao's avatar
wuhao committed
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 161 162 163 164 165 166 167
  };

  return (
    <>
      <RouteContext.Consumer>
        {(value) => {
          setTimeout(() => {
            setCurrentPath(value.currentMenu?.path);
          }, 0);
          routeContextRef.current = value;
          return null;
        }}
      </RouteContext.Consumer>
      <div className={styles.tag_view}>
        <div className={styles.tags_container}>
          <Tags
            tagList={tagList}
            closeTag={handleCloseTag}
            closeAllTag={handleCloseAll}
            closeOtherTag={handleCloseOther}
            refreshTag={handleRefreshTag}
            home={home}
          />
        </div>
      </div>
      <div className={styles.child}>
        <div className={styles.contianbox}>
          <Scrollbars
            thumbMinSize={10}
            autoHide
            style={{ width: '100%', height: '100%' }}
            hideTracksWhenNotNeeded={true}
          >
wuhao's avatar
wuhao committed
168
            {refresh && children}
wuhao's avatar
wuhao committed
169 170 171 172 173 174 175 176
          </Scrollbars>
        </div>
      </div>
    </>
  );
};

export default TagView;