king
2021-09-16 73c0258e4e30f1d2e407cb067feea8e79fad081d
src/menu/components/card/cardcomponent/index.jsx
@@ -1,36 +1,37 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Modal, Popover, Icon, Switch } from 'antd'
import { Popover, Icon, Switch, Col } from 'antd'
import asyncComponent from '@/utils/asyncComponent'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import SettingForm from './settingform'
import asyncIconComponent from '@/utils/asyncIconComponent'
import { resetStyle } from '@/utils/utils-custom.js'
import getSettingForm from './options'
import Utils from '@/utils/utils.js'
import MKEmitter from '@/utils/events.js'
import './index.scss'
const NormalForm = asyncIconComponent(() => import('@/components/normalform'))
const CardCellComponent = asyncComponent(() => import('../cardcellcomponent'))
const CopyComponent = asyncIconComponent(() => import('@/menu/components/share/copycomponent'))
const PasteController = asyncIconComponent(() => import('@/components/paste'))
class CardBoxComponent extends Component {
  static propTpyes = {
    MenuType: PropTypes.any,         // 菜单类型
    offset: PropTypes.any,           // 偏移量
    cards: PropTypes.object,         // 卡片行配置信息
    card: PropTypes.object,          // 卡片配置信息
    move: PropTypes.func,            // 卡片移动
    deleteElement: PropTypes.func,   // 卡片删除
    updateElement: PropTypes.func    // 菜单配置更新
  }
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    card: null,            // 卡片信息,包括正反面
    formlist: null,        // 设置表单信息
    elements: null,        // 编辑组
    visible: false,        // 模态框控制
    settingVisible: false,
    side: 'front'
    side: 'front',
    appType: sessionStorage.getItem('appType')
  }
  /**
@@ -50,9 +51,9 @@
  }
  shouldComponentUpdate (nextProps, nextState) {
    const { cards, MenuType } = this.props
    const { cards } = this.props
    
    return !is(fromJS(cards), fromJS(nextProps.cards)) || !is(fromJS(this.state), fromJS(nextState)) || MenuType !== nextProps.MenuType
    return !is(fromJS(cards), fromJS(nextProps.cards)) || !is(fromJS(this.state), fromJS(nextState))
  }
  /**
@@ -85,7 +86,7 @@
    this.props.updateElement(_card)
  }
  updateCard = (elements) => {
  updateCard = (elements, btn) => {
    const { card, side } = this.state
    let _card = {}
@@ -100,7 +101,7 @@
      card: _card
    })
    this.props.updateElement(_card)
    this.props.updateElement(_card, btn)
  }
  changeSide = () => {
@@ -144,24 +145,9 @@
    const { cards } = this.props
    const { card } = this.state
    let newcard = {}
    let newcard = {eleType: 'button', label: 'button', verify: null, show: 'link', sqlType: '', Ot: 'requiredSgl', OpenType: 'prompt', icon: '', class: 'primary', intertype: 'system', execSuccess: 'grid', execError: 'never', popClose: 'never'}
    newcard.uuid = Utils.getuuid()
    newcard.focus = true
    newcard.eleType = 'button'
    newcard.label = 'button'
    newcard.sqlType = ''
    newcard.Ot = 'requiredSgl'
    newcard.OpenType = 'prompt'
    newcard.icon = ''
    newcard.class = 'primary'
    newcard.intertype = 'system'
    newcard.execSuccess = 'grid'
    newcard.execError = 'never'
    newcard.popClose = 'never'
    newcard.errorTime = 10
    newcard.verify = null
    newcard.show = 'link'
    // 注册事件-添加元素
    MKEmitter.emit('cardAddElement', [cards.uuid, card.uuid], newcard)
@@ -172,7 +158,7 @@
    const { card, side } = this.state
    let _style = null
    let options = ['height', 'background', 'border', 'padding', 'margin']
    let options = ['height', 'background', 'border', 'padding', 'margin', 'shadow']
    if (side === 'front') {
      _style = card.style ? fromJS(card.style).toJS() : {}
    } else if (side === 'back') {
@@ -183,75 +169,160 @@
    MKEmitter.emit('changeStyle', [cards.uuid, card.uuid], options, _style)
  }
  settingSubmit = () => {
    const { card, side } = this.state
  getSettingForms = () => {
    const { cards } = this.props
    const { card, appType } = this.state
    this.settingRef.handleConfirm().then(res => {
      this.setState({
        settingVisible: false,
        card: {...card, setting: res}
      })
      if (side === 'back' && res.type === 'simple') {
        this.setState({
          side: 'front',
          elements: fromJS(card.elements).toJS()
    let buttons = []
    card.elements && card.elements.forEach(item => {
      if (item.eleType === 'button') {
        buttons.push({
          value: item.uuid,
          label: item.label
        })
      }
      this.props.updateElement({...card, setting: res})
    })
    if (appType !== 'mob' && card.backElements) {
      card.backElements.forEach(item => {
        if (item.eleType === 'button') {
          buttons.push({
            value: item.uuid,
            label: item.label
          })
        }
      })
    }
    return getSettingForm(card.setting, cards.subtype === 'propcard', buttons)
  }
  updateSetting = (res) => {
    const { card, side, appType } = this.state
    if (appType === '' && res.menu) {
      let list = null
      try {
        list = JSON.parse(sessionStorage.getItem('thdMenuList')) || []
      } catch (e) {
        list = []
      }
      let id = res.menu[res.menu.length - 1]
      list.forEach(item => {
        if (item.MenuID === id) {
          res.MenuID = id
          res.MenuName = item.MenuName
          res.MenuNo = item.MenuNo
          res.tabType = item.type
        }
      })
    }
    this.setState({
      card: {...card, setting: res}
    })
    if (side === 'back' && res.type === 'simple') {
      this.setState({
        side: 'front',
        elements: fromJS(card.elements).toJS()
      })
    }
    this.props.updateElement({...card, setting: res})
  }
  paste = (element, resolve) => {
    const { cards } = this.props
    const { card } = this.state
    let _uuid = Utils.getuuid()
    if (element.copyType === 'action') {
      element.eleType = 'button'
      if (element.OpenType === 'popview') { // 弹窗标签复制
        let _cell = fromJS(element).toJS()
        _cell.$originUuid = element.uuid
        _cell.uuid = _uuid
        MKEmitter.emit('copyButtons', [_cell])
      }
    }
    element.uuid = _uuid
    element.focus = true
    resolve({status: true})
    // 注册事件-添加元素
    MKEmitter.emit('cardAddElement', [cards.uuid, card.uuid], element)
  }
  clickComponent = (e) => {
    if ((sessionStorage.getItem('style-control') === 'true' || sessionStorage.getItem('style-control') === 'propcard') && this.props.cards.subtype === 'propcard') {
      e.stopPropagation()
      MKEmitter.emit('clickComponent', this.state.card, this.props.cards, 'propcard')
    }
  }
  doubleClickCard = () => {
    const { card } = this.state
    if (card.setting.click === 'menu' && card.setting.menu) {
      MKEmitter.emit('changeEditMenu', {MenuID: card.setting.menu})
    }
  }
  render() {
    const { cards, MenuType } = this.props
    const { card, elements, side, settingVisible, dict } = this.state
    const { cards, offset } = this.props
    const { card, elements, side } = this.state
    let _style = card.style
    let _style = {...card.style}
    if (side === 'back') {
      _style = {...card.backStyle, height: card.style.height}
      _style = {
        ...card.backStyle,
        height: card.style.height,
        marginBottom: card.style.marginBottom,
        marginLeft: card.style.marginLeft,
        marginRight: card.style.marginRight,
        marginTop: card.style.marginTop
      }
    }
    _style = resetStyle(_style)
    return (
      <div className={'ant-col ant-col-' + (card.setting.width || 6)}>
        <div className="card-item" style={_style}>
      <Col span={card.setting.width || 6} offset={offset || 0}>
        <div className="card-item" style={_style} onClick={this.clickComponent} onDoubleClick={(e) => {e.stopPropagation(); this.doubleClickCard()}} id={card.uuid}>
          <CardCellComponent cards={cards} cardCell={card} side={side} elements={elements} updateElement={this.updateCard}/>
          <div className="card-control">
          <div className="card-control" onDoubleClick={(e) => e.stopPropagation()}>
            <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
              <div className="mk-popover-control">
                <Icon className="plus" title="添加元素" onClick={this.addElement} type="plus" />
                {MenuType !== 'billPrint' ? <Icon className="plus" title="添加按钮" onClick={this.addButton} type="plus-square" /> : null}
                <Icon className="edit" type="edit" onClick={() => this.setState({settingVisible: true})} />
                <Icon className="plus" title="添加按钮" onClick={this.addButton} type="plus-square" />
                <NormalForm title="卡片设置" width={800} update={this.updateSetting} getForms={this.getSettingForms}>
                  <Icon type="edit" className="edit" title="编辑"/>
                </NormalForm>
                <CopyComponent type="cardcell" card={card}/>
                <PasteController options={['action', 'customCardElement']} updateConfig={this.paste} />
                <Icon className="style" title="调整样式" onClick={this.changeStyle} type="font-colors" />
                {cards.subtype === 'propcard' ? <Icon className="close" title="删除卡片" type="delete" onClick={() => this.props.deleteElement(card)} /> : null}
                {MenuType !== 'billPrint' && card.setting.type === 'multi' ? <Switch size="small" onClick={this.changeSide} defaultChecked /> : null}
                <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
                  <div className="mk-popover-control">
                    <Icon className="plus" title="前移" type="arrow-left" onClick={() => this.props.move(card, 'left')} />
                    <Icon className="close" title="后移" type="arrow-right" onClick={() => this.props.move(card, 'right')} />
                  </div>
                } trigger="hover" getPopupContainer={() => document.getElementById(card.uuid + 'swap')}>
                  <Icon type="swap" id={card.uuid + 'swap'}/>
                </Popover>
                {cards.subtype === 'propcard' || card.$cardType === 'extendCard' ? <Icon className="close" title="删除卡片" type="delete" onClick={() => this.props.deleteElement(card)} /> : null}
                {card.setting.type === 'multi' ? <Switch size="small" onClick={this.changeSide} defaultChecked /> : null}
              </div>
            } trigger="hover">
              <Icon type="tool" />
            </Popover>
          </div>
        </div>
        <Modal
          wrapClassName="popview-modal"
          title={'卡片设置'}
          visible={settingVisible}
          width={700}
          maskClosable={false}
          okText={dict['model.submit']}
          onOk={this.settingSubmit}
          onCancel={() => { this.setState({ settingVisible: false }) }}
          destroyOnClose
        >
          <SettingForm
            dict={dict}
            cards={cards}
            MenuType={MenuType}
            setting={card.setting}
            inputSubmit={this.settingSubmit}
            wrappedComponentRef={(inst) => this.settingRef = inst}
          />
        </Modal>
      </div>
      </Col>
    )
  }
}