buttons.js 2.2 KB

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