index.js 4.03 KB
Newer Older
秦文海's avatar
秦文海 committed
1
import React, { useState, useContext, forwardRef } from 'react';
2 3 4 5 6 7 8
import classNames from 'classnames';
import { ConfigProvider } from 'antd';
import { Header } from './header';
import { LeftList } from './leftList';
import _ from 'lodash';
import './index.less';

秦文海's avatar
秦文海 committed
9
const TipTool = forwardRef((props, ref) => {
10 11 12 13 14 15 16
  const { style } = props;
  const { leftSetting, headerSetting, events } = props.dataList;
  const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('panda-gis-tip');
  const data = [
    {
      name: 'middle',
秦文海's avatar
秦文海 committed
17 18
      width: style.minWidth ?? '600px',
      height: style.minHeight ?? '300px',
19 20 21 22
      active: true,
    },
    {
      name: 'large',
秦文海's avatar
秦文海 committed
23 24
      width: style.maxWidth ?? '800px',
      height: style.maxHeight ?? '400px',
25 26 27 28 29 30 31
      active: false,
    },
  ];

  const scale = {
    name: 'small',
    width: '200px',
秦文海's avatar
秦文海 committed
32
    height: '45px',
33 34 35 36
    active: true,
  };
  const [styleState, setStyleState] = useState(data);
  const [styleScale, setScaleState] = useState(scale);
秦文海's avatar
秦文海 committed
37
  const changeStyle = ({ name }) => {
38 39 40 41 42 43 44 45 46
    let data = styleState.slice();
    data.map((item) => {
      if (name == item.name) {
        item.active = true;
      } else {
        item.active = false;
      }
    });
    setStyleState(data);
秦文海's avatar
秦文海 committed
47
    return getWidthAndHeight(data);
48 49 50 51 52 53
  };

  const changeScale = (flag) => {
    let obj = { ...styleScale };
    obj.active = flag;
    setScaleState(obj);
秦文海's avatar
秦文海 committed
54 55 56 57 58
    if (flag) {
      return getWidthAndHeight(styleState);
    } else {
      return { height: obj.height, width: obj.width };
    }
59 60 61 62 63 64
  };

  const computedStyle = () => {
    if (!styleScale.active) {
      return { width: styleScale.width, height: styleScale.height };
    } else {
秦文海's avatar
秦文海 committed
65
      return getWidthAndHeight(styleState);
66 67 68
    }
  };

秦文海's avatar
秦文海 committed
69 70 71 72 73 74 75 76
  const getWidthAndHeight = (data) => {
    return data
      .filter((item) => item.active)
      .map(({ width, height }) => {
        return { width, height };
      })[0];
  };

77 78 79 80 81 82 83 84
  const displayComput = () => {
    if (styleScale.active) {
      return { display: 'inline-block' };
    } else {
      return { display: 'none' };
    }
  };
  return (
秦文海's avatar
秦文海 committed
85
    <div className={classNames(`${prefixCls}-Tipwripconter`)} ref={ref} style={computedStyle()}>
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      <Header
        headerSetting={headerSetting}
        events={events}
        changeStyle={changeStyle}
        changeScale={changeScale}
      />
      <div className={classNames(`${prefixCls}-TipAll`)} style={displayComput()}>
        <div className={classNames(`${prefixCls}-Tipconter`)}>
          <div className={classNames(`${prefixCls}-Tipleft`)}>
            <LeftList leftSetting={leftSetting} />
          </div>
        </div>
        <div className={classNames(`${prefixCls}-tipfooter`)}></div>
      </div>
    </div>
  );
秦文海's avatar
秦文海 committed
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 148 149 150 151
//分类可见与不可见属性的数组
export const getVisibleAttribues = function (attribuesArr, fields) {
  var attrs = [];
  fields.forEach(function (item, i, arr) {
    attribuesArr.forEach(function (attr, index) {
      if (i == 0) {
        attrs[index] = {
          show: {},
          hide: {},
          fields: [],
        };
      }
      // 属性值
      var attrValue = attr[item.name];
      if (_.isUndefined(attrValue) || _.isNull(attrValue)) {
        attrValue = '';
      }

      // 界面展示值-处理后的
      var handleValue = attrValue;
      if (handleValue == '') {
        handleValue = '--';
      }
      if (item.visible) {
        attrs[index]['show'][item.alias] = [attrValue, handleValue, item.name, item];
        attrs[index]['fields'].push(item.alias);
      } else {
        attrs[index]['hide'][item.alias] = attrValue;
      }
    });
  });
  attrsFieldHandle(attrs);
  return attrs;
};
//字段类型读取
function attrsFieldHandle(attrs) {
  attrs.forEach((item) => {
    var showFieldObj = item['show'];
    for (var k in showFieldObj) {
      var testField = showFieldObj[k][0];
      if (typeof testField == 'number') {
        if (Number.isInteger(testField)) showFieldObj[k][1] = testField;
        else showFieldObj[k][1] = Number(testField.toFixed(3));
      }
    }
  });
}

export default TipTool;