Buttons.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import { Icon } from 'antd'
  8. import classnames from 'classnames'
  9. import Agent from '../Agent'
  10. import styles from './Buttons.less'
  11. export default class Buttons extends React.Component {
  12. constructor (props) {
  13. super(props)
  14. this.state = {
  15. top_toggle_on: true,
  16. search_on: false
  17. }
  18. this.on_ids = []
  19. }
  20. static btnAdd () {
  21. Agent.emit('add_hosts')
  22. }
  23. btnToggle () {
  24. let doToggle = () => {
  25. let on = !this.state.top_toggle_on
  26. Agent.emit('top_toggle', on, this.on_ids, (e) => {
  27. if (e) {
  28. console.log(e)
  29. return
  30. }
  31. this.setState({
  32. top_toggle_on: on
  33. }, () => {
  34. if (this.state.top_toggle_on) {
  35. this.on_ids = []
  36. }
  37. })
  38. })
  39. }
  40. if (this.state.top_toggle_on) {
  41. Agent.emit('get_on_hosts', (ids) => {
  42. this.on_ids = ids
  43. doToggle()
  44. })
  45. } else {
  46. doToggle()
  47. }
  48. }
  49. btnSearch () {
  50. this.setState({
  51. search_on: !this.state.search_on
  52. }, () => {
  53. Agent.emit(this.state.search_on ? 'search:start' : 'search:end')
  54. })
  55. }
  56. cancelSearch () {
  57. this.setState({
  58. search_on: false
  59. }, () => {
  60. Agent.emit('search_off')
  61. })
  62. }
  63. componentDidMount () {
  64. Agent.on('to_search', () => {
  65. this.btnSearch()
  66. })
  67. Agent.on('esc', () => {
  68. if (this.state.search_on) {
  69. this.btnSearch()
  70. }
  71. })
  72. Agent.on('cancel_search', () => this.setState({search_on: false}))
  73. }
  74. render () {
  75. return (
  76. <div id="sh-buttons" className={styles.root}>
  77. <div className={styles.left}>
  78. <a
  79. className={styles['btn-add']}
  80. href="#"
  81. onClick={() => Buttons.btnAdd()}
  82. >
  83. <Icon type="plus"/>
  84. </a>
  85. </div>
  86. <div className={styles.right}>
  87. <Icon
  88. type="search"
  89. className={classnames({
  90. on: this.state.search_on
  91. })}
  92. onClick={() => this.btnSearch()}
  93. />
  94. {/*<i*/}
  95. {/* className={classnames({*/}
  96. {/* iconfont: 1,*/}
  97. {/* 'icon-switchon': this.state.top_toggle_on,*/}
  98. {/* 'icon-switchoff': !this.state.top_toggle_on*/}
  99. {/* })}*/}
  100. {/* onClick={() => this.btnToggle()}*/}
  101. {/*/>*/}
  102. <Icon type="setting" onClick={() => Agent.emit('show_preferences')}/>
  103. </div>
  104. </div>
  105. )
  106. }
  107. }