JengaNavNavigateNodes.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react'
  2. import treeActions from 'actions/tree'
  3. import newEventActions from 'actions/newEvent'
  4. import newEventStores from 'stores/newEvent'
  5. export default class JengaNavNavigateNodes extends React.Component {
  6. constructor(props) {
  7. super(props)
  8. this.state = {
  9. curNavIds: newEventStores.curNavIds,
  10. curNavId: newEventStores.curNavId
  11. }
  12. this.onEventChange = this.onEventChange.bind(this)
  13. this.curNavIdIndex = this.curNavIdIndex.bind(this)
  14. this.onClick = this.onClick.bind(this)
  15. this.isAllow = this.isAllow.bind(this)
  16. this.allowBackward = this.allowBackward.bind(this)
  17. this.allowForward = this.allowForward.bind(this)
  18. }
  19. componentDidMount() {
  20. this.unsubscribe = newEventStores.listen(this.onEventChange)
  21. }
  22. componentWillUnmount() {
  23. this.unsubscribe()
  24. }
  25. onEventChange(status) {
  26. let obj = {}
  27. if (status && status.types) {
  28. if (status.types.includes('curNavIds')) {
  29. obj.curNavIds = status.data.curNavIds
  30. }
  31. if (status.types.includes('curNavId')) {
  32. if (this.state.curNavId !== status.data.curNavId) {
  33. obj.curNavId = status.data.curNavId
  34. }
  35. }
  36. }
  37. if (Object.keys(obj).length > 0) {
  38. this.setState(obj)
  39. this.forceUpdate()
  40. }
  41. }
  42. curNavIdIndex() {
  43. let { curNavIds, curNavId } = this.state
  44. return curNavIds.findIndex(v => {
  45. return v.id === curNavId
  46. })
  47. }
  48. onClick(type) {
  49. let index = this.curNavIdIndex()
  50. let { curNavIds } = this.state
  51. let navInfo = undefined
  52. switch (type) {
  53. case 'backward':
  54. navInfo = curNavIds[index - 1]
  55. break
  56. case 'forward':
  57. navInfo = curNavIds[index + 1]
  58. break
  59. }
  60. if (navInfo) {
  61. treeActions.selectNode(navInfo.nodeId)
  62. newEventActions.toggleEventView({
  63. show: true,
  64. nodeId: navInfo.nodeId,
  65. mode: navInfo.mode,
  66. navId: navInfo.id
  67. })
  68. }
  69. }
  70. isAllow() {
  71. let { curNavIds, curNavId } = this.state
  72. return !!(curNavIds && curNavIds.length > 0 && curNavId)
  73. }
  74. allowBackward() {
  75. return this.isAllow() && !([-1, 0].indexOf(this.curNavIdIndex()) >= 0)
  76. }
  77. allowForward() {
  78. let { curNavIds } = this.state
  79. return (
  80. this.isAllow() &&
  81. !([-1, curNavIds.length - 1].indexOf(this.curNavIdIndex()) >= 0)
  82. )
  83. }
  84. render() {
  85. return (
  86. <div className="jenga-nav-navigate-nodes-wrap f--hlc">
  87. <button
  88. className="btn-clear btn-navigate-backward"
  89. disabled={!this.allowBackward()}
  90. onClick={this.onClick.bind(this, 'backward')}
  91. />
  92. <button
  93. className="btn-clear btn-navigate-forward"
  94. disabled={!this.allowForward()}
  95. onClick={this.onClick.bind(this, 'forward')}
  96. />
  97. </div>
  98. )
  99. }
  100. }