ConItem.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import React from 'react'
  2. import { is } from 'immutable'
  3. import i18n from 'i18next'
  4. import newEventStores from 'stores/newEvent'
  5. import uiStore from 'stores/ui'
  6. import { conFlags, conOperatorOps } from 'const/enum'
  7. import { shallowEqualImmutable } from 'utils/utils'
  8. import { getConSpecialParams } from 'src/components/common/formulaEditor/utils'
  9. export default class ConItem extends React.Component {
  10. constructor(props) {
  11. super(props)
  12. this.state = {
  13. conItem: this.getConItem(props.index),
  14. conFlags: conFlags,
  15. conOperatorOptions: conOperatorOps,
  16. widthDelta: uiStore.jengaViewWidthDelta
  17. }
  18. this.onEventChange = this.onEventChange.bind(this)
  19. this.onUiChange = this.onUiChange.bind(this)
  20. this.onToggleEnable = this.onToggleEnable.bind(this)
  21. this.onChangeValue = this.onChangeValue.bind(this)
  22. this.onChangeFlag = this.onChangeFlag.bind(this)
  23. this.onChangeOperator = this.onChangeOperator.bind(this)
  24. this.onChangeCon = this.onChangeCon.bind(this)
  25. this.onAddCon = this.onAddCon.bind(this)
  26. this.isSelectedFlag = this.isSelectedFlag.bind(this)
  27. this.isSelectedOperator = this.isSelectedOperator.bind(this)
  28. this.getConItem = this.getConItem.bind(this)
  29. this.getConItemFlagName = this.getConItemFlagName.bind(this)
  30. this.getConItemOperatorName = this.getConItemOperatorName.bind(this)
  31. this.getOptionName = this.getOptionName.bind(this)
  32. }
  33. shouldComponentUpdate(nextProps, nextState) {
  34. return (
  35. !shallowEqualImmutable(this.props, nextProps) ||
  36. !shallowEqualImmutable(this.state, nextState)
  37. )
  38. }
  39. componentDidMount() {
  40. this.unsubscribe = newEventStores.listen(this.onEventChange)
  41. this.unsubscribeUI = uiStore.listen(this.onUiChange)
  42. }
  43. componentWillUnmount() {
  44. this.unsubscribe()
  45. this.unsubscribeUI()
  46. }
  47. componentDidUpdate(preProps) {
  48. let conItem = this.getConItem()
  49. if (!is(this.state.conItem, conItem) || preProps.bid !== this.props.bid) {
  50. this.setState({
  51. conItem: conItem
  52. })
  53. }
  54. }
  55. onEventChange(status) {
  56. let obj = {}
  57. if (status && status.types) {
  58. if (
  59. status.types.includes('updateBlockProp') &&
  60. this.props.bid === status.data.affectBlockId &&
  61. status.data.prop === 'cons'
  62. ) {
  63. this.forceUpdate()
  64. }
  65. }
  66. if (Object.keys(obj).length > 0) {
  67. this.setState(obj)
  68. }
  69. }
  70. onUiChange(status) {
  71. if (status && status.types) {
  72. if (status.types.includes('changeJengaViewWidthDelta')) {
  73. if (status.data.jengaViewWidthDelta !== this.state.widthDelta) {
  74. this.setState({
  75. widthDelta: status.data.jengaViewWidthDelta
  76. })
  77. }
  78. }
  79. }
  80. }
  81. onToggleEnable() {
  82. this.onChangeCon('enable', !this.state.conItem.enable)
  83. }
  84. onChangeValue(prop, value) {
  85. this.onChangeCon(prop, value, false)
  86. }
  87. onChangeFlag(option) {
  88. if (option && option.key) {
  89. let value = option.key
  90. this.onChangeCon('flag', value)
  91. }
  92. }
  93. onChangeOperator(option) {
  94. if (option && option.key) {
  95. let value = option.key
  96. this.onChangeCon('operator', value)
  97. }
  98. }
  99. onAddCon() {
  100. this.props.onAddBlockCon()
  101. this.props.onConsChange()
  102. }
  103. onChangeCon(prop, value, forceUpdate = true) {
  104. let con = this.state.conItem
  105. con[prop] = value
  106. this.props.onConsChange()
  107. }
  108. isSelectedFlag(flag) {
  109. return this.state.conItem ? flag === this.state.conItem.flag : false
  110. }
  111. isSelectedOperator(operator) {
  112. return this.state.conItem ? operator === this.state.conItem.operator : false
  113. }
  114. getConItem() {
  115. return this.props.con || {}
  116. }
  117. getConItemFlagName() {
  118. return this.state.conItem && this.state.conItem.flag
  119. ? i18n.t('EventView.jenga_cons.' + this.state.conItem.flag)
  120. : ''
  121. }
  122. getConItemOperatorName() {
  123. return this.state.conItem && this.state.conItem.operator
  124. ? i18n.t('EventView.jenga_cons.' + this.state.conItem.operator)
  125. : ''
  126. }
  127. getOptionName(option) {
  128. return i18n.t('EventView.jenga_cons.' + option)
  129. }
  130. formulaExtraParams() {
  131. if (
  132. this.state.conItem &&
  133. ['match', 'notMatch'].indexOf(this.state.conItem.operator) >= 0
  134. ) {
  135. return getConSpecialParams()
  136. } else {
  137. return undefined
  138. }
  139. }
  140. render() {
  141. return null
  142. }
  143. }