BlockLoopOption.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React from 'react'
  2. import { Menu, Dropdown } from 'antd'
  3. import i18n from 'i18next'
  4. import cls from 'classnames'
  5. import newEventActions from 'actions/newEvent'
  6. import newEventStores from 'stores/newEvent'
  7. import { getEventBlockByBid } from 'stores/funcs/newEvent'
  8. import { shallowEqualImmutable } from 'utils/utils'
  9. import { loopOptions } from 'const/enum'
  10. import { BLOCK_LOOP_TYPE } from 'const/const'
  11. import BlockEnable from '../BlockEnable'
  12. const MenuItem = Menu.Item
  13. export default class BlockLoopOption extends React.Component {
  14. constructor(props) {
  15. super(props)
  16. this.curEventNodeId = newEventStores.curEventNodeId
  17. this.state = {
  18. option: this.getLoopOption(props.bid)
  19. }
  20. this.onEventChange = this.onEventChange.bind(this)
  21. this.getLoopOption = this.getLoopOption.bind(this)
  22. this.getOptionName = this.getOptionName.bind(this)
  23. this.onSelectOption = this.onSelectOption.bind(this)
  24. this.loopOptions = this.loopOptions.bind(this)
  25. }
  26. shouldComponentUpdate(nextProps, nextState) {
  27. return (
  28. !shallowEqualImmutable(this.props, nextProps) ||
  29. !shallowEqualImmutable(this.state, nextState)
  30. )
  31. }
  32. componentDidMount() {
  33. this.unsubscribe = newEventStores.listen(this.onEventChange)
  34. }
  35. componentWillUnmount() {
  36. this.unsubscribe()
  37. }
  38. componentDidUpdate(preProps) {
  39. if (preProps.bid !== this.props.bid) {
  40. this.setState({
  41. option: this.getLoopOption()
  42. })
  43. }
  44. }
  45. onEventChange(status) {
  46. let obj = {}
  47. if (status && status.types) {
  48. if (
  49. status.types.includes('curEventNodeId') &&
  50. this.curEventNodeId !== status.data.curEventNodeId
  51. ) {
  52. this.curEventNodeId = status.data.curEventNodeId
  53. }
  54. if (
  55. status.types.includes('updateBlockProp') &&
  56. this.props.bid === status.data.affectBlockId &&
  57. status.data.prop === 'op'
  58. ) {
  59. let option = this.getLoopOption()
  60. if (this.state.option !== option) {
  61. obj.option = option
  62. }
  63. }
  64. }
  65. if (Object.keys(obj).length > 0) {
  66. this.setState(obj)
  67. }
  68. }
  69. getLoopOption(bid) {
  70. let _bid = bid || this.props.bid
  71. let nodeId = this.props.eventNodeId || this.curEventNodeId
  72. let block = getEventBlockByBid(_bid, nodeId)
  73. if (block) {
  74. return block.op || BLOCK_LOOP_TYPE.FOR
  75. }
  76. return BLOCK_LOOP_TYPE.FOR
  77. }
  78. getOptionName() {
  79. let option = this.state.option
  80. return i18n.t('EventView.jenga_loop_block.' + option)
  81. }
  82. onSelectOption(result) {
  83. if (result) {
  84. let option = result.key
  85. this.setState(
  86. {
  87. option: option
  88. },
  89. () => {
  90. newEventActions.updateBlockProp({
  91. blockId: this.props.bid,
  92. prop: 'op',
  93. value: option,
  94. trigger: true,
  95. addRecord: true
  96. })
  97. }
  98. )
  99. }
  100. }
  101. loopOptions() {
  102. return loopOptions.slice(0, 2)
  103. }
  104. render() {
  105. let loopOptsMenu = () => {
  106. let curLoopOptions = this.loopOptions()
  107. return (
  108. <Menu className="loop-option-menu" onClick={this.onSelectOption}>
  109. {!curLoopOptions || curLoopOptions.length === 0
  110. ? null
  111. : curLoopOptions.map(opt => {
  112. return (
  113. <MenuItem
  114. key={opt}
  115. className={cls({
  116. selected: this.state.option === opt
  117. })}
  118. >
  119. {i18n.t('EventView.jenga_loop_block.' + opt)}
  120. </MenuItem>
  121. )
  122. })}
  123. </Menu>
  124. )
  125. }
  126. return (
  127. <div className="loop-option f--hlc">
  128. <BlockEnable
  129. bid={this.props.bid}
  130. isEnable={this.props.isEnable}
  131. eventNodeId={this.props.eventNodeId}
  132. />
  133. <Dropdown
  134. trigger={['click']}
  135. getPopupContainer={triggerNode => triggerNode.parentNode}
  136. overlayClassName={'loop-option-dropdown'}
  137. overlay={loopOptsMenu()}
  138. >
  139. <div className="loop-select f--hlc">
  140. <input
  141. value={this.getOptionName()}
  142. readOnly={true}
  143. className="ant-dropdown-input"
  144. placeholder={i18n.t('EventView.jenga_loop_block.selectLoop')}
  145. />
  146. <div className="ant-dropdown-icon" />
  147. </div>
  148. </Dropdown>
  149. </div>
  150. )
  151. }
  152. }