BlockConCons.jsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import React from 'react'
  2. import newEventActions from 'actions/newEvent'
  3. import treeStores from 'stores/tree'
  4. import { getEventBlockByBid, emptyCon } from 'stores/funcs/newEvent'
  5. import newEventStores from 'stores/newEvent'
  6. import { cpJson, shallowEqualImmutable } from 'utils/utils'
  7. import { BLOCK_OP_TYPE } from 'const/const'
  8. import BlockEventConItem from './BlockConConItem'
  9. export default class BlockConCons extends React.Component {
  10. constructor(props) {
  11. super(props)
  12. this.state = {
  13. cons: this.getCons()
  14. }
  15. this.onEventChange = this.onEventChange.bind(this)
  16. this.getCons = this.getCons.bind(this)
  17. this.changeConsAst = this.changeConsAst.bind(this)
  18. this.addBlockCon = this.addBlockCon.bind(this)
  19. this.delBlockCon = this.delBlockCon.bind(this)
  20. }
  21. shouldComponentUpdate(nextProps, nextState) {
  22. return (
  23. !shallowEqualImmutable(this.props, nextProps) ||
  24. !shallowEqualImmutable(this.state, nextState)
  25. )
  26. }
  27. componentDidMount() {
  28. this.unsubscribe = newEventStores.listen(this.onEventChange)
  29. }
  30. componentWillUnmount() {
  31. this.unsubscribe()
  32. }
  33. componentDidUpdate(preProps) {
  34. if (preProps.bid !== this.props.bid) {
  35. this.setState({
  36. cons: this.getCons()
  37. })
  38. }
  39. }
  40. onEventChange(status) {
  41. let obj = {}
  42. if (status && status.types) {
  43. if (
  44. status.types.includes('updateBlockProp') &&
  45. this.props.bid === status.data.affectBlockId &&
  46. status.data.prop === 'cons'
  47. ) {
  48. // 这里需要强制更新, 因为cons是个对象
  49. this.forceUpdate()
  50. }
  51. }
  52. if (Object.keys(obj).length > 0) {
  53. this.setState(obj)
  54. }
  55. }
  56. genConItem({ obj, flag }) {
  57. let result = {}
  58. result['flag'] = flag
  59. result['operator'] = obj.op
  60. result['value1'] = obj.args[0].expVal
  61. result['value2'] = obj.args[1].expVal
  62. return result
  63. }
  64. getCons() {
  65. let result = []
  66. let nodeId = this.props.eventNodeId || treeStores.curEventNodeId
  67. let block = getEventBlockByBid(this.props.bid, nodeId)
  68. if (
  69. block &&
  70. (block.op === BLOCK_OP_TYPE.IF || block.op === BLOCK_OP_TYPE.ELSE)
  71. ) {
  72. let conObj = block.args[0]
  73. if (conObj && conObj.op) {
  74. switch (conObj.op) {
  75. case 'or':
  76. let orArgs = conObj.args
  77. orArgs.forEach(orItem => {
  78. if (orItem.op === 'and') {
  79. let andArgs = orItem.args
  80. andArgs.forEach((andItem, index) => {
  81. let flag = index === 0 ? 'or' : 'and'
  82. let con = this.genConItem({ obj: andItem, flag })
  83. result.push(con)
  84. })
  85. } else {
  86. let con = this.genConItem({ obj: orItem, flag: 'or' })
  87. result.push(con)
  88. }
  89. })
  90. break
  91. case 'and':
  92. let andArgs = conObj.args
  93. andArgs.forEach(andItem => {
  94. let con = this.genConItem({ obj: andItem, flag: 'and' })
  95. result.push(con)
  96. })
  97. break
  98. default:
  99. let con = this.genConItem({ obj: conObj, flag: 'and' })
  100. result.push(con)
  101. }
  102. }
  103. }
  104. return result
  105. }
  106. genConObj(conItem) {
  107. let obj = {
  108. op: conItem.operator,
  109. args: [
  110. { op: 'var', expVal: conItem.value1, args: [] },
  111. { op: 'var', expVal: conItem.value2, args: [] }
  112. ]
  113. }
  114. return obj
  115. }
  116. changeConsAst() {
  117. // 将cons数组转为ast格式
  118. let cons = cpJson(this.state.cons)
  119. let ors = []
  120. let ands = []
  121. cons.forEach((con, index) => {
  122. if (con.flag !== 'or') {
  123. ands.push(con)
  124. } else {
  125. ors.push(ands)
  126. ands = []
  127. ands.push(con)
  128. }
  129. if (index === cons.length - 1) {
  130. ors.push(ands)
  131. }
  132. })
  133. let consAst = {}
  134. if (ors.length > 1) {
  135. // 有or
  136. consAst.op = 'or'
  137. consAst.args = []
  138. ors.forEach(orItem => {
  139. if (orItem.length > 1) {
  140. // 多个条件
  141. let andObj = {
  142. op: 'and',
  143. args: []
  144. }
  145. orItem.forEach(conItem => {
  146. let conObj = this.genConObj(conItem)
  147. andObj.args.push(conObj)
  148. })
  149. consAst.args.push(andObj)
  150. } else {
  151. // 单个条件
  152. let conObj = this.genConObj(orItem[0])
  153. consAst.args.push(conObj)
  154. }
  155. })
  156. } else {
  157. // 没有or
  158. consAst.op = 'and'
  159. consAst.args = []
  160. let andArray = ors[0]
  161. if (andArray.length > 1) {
  162. // 多个条件
  163. andArray.forEach(conItem => {
  164. let conObj = this.genConObj(conItem)
  165. consAst.args.push(conObj)
  166. })
  167. } else {
  168. // 单个条件
  169. consAst = this.genConObj(andArray[0])
  170. }
  171. }
  172. let nodeId = this.props.eventNodeId || treeStores.curEventNodeId
  173. newEventActions.updateBlockProp({
  174. nodeId,
  175. blockId: this.props.bid,
  176. prop: 'cons',
  177. value: consAst,
  178. trigger: true,
  179. addRecord: true
  180. })
  181. }
  182. addBlockCon() {
  183. let cons = this.state.cons
  184. let newEmptyCon = emptyCon()
  185. cons.push(newEmptyCon)
  186. }
  187. delBlockCon({ index, resetLast }) {
  188. let cons = this.state.cons
  189. if (resetLast && cons.length === 1) {
  190. cons[0] = emptyCon()
  191. } else {
  192. cons.splice(index, 1)
  193. }
  194. if (cons.length === 1) {
  195. cons[0].flag = 'and'
  196. }
  197. }
  198. render() {
  199. let cons = (
  200. <div className="con-items-wrap">
  201. {this.state.cons.map((con, index) => {
  202. return (
  203. <BlockEventConItem
  204. index={index}
  205. con={con}
  206. key={index}
  207. layer={this.props.layer}
  208. groupLayer={this.props.groupLayer}
  209. nestGroupLayer={this.props.nestGroupLayer}
  210. extraGapGroupLayer={this.props.extraGapGroupLayer}
  211. bid={this.props.bid}
  212. eventNodeId={this.props.eventNodeId}
  213. onConsChange={this.changeConsAst}
  214. onAddBlockCon={this.addBlockCon}
  215. onDelBlockCon={this.delBlockCon}
  216. />
  217. )
  218. })}
  219. </div>
  220. )
  221. return (
  222. <div className="con-cons flex-1">
  223. {!this.state.cons || this.state.cons.length === 0 ? null : cons}
  224. </div>
  225. )
  226. }
  227. }