JengaItemEvent.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import React from 'react'
  2. import LazyLoad from 'react-lazyload'
  3. import newEventStores from 'stores/newEvent'
  4. import BlockEvent from '../block/BlockEvent'
  5. import { getEventNodeById } from 'stores/funcs/newEvent'
  6. import { shallowEqualImmutable } from 'utils/utils'
  7. export default class JengaItemEvent extends React.Component {
  8. constructor(props) {
  9. super(props)
  10. this.curEventNodeId = newEventStores.curEventNodeId
  11. this.state = {
  12. eventList: this.getEventList()
  13. }
  14. this.onEventChange = this.onEventChange.bind(this)
  15. this.isCurEventNodeId = this.isCurEventNodeId.bind(this)
  16. this.getEventList = this.getEventList.bind(this)
  17. this.getBlockRoot = this.getBlockRoot.bind(this)
  18. }
  19. shouldComponentUpdate(nextProps, nextState) {
  20. return (
  21. !shallowEqualImmutable(this.props, nextProps) ||
  22. !shallowEqualImmutable(this.state, nextState)
  23. )
  24. }
  25. componentDidMount() {
  26. this.unsubscribe = newEventStores.listen(this.onEventChange)
  27. }
  28. componentWillUnmount() {
  29. this.unsubscribe()
  30. }
  31. componentDidUpdate(preProps) {
  32. if (preProps.eventNodeId !== this.props.eventNodeId) {
  33. this.setState({
  34. eventList: this.getEventList()
  35. })
  36. }
  37. }
  38. onEventChange(status) {
  39. let obj = {}
  40. let forceUpdate = false
  41. if (status) {
  42. if (
  43. status.types.includes('curEventNodeId') &&
  44. this.curEventNodeId !== status.data.curEventNodeId
  45. ) {
  46. this.curEventNodeId = status.data.curEventNodeId
  47. if (this.isCurEventNodeId(status.data.curEventNodeId)) {
  48. obj.eventList = this.getEventList()
  49. forceUpdate = true
  50. }
  51. }
  52. if (
  53. status.types.includes('changeEventList') &&
  54. status.data &&
  55. this.curEventNodeId === status.data.curEventNodeId
  56. ) {
  57. if (this.isCurEventNodeId(status.data.curEventNodeId)) {
  58. obj.eventList = this.getEventList()
  59. forceUpdate = true
  60. }
  61. }
  62. }
  63. if (Object.keys(obj).length > 0) {
  64. this.setState(obj, () => {
  65. if (forceUpdate) {
  66. // eventList是对象,所以shouldComponentUpdate不会被触发,需强制更新
  67. this.forceUpdate()
  68. }
  69. })
  70. }
  71. }
  72. isCurEventNodeId(curEventNodeId) {
  73. return !this.props.eventNodeId || this.props.eventNodeId === curEventNodeId
  74. }
  75. getEventList() {
  76. let nodeId = this.props.eventNodeId || this.curEventNodeId
  77. if (nodeId) {
  78. let node = getEventNodeById(nodeId)
  79. if (node) {
  80. return node.events.list
  81. }
  82. }
  83. return []
  84. }
  85. getBlockRoot(event, index) {
  86. return (
  87. <BlockEvent
  88. key={index}
  89. layer={0}
  90. groupLayer={0}
  91. nestGroupLayer={0}
  92. offspringInNestGroupLayer={0}
  93. bid={event.eventId}
  94. eventNodeId={this.props.eventNodeId}
  95. visibleBlockIds={this.props.visibleBlockIds}
  96. isLast={index === this.state.eventList.length - 1}
  97. />
  98. )
  99. }
  100. render() {
  101. let jengaContent = document.getElementsByClassName('jenga-content')[0]
  102. if (this.props.visibleBlockIds) {
  103. return this.state.eventList.map((event, index) => {
  104. if (
  105. event.eventId &&
  106. this.props.visibleBlockIds.indexOf(event.eventId) >= 0
  107. ) {
  108. // 加lazyload是为了避免load太多
  109. return (
  110. <LazyLoad
  111. key={index}
  112. height={200}
  113. overflow={true}
  114. scrollContainer={jengaContent}
  115. >
  116. {this.getBlockRoot(event, index)}
  117. </LazyLoad>
  118. )
  119. } else {
  120. return null
  121. }
  122. })
  123. }
  124. return this.state.eventList.map((event, index) => {
  125. if (event.eventId) {
  126. // 加lazyload是为了避免load太多
  127. return (
  128. <LazyLoad
  129. key={index}
  130. height={200}
  131. overflow={true}
  132. scrollContainer={jengaContent}
  133. >
  134. {this.getBlockRoot(event, index)}
  135. </LazyLoad>
  136. )
  137. } else {
  138. return null
  139. }
  140. })
  141. }
  142. }