TimerServiceParams.jsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import React from 'react'
  2. import { Switch, Tooltip } from 'antd'
  3. import i18n from 'i18next'
  4. import cls from 'classnames'
  5. import treeActions from 'actions/tree'
  6. import uiActions from 'actions/ui'
  7. import widgetStore from 'stores/widget'
  8. import treeStores from 'stores/tree'
  9. import { getEventNodeById } from 'stores/funcs/newEvent'
  10. import { getDate } from 'utils/commons/getDate'
  11. import { isHySa } from 'utils/commons/ctrlHide'
  12. export default class TimerServiceParams extends React.Component {
  13. constructor(props) {
  14. super(props)
  15. this.state = {
  16. params: this.getParams(),
  17. defaultParams: [
  18. {
  19. name: 'param1',
  20. value: 'now'
  21. },
  22. {
  23. name: 'param2',
  24. value: 'finishTimes'
  25. },
  26. {
  27. name: 'param3',
  28. value: 'workerIndex'
  29. },
  30. {
  31. name: 'param4',
  32. value: 'workerCount'
  33. }
  34. ],
  35. title: this.getTitleName()
  36. }
  37. this.onChangeParam = this.onChangeParam.bind(this)
  38. this.onBlurParam = this.onBlurParam.bind(this)
  39. this.dealPeriodValue = this.dealPeriodValue.bind(this)
  40. this.onToggleRecordsPanel = this.onToggleRecordsPanel.bind(this)
  41. this.getParams = this.getParams.bind(this)
  42. this.getTitleName = this.getTitleName.bind(this)
  43. this.genInput = this.genInput.bind(this)
  44. this.genDefaultParams = this.genDefaultParams.bind(this)
  45. }
  46. componentDidUpdate(preProps) {
  47. if (preProps.nodeId !== this.props.nodeId) {
  48. this.setState({
  49. params: this.getParams(),
  50. title: this.getTitleName()
  51. })
  52. }
  53. }
  54. onChangeNoAuto(propName, index) {
  55. let obj = {}
  56. let newParams = JSON.parse(JSON.stringify(this.state.params))
  57. let param = newParams[index]
  58. param.value = !param.value
  59. obj[propName] = param.value
  60. this.setState(
  61. {
  62. params: newParams
  63. },
  64. () => {
  65. treeActions.changeNodeProps({
  66. nodeId: this.props.nodeId,
  67. props: obj
  68. })
  69. }
  70. )
  71. }
  72. onChangeParam(propName, index, e) {
  73. let value = e && e.target ? e.target.value : e
  74. let param = this.state.params[index]
  75. param.value = value
  76. this.setState({
  77. params: this.state.params
  78. })
  79. }
  80. onBlurParam(propName, index, e) {
  81. let obj = {}
  82. let value = e && e.target ? e.target.value : e
  83. let param = this.state.params[index]
  84. if (propName === 'startTime') {
  85. let regExp = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/
  86. if (!regExp.test(value)) {
  87. value = getDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
  88. }
  89. } else {
  90. // 对企业id为10000586作特殊处理
  91. value = parseFloat(value)
  92. if (isNaN(value) || value < 0) {
  93. // 用回对象原来的值
  94. let node = getEventNodeById(this.props.nodeId)
  95. value = node.props[propName] || 0
  96. if (propName === 'period') {
  97. // 最小时间间隔:60s
  98. value =
  99. value < 60
  100. ? treeStores.curCaseInfo.eid === 10000586
  101. ? value
  102. : 60
  103. : value
  104. } else if (propName === 'repeatTimes') {
  105. value = value > 9999999 ? 9999999 : value
  106. } else if (propName === 'workers') {
  107. // 并发线程大于等于1,小于100,默认1
  108. value = value === 0 ? 1 : value
  109. }
  110. } else {
  111. if (propName === 'period') {
  112. // 最小时间间隔:60s
  113. // value =
  114. // value < 1
  115. // ? treeStores.curCaseInfo.eid === 10000586
  116. // ? value * 60
  117. // : 60
  118. // : value * 60
  119. value =
  120. value < 60
  121. ? treeStores.curCaseInfo.eid === 10000586
  122. ? value
  123. : 60
  124. : value
  125. } else if (propName === 'repeatTimes') {
  126. value = value > 9999999 ? 9999999 : value
  127. } else if (propName === 'workers') {
  128. value = parseInt(value)
  129. // 并发线程大于等于1,小于100,默认1
  130. value = value === 0 ? 1 : value > 100 ? 100 : value
  131. }
  132. }
  133. }
  134. if (propName === 'period') {
  135. param.value = this.dealPeriodValue(value)
  136. obj[propName] = this.dealPeriodValue(value)
  137. } else {
  138. param.value = value
  139. obj[propName] = value
  140. }
  141. this.setState(
  142. {
  143. params: this.state.params
  144. },
  145. () => {
  146. treeActions.changeNodeProps({
  147. nodeId: this.props.nodeId,
  148. props: obj
  149. })
  150. }
  151. )
  152. }
  153. dealPeriodValue(value) {
  154. // let newValue = parseFloat(value / 60)
  155. // if (!isNaN(newValue)) {
  156. // // newValue小于1则自动变为最多2位小数
  157. // if (newValue < 1) {
  158. // newValue = parseFloat((value / 60).toFixed(2))
  159. // }
  160. // }
  161. // return isNaN(newValue) ? 1 : newValue
  162. let newValue = parseInt(value)
  163. return newValue
  164. }
  165. onToggleRecordsPanel() {
  166. uiActions.toggleServicePanel({ show: true, nodeId: this.props.nodeId })
  167. }
  168. getParams() {
  169. let params = []
  170. let node = getEventNodeById(this.props.nodeId)
  171. if (node && widgetStore.widget[node.type]) {
  172. widgetStore.widget[node.type].map.props.forEach(prop => {
  173. let value =
  174. node.props[prop.name] === undefined
  175. ? prop.name === 'noAuto'
  176. ? false
  177. : ''
  178. : node.props[prop.name]
  179. if (prop.name === 'period') {
  180. value = this.dealPeriodValue(value)
  181. }
  182. params.push({
  183. name: prop.name,
  184. value: value,
  185. locale: prop.locale,
  186. desc: prop.desc,
  187. showDes: prop.showDes,
  188. placeholder: prop.placeholder
  189. })
  190. })
  191. }
  192. return params
  193. }
  194. getTitleName() {
  195. let title = ''
  196. let node = getEventNodeById(this.props.nodeId)
  197. if (node && widgetStore.widget[node.type]) {
  198. let map = widgetStore.widget[node.type].map
  199. title = map.locale ? map.locale[i18n.language] : map.name
  200. }
  201. return title ? title + i18n.t('EventView.jenga_extra.defination') : title
  202. }
  203. genInput(param, index) {
  204. let { desc, showDes } = param
  205. let tipContent = desc && desc[i18n.language]
  206. return (
  207. <div
  208. className={cls('param-item f--hlc', {
  209. 'param-item-left': index % 3 !== 0,
  210. 'param-item-bottom': index < 3
  211. })}
  212. key={index}
  213. >
  214. <div className="item-name">
  215. {param.locale ? param.locale[i18n.language] : param.name}
  216. </div>
  217. {!isHySa() && showDes && showDes === 1 && (
  218. <Tooltip
  219. title={tipContent}
  220. placement="right"
  221. destroyTooltipOnHide={true}
  222. >
  223. <div className="service-param-help">
  224. <div className="ih5-icon-help" />
  225. </div>
  226. </Tooltip>
  227. )}
  228. {index === 3 ? (
  229. <div className="item-input-wrap default-input-wrap f--hlc flex-1">
  230. <Switch
  231. checked={param.value}
  232. onChange={this.onChangeNoAuto.bind(this, param.name, index)}
  233. className="no-auto-switch"
  234. />
  235. </div>
  236. ) : (
  237. <div className="item-input-wrap default-input-wrap f--hlc flex-1">
  238. <input
  239. value={param.value}
  240. onChange={this.onChangeParam.bind(this, param.name, index)}
  241. onBlur={this.onBlurParam.bind(this, param.name, index)}
  242. />
  243. {index === 0 || index === 4 ? null : (
  244. <div className="input-postfix">
  245. {i18n.t(
  246. 'EventView.jenga_extra.postfix_' +
  247. (index === 2 ? 'times' : 'mins')
  248. )}
  249. </div>
  250. )}
  251. </div>
  252. )}
  253. </div>
  254. )
  255. }
  256. genDefaultParams(param, index) {
  257. return (
  258. <div
  259. className={cls('param-item f--hlc', {
  260. 'param-item-left': index % 3 !== 0
  261. })}
  262. key={index}
  263. >
  264. <div className="item-name">
  265. {i18n.t('EventView.jenga_extra.param') + (index + 1)}
  266. </div>
  267. <div className="item-input-wrap default-input-wrap f--hlc flex-1">
  268. <input
  269. className="default"
  270. readOnly={true}
  271. defaultValue={i18n.t('EventView.jenga_extra.' + param.value)}
  272. />
  273. </div>
  274. </div>
  275. )
  276. }
  277. render() {
  278. return (
  279. <div className="jenga-timer-service-params">
  280. <div className={cls('params f--h')}>
  281. <div className={cls('params-title f--hlc')}>
  282. {i18n.t('EventView.jenga_extra.serviceParams')}
  283. </div>
  284. <div className="main-wrap f--hlc">
  285. {this.state.params.map((param, index) => {
  286. return this.genInput(param, index)
  287. })}
  288. </div>
  289. </div>
  290. <div className={cls('params f--h')}>
  291. <div className={cls('params-title f--hlc')}>
  292. {i18n.t('EventView.jenga_extra.defaultParams')}
  293. </div>
  294. <div className="main-wrap f--hlc">
  295. {this.state.defaultParams.map((param, index) => {
  296. return this.genDefaultParams(param, index)
  297. })}
  298. <div className="record-break f--hcc">
  299. <div className="break" />
  300. </div>
  301. <button
  302. className="btn-clear btn-records"
  303. onClick={this.onToggleRecordsPanel}
  304. >
  305. {i18n.t('EventView.jenga_extra.recordsPanel')}
  306. </button>
  307. </div>
  308. </div>
  309. <div className="params-title params-def-title">
  310. {this.getTitleName()}
  311. </div>
  312. </div>
  313. )
  314. }
  315. }