BlockEventTrigger.jsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React from 'react'
  2. import i18n from 'i18next'
  3. import cls from 'classnames'
  4. import { Menu, Dropdown, Tooltip } from 'antd'
  5. import newEventActions from 'actions/newEvent'
  6. import newEventStores from 'stores/newEvent'
  7. import widgetStore from 'stores/widget'
  8. import { shallowEqualImmutable } from 'utils/utils'
  9. import { getEventBlockByBid, getEventNodeById } from 'stores/funcs/newEvent'
  10. import { getModuleInstanceNameLocale } from 'src/stores/funcs/event/methodUtils'
  11. import { getTriggerEvents } from 'stores/funcs/event/eventUtils'
  12. import { isHySa } from 'utils/commons/ctrlHide'
  13. import BlockEnable from '../BlockEnable'
  14. import iconStrObj from 'src/assets/icons/iconsStr'
  15. const MenuItem = Menu.Item
  16. export default class BlockEventTrigger extends React.Component {
  17. constructor(props) {
  18. super(props)
  19. this.curEventNodeId = newEventStores.curEventNodeId
  20. this.state = {
  21. events: this.getEvents(),
  22. trigger: this.getTrigger().name,
  23. mini: this.getTrigger().mini
  24. }
  25. this.onEventChange = this.onEventChange.bind(this)
  26. this.getEvents = this.getEvents.bind(this)
  27. this.getTrigger = this.getTrigger.bind(this)
  28. this.onSelectTrigger = this.onSelectTrigger.bind(this)
  29. this.getTriggerName = this.getTriggerName.bind(this)
  30. this.getTipInfo = this.getTipInfo.bind(this)
  31. }
  32. shouldComponentUpdate(nextProps, nextState) {
  33. return (
  34. !shallowEqualImmutable(this.props, nextProps) ||
  35. !shallowEqualImmutable(this.state, nextState)
  36. )
  37. }
  38. componentDidMount() {
  39. this.unsubscribe = newEventStores.listen(this.onEventChange)
  40. }
  41. componentWillUnmount() {
  42. this.unsubscribe()
  43. }
  44. componentDidUpdate(preProps) {
  45. if (preProps.bid !== this.props.bid) {
  46. this.setState({
  47. events: this.getEvents(),
  48. trigger: this.getTrigger().name,
  49. mini: this.getTrigger().mini
  50. })
  51. }
  52. }
  53. onEventChange(status) {
  54. let obj = {}
  55. if (status) {
  56. if (
  57. status.types.includes('curEventNodeId') &&
  58. this.curEventNodeId !== status.data.curEventNodeId
  59. ) {
  60. this.curEventNodeId = status.data.curEventNodeId
  61. }
  62. if (
  63. status.types.includes('updateBlockProp') &&
  64. this.props.bid === status.data.affectBlockId &&
  65. status.data.prop === 'trigger'
  66. ) {
  67. this.setState({
  68. trigger: this.getTrigger().name,
  69. mini: this.getTrigger().mini
  70. })
  71. }
  72. }
  73. if (Object.keys(obj).length > 0) {
  74. this.setState(obj)
  75. }
  76. }
  77. onSelectTrigger(option) {
  78. if (option.key === 'emptyContent') {
  79. return
  80. }
  81. let name = option.key
  82. let trigger = this.state.trigger
  83. if (trigger === name) {
  84. return
  85. }
  86. this.setState({
  87. trigger: name
  88. })
  89. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  90. newEventActions.updateBlockProp({
  91. nodeId: curNodeId,
  92. blockId: this.props.bid,
  93. prop: 'trigger',
  94. value: name
  95. })
  96. }
  97. getEvents() {
  98. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  99. return getTriggerEvents(curNodeId)
  100. }
  101. getTrigger() {
  102. let result = {}
  103. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  104. let block = getEventBlockByBid(this.props.bid, curNodeId)
  105. if (block && block.eventId) {
  106. result['name'] = block.name
  107. result['mini'] = !!block.mini
  108. }
  109. return result
  110. }
  111. getTriggerName() {
  112. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  113. let curEventNode = getEventNodeById(curNodeId)
  114. let nodeInfo = curEventNode
  115. ? widgetStore.widget[curEventNode.type]
  116. : undefined
  117. let name = this.state.trigger || ''
  118. if (curEventNode) {
  119. let isMini = this.state.mini
  120. if (isMini) {
  121. name = getModuleInstanceNameLocale({
  122. nodeId: curEventNode.id,
  123. type: 'events',
  124. name: name
  125. })
  126. } else {
  127. if (nodeInfo && nodeInfo.map && nodeInfo.map.eventsMap) {
  128. name =
  129. nodeInfo.map.eventsMap[name] && nodeInfo.map.eventsMap[name].locale
  130. ? nodeInfo.map.eventsMap[name].locale[i18n.language]
  131. : name
  132. }
  133. }
  134. }
  135. return name
  136. }
  137. getTipInfo(item) {
  138. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  139. let objNode = getEventNodeById(curNodeId)
  140. let type = objNode && objNode.type
  141. if (type && item) {
  142. let stopPro = e => {
  143. e.stopPropagation()
  144. }
  145. let info = widgetStore.widget[type].map.eventsMap[item.name]
  146. let content = info && info.desc && info.desc[i18n.language]
  147. let link =
  148. i18n.language === 'en' ? info && info.linkEN : info && info.linkZH
  149. content = content ? (
  150. <div onClick={stopPro}>
  151. {content}
  152. {info && link ? (
  153. <a className="a-tag" href={link} target="_blank">
  154. {i18n.t('Tips.more')}
  155. </a>
  156. ) : (
  157. undefined
  158. )}
  159. </div>
  160. ) : (
  161. undefined
  162. )
  163. return (
  164. (!isHySa() && info && content && (
  165. <Tooltip
  166. title={content}
  167. placement="right"
  168. destroyTooltipOnHide={true}
  169. >
  170. <div className="block-map-help">
  171. <div
  172. className="ih5-icon-help"
  173. dangerouslySetInnerHTML={{ __html: iconStrObj.help }}
  174. />
  175. </div>
  176. </Tooltip>
  177. )) ||
  178. null
  179. )
  180. } else {
  181. return null
  182. }
  183. }
  184. render() {
  185. let eventTriggerMenu = () => {
  186. return (
  187. <Menu className="event-trigger-menu" onClick={this.onSelectTrigger}>
  188. {!this.state.events || this.state.events.length === 0 ? (
  189. <MenuItem key={'emptyContent'} className="empty-content">
  190. {i18n.t('EventView.jenga_event_block.emptyTrigger')}
  191. </MenuItem>
  192. ) : (
  193. this.state.events.map(event => {
  194. return (
  195. <MenuItem
  196. key={event.name}
  197. className={cls({
  198. selected: this.state.trigger === event.name,
  199. last: event.last
  200. })}
  201. >
  202. <div className="f--hlc">
  203. {event.locale ? event.locale[i18n.language] : event.name}
  204. {this.getTipInfo(event)}
  205. </div>
  206. </MenuItem>
  207. )
  208. })
  209. )}
  210. </Menu>
  211. )
  212. }
  213. return (
  214. <div className="event-trigger f--h">
  215. <BlockEnable
  216. bid={this.props.bid}
  217. isEnable={this.props.isEnable}
  218. eventNodeId={this.props.eventNodeId}
  219. />
  220. <div className="event-wrap">
  221. <div className="select-wrap f--hlc">
  222. <Dropdown
  223. trigger={['click']}
  224. getPopupContainer={triggerNode => triggerNode.parentNode}
  225. overlayClassName={'event-trigger-dropdown'}
  226. overlay={eventTriggerMenu()}
  227. >
  228. <div className="trigger-select f--hlc">
  229. <div
  230. className={cls('ant-dropdown-input f--hlc', {
  231. placeholder: !this.getTriggerName()
  232. })}
  233. >
  234. {this.getTriggerName() ||
  235. i18n.t('EventView.jenga_event_block.selectTrigger')}
  236. {this.getTipInfo(this.state.trigger)}
  237. </div>
  238. <div className="ant-dropdown-icon" />
  239. </div>
  240. </Dropdown>
  241. </div>
  242. </div>
  243. </div>
  244. )
  245. }
  246. }