NodeParams.jsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import React from 'react'
  2. import i18n from 'i18next'
  3. import cls from 'classnames'
  4. import treeActions from 'actions/tree'
  5. import widgetStore from 'stores/widget'
  6. import newEventStores from 'stores/newEvent'
  7. import { getEventNodeById } from 'stores/funcs/newEvent'
  8. import { nodeIsDyTransaction } from 'stores/funcs/nodeType'
  9. import { isValidParamName } from 'stores/funcs/event/eventUtils'
  10. export default class NodeParams extends React.Component {
  11. constructor(props) {
  12. super(props)
  13. this.state = {
  14. inParams: this.getInParams(),
  15. outParams: this.getOutParams(),
  16. title: this.getTitleName(),
  17. paramWarning: undefined
  18. }
  19. this.reloadAction = this.reloadAction.bind(this)
  20. this.onAddParam = this.onAddParam.bind(this)
  21. this.getInParams = this.getInParams.bind(this)
  22. this.getOutParams = this.getOutParams.bind(this)
  23. this.hasNoOutParams = this.hasNoOutParams.bind(this)
  24. this.getInParamsName = this.getInParamsName.bind(this)
  25. this.getOutParamsName = this.getOutParamsName.bind(this)
  26. this.getTitleName = this.getTitleName.bind(this)
  27. this.genAddButton = this.genAddButton.bind(this)
  28. this.genInput = this.genInput.bind(this)
  29. this.genError = this.genError.bind(this)
  30. }
  31. componentDidUpdate(preProps) {
  32. if (preProps.nodeId !== this.props.nodeId) {
  33. this.setState({
  34. inParams: this.getInParams(),
  35. outParams: this.getOutParams(),
  36. title: this.getTitleName(),
  37. paramWarning: undefined
  38. })
  39. }
  40. }
  41. reloadAction() {
  42. newEventStores.reloadAction(this.props.nodeId)
  43. }
  44. onAddParam(type) {
  45. let obj = { paramWarning: undefined }
  46. let params = undefined
  47. switch (type) {
  48. case 'in':
  49. params = this.state.inParams
  50. params.push('param' + (this.state.inParams.length + 1))
  51. obj.inParams = params
  52. break
  53. case 'out':
  54. params = this.state.outParams
  55. params.push('param' + (this.state.outParams.length + 1))
  56. obj.outParams = params
  57. break
  58. }
  59. this.setState(obj, () => {
  60. delete obj.paramWarning
  61. treeActions.changeNodeProps({
  62. nodeId: this.props.nodeId,
  63. props: obj
  64. })
  65. this.reloadAction()
  66. })
  67. }
  68. onDeleteParam(index, type) {
  69. let obj = { paramWarning: undefined }
  70. let params = undefined
  71. switch (type) {
  72. case 'in':
  73. params = this.state.inParams
  74. params.splice(index, 1)
  75. obj.inParams = params
  76. break
  77. case 'out':
  78. params = this.state.outParams
  79. params.splice(index, 1)
  80. obj.outParams = params
  81. break
  82. }
  83. this.setState(obj, () => {
  84. delete obj.paramWarning
  85. treeActions.changeNodeProps({
  86. nodeId: this.props.nodeId,
  87. props: obj
  88. })
  89. this.reloadAction()
  90. })
  91. }
  92. onChangeParam(index, type, applyChange, e) {
  93. let value = e && e.target ? e.target.value : e
  94. let obj = {}
  95. let params = undefined
  96. // 处理有问题字段
  97. if (applyChange && !isValidParamName(value)) {
  98. let errorValue = value
  99. if (value !== 'detail') {
  100. // outParam为detail时,不对其强行修改,只给出提示.其余情况将值改为paramX
  101. value = 'param' + (index + 1)
  102. }
  103. obj.paramWarning = {
  104. error: 'paramError',
  105. type: type,
  106. index,
  107. value: errorValue
  108. }
  109. } else {
  110. obj.paramWarning = undefined
  111. }
  112. switch (type) {
  113. case 'in':
  114. params = this.state.inParams
  115. params[index] = value
  116. obj.inParams = params
  117. break
  118. case 'out':
  119. params = this.state.outParams
  120. params[index] = value
  121. obj.outParams = params
  122. break
  123. }
  124. let nodeId = this.props.nodeId
  125. this.setState(obj, () => {
  126. if (applyChange) {
  127. delete obj.paramWarning
  128. treeActions.changeNodeProps({
  129. nodeId: nodeId,
  130. props: obj
  131. })
  132. this.reloadAction()
  133. }
  134. })
  135. }
  136. getInParams() {
  137. let inParams = []
  138. let node = getEventNodeById(this.props.nodeId)
  139. if (node) {
  140. inParams = node.props.inParams || inParams
  141. }
  142. return inParams
  143. }
  144. getOutParams() {
  145. let outParams = []
  146. let node = getEventNodeById(this.props.nodeId)
  147. if (node) {
  148. outParams = node.props.outParams || outParams
  149. }
  150. return outParams
  151. }
  152. hasNoOutParams() {
  153. let node = getEventNodeById(this.props.nodeId)
  154. return nodeIsDyTransaction(node && node.type)
  155. }
  156. getInParamsName() {
  157. let title = ''
  158. let node = getEventNodeById(this.props.nodeId)
  159. if (node && widgetStore.widget[node.type]) {
  160. let prop = widgetStore.widget[node.type].map.propsMap['inParams']
  161. title = prop.locale ? prop.locale[i18n.language] : prop.name
  162. }
  163. return title + ':'
  164. }
  165. getOutParamsName() {
  166. let title = ''
  167. let node = getEventNodeById(this.props.nodeId)
  168. if (node && widgetStore.widget[node.type]) {
  169. let prop = widgetStore.widget[node.type].map.propsMap['outParams']
  170. title = prop.locale ? prop.locale[i18n.language] : prop.name
  171. }
  172. return title + ':'
  173. }
  174. getTitleName() {
  175. let title = ''
  176. let node = getEventNodeById(this.props.nodeId)
  177. if (node && widgetStore.widget[node.type]) {
  178. let map = widgetStore.widget[node.type].map
  179. title = map.locale ? map.locale[i18n.language] : map.name
  180. }
  181. return title ? title + i18n.t('EventView.jenga_extra.defination') : title
  182. }
  183. genAddButton(type) {
  184. return (
  185. <button
  186. className="btn-clear btn-add-param"
  187. onClick={this.onAddParam.bind(this, type)}
  188. >
  189. <div className="icon">
  190. <span className="horizon" />
  191. <span className="vertical" />
  192. </div>
  193. </button>
  194. )
  195. }
  196. genInput(value, index, type) {
  197. let length =
  198. type === 'in' ? this.state.inParams.length : this.state.outParams.length
  199. let extraBtn = length - 1 === index ? this.genAddButton(type) : null
  200. return (
  201. <div
  202. className={cls('param-item f--hlc', {
  203. 'param-item-left': index % 3 !== 0,
  204. 'param-item-bottom': length > 3 && index < length - (length % 3 || 3)
  205. })}
  206. key={index}
  207. >
  208. <div className="item-name">
  209. {i18n.t('EventView.jenga_extra.param') + (index + 1)}
  210. </div>
  211. <div className="item-input-wrap f--hlc flex-1">
  212. <input
  213. value={value}
  214. onChange={this.onChangeParam.bind(this, index, type, false)}
  215. onBlur={this.onChangeParam.bind(this, index, type, true)}
  216. />
  217. <button
  218. className="btn-clear btn-delete-param"
  219. onClick={this.onDeleteParam.bind(this, index, type)}
  220. />
  221. </div>
  222. {extraBtn}
  223. </div>
  224. )
  225. }
  226. genError(type) {
  227. return this.state.paramWarning && this.state.paramWarning.type === type ? (
  228. <div className="param-warning f--hlc">
  229. {'* ' +
  230. i18n.t('EventView.jenga_extra.param') +
  231. (this.state.paramWarning.index + 1) +
  232. i18n.t('EventView.jenga_extra.setAs') +
  233. '"' +
  234. this.state.paramWarning.value +
  235. '"' +
  236. ',' +
  237. i18n.t('EventView.jenga_extra.' + this.state.paramWarning.error)}
  238. </div>
  239. ) : null
  240. }
  241. render() {
  242. return (
  243. <div className="jenga-node-params">
  244. <div
  245. className={cls(
  246. 'params params-in',
  247. this.state.inParams.length > 0 ? 'f--h' : 'f--hlc'
  248. )}
  249. >
  250. <div
  251. className={cls(
  252. 'params-title params-in-title',
  253. this.state.inParams.length > 3
  254. ? 'params-title-top f--h'
  255. : 'f--hlc'
  256. )}
  257. >
  258. {this.getInParamsName()}
  259. </div>
  260. <div>
  261. <div className="main-wrap f--hlc">
  262. {this.state.inParams.length === 0
  263. ? this.genAddButton('in')
  264. : this.state.inParams.map((param, index) => {
  265. return this.genInput(param, index, 'in')
  266. })}
  267. </div>
  268. {this.genError('in')}
  269. </div>
  270. </div>
  271. {this.hasNoOutParams() ? null : (
  272. <div
  273. className={cls(
  274. 'params params-out',
  275. this.state.outParams.length > 0 ? 'f--h' : 'f--hlc'
  276. )}
  277. >
  278. <div
  279. className={cls(
  280. 'params-title params-out-title',
  281. this.state.outParams.length > 3
  282. ? 'params-title-top f--h'
  283. : 'f--hlc'
  284. )}
  285. >
  286. {this.getOutParamsName()}
  287. </div>
  288. <div>
  289. <div className="main-wrap f--hlc">
  290. {this.state.outParams.length === 0
  291. ? this.genAddButton('out')
  292. : this.state.outParams.map((param, index) => {
  293. return this.genInput(param, index, 'out')
  294. })}
  295. </div>
  296. {this.genError('out')}
  297. </div>
  298. </div>
  299. )}
  300. <div className="params-title params-def-title">
  301. {this.getTitleName()}
  302. </div>
  303. </div>
  304. )
  305. }
  306. }