king
2022-09-06 b8e1395f02c929eaa96b949cf6027ee2a43856a6
src/tabviews/zshare/actionList/tabbutton/index.jsx
@@ -4,51 +4,123 @@
import { is, fromJS } from 'immutable'
import { Button, notification } from 'antd'
import { modifyTabview } from '@/store/action'
import zhCN from '@/locales/zh-CN/main.js'
import enUS from '@/locales/en-US/main.js'
import './index.scss'
import MKEmitter from '@/utils/events.js'
import MkIcon from '@/components/mk-icon'
// import './index.scss'
class TabButton extends Component {
  static propTpyes = {
    show: PropTypes.any,              // 按钮显示样式控制
    BData: PropTypes.any,             // 主表数据
    MenuID: PropTypes.string,         // 菜单ID
    btn: PropTypes.object,            // 按钮
    selectedData: PropTypes.any,      // 子表中选择数据
    setting: PropTypes.any,           // 页面通用设置
    triggerBtn: PropTypes.any,
    disabled: PropTypes.any,          // 行按钮禁用
  }
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    disabled: false,
    hidden: false,
    primaryId: ''
  }
  /**
   * @description 外部触发按钮点击
   */
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.triggerBtn && !is(fromJS(this.props.triggerBtn), fromJS(nextProps.triggerBtn)) && nextProps.triggerBtn.button.uuid === this.props.btn.uuid) {
      this.actionTrigger(nextProps.triggerBtn.data)
  UNSAFE_componentWillMount () {
    const { btn, selectedData, BData } = this.props
    let disabled = false
    if (btn.controlField && selectedData && selectedData.length > 0) { // 表格中按钮隐藏控制
      selectedData.forEach(item => {
        let s = item[btn.controlField] !== undefined ? item[btn.controlField] + '' : ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          disabled = true
        }
      })
      this.setState({hidden: disabled && btn.control === 'hidden'})
    } else if (btn.control === 'parent') {
      if (!BData || !BData.hasOwnProperty(btn.controlField)) {
        this.setState({hidden: true})
      } else {
        let s = BData[btn.controlField] + ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          this.setState({hidden: true})
        } else {
          this.setState({hidden: false})
        }
      }
    }
    if (this.props.disabled || disabled) {
      this.setState({disabled: true})
    }
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
  componentDidMount () {
    MKEmitter.addListener('triggerBtnId', this.actionTrigger)
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { btn, selectedData, BData } = this.props
    let disabled = false
    if (btn.controlField && !is(fromJS(nextProps.selectedData || []), fromJS(selectedData || []))) {
      if (nextProps.selectedData && nextProps.selectedData.length > 0) { // 表格中按钮隐藏控制
        nextProps.selectedData.forEach(item => {
          let s = item[btn.controlField] !== undefined ? item[btn.controlField] + '' : ''
          if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
            disabled = true
          }
        })
      }
      this.setState({hidden: disabled && btn.control === 'hidden'})
    } else if (btn.control === 'parent' && !is(fromJS(nextProps.BData || {}), fromJS(BData || {}))) {
      if (!nextProps.BData || !nextProps.BData.hasOwnProperty(btn.controlField)) {
        this.setState({hidden: true})
      } else {
        let s = nextProps.BData[btn.controlField] + ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          this.setState({hidden: true})
        } else {
          this.setState({hidden: false})
        }
      }
    }
    if (nextProps.disabled || disabled) {
      this.setState({disabled: true})
    } else {
      this.setState({disabled: false})
    }
  }
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
  }
  
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (record) => {
    const { setting, btn, tabviews, MenuID, selectedData } = this.props
    let data = []
  actionTrigger = (triggerId, record, type) => {
    const { setting, btn, selectedData } = this.props
    const { disabled } = this.state
    if (record) { // 表格中触发按钮
      data = [record]
    } else {
      data = selectedData || []
    if (disabled) return
    if (triggerId && btn.uuid !== triggerId) return
    if (type === 'linkbtn' && !btn.$toolbtn && !is(fromJS(selectedData || []), fromJS(record))) {
      return
    }
    let data = record || selectedData || []
    if (btn.Ot === 'requiredSgl' && data.length !== 1) {
      // 需要选择单行时,校验数据
@@ -58,86 +130,126 @@
        duration: 5
      })
      return
    } else if (!setting.primaryKey) {
      // 需要选择行时,校验是否设置主键
      notification.warning({
        top: 92,
        message: '未设置主键!',
        duration: 5
      })
      return
    }
    let primaryId = ''
    if (btn.Ot === 'requiredSgl') {
    if (btn.Ot === 'requiredSgl' && setting.primaryKey) {
      primaryId = data[0][setting.primaryKey] || ''
    }
    let newtab = {
      MenuNo: '',
      MenuID: btn.uuid,
      MenuName: btn.label,
      type: btn.tabTemplate,
      selected: true,
      param: {
        parentId: this.props.MenuID,
        btn: btn,
        data: data[0] || null,
        primaryId: primaryId
    let newtab = {}
    if (btn.tabTemplate === 'FormTab') {
      newtab = {
        MenuNo: '',
        MenuID: btn.uuid,
        MenuName: btn.label,
        type: btn.tabTemplate,
        param: {
          parentId: this.props.MenuID,
          btn: btn,
          data: data[0] || null,
          primaryId: primaryId
        }
      }
    } else {
      let menu = null
      if (btn.MenuID) {
        let _menu = this.props.permMenus.filter(m => m.MenuID === btn.MenuID)[0] || ''
        menu = {
          MenuID: btn.MenuID,
          MenuName: _menu ? _menu.MenuName : btn.MenuName,
          MenuNo: btn.MenuNo,
          type: _menu ? _menu.type : btn.tabType
        }
      } else if (btn.linkmenu && btn.linkmenu.length > 0) {
        let menu_id = btn.linkmenu.slice(-1)[0]
        menu = this.props.permMenus.filter(m => m.MenuID === menu_id)[0] || ''
      }
      if (!menu) {
        notification.warning({
          top: 92,
          message: '菜单已删除或没有访问权限!',
          duration: 5
        })
        return
      }
      newtab = {
        ...menu,
        param: {
          $BID: primaryId
        }
      }
    }
    let index = 0
    let isexit = false
    let tabs = tabviews.map((tab, i) => {
      tab.selected = false
      if (tab.MenuID === MenuID) {
        index = i
      } else if (tab.MenuID === btn.uuid) {
        tab.selected = true
        isexit = true
      }
      return tab
    })
    if (!isexit) {
      tabs.splice(index + 1, 0, newtab)
    if (['linkage_navigation', 'linkage', 'menu_board'].includes(window.GLOB.navBar)) {
      MKEmitter.emit('modifyTabs', newtab, 'replace')
    } else {
      MKEmitter.emit('modifyTabs', newtab, 'plus', true)
    }
    this.props.modifyTabview(tabs)
    MKEmitter.emit('openNewTab')
    if (window.GLOB.systemType === 'production') {
      MKEmitter.emit('queryTrigger', {menuId: btn.uuid, name: '标签页'})
    }
  }
  render() {
    const { btn, show } = this.props
    const { btn } = this.props
    const { disabled, hidden } = this.state
    if (hidden) return null
    let label = ''
    let icon = ''
    let type = 'link'
    let className = ''
    if (btn.show === 'button') {
      label = btn.label
      icon = btn.icon || ''
    } else if (btn.show === 'link') {
      label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
      icon = ''
    } else if (btn.show === 'icon') {
      icon = btn.icon || ''
    } else if (!btn.$toolbtn) {
      icon = btn.icon || ''
      label = btn.label
      className = 'mk-btn mk-' + btn.class
    } else {
      type = ''
      icon = btn.icon || ''
      label = btn.label
      className = 'mk-btn mk-' + btn.class
    }
    return (
      <div className="mk-btn-wrap">
        {!show ? <Button
          className={'mk-btn mk-' + btn.class}
          icon={btn.icon}
          onClick={() => {this.actionTrigger()}}
        >{btn.label}</Button> : null}
        {show === 'icon' ? <Button className="action-cell" icon={btn.icon || 'dash'} onClick={() => {this.actionTrigger()}}></Button> : null}
        {show === 'text' ? <Button className="action-cell" onClick={() => {this.actionTrigger()}}>{btn.label}</Button> : null}
        {show === 'all' ? <Button className="action-cell" icon={btn.icon || ''} onClick={() => {this.actionTrigger()}}>{btn.label}</Button> : null}
      </div>
      <Button
        type={type}
        title={disabled ? (btn.reason || '') : (btn.show === 'icon' ? btn.label : '')}
        style={btn.style || null}
        disabled={disabled}
        icon={icon}
        className={className}
        onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
      >{label}</Button>
    )
  }
}
const mapStateToProps = (state) => {
  return {
    tabviews: state.tabviews,
    permMenus: state.permMenus,
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    modifyTabview: (tabviews) => dispatch(modifyTabview(tabviews))
  }
const mapDispatchToProps = () => {
  return {}
}
export default connect(mapStateToProps, mapDispatchToProps)(TabButton)