IotMessageConfig.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React from 'react'
  2. import cls from 'classnames'
  3. import i18n from 'i18next'
  4. import treeActions from 'actions/tree'
  5. import widgetStore from 'stores/widget'
  6. import { getEventNodeById } from 'stores/funcs/newEvent'
  7. export default class IotMessageConfig extends React.Component {
  8. constructor(props) {
  9. super(props)
  10. this.state = {
  11. groupId: this.getGroupId(),
  12. releaseGroupId: this.getReleaseGroupId(),
  13. topics: this.getTopics()
  14. }
  15. // this.msgData = undefined
  16. this.prefix = '${ProductKey}/${deviceName}/'
  17. this.getGroupId = this.getGroupId.bind(this)
  18. this.getTopics = this.getTopics.bind(this)
  19. this.getTitleName = this.getTitleName.bind(this)
  20. this.onChangeGroupId = this.onChangeGroupId.bind(this)
  21. this.onAddTopic = this.onAddTopic.bind(this)
  22. this.onDeleteTopic = this.onDeleteTopic.bind(this)
  23. this.onChangeTopic = this.onChangeTopic.bind(this)
  24. this.genAddButton = this.genAddButton.bind(this)
  25. this.genTopic = this.genTopic.bind(this)
  26. }
  27. componentDidUpdate(preProps) {
  28. if (preProps.nodeId !== this.props.nodeId) {
  29. this.setState({
  30. groupId: this.getGroupId(),
  31. releaseGroupId: this.getReleaseGroupId(),
  32. topics: this.getTopics()
  33. })
  34. }
  35. }
  36. getGroupId() {
  37. // 通过config获取
  38. let groupId = ''
  39. let node = getEventNodeById(this.props.nodeId)
  40. if (node) {
  41. groupId = node.props.groupId || groupId
  42. }
  43. return groupId
  44. }
  45. getReleaseGroupId() {
  46. // 通过config获取
  47. let groupId = ''
  48. let node = getEventNodeById(this.props.nodeId)
  49. if (node) {
  50. groupId = node.props.releaseGroupId || groupId
  51. }
  52. return groupId
  53. }
  54. getTopics() {
  55. let topics = []
  56. let node = getEventNodeById(this.props.nodeId)
  57. if (node) {
  58. topics = node.props.topics || topics
  59. }
  60. return topics
  61. }
  62. getTitleName() {
  63. let title = ''
  64. let node = getEventNodeById(this.props.nodeId)
  65. if (node && widgetStore.widget[node.type]) {
  66. let map = widgetStore.widget[node.type].map
  67. title = map.locale ? map.locale[i18n.language] : map.name
  68. }
  69. return title ? title + i18n.t('EventView.jenga_extra.defination') : title
  70. }
  71. onChangeGroupId(applyChange, release, e) {
  72. let value = e && e.target ? e.target.value : e
  73. let obj = {}
  74. if (release) {
  75. obj.releaseGroupId = value
  76. } else {
  77. obj.groupId = value
  78. }
  79. this.setState(obj, () => {
  80. if (applyChange) {
  81. treeActions.changeNodeProps({
  82. nodeId: this.props.nodeId,
  83. props: obj
  84. })
  85. }
  86. })
  87. }
  88. onAddTopic() {
  89. let obj = {}
  90. let topics = this.state.topics
  91. topics.push({
  92. name: 'topic' + (this.state.topics.length + 1),
  93. def: this.prefix + ''
  94. })
  95. obj.topics = topics
  96. this.setState(obj, () => {
  97. treeActions.changeNodeProps({
  98. nodeId: this.props.nodeId,
  99. props: obj
  100. })
  101. })
  102. }
  103. onDeleteTopic(index) {
  104. let obj = {}
  105. let topics = this.state.topics
  106. topics.splice(index, 1)
  107. obj.topics = topics
  108. this.setState(obj, () => {
  109. treeActions.changeNodeProps({
  110. nodeId: this.props.nodeId,
  111. props: obj
  112. })
  113. })
  114. }
  115. onChangeTopic(index, param, applyChange, e) {
  116. let value = e && e.target ? e.target.value : e
  117. let obj = {}
  118. let topics = this.state.topics
  119. topics[index][param] = value
  120. obj.topics = topics
  121. let nodeId = this.props.nodeId
  122. this.setState(obj, () => {
  123. if (applyChange) {
  124. treeActions.changeNodeProps({
  125. nodeId: nodeId,
  126. props: obj
  127. })
  128. }
  129. })
  130. }
  131. genAddButton() {
  132. return (
  133. <button className="btn-clear btn-add-param" onClick={this.onAddTopic}>
  134. <div className="icon">
  135. <span className="horizon" />
  136. <span className="vertical" />
  137. </div>
  138. </button>
  139. )
  140. }
  141. genTopic(topic, index) {
  142. let length = this.state.topics.length
  143. let extraBtn = length - 1 === index ? this.genAddButton() : null
  144. return (
  145. <div
  146. className={cls('param-item f--hlc', {
  147. 'param-item-left': index % 2 !== 0,
  148. 'param-item-bottom': length > 2 && index < length - (length % 2 || 2)
  149. })}
  150. key={index}
  151. >
  152. <div className="item-input-wrap f--hlc flex-1">
  153. <div className="item-inner-wrap name-wrap f--hlc">
  154. <div className="item-name item-inner-name">
  155. {i18n.t('EventView.jenga_extra.topicName')}
  156. </div>
  157. <input
  158. value={topic.name}
  159. onChange={this.onChangeTopic.bind(this, index, 'name', false)}
  160. onBlur={this.onChangeTopic.bind(this, index, 'name', true)}
  161. />
  162. </div>
  163. <div className="item-inner-wrap def-wrap f--hlc">
  164. <div className="item-name item-inner-name">
  165. {i18n.t('EventView.jenga_extra.topicDef')}
  166. </div>
  167. {/*<div className="def-prefix">{this.prefix}</div>*/}
  168. <input
  169. title={topic.def}
  170. value={topic.def}
  171. className="value"
  172. onChange={this.onChangeTopic.bind(this, index, 'def', false)}
  173. onBlur={this.onChangeTopic.bind(this, index, 'def', true)}
  174. />
  175. </div>
  176. <button
  177. className="btn-clear btn-delete-param"
  178. onClick={this.onDeleteTopic.bind(this, index)}
  179. />
  180. </div>
  181. {extraBtn}
  182. </div>
  183. )
  184. }
  185. render() {
  186. return (
  187. <div className="jenga-iot-message-config">
  188. <div className="config-wrap f--hlc">
  189. <div className="title">{i18n.t('EventView.jenga_extra.groupId')}</div>
  190. <input
  191. className="groupId-input"
  192. value={this.state.groupId}
  193. onChange={this.onChangeGroupId.bind(this, false, false)}
  194. onBlur={this.onChangeGroupId.bind(this, true, false)}
  195. />
  196. <div className="title next">
  197. {i18n.t('EventView.jenga_extra.releaseGroupId')}
  198. </div>
  199. <input
  200. className="groupId-input"
  201. value={this.state.releaseGroupId}
  202. onChange={this.onChangeGroupId.bind(this, false, true)}
  203. onBlur={this.onChangeGroupId.bind(this, true, true)}
  204. />
  205. </div>
  206. <div
  207. className={cls(
  208. 'params',
  209. this.state.topics.length > 0 ? 'f--h' : 'f--hlc'
  210. )}
  211. >
  212. <div
  213. className={cls(
  214. 'params-title',
  215. this.state.topics.length > 2 ? 'params-title-top f--h' : 'f--hlc'
  216. )}
  217. >
  218. {i18n.t('EventView.jenga_extra.topics')}
  219. </div>
  220. <div className="main-wrap f--hlc">
  221. {this.state.topics.length === 0
  222. ? this.genAddButton('in')
  223. : this.state.topics.map((topic, index) => {
  224. return this.genTopic(topic, index)
  225. })}
  226. </div>
  227. </div>
  228. <div className="params-title params-def-title">
  229. {this.getTitleName()}
  230. </div>
  231. </div>
  232. )
  233. }
  234. }