闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.9 KiB

  1. import ColumnsArea from './components/columns_area';
  2. import NotificationsContainer from './containers/notifications_container';
  3. import PureRenderMixin from 'react-addons-pure-render-mixin';
  4. import LoadingBarContainer from './containers/loading_bar_container';
  5. import HomeTimeline from '../home_timeline';
  6. import Compose from '../compose';
  7. import TabsBar from './components/tabs_bar';
  8. import ModalContainer from './containers/modal_container';
  9. import Notifications from '../notifications';
  10. import { connect } from 'react-redux';
  11. import { isMobile } from '../../is_mobile';
  12. import { debounce } from 'react-decoration';
  13. import { uploadCompose } from '../../actions/compose';
  14. import { refreshTimeline } from '../../actions/timelines';
  15. import { refreshNotifications } from '../../actions/notifications';
  16. import UploadArea from './components/upload_area';
  17. const UI = React.createClass({
  18. propTypes: {
  19. dispatch: React.PropTypes.func.isRequired,
  20. children: React.PropTypes.node
  21. },
  22. getInitialState () {
  23. return {
  24. width: window.innerWidth,
  25. draggingOver: false
  26. };
  27. },
  28. mixins: [PureRenderMixin],
  29. @debounce(500)
  30. handleResize () {
  31. this.setState({ width: window.innerWidth });
  32. },
  33. handleDragEnter (e) {
  34. e.preventDefault();
  35. if (!this.dragTargets) {
  36. this.dragTargets = [];
  37. }
  38. if (this.dragTargets.indexOf(e.target) === -1) {
  39. this.dragTargets.push(e.target);
  40. }
  41. if (e.dataTransfer && e.dataTransfer.files.length > 0) {
  42. this.setState({ draggingOver: true });
  43. }
  44. },
  45. handleDragOver (e) {
  46. e.preventDefault();
  47. e.stopPropagation();
  48. try {
  49. e.dataTransfer.dropEffect = 'copy';
  50. } catch (err) {
  51. }
  52. return false;
  53. },
  54. handleDrop (e) {
  55. e.preventDefault();
  56. this.setState({ draggingOver: false });
  57. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  58. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  59. }
  60. },
  61. handleDragLeave (e) {
  62. e.preventDefault();
  63. e.stopPropagation();
  64. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  65. if (this.dragTargets.length > 0) {
  66. return;
  67. }
  68. this.setState({ draggingOver: false });
  69. },
  70. componentWillMount () {
  71. window.addEventListener('resize', this.handleResize, { passive: true });
  72. document.addEventListener('dragenter', this.handleDragEnter, false);
  73. document.addEventListener('dragover', this.handleDragOver, false);
  74. document.addEventListener('drop', this.handleDrop, false);
  75. document.addEventListener('dragleave', this.handleDragLeave, false);
  76. this.props.dispatch(refreshTimeline('home'));
  77. this.props.dispatch(refreshNotifications());
  78. },
  79. componentWillUnmount () {
  80. window.removeEventListener('resize', this.handleResize);
  81. document.removeEventListener('dragenter', this.handleDragEnter);
  82. document.removeEventListener('dragover', this.handleDragOver);
  83. document.removeEventListener('drop', this.handleDrop);
  84. document.removeEventListener('dragleave', this.handleDragLeave);
  85. },
  86. setRef (c) {
  87. this.node = c;
  88. },
  89. render () {
  90. const { width, draggingOver } = this.state;
  91. const { children } = this.props;
  92. let mountedColumns;
  93. if (isMobile(width)) {
  94. mountedColumns = (
  95. <ColumnsArea>
  96. {children}
  97. </ColumnsArea>
  98. );
  99. } else {
  100. mountedColumns = (
  101. <ColumnsArea>
  102. <Compose withHeader={true} />
  103. <HomeTimeline trackScroll={false} />
  104. <Notifications trackScroll={false} />
  105. {children}
  106. </ColumnsArea>
  107. );
  108. }
  109. return (
  110. <div className='ui' ref={this.setRef}>
  111. <TabsBar />
  112. {mountedColumns}
  113. <NotificationsContainer />
  114. <LoadingBarContainer style={{ backgroundColor: '#2b90d9', left: '0', top: '0' }} />
  115. <ModalContainer />
  116. <UploadArea active={draggingOver} />
  117. </div>
  118. );
  119. }
  120. });
  121. export default connect()(UI);