index.js 9.43 KB
Newer Older
曾婧's avatar
曾婧 committed
1
import React, { useContext, useState, useEffect, useCallback } from 'react';
2 3 4 5 6
import { ConfigProvider, Divider, Skeleton, Tooltip } from 'antd';
import classNames from 'classnames';
import { ReactSVG } from 'react-svg';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import { useHistory } from '@wisdom-utils/runtime';
曾婧's avatar
曾婧 committed
7
import ProLayout, { getPageTitle } from '@ant-design/pro-layout';
8
import { findPathByWidget } from '@wisdom-utils/components/lib/AppLayout/helpers';
9
const LoadingSkeleton = props => {
10
  const { route } = props;
11 12 13 14 15 16 17 18 19
  return (
    <div style={{ padding: route && !route.routes ? '20px 6px 6px 6px' : '0px' }}>
      <Skeleton loading={route && !route.routes} active title={false} paragraph={{ rows: 10 }}>
        {props.children}
      </Skeleton>
    </div>
  );
};
const Menu = props => {
曾婧's avatar
曾婧 committed
20
  const { prefixCls, collapsed, onCollapse, onMenuClick, menuHeaderRender, onMenuHeaderClick } = props;
21
  const [pathname, setPathname] = useState('');
曾婧's avatar
曾婧 committed
22
  const { route } = props;
23
  const history = useHistory();
24
  const [activeMenu, setActiveMenu] = useState('首页');
曾婧's avatar
曾婧 committed
25 26 27 28 29
  const [sunRoute, setsunRoute] = useState(route);
  const [selectIndex, setSelectIndex] = useMergedState(() => -1, {
    value: props.active,
    onChange: props.onActiveChange,
  });
30 31 32 33 34 35 36
  const toLink = path => {
    if (!window.__POWERED_BY_QIANKUN__) {
      history.push(path);
    } else {
      window.history.pushState(null, '', path);
    }
  };
曾婧's avatar
曾婧 committed
37
  const onSelectItem = (item, index) => {
38 39
    let current = void 0;
    let childrenRoutes = [];
曾婧's avatar
曾婧 committed
40
    setActiveMenu(item.name);
41 42 43 44 45 46 47
    if (item && item.child) {
      current = item.child[0];
      childrenRoutes = item.child;
    } else {
      current = item;
      childrenRoutes = [item];
    }
48
    setPathname(current.path);
曾婧's avatar
曾婧 committed
49
    setSelectIndex(index);
50 51 52 53 54 55 56 57
    if (/iframe/.test(current.path)) {
      history.push({
        path: 'iframe',
        state: current.params,
      });
    } else {
      window.__POWERED_BY_QIANKUN__ ? toLink(`/civbase${current.path}`) : toLink(current.path);
    }
曾婧's avatar
曾婧 committed
58
    onMenuClick && onMenuClick(current, childrenRoutes, index);
59
  };
60 61 62 63 64 65 66
  useEffect(()=>{
    var allWidgets = window.globalConfig && window.globalConfig.allWidgets;
    let homgUrl=props.config && props.config.homepage.replace(/(civ_energy\/)/g,'');
    let curItem = findPathByWidget(homgUrl, allWidgets, '', 'url');
    curItem&&setActiveMenu(curItem.label);
    curItem&&setPathname(curItem.path)
  },[])
67
  useEffect(() => {
曾婧's avatar
曾婧 committed
68 69 70 71 72 73
    if (route && route.routes) {
      setsunRoute(route);
      let tmp = treeRoutes(route.routes);
      setsunRoute({ ...sunRoute, routes: tmp });
    }
  }, [route]);
曾婧's avatar
曾婧 committed
74
  const treeRoutes = useCallback(iroute =>
75
    iroute
曾婧's avatar
曾婧 committed
76
      .filter(item => item.level < 4)
77 78
      .map(item => {
        if (item.routes && Array.isArray(item.routes) && item.routes.length >= 1) {
曾婧's avatar
曾婧 committed
79
          if (item.level === 3) {
80 81 82 83 84
            item.child = item.routes;
          }
          item.routes = treeRoutes(item.routes);
        }
        return item;
曾婧's avatar
曾婧 committed
85
      }),
86
  );
87 88
  return (
    <LoadingSkeleton {...props}>
曾婧's avatar
曾婧 committed
89
      {route && route.routes && route.routes.length > 0 && (
曾婧's avatar
曾婧 committed
90
        <React.Fragment>
曾婧's avatar
曾婧 committed
91
          {/* {menuHeaderRender ? (
曾婧's avatar
曾婧 committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            menuHeaderRender(props)
          ) : (
            <>
              <ul className={classNames(`${prefixCls}-menu`)} style={{ minHeight: '0px', marginBottom: '0px' }}>
                <li className={`${prefixCls}-menu-item`} onClick={onMenuHeaderClick}>
                  <Tooltip placement="right" title={collapsed ? route.name : ''}>
                    <a>
                      {route && route.extData && route.extData.icon && <img src={route.extData.icon} />}
                      {collapsed ? null : (
                        <span className={classNames(`${prefixCls}-menu-item-text`, `${prefixCls}-menu-item-current`)}>
                          {route && route.name}
                        </span>
                      )}
                    </a>
                  </Tooltip>
                </li>
              </ul>
              <div className={`${prefixCls}-line`} />
            </>
曾婧's avatar
曾婧 committed
111
          )} */}
曾婧's avatar
曾婧 committed
112 113 114 115 116 117 118
          <ul className={classNames(`${prefixCls}-menu ${prefixCls}-menu-vertical`)}>
            <ProLayout
              route={sunRoute}
              title={props.title}
              logo={null}
              siderWidth={140}
              config={props.global}
曾婧's avatar
曾婧 committed
119 120 121
              defaultCollapsed={false}
              collapsed={false}
              breakpoint={'xs'}
122 123 124
              location={{
                pathname,
              }}
曾婧's avatar
曾婧 committed
125 126 127 128 129 130 131 132 133 134 135 136 137
              // onHeaderLogo={handlerIndustry}
              // onPageChange={handlerPageChange}
              // onMenuPanelItemClick={handleUpdateCurrentIndex}
              menuItemRender={(item, dom) =>
                item.level < 4 ? (
                  <div
                    onClick={() => {
                      onSelectItem(item, dom);
                    }}
                    className={classNames(`test-icon`, {
                      [`hight-active`]: item.name === activeMenu,
                    })}
                  >
曾婧's avatar
曾婧 committed
138 139 140
                    {item.extData.icon == '1' ? (
                      <span />
                    ) : /.svg/.test(item.extData.icon) ? (
曾婧's avatar
曾婧 committed
141 142 143 144 145 146 147 148 149 150 151 152
                      <div className="my-svg">
                        <ReactSVG src={item.extData.icon} style={{ width: '100%', height: '100%' }} />
                      </div>
                    ) : (
                      <div className="my-img">
                        <img src={item.extData.icon} style={{ width: '100%', height: '100%' }} />
                      </div>
                    )}
                    <span className="my-desc" title={item.name}>
                      {' '}
                      {item.name}{' '}
                    </span>
153
                  </div>
曾婧's avatar
曾婧 committed
154 155 156
                ) : null
              }
              subMenuItemRender={(item, dom) => (
157
                <div className={classNames(`test-icon`)}>
曾婧's avatar
曾婧 committed
158 159 160
                  {item.extData.icon == '1' ? (
                    <span />
                  ) : /.svg/.test(item.extData.icon) ? (
曾婧's avatar
曾婧 committed
161 162 163
                    <div className="my-svg">
                      <ReactSVG src={item.extData.icon} style={{ width: '100%', height: '100%' }} />
                    </div>
曾婧's avatar
曾婧 committed
164
                  ) : (
曾婧's avatar
曾婧 committed
165 166 167 168 169 170 171 172
                    <div className="my-img">
                      <img src={item.extData.icon} style={{ width: '100%', height: '100%' }} />
                    </div>
                  )}
                  <span className="my-desc" title={item.name}>
                    {' '}
                    {item.name}{' '}
                  </span>
173 174
                </div>
              )}
曾婧's avatar
曾婧 committed
175 176 177
            />
          </ul>
        </React.Fragment>
178 179 180 181
      )}
    </LoadingSkeleton>
  );
};
曾婧's avatar
曾婧 committed
182 183 184 185 186 187 188 189
export default props => {
  const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('layout-slider');
  const { menuItemRender, menuHeaderRender, siderWidth } = props;
  const { collapsed, onCollapse, route, style } = props;
  return (
    <>
      <aside
曾婧's avatar
曾婧 committed
190 191 192
        // className={classNames(`${prefixCls}`, `${prefixCls}-fixed`, {
        //   [`${prefixCls}-collapsed`]: collapsed,
        // })}
193
        style={{ paddingTop: '50px' }}
曾婧's avatar
曾婧 committed
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
      >
        <div className={`${prefixCls}-children`}>
          <div className={`${prefixCls}-wrapper`} style={{ flex: '1 1 0%', overflow: 'hidden auto' }}>
            {/* {LoadingSkeleton(props)} */}
            {menuItemRender ? menuItemRender(props) : Menu(Object.assign({}, props, { prefixCls }))}
          </div>
          {route && route.routes && route.routes.length > 0 && (
            <div className={`${prefixCls}-pro-sider-links`}>
              <ul
                className={`${prefixCls}-menu ${prefixCls}-menu-root ${prefixCls}-menu-inline ${prefixCls}-menu-dark ${prefixCls}-pro-sider-link-menu`}
                role="menu"
                tabIndex="0"
              >
                <li
                  className={`${prefixCls}-menu-item ${prefixCls}-menu-item-only-child ${prefixCls}-pro-sider-collapsed-button`}
                  style={{ height: '40px', lineHeight: '40px' }}
                  role="menuitem"
                  tabIndex="-1"
                  onClick={onCollapse}
                >
                  <span className={`${prefixCls}-menu-title-content`}>
                    <span
                      role="img"
                      aria-label="menu-fold"
                      className="anticon anticon-menu-fold"
                      style={{ color: '#fff' }}
                    >
                      <svg
                        viewBox="64 64 896 896"
                        focusable="false"
                        width="1em"
                        height="1em"
                        fill="currentColor"
                        aria-hidden="true"
                      >
                        <path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 000 13.8z" />
                      </svg>
                    </span>
                  </span>
                </li>
              </ul>
              <div aria-hidden="true" style={{ display: 'none' }} />
            </div>
          )}
        </div>
      </aside>
    </>
  );
};