ConItem.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.onDeleteCon = this.onDeleteCon.bind(this)
  27. this.isSelectedFlag = this.isSelectedFlag.bind(this)
  28. this.isSelectedOperator = this.isSelectedOperator.bind(this)
  29. this.getConItem = this.getConItem.bind(this)
  30. this.getConItemFlagName = this.getConItemFlagName.bind(this)
  31. this.getConItemOperatorName = this.getConItemOperatorName.bind(this)
  32. this.getOptionName = this.getOptionName.bind(this)
  33. }
  34. shouldComponentUpdate(nextProps, nextState) {
  35. return (
  36. !shallowEqualImmutable(this.props, nextProps) ||
  37. !shallowEqualImmutable(this.state, nextState)
  38. )
  39. }
  40. componentDidMount() {
  41. this.unsubscribe = newEventStores.listen(this.onEventChange)
  42. this.unsubscribeUI = uiStore.listen(this.onUiChange)
  43. }
  44. componentWillUnmount() {
  45. this.unsubscribe()
  46. this.unsubscribeUI()
  47. }
  48. componentDidUpdate(preProps) {
  49. let conItem = this.getConItem()
  50. if (!is(this.state.conItem, conItem) || preProps.bid !== this.props.bid) {
  51. this.setState({
  52. conItem: conItem
  53. })
  54. }
  55. }
  56. onEventChange(status) {
  57. let obj = {}
  58. if (status && status.types) {
  59. if (
  60. status.types.includes('updateBlockProp') &&
  61. this.props.bid === status.data.affectBlockId &&
  62. status.data.prop === 'cons'
  63. ) {
  64. this.forceUpdate()
  65. }
  66. }
  67. if (Object.keys(obj).length > 0) {
  68. this.setState(obj)
  69. }
  70. }
  71. onUiChange(status) {
  72. if (status && status.types) {
  73. if (status.types.includes('changeJengaViewWidthDelta')) {
  74. if (status.data.jengaViewWidthDelta !== this.state.widthDelta) {
  75. this.setState({
  76. widthDelta: status.data.jengaViewWidthDelta
  77. })
  78. }
  79. }
  80. }
  81. }
  82. onToggleEnable() {
  83. this.onChangeCon('enable', !this.state.conItem.enable)
  84. }
  85. onChangeValue(prop, value) {
  86. this.onChangeCon(prop, value, false)
  87. }
  88. onChangeFlag(option) {
  89. if (option && option.key) {
  90. let value = option.key
  91. this.onChangeCon('flag', value)
  92. }
  93. }
  94. onChangeOperator(option) {
  95. if (option && option.key) {
  96. let value = option.key
  97. this.onChangeCon('operator', value)
  98. }
  99. }
  100. onAddCon() {
  101. this.props.onAddBlockCon()
  102. this.props.onConsChange()
  103. }
  104. onDeleteCon() {
  105. this.props.onDelBlockCon({ index: this.props.index, resetLast: true })
  106. this.props.onConsChange()
  107. }
  108. onChangeCon(prop, value, forceUpdate = true) {
  109. let con = this.state.conItem
  110. con[prop] = value
  111. this.props.onConsChange()
  112. }
  113. isSelectedFlag(flag) {
  114. return this.state.conItem ? flag === this.state.conItem.flag : false
  115. }
  116. isSelectedOperator(operator) {
  117. return this.state.conItem ? operator === this.state.conItem.operator : false
  118. }
  119. getConItem() {
  120. return this.props.con || {}
  121. }
  122. getConItemFlagName() {
  123. return this.state.conItem && this.state.conItem.flag
  124. ? i18n.t('EventView.jenga_cons.' + this.state.conItem.flag)
  125. : ''
  126. }
  127. getConItemOperatorName() {
  128. return this.state.conItem && this.state.conItem.operator
  129. ? i18n.t('EventView.jenga_cons.' + this.state.conItem.operator)
  130. : ''
  131. }
  132. getOptionName(option) {
  133. return i18n.t('EventView.jenga_cons.' + option)
  134. }
  135. formulaExtraParams() {
  136. if (
  137. this.state.conItem &&
  138. ['match', 'notMatch'].indexOf(this.state.conItem.operator) >= 0
  139. ) {
  140. return getConSpecialParams()
  141. } else {
  142. return undefined
  143. }
  144. }
  145. render() {
  146. return null
  147. }
  148. }