king
2023-06-25 5025901e459ae49d85210573ad38fb3f3c1d9230
src/tabviews/custom/components/chart/antv-G6/index.jsx
@@ -804,7 +804,6 @@
  state = {
    config: null,
    data: null,
    BID: '',
    BData: '',
    plot: null,
@@ -814,11 +813,14 @@
    empty: true
  }
  data = []
  mkgraph = null
  selectedId = ''
  UNSAFE_componentWillMount () {
    const { config, data, initdata } = this.props
    let _config = fromJS(config).toJS()
    let _data = null
    let _sync = _config.setting.sync === 'true'
    let BID = ''
@@ -834,10 +836,10 @@
    }
    if (_sync && data) {
      _data = data[config.dataName] || []
      this.data = data[config.dataName] || []
      _sync = false
    } else if (_sync && initdata) {
      _data = initdata || []
      this.data = initdata || []
      _sync = false
    }
@@ -850,7 +852,6 @@
    this.setState({
      config: _config,
      data: _data,
      BID: BID || '',
      BData: BData || '',
      arr_field: _config.columns.map(col => col.field).join(','),
@@ -861,44 +862,13 @@
        setTimeout(() => {
          this.loadData()
        }, _config.setting.delay || 0)
      } else if (config.setting.sync === 'true' && _data) {
        this.handleData()
      }
    })
  }
  /**
   * @description 图表数据更新,刷新内容
   */
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { sync, config } = this.state
    if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) {
      let _data = []
      if (nextProps.data && nextProps.data[config.dataName]) {
        _data = nextProps.data[config.dataName]
      }
      this.setState({sync: false, data: _data}, () => {
        this.handleData()
      })
    } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      this.setState({}, () => {
        this.loadData()
      })
    if (this.data.length > 0) {
      this.handleData()
    }
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
  componentDidMount () {
    const { config } = this.state
    MKEmitter.addListener('reloadData', this.reloadData)
    MKEmitter.addListener('resetSelectLine', this.resetParentParam)
    let that = this
    if (config.plot.subtype === 'mindmap') {
@@ -1028,6 +998,53 @@
  }
  /**
   * @description 图表数据更新,刷新内容
   */
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { sync, config } = this.state
    if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) {
      let _data = []
      if (nextProps.data && nextProps.data[config.dataName]) {
        _data = nextProps.data[config.dataName]
      }
      if (!is(fromJS(this.data), fromJS(_data))) {
        this.data = _data
        this.handleData()
      }
      this.setState({sync: false})
    } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      this.setState({}, () => {
        this.loadData()
      })
    }
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
  componentDidMount () {
    const { config, sync } = this.state
    MKEmitter.addListener('reloadData', this.reloadData)
    MKEmitter.addListener('resetSelectLine', this.resetParentParam)
    if (config.$cache && (config.setting.sync !== 'true' || sync)) {
      Api.getLCacheConfig(config.uuid).then(res => {
        if (!res || this.data.length > 0) return
        if (!is(fromJS(this.data), fromJS(res))) {
          this.data = res
          this.handleData()
        }
      })
    }
  }
  /**
   * @description 组件销毁,清除state更新,清除快捷键设置
   */
  componentWillUnmount () {
@@ -1062,11 +1079,10 @@
    const { config, arr_field, BID } = this.state
    if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
      this.setState({
        data: {}
      }, () => {
      if (!is(fromJS(this.data), fromJS([]))) {
        this.data = []
        this.handleData()
      })
      }
      return
    }
@@ -1086,24 +1102,42 @@
    let result = await Api.genericInterface(param)
    if (result.status) {
      let data = result.data || []
      if (config.$cache && config.setting.onload !== 'false') {
        Api.writeCacheConfig(config.uuid, result.data || '')
      }
      this.setState({
        data,
        loading: false
      }, () => {
        this.handleData()
      })
      if (!is(fromJS(this.data), fromJS(result.data || []))) {
        this.data = result.data || []
        this.handleData()
      }
      if (result.message) {
        if (result.ErrCode === 'Y') {
          Modal.success({
            title: result.message
          })
        } else if (result.ErrCode === 'S') {
          notification.success({
            top: 92,
            message: result.message,
            duration: 2
          })
        }
      }
    } else {
      this.setState({
        loading: false
      })
      
      if (!result.message) return
      if (result.ErrCode === 'N') {
        Modal.error({
          title: result.message,
        })
      } else {
      } else if (result.ErrCode !== '-2') {
        notification.error({
          top: 92,
          message: result.message,
@@ -1114,7 +1148,7 @@
  }
  getdata = () => {
    const { plot, data, config, BData } = this.state
    const { plot, config, BData } = this.state
    
    let root = {
      label: 'Root',
@@ -1140,7 +1174,7 @@
    let _options = []
    let logMap = new Map()
    data.forEach(item => {
    this.data.forEach(item => {
      let pval = item[plot.parentField]
      let val = item[plot.valueField]
      let label = item[plot.labelField] || ''
@@ -1165,9 +1199,20 @@
      _options.push(_item)
    })
    root = this.getTree(root, _options)
    root.nodeNumber = 0
    root = this.getTree(root, _options, root)
    root.children = root.children || []
    if (root.selectedItem) {
      let option = {...root.selectedItem}
      setTimeout(() => {
        MKEmitter.emit('resetSelectLine', config.uuid, option.id, option)
      }, 20)
      delete root.selectedItem
    } else {
      this.selectedId = ''
    }
    if (plot.subtype === 'mindmap') {
      root.type = 'dice-mind-map-root'
@@ -1230,13 +1275,22 @@
    return root
  }
  getTree = (parent, options) => {
  getTree = (parent, options, root) => {
    parent.children = []
    // 添加菜单的子元素
    options = options.filter(option => {
      if (option.$parentId === parent.id) {
        delete option.$parentId
        if (this.selectedId && option.id === this.selectedId) {
          root.selectedItem = {...option}
          option.selected = true
        }
        parent.children.push(option)
        root.nodeNumber++
        return false
      }
      return true
@@ -1246,7 +1300,7 @@
      parent.children = null
    } else {
      parent.children = parent.children.map(item => {
        item = this.getTree(item, options)
        item = this.getTree(item, options, root)
        return item
      })
@@ -1256,24 +1310,71 @@
  }
  handleData = () => {
    const { plot, data } = this.state
    const { config } = this.state
    let _element = document.getElementById(this.state.chartId)
    if (_element) {
      _element.innerHTML = ''
    MKEmitter.emit('resetSelectLine', config.uuid, '', '')
    if (this.mkgraph) {
      this.mkgraph.clear()
      this.resetrender()
    } else {
      this.viewrender()
    }
  }
    if (!data || data.length === 0) {
  viewrender = () => {
    const { plot } = this.state
    this.selectedId = ''
    if (this.data.length === 0) {
      this.setState({empty: true})
    } else {
      this.setState({empty: false})
      if (plot.subtype === 'mindmap') {
        this.ponitrender()
        this.mindrender()
      } else if (plot.subtype === 'indentTree') {
        this.indentrender()
      } else if (plot.subtype === 'kapmap') {
        this.kapmaprender()
      }
    }
  }
  resetrender = () => {
    const { plot } = this.state
    if (this.data.length === 0) {
      this.setState({empty: true})
      this.selectedId = ''
    } else {
      this.setState({empty: false})
      const data = this.getdata()
      if (plot.subtype === 'mindmap') {
        this.mkgraph.data(data)
        this.mkgraph.render()
        const width = this.wrap.scrollWidth - 30
        if (plot.collapsed === 'true' && plot.dirField) {
          this.mkgraph.zoomTo(1, { x: width / 2, y: plot.height / 2 })
        } else if (plot.collapsed === 'true' || data.nodeNumber < 5) {
          this.mkgraph.zoomTo(1, { x: 0, y: plot.height / 2 })
        }
      } else if (plot.subtype === 'indentTree') {
        this.mkgraph.data(dataIndTransform(data))
        this.mkgraph.render()
      } else if (plot.subtype === 'kapmap') {
        this.mkgraph.data(data)
        this.mkgraph.render()
        this.mkgraph.fitView()
        if (plot.collapsed === 'true' || data.nodeNumber < 5) {
          this.mkgraph.zoomTo(1, { x: 0, y: plot.height / 2 })
        }
      }
    }
  }
@@ -1333,9 +1434,11 @@
    graph.render()
    graph.fitView()
    if (plot.collapsed === 'true') {
    if (plot.collapsed === 'true' || data.nodeNumber < 5) {
      graph.zoomTo(1, { x: 0, y: plot.height / 2 })
    }
    this.mkgraph = graph
  }
  indentrender = () => {
@@ -1397,12 +1500,14 @@
    tree.data(dataIndTransform(data))
    
    tree.render()
    this.mkgraph = tree
  }
  /**
   * @description 散点图
   * @description 思维导图
   */
  ponitrender = () => {
  mindrender = () => {
    const { config, plot, chartId } = this.state
    const data = this.getdata()
    const width = this.wrap.scrollWidth - 30
@@ -1456,9 +1561,11 @@
    if (plot.collapsed === 'true' && plot.dirField) {
      tree.zoomTo(1, { x: width / 2, y: plot.height / 2 })
    } else if (plot.collapsed === 'true') {
    } else if (plot.collapsed === 'true' || data.nodeNumber < 5) {
      tree.zoomTo(1, { x: 0, y: plot.height / 2 })
    }
    this.mkgraph = tree
  }
  handleClick = (data = null) => {
@@ -1470,7 +1577,7 @@
      let menu = null
      
      if (plot.menus && plot.menus.length > 0) {
        let s = data[plot.menuType] || ''
        let s = data[plot.menuType] + ''
        plot.menus.forEach(m => {
          if (s !== m.sign) return
          menu = m
@@ -1527,7 +1634,13 @@
      MKEmitter.emit('modifyTabs', newtab, true)
    } else {
      MKEmitter.emit('resetSelectLine', config.uuid, (data ? data.$$uuid : ''), data)
      if (data) {
        this.selectedId = data.$$uuid || ''
        MKEmitter.emit('resetSelectLine', config.uuid, data.$$uuid || '', data)
      } else {
        this.selectedId = ''
        MKEmitter.emit('resetSelectLine', config.uuid, '', '')
      }
    }
  }