list_item.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 {kw2re} from '../../libs/kw';
  9. import './list_item.less';
  10. export default class ListItem extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.is_sys = !!this.props.sys;
  14. this.state = {
  15. is_selected: false,
  16. search_kw: '',
  17. search_re: null,
  18. // on: this.props.data.on,
  19. };
  20. SH_event.on('search', (kw) => {
  21. this.setState({
  22. search_kw: kw,
  23. search_re: kw ? kw2re(kw) : null
  24. });
  25. });
  26. ipcRenderer.on('tray_toggle_hosts', (e, idx) => {
  27. // ipcRenderer.send('send_host_list', this.state.list);
  28. // this.toggleOne(idx);
  29. if (idx === this.props.idx) {
  30. this.toggle();
  31. }
  32. });
  33. }
  34. getTitle() {
  35. return this.is_sys ? SH_Agent.lang.sys_host_title : this.props.data.title || SH_Agent.lang.untitled;
  36. }
  37. beSelected() {
  38. // this.setState({
  39. // is_selected: true
  40. // });
  41. this.props.selectOne(this.props.data);
  42. }
  43. toEdit() {
  44. SH_event.emit('edit_host', this.props.data);
  45. }
  46. toggle() {
  47. let on = !this.props.data.on;
  48. this.props.onToggle(() => {
  49. this.props.data.on = on;
  50. this.forceUpdate();
  51. });
  52. SH_event.emit('toggle_host', on);
  53. }
  54. allowedDrop(e) {
  55. e.preventDefault();
  56. }
  57. onDrop(e) {
  58. if (this.props.sys) {
  59. e.preventDefault();
  60. return false;
  61. }
  62. let source_idx = parseInt(e.dataTransfer.getData('text'));
  63. this.props.dragOrder(source_idx, this.props.idx);
  64. }
  65. onDrag(e) {
  66. e.dataTransfer.setData('text', this.props.idx);
  67. }
  68. isMatched() {
  69. if (this.props.sys) return true;
  70. let kw = this.state.search_kw;
  71. let re = this.state.search_re;
  72. if (!kw || kw === '/') return true;
  73. let {title, content} = this.props.data;
  74. if (re) {
  75. return re.test(title) || re.test(content);
  76. } else {
  77. return title.indexOf(kw) > -1 || content.indexOf(kw) > -1;
  78. }
  79. }
  80. render() {
  81. let {data, sys, current} = this.props;
  82. let is_selected = data == current;
  83. return (
  84. <div className={classnames({
  85. 'list-item': 1
  86. , 'hidden': !this.isMatched()
  87. , 'sys-host': sys
  88. , 'selected': is_selected
  89. })}
  90. onClick={this.beSelected.bind(this)}
  91. draggable={!sys}
  92. onDragStart={(e) => this.onDrag(e)}
  93. onDragOver={(e) => this.allowedDrop(e)}
  94. onDrop={(e) => this.onDrop(e)}
  95. >
  96. { sys ? null :
  97. (
  98. <div>
  99. <i className={classnames({
  100. 'switch': 1
  101. , 'iconfont': 1
  102. , 'icon-on': data.on
  103. , 'icon-off': !data.on
  104. })}
  105. onClick={this.toggle.bind(this)}
  106. />
  107. <i
  108. className="iconfont icon-edit"
  109. onClick={this.toEdit.bind(this)}
  110. />
  111. </div>
  112. )
  113. }
  114. <i className={classnames({
  115. 'iconfont': 1
  116. , 'item-icon': 1
  117. , 'icon-warn': !!data.error
  118. , 'icon-file': !sys && !data.error && data.where !== 'group'
  119. , 'icon-files': data.where === 'group'
  120. , 'icon-sysserver': sys && !data.error
  121. })}
  122. title={data.error || ''}
  123. />
  124. <span>{this.getTitle()}</span>
  125. </div>
  126. );
  127. }
  128. }