buttons.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 './buttons.less';
  9. export default class Buttons extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. top_toggle_on: true,
  14. search_on: false
  15. };
  16. this.on_items = null;
  17. SH_event.on('toggle_host', (on) => {
  18. if (on && !this.state.top_toggle_on) {
  19. this.setState({
  20. top_toggle_on: true
  21. });
  22. this.on_items = null;
  23. }
  24. });
  25. SH_event.on('cancel_search', () => {
  26. this.calcelSearch();
  27. });
  28. ipcRenderer.on('to_add_host', () => {
  29. SH_event.emit('add_host');
  30. });
  31. }
  32. static btnAdd() {
  33. SH_event.emit('add_host');
  34. }
  35. btnToggle() {
  36. if (this.state.top_toggle_on) {
  37. SH_event.emit('get_on_hosts', (items) => {
  38. this.on_items = items;
  39. });
  40. }
  41. this.setState({
  42. top_toggle_on: !this.state.top_toggle_on
  43. }, () => {
  44. SH_event.emit('top_toggle', this.state.top_toggle_on, this.on_items);
  45. if (this.state.top_toggle_on) {
  46. this.on_items = null;
  47. }
  48. });
  49. }
  50. btnSearch() {
  51. this.setState({
  52. search_on: !this.state.search_on
  53. }, () => {
  54. SH_event.emit(this.state.search_on ? 'search_on' : 'search_off');
  55. });
  56. }
  57. calcelSearch() {
  58. this.setState({
  59. search_on: false
  60. }, () => {
  61. SH_event.emit('search_off');
  62. });
  63. }
  64. componentDidMount() {
  65. ipcRenderer.on('to_search', () => {
  66. this.btnSearch();
  67. });
  68. }
  69. render() {
  70. return (
  71. <div id="sh-buttons">
  72. <div className="left">
  73. <a
  74. className="btn-add"
  75. href="#"
  76. onClick={() => Buttons.btnAdd()}
  77. >+</a>
  78. </div>
  79. <div className="right">
  80. <i
  81. className={classnames({
  82. iconfont: 1,
  83. 'icon-search': 1,
  84. 'on': this.state.search_on
  85. })}
  86. onClick={() => this.btnSearch()}
  87. />
  88. <i
  89. className={classnames({
  90. iconfont: 1,
  91. 'icon-switchon': this.state.top_toggle_on,
  92. 'icon-switchoff': !this.state.top_toggle_on
  93. })}
  94. onClick={() => this.btnToggle()}
  95. />
  96. </div>
  97. </div>
  98. );
  99. }
  100. }