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.

166 lines
4.6 KiB

  1. import ColumnsArea from './components/columns_area';
  2. import NotificationsContainer from './containers/notifications_container';
  3. import PropTypes from 'prop-types';
  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. class UI extends React.PureComponent {
  18. constructor (props, context) {
  19. super(props, context);
  20. this.state = {
  21. width: window.innerWidth,
  22. draggingOver: false
  23. };
  24. this.handleResize = this.handleResize.bind(this);
  25. this.handleDragEnter = this.handleDragEnter.bind(this);
  26. this.handleDragOver = this.handleDragOver.bind(this);
  27. this.handleDrop = this.handleDrop.bind(this);
  28. this.handleDragLeave = this.handleDragLeave.bind(this);
  29. this.handleDragEnd = this.handleDragLeave.bind(this)
  30. this.closeUploadModal = this.closeUploadModal.bind(this)
  31. this.setRef = this.setRef.bind(this);
  32. }
  33. @debounce(500)
  34. handleResize () {
  35. this.setState({ width: window.innerWidth });
  36. }
  37. handleDragEnter (e) {
  38. e.preventDefault();
  39. if (!this.dragTargets) {
  40. this.dragTargets = [];
  41. }
  42. if (this.dragTargets.indexOf(e.target) === -1) {
  43. this.dragTargets.push(e.target);
  44. }
  45. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  46. this.setState({ draggingOver: true });
  47. }
  48. }
  49. handleDragOver (e) {
  50. e.preventDefault();
  51. e.stopPropagation();
  52. try {
  53. e.dataTransfer.dropEffect = 'copy';
  54. } catch (err) {
  55. }
  56. return false;
  57. }
  58. handleDrop (e) {
  59. e.preventDefault();
  60. this.setState({ draggingOver: false });
  61. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  62. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  63. }
  64. }
  65. handleDragLeave (e) {
  66. e.preventDefault();
  67. e.stopPropagation();
  68. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  69. if (this.dragTargets.length > 0) {
  70. return;
  71. }
  72. this.setState({ draggingOver: false });
  73. }
  74. closeUploadModal() {
  75. this.setState({ draggingOver: false });
  76. }
  77. componentWillMount () {
  78. window.addEventListener('resize', this.handleResize, { passive: true });
  79. document.addEventListener('dragenter', this.handleDragEnter, false);
  80. document.addEventListener('dragover', this.handleDragOver, false);
  81. document.addEventListener('drop', this.handleDrop, false);
  82. document.addEventListener('dragleave', this.handleDragLeave, false);
  83. document.addEventListener('dragend', this.handleDragEnd, false);
  84. this.props.dispatch(refreshTimeline('home'));
  85. this.props.dispatch(refreshNotifications());
  86. }
  87. componentWillUnmount () {
  88. window.removeEventListener('resize', this.handleResize);
  89. document.removeEventListener('dragenter', this.handleDragEnter);
  90. document.removeEventListener('dragover', this.handleDragOver);
  91. document.removeEventListener('drop', this.handleDrop);
  92. document.removeEventListener('dragleave', this.handleDragLeave);
  93. document.removeEventListener('dragend', this.handleDragEnd);
  94. }
  95. setRef (c) {
  96. this.node = c;
  97. }
  98. render () {
  99. const { width, draggingOver } = this.state;
  100. const { children } = this.props;
  101. let mountedColumns;
  102. if (isMobile(width)) {
  103. mountedColumns = (
  104. <ColumnsArea>
  105. {children}
  106. </ColumnsArea>
  107. );
  108. } else {
  109. mountedColumns = (
  110. <ColumnsArea>
  111. <Compose withHeader={true} />
  112. <HomeTimeline shouldUpdateScroll={() => false} />
  113. <Notifications shouldUpdateScroll={() => false} />
  114. <div style={{display: 'flex', flex: '1 1 auto', position: 'relative'}}>{children}</div>
  115. </ColumnsArea>
  116. );
  117. }
  118. return (
  119. <div className='ui' ref={this.setRef}>
  120. <TabsBar />
  121. {mountedColumns}
  122. <NotificationsContainer />
  123. <LoadingBarContainer className="loading-bar" />
  124. <ModalContainer />
  125. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  126. </div>
  127. );
  128. }
  129. }
  130. UI.propTypes = {
  131. dispatch: PropTypes.func.isRequired,
  132. children: PropTypes.node
  133. };
  134. export default connect()(UI);