The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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