BlockEventTrigger.jsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. obj.trigger = this.getTrigger().name
  68. obj.mini = this.getTrigger().mini
  69. }
  70. }
  71. if (Object.keys(obj).length > 0) {
  72. this.setState(obj)
  73. }
  74. }
  75. onSelectTrigger(option) {
  76. if (option.key === 'emptyContent') {
  77. return
  78. }
  79. let name = option.key
  80. let trigger = this.state.trigger
  81. if (trigger === name) {
  82. return
  83. }
  84. this.setState({
  85. trigger: name
  86. })
  87. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  88. newEventActions.updateBlockProp({
  89. nodeId: curNodeId,
  90. blockId: this.props.bid,
  91. prop: 'trigger',
  92. value: name
  93. })
  94. }
  95. getEvents() {
  96. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  97. return getTriggerEvents(curNodeId)
  98. }
  99. getTrigger() {
  100. let result = {}
  101. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  102. let block = getEventBlockByBid(this.props.bid, curNodeId)
  103. if (block && block.eventId) {
  104. result['name'] = block.name
  105. result['mini'] = !!block.mini
  106. }
  107. return result
  108. }
  109. getTriggerName() {
  110. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  111. let curEventNode = getEventNodeById(curNodeId)
  112. let nodeInfo = curEventNode
  113. ? widgetStore.widget[curEventNode.type]
  114. : undefined
  115. let name = this.state.trigger || ''
  116. if (curEventNode) {
  117. let isMini = this.state.mini
  118. if (isMini) {
  119. name = getModuleInstanceNameLocale({
  120. nodeId: curEventNode.id,
  121. type: 'events',
  122. name: name
  123. })
  124. } else {
  125. if (nodeInfo && nodeInfo.map && nodeInfo.map.eventsMap) {
  126. name =
  127. nodeInfo.map.eventsMap[name] && nodeInfo.map.eventsMap[name].locale
  128. ? nodeInfo.map.eventsMap[name].locale[i18n.language]
  129. : name
  130. }
  131. }
  132. }
  133. return name
  134. }
  135. getTipInfo(item) {
  136. let curNodeId = this.props.eventNodeId || this.curEventNodeId
  137. let objNode = getEventNodeById(curNodeId)
  138. let type = objNode && objNode.type
  139. if (type && item) {
  140. let stopPro = e => {
  141. e.stopPropagation()
  142. }
  143. let info = widgetStore.widget[type].map.eventsMap[item.name]
  144. let content = info && info.desc && info.desc[i18n.language]
  145. let link =
  146. i18n.language === 'en' ? info && info.linkEN : info && info.linkZH
  147. content = content ? (
  148. <div onClick={stopPro}>
  149. {content}
  150. {info && link ? (
  151. <a className="a-tag" href={link} target="_blank">
  152. {i18n.t('Tips.more')}
  153. </a>
  154. ) : (
  155. undefined
  156. )}
  157. </div>
  158. ) : (
  159. undefined
  160. )
  161. return (
  162. (!isHySa() && info && content && (
  163. <Tooltip
  164. title={content}
  165. placement="right"
  166. destroyTooltipOnHide={true}
  167. >
  168. <div className="block-map-help">
  169. <div
  170. className="ih5-icon-help"
  171. dangerouslySetInnerHTML={{ __html: iconStrObj.help }}
  172. />
  173. </div>
  174. </Tooltip>
  175. )) ||
  176. null
  177. )
  178. } else {
  179. return null
  180. }
  181. }
  182. render() {
  183. let eventTriggerMenu = () => {
  184. return (
  185. <Menu className="event-trigger-menu" onClick={this.onSelectTrigger}>
  186. {!this.state.events || this.state.events.length === 0 ? (
  187. <MenuItem key={'emptyContent'} className="empty-content">
  188. {i18n.t('EventView.jenga_event_block.emptyTrigger')}
  189. </MenuItem>
  190. ) : (
  191. this.state.events.map(event => {
  192. return (
  193. <MenuItem
  194. key={event.name}
  195. className={cls({
  196. selected: this.state.trigger === event.name,
  197. last: event.last
  198. })}
  199. >
  200. <div className="f--hlc">
  201. {event.locale ? event.locale[i18n.language] : event.name}
  202. {this.getTipInfo(event)}
  203. </div>
  204. </MenuItem>
  205. )
  206. })
  207. )}
  208. </Menu>
  209. )
  210. }
  211. return (
  212. <div className="event-trigger f--h">
  213. <BlockEnable
  214. bid={this.props.bid}
  215. isEnable={this.props.isEnable}
  216. eventNodeId={this.props.eventNodeId}
  217. />
  218. <div className="event-wrap">
  219. <div className="select-wrap f--hlc">
  220. <Dropdown
  221. trigger={['click']}
  222. getPopupContainer={triggerNode => triggerNode.parentNode}
  223. overlayClassName={'event-trigger-dropdown'}
  224. overlay={eventTriggerMenu()}
  225. >
  226. <div className="trigger-select f--hlc">
  227. <div
  228. className={cls('ant-dropdown-input f--hlc', {
  229. placeholder: !this.getTriggerName()
  230. })}
  231. >
  232. {this.getTriggerName() ||
  233. i18n.t('EventView.jenga_event_block.selectTrigger')}
  234. {this.getTipInfo(this.state.trigger)}
  235. </div>
  236. <div className="ant-dropdown-icon" />
  237. </div>
  238. </Dropdown>
  239. </div>
  240. </div>
  241. </div>
  242. )
  243. }
  244. }