buttons.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import classnames from 'classnames'
  8. import Agent from '../../../renderer/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. Agent.on('toggle_host', (on) => {
  19. if (on && !this.state.top_toggle_on) {
  20. this.setState({
  21. top_toggle_on: true
  22. })
  23. this.on_items = null
  24. }
  25. })
  26. Agent.on('cancel_search', () => {
  27. this.calcelSearch()
  28. })
  29. Agent.on('to_add_host', () => {
  30. Agent.emit('add_host')
  31. })
  32. }
  33. static btnAdd () {
  34. Agent.emit('add_host')
  35. }
  36. btnToggle () {
  37. if (this.state.top_toggle_on) {
  38. Agent.emit('get_on_hosts', (items) => {
  39. this.on_items = items
  40. })
  41. }
  42. this.setState({
  43. top_toggle_on: !this.state.top_toggle_on
  44. }, () => {
  45. Agent.emit('top_toggle', this.state.top_toggle_on, this.on_items)
  46. if (this.state.top_toggle_on) {
  47. this.on_items = null
  48. }
  49. })
  50. }
  51. btnSearch () {
  52. this.setState({
  53. search_on: !this.state.search_on
  54. }, () => {
  55. Agent.emit(this.state.search_on ? 'search_on' : 'search_off')
  56. })
  57. }
  58. calcelSearch () {
  59. this.setState({
  60. search_on: false
  61. }, () => {
  62. Agent.emit('search_off')
  63. })
  64. }
  65. componentDidMount () {
  66. Agent.on('to_search', () => {
  67. this.btnSearch()
  68. })
  69. }
  70. render () {
  71. return (
  72. <div id="sh-buttons">
  73. <div className="left">
  74. <a
  75. className="btn-add"
  76. href="#"
  77. onClick={() => Buttons.btnAdd()}
  78. >+</a>
  79. </div>
  80. <div className="right">
  81. <i
  82. className={classnames({
  83. iconfont: 1,
  84. 'icon-search': 1,
  85. 'on': this.state.search_on
  86. })}
  87. onClick={() => this.btnSearch()}
  88. />
  89. <i
  90. className={classnames({
  91. iconfont: 1,
  92. 'icon-switchon': this.state.top_toggle_on,
  93. 'icon-switchoff': !this.state.top_toggle_on
  94. })}
  95. onClick={() => this.btnToggle()}
  96. />
  97. </div>
  98. </div>
  99. )
  100. }
  101. }