buttons.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_items = null
  18. }
  19. static btnAdd () {
  20. Agent.emit('add_host')
  21. }
  22. btnToggle () {
  23. if (this.state.top_toggle_on) {
  24. Agent.emit('get_on_hosts', (items) => {
  25. this.on_items = items
  26. })
  27. }
  28. this.setState({
  29. top_toggle_on: !this.state.top_toggle_on
  30. }, () => {
  31. Agent.emit('top_toggle', this.state.top_toggle_on, this.on_items)
  32. if (this.state.top_toggle_on) {
  33. this.on_items = null
  34. }
  35. })
  36. }
  37. btnSearch () {
  38. this.setState({
  39. search_on: !this.state.search_on
  40. }, () => {
  41. Agent.emit(this.state.search_on ? 'search_on' : 'search_off')
  42. })
  43. }
  44. cancelSearch () {
  45. this.setState({
  46. search_on: false
  47. }, () => {
  48. Agent.emit('search_off')
  49. })
  50. }
  51. componentDidMount () {
  52. Agent.on('to_search', () => {
  53. this.btnSearch()
  54. })
  55. }
  56. render () {
  57. return (
  58. <div id="sh-buttons">
  59. <div className="left">
  60. <a
  61. className="btn-add"
  62. href="#"
  63. onClick={() => Buttons.btnAdd()}
  64. >+</a>
  65. </div>
  66. <div className="right">
  67. <i
  68. className={classnames({
  69. iconfont: 1,
  70. 'icon-search': 1,
  71. 'on': this.state.search_on
  72. })}
  73. onClick={() => this.btnSearch()}
  74. />
  75. <i
  76. className={classnames({
  77. iconfont: 1,
  78. 'icon-switchon': this.state.top_toggle_on,
  79. 'icon-switchoff': !this.state.top_toggle_on
  80. })}
  81. onClick={() => this.btnToggle()}
  82. />
  83. </div>
  84. </div>
  85. )
  86. }
  87. }