| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import React from 'react'
- import LazyLoad from 'react-lazyload'
- import newEventStores from 'stores/newEvent'
- import { getEventNodeById } from 'stores/funcs/newEvent'
- import { shallowEqualImmutable } from 'utils/utils'
- import BlockEvent from '../block/BlockEvent'
- export default class JengaItemEvent extends React.Component {
- constructor(props) {
- super(props)
- this.curEventNodeId = newEventStores.curEventNodeId
- this.state = {
- eventList: this.getEventList()
- }
- this.onEventChange = this.onEventChange.bind(this)
- this.isCurEventNodeId = this.isCurEventNodeId.bind(this)
- this.getEventList = this.getEventList.bind(this)
- this.getBlockRoot = this.getBlockRoot.bind(this)
- }
- shouldComponentUpdate(nextProps, nextState) {
- return (
- !shallowEqualImmutable(this.props, nextProps) ||
- !shallowEqualImmutable(this.state, nextState)
- )
- }
- componentDidMount() {
- this.unsubscribe = newEventStores.listen(this.onEventChange)
- }
- componentWillUnmount() {
- this.unsubscribe()
- }
- componentDidUpdate(preProps) {
- if (preProps.eventNodeId !== this.props.eventNodeId) {
- this.setState({
- eventList: this.getEventList()
- })
- }
- }
- onEventChange(status) {
- let obj = {}
- let forceUpdate = false
- if (status) {
- if (
- status.types.includes('curEventNodeId') &&
- this.curEventNodeId !== status.data.curEventNodeId
- ) {
- this.curEventNodeId = status.data.curEventNodeId
- if (this.isCurEventNodeId(status.data.curEventNodeId)) {
- obj.eventList = this.getEventList()
- forceUpdate = true
- }
- }
- if (
- status.types.includes('changeEventList') &&
- status.data &&
- this.curEventNodeId === status.data.curEventNodeId
- ) {
- if (this.isCurEventNodeId(status.data.curEventNodeId)) {
- obj.eventList = this.getEventList()
- forceUpdate = true
- }
- }
- }
- if (Object.keys(obj).length > 0) {
- this.setState(obj, () => {
- if (forceUpdate) {
- // eventList是对象,所以shouldComponentUpdate不会被触发,需强制更新
- this.forceUpdate()
- }
- })
- }
- }
- isCurEventNodeId(curEventNodeId) {
- return !this.props.eventNodeId || this.props.eventNodeId === curEventNodeId
- }
- getEventList() {
- let nodeId = this.props.eventNodeId || this.curEventNodeId
- if (nodeId) {
- let node = getEventNodeById(nodeId)
- if (node) {
- return node.events.list
- }
- }
- return []
- }
- getBlockRoot(event, index) {
- return (
- <BlockEvent
- key={index}
- layer={0}
- groupLayer={0}
- nestGroupLayer={0}
- offspringInNestGroupLayer={0}
- bid={event.eventId}
- eventNodeId={this.props.eventNodeId}
- visibleBlockIds={this.props.visibleBlockIds}
- isLast={index === this.state.eventList.length - 1}
- />
- )
- }
- render() {
- let jengaContent = document.getElementsByClassName('jenga-content')[0]
- if (this.props.visibleBlockIds) {
- return this.state.eventList.map((event, index) => {
- if (
- event.eventId &&
- this.props.visibleBlockIds.indexOf(event.eventId) >= 0
- ) {
- // 加lazyload是为了避免load太多
- return (
- <LazyLoad
- key={index}
- height={200}
- overflow={true}
- scrollContainer={jengaContent}
- >
- {this.getBlockRoot(event, index)}
- </LazyLoad>
- )
- } else {
- return null
- }
- })
- }
- return this.state.eventList.map((event, index) => {
- if (event.eventId) {
- // 加lazyload是为了避免load太多
- return (
- <LazyLoad
- key={index}
- height={200}
- overflow={true}
- scrollContainer={jengaContent}
- >
- {this.getBlockRoot(event, index)}
- </LazyLoad>
- )
- } else {
- return null
- }
- })
- }
- }
|