king
2020-06-16 6c16e43cd6521460c804391c042348dbb14086fc
src/tabviews/zshare/cardcomponent/index.jsx
@@ -1,70 +1,76 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Icon, Card, Dropdown, Menu, Spin, Empty, Tabs } from 'antd'
import { Icon, Card, Spin, Empty, Tabs } from 'antd'
import asyncComponent from '@/utils/asyncComponent'
import '@/assets/css/table.scss'
import './index.scss'
const NormalButton = asyncComponent(() => import('@/tabviews/zshare/actionList/normalbutton'))
const PopupButton = asyncComponent(() => import('@/tabviews/zshare/actionList/popupbutton'))
const TabButton = asyncComponent(() => import('@/tabviews/zshare/actionList/tabbutton'))
const NewPageButton = asyncComponent(() => import('@/tabviews/zshare/actionList/newpagebutton'))
const ChangeUserButton = asyncComponent(() => import('@/tabviews/zshare/actionList/changeuserbutton'))
const PrintButton = asyncComponent(() => import('@/tabviews/zshare/actionList/printbutton'))
const { TabPane } = Tabs
class CardCell extends Component {
  static propTpyes = {
    card: PropTypes.object,
    data: PropTypes.object,
    selectKey: PropTypes.string,
    colMap: PropTypes.any,
    triggerBtn: PropTypes.func,
    switchCard: PropTypes.func
    BID: PropTypes.any,               // 主表ID
    BData: PropTypes.any,             // 主表数据
    Tab: PropTypes.any,               // 如果当前元素为标签时,tab为标签信息
    MenuID: PropTypes.string,         // 菜单ID
    setting: PropTypes.object,        // 页面设定
    logcolumns: PropTypes.array,      // 字段列
    card: PropTypes.object,           // 卡片设置信息
    data: PropTypes.object,           // 卡片数据
    selectKey: PropTypes.string,      // 选择卡片的序号
    colMap: PropTypes.any,            // 显示列信息,用于设置标记
    refreshdata: PropTypes.func,      // 按钮操作后数据刷新
    switchCard: PropTypes.func        // 卡片切换
  }
  state = {
    card: null,
    extra: null,
    actions: null
    extra: null,    // 顶部按钮信息
    actions: null   // 底部按钮行
  }
  UNSAFE_componentWillMount () {
    this.getCardActions()
  }
  /**
   * @description 数据切换,重新绑定按钮数据
   */
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (!is(fromJS(this.props.data), fromJS(nextProps.data))) {
      this.setState({}, () => {
        this.getCardActions()
      })
    }
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
  /**
   * @description 获取顶部及按钮行的按钮集
   */
  getCardActions = () => {
    const { card } = this.props
    
    let extra = null
    if (card.header && card.header.actions) {
      let actions = card.header.actions.map(item => {
        let _action = null
        if (card.header.show === 'icon') {
          _action = <span className="action-cell" onClick={() => this.triggerAction(item)} key={item.uuid}><Icon type={item.icon || 'dash'} /></span>
        } else if (card.bottom.show === 'text') {
          _action = <span className="action-cell" onClick={() => this.triggerAction(item)} key={item.uuid}>{item.label}</span>
        } else {
          _action = <span className="action-cell" onClick={() => this.triggerAction(item)} key={item.uuid}><Icon type={item.icon || 'dash'} /> {item.label}</span>
        }
        return _action
      })
      if (actions.length === 1) {
        extra = actions[0]
      } else {
        extra = <Dropdown overlay={
          <Menu>{actions.map((item, i) => <Menu.Item key={i}> {item} </Menu.Item>)}</Menu>
        } placement="bottomRight">
          Action
        </Dropdown>
      }
      let actions = this.getActionList(card.header.actions, card.header.show)
      extra = actions[0]
    }
    let _actions = null
    if (card.bottom && card.bottom.actions) {
      _actions = card.bottom.actions.map(item => {
        let _action = null
        if (card.bottom.show === 'icon') {
          _action = <span className="action-cell" onClick={() => this.triggerAction(item)} key={item.uuid}><Icon type={item.icon || 'dash'} /></span>
        } else if (card.bottom.show === 'text') {
          _action = <span className="action-cell" onClick={() => this.triggerAction(item)} key={item.uuid}>{item.label}</span>
        } else {
          _action = <span className="action-cell" onClick={() => this.triggerAction(item)} key={item.uuid}><Icon type={item.icon || 'dash'} /> {item.label}</span>
        }
        return _action
      })
      _actions = this.getActionList(card.bottom.actions, card.bottom.show)
    }
    this.setState({
@@ -72,15 +78,113 @@
      extra: extra
    })
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  /**
   * @description 获取按钮元素
   */
  getActionList = (actions, show) => {
    const { BID, BData, Tab, setting, logcolumns, ContainerId, data, MenuID } = this.props
    return actions.map(item => {
      if (['exec', 'prompt', 'pop'].includes(item.OpenType)) {
        return (
          <NormalButton
            key={item.uuid}
            BID={BID}
            Tab={Tab}
            btn={item}
            show={show}
            BData={BData}
            setting={setting}
            columns={logcolumns}
            selectedData={[data]}
            ContainerId={ContainerId}
            updateStatus={this.updateStatus}
          />
        )
      } else if (item.OpenType === 'popview') {
        return (
          <PopupButton
            key={item.uuid}
            BID={BID}
            Tab={Tab}
            btn={item}
            show={show}
            BData={BData}
            setting={setting}
            selectedData={[data]}
            updateStatus={this.updateStatus}
          />
        )
      } else if (item.OpenType === 'tab' || item.OpenType === 'blank') {
        return (
          <TabButton
            key={item.uuid}
            btn={item}
            show={show}
            MenuID={MenuID}
            setting={setting}
            selectedData={[data]}
            updateStatus={this.updateStatus}
          />
        )
      } else if (item.OpenType === 'innerpage' || item.OpenType === 'outerpage') {
        return (
          <NewPageButton
            key={item.uuid}
            btn={item}
            show={show}
            setting={setting}
            selectedData={[data]}
          />
        )
      } else if (item.OpenType === 'funcbutton') {
        if (item.funcType === 'changeuser') {
          return (
            <ChangeUserButton
              key={item.uuid}
              BID={BID}
              btn={item}
              show={show}
              setting={setting}
              selectedData={[data]}
              updateStatus={this.updateStatus}
            />
          )
        } else if (item.funcType === 'print') {
          return (
            <PrintButton
              key={item.uuid}
              BID={BID}
              Tab={Tab}
              btn={item}
              show={show}
              BData={BData}
              setting={setting}
              selectedData={[data]}
              ContainerId={ContainerId}
              updateStatus={this.updateStatus}
            />
          )
        }
      }
      return null
    })
  }
  triggerAction = (btn) => {
    this.props.triggerBtn(btn, [this.props.data])
  /**
   * @description 操作完成后,数据刷新
   */
  updateStatus = (type, positon) => {
    if (type === 'refresh') {
      this.props.refreshdata(positon)
    }
  }
  /**
   * @description 卡片中元素标记设置
   */
  getMark = (record, marks) => {
    const { colMap } = this.props
    let className = ''
@@ -118,6 +222,8 @@
      if (mark.match === '=' && originVal === contrastVal) {
        className = mark.color[1]
      } else if (mark.match === '!=' && originVal !== contrastVal) {
        className = mark.color[1]
      } else if (mark.match === 'like' && originVal.indexOf(contrastVal) > -1) {
        className = mark.color[1]
      } else if (mark.match === '>' && originVal > contrastVal) {
@@ -150,6 +256,9 @@
    }
  }
  /**
   * @description 获取卡片内容
   */
  getContent = (col) => {
    const { data } = this.props
@@ -232,6 +341,9 @@
    }
  }
  /**
   * @description 获取卡片中图片或图标设置
   */
  getAvatar = () => {
    const { card, data } = this.props
@@ -252,6 +364,9 @@
    }
  }
  /**
   * @description 卡片点击切换事件
   */
  switchCard = () => {
    const { card, data } = this.props
@@ -283,18 +398,18 @@
          extra={extra}
          actions={actions}
        >
          <div className={'ant-card-meta' + (card.switch ? ' switch' : '')} onClick={this.switchCard}>
          <div className={'ant-card-meta' + (card.switch ? ' switch' : '')} style={card.flex ? {display: 'flex'} : null} onClick={this.switchCard}>
            {card.avatar ?
              <div className="ant-card-meta-avatar">
                <span className="ant-avatar ant-avatar-circle ant-avatar-image" style={card.avatar.class}>
              <div className="ant-card-meta-avatar" style={card.avatar.class}>
                <span className="ant-avatar ant-avatar-image" style={card.avatar.radius ? {borderRadius: '50%'} : null}>
                  {this.getAvatar()}
                </span>
              </div> : null
            }
            <div className="ant-card-meta-detail" style={card.marginLeft || null}>
            <div className="ant-card-meta-detail" style={card.flex ? {flex: 1} : null}>
              {card.details.map((detail, i) => {
                return (
                  <div className={detail.class} key={i}>
                  <div className={detail.class} style={detail.style} key={i}>
                    {detail.column ? this.getContent(detail.column) : detail.content}
                  </div>
                )
@@ -309,12 +424,18 @@
class CardChart extends Component {
  static propTpyes = {
    BID: PropTypes.any,               // 主表ID
    BData: PropTypes.any,             // 主表数据
    Tab: PropTypes.any,               // 如果当前元素为标签时,tab为标签信息
    MenuID: PropTypes.string,         // 菜单ID
    config: PropTypes.object,         // 页面配置信息
    logcolumns: PropTypes.array,      // 显示列
    ContainerId: PropTypes.any,       // tab页面ID,用于弹窗控制
    plot: PropTypes.object,
    config: PropTypes.object,
    tableId: PropTypes.string,
    loading: PropTypes.bool,
    data: PropTypes.array,
    buttonTrigger: PropTypes.func,
    refreshdata: PropTypes.func,
    handleTableId: PropTypes.func
  }
@@ -324,6 +445,9 @@
    selectKey: ''
  }
  /**
   * @description 卡片初始化,设置卡片的配置信息
   */
  UNSAFE_componentWillMount () {
    const { plot, config } = this.props
    let card = {}
@@ -340,7 +464,7 @@
    })
    actions.forEach(item => {
      if (item.Ot && item.Ot !== 'notRequired' && !['excelIn', 'excelOut'].includes(item.OpenType)) {
      if ((item.Ot && item.Ot !== 'notRequired' && !['excelIn', 'excelOut'].includes(item.OpenType)) || item.funcType === 'changeuser') {
        actionMap.set(item.uuid, item)
      }
    })
@@ -362,6 +486,7 @@
          if (!actionMap.get(item.value)) return
          card.header.actions.push(actionMap.get(item.value))
        })
        card.header.actions.length = 1
      }
      if (card.header.actions.length === 0) {
        card.header.actions = null
@@ -381,44 +506,54 @@
          card.avatar = null
        } else {
          card.avatar.marks = _marks
          card.avatar.outWidth = plot.avatar.size + 16
          card.avatar.class = {width: plot.avatar.size, fontSize: plot.avatar.size + 'px'}
          card.avatar.width = plot.avatar.size
          card.avatar.class = {width: plot.avatar.size, paddingTop: plot.avatar.size, fontSize: plot.avatar.size + 'px'}
        }
      } else if (card.avatar.type === 'picture' && colMap.get(plot.avatar.field)) {
        card.avatar.field = plot.avatar.field
        card.avatar.outWidth = plot.avatar.width + 16
        card.avatar.width = plot.avatar.width
        card.avatar.widthType = plot.avatar.widthType
        card.avatar.radius = plot.avatar.radius !== 'false'
        card.avatar.class = {width: plot.avatar.width}
        let _width = card.avatar.width
        if (plot.avatar.widthType === 'ratio') {
          _width = _width + '%'
        }
        card.avatar.class = {width: _width, paddingTop: _width}
      }
    }
    if (card.avatar) {
      card.marginLeft = {marginLeft: card.avatar.outWidth}
    }
    card.details = []
    if (plot.details.length > 0) {
      card.details = plot.details.map(item => {
      card.details = plot.details.map(_item => {
        let item = fromJS(_item).toJS()
        if (item.datatype === 'dynamic' && colMap.get(item.field)) {
          item.column = colMap.get(item.field)
        } else {
          item.datatype = 'static'
        }
        let _class = ''
        if (item.bold === 'true') {
          _class = ' ant-card-meta-title'
        } else {
          _class = ' ant-card-meta-description'
        }
        if (item.width) {
          _class += ' ' + item.width
        }
        item.fontSize = item.fontSize || 14
        item.height = item.height || 1
        item.fontWeight = item.fontWeight || 'normal'
        item.width = item.width + '%'
        let _style = {}
        _style.fontWeight = item.fontWeight
        _style.width = item.width
        _style.fontSize = item.fontSize + 'px'
        _style.height = item.height * item.fontSize * 1.5
        item.style = _style
        let _class = 'mk-card-detail-item '
        if (item.align) {
          _class += ' ' + item.align
          _class += item.align + ' '
        }
        _class += 'line' + item.height
        item.class = _class
        return item
@@ -482,10 +617,41 @@
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
  triggerBtn = (btn, data) => {
    this.props.buttonTrigger(btn, data)
  componentDidMount () {
    const { plot } = this.props
    const { card } = this.state
    let _flex = false
    if (card.avatar) {
      let _cardWidth = 300
      if (plot.widthType === 'absolute') {
        _cardWidth = plot.cardWidth
      } else {
        let _outWidth = document.body.offsetWidth - 260
        if (this.cardRef.offsetWidth) {
          _outWidth = this.cardRef.offsetWidth
        }
        _cardWidth = Math.floor(_outWidth * plot.cardWidth / 24 - 20)
      }
      if (card.avatar.widthType === 'ratio') {
        if (card.avatar.width < 90) {
          _flex = true
        }
      } else if (card.avatar.width < _cardWidth * 0.9) {
        _flex = true
      }
      this.setState({
        card: {...this.state.card, flex: _flex}
      })
    }
  }
  /**
   * @description 卡片切换
   */
  switchCard = (data) => {
    const { config } = this.props
@@ -497,11 +663,11 @@
  }
  render() {
    const { plot, data, loading } = this.props
    const { plot, data, loading, BID, BData, Tab, MenuID, config, logcolumns, ContainerId } = this.props
    const { card, colMap, selectKey } = this.state
    return (
      <div className="card-row-box mingke-table" style={!plot.title ? {paddingTop: '15px'} : null}>
      <div className="card-row-box mingke-table" ref={(ref) => this.cardRef = ref} style={!plot.title ? {paddingTop: '15px'} : null}>
        {loading ?
          <div className="loading-mask">
            <div className="ant-spin-blur"></div>
@@ -513,12 +679,19 @@
          data.map((item, i) => (
            <CardCell
              key={i}
              BID={BID}
              Tab={Tab}
              card={card}
              data={item}
              BData={BData}
              MenuID={MenuID}
              colMap={colMap}
              selectKey={selectKey}
              triggerBtn={this.triggerBtn}
              logcolumns={logcolumns}
              setting={config.setting}
              ContainerId={ContainerId}
              switchCard={this.switchCard}
              refreshdata={this.props.refreshdata}
            />
          ))
        }
@@ -526,12 +699,19 @@
          <Tabs activeKey="">
            {data.map((item, i) => (
              <TabPane tab={<CardCell
                BID={BID}
                Tab={Tab}
                card={card}
                data={item}
                BData={BData}
                MenuID={MenuID}
                colMap={colMap}
                selectKey={selectKey}
                triggerBtn={this.triggerBtn}
                logcolumns={logcolumns}
                setting={config.setting}
                ContainerId={ContainerId}
                switchCard={this.switchCard}
                refreshdata={this.props.refreshdata}
              />} key={i}></TabPane>
              
            ))}