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.

133 lines
3.4 KiB

  1. import LoadingIndicator from '../../../components/loading_indicator';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import ExtendedVideoPlayer from '../../../components/extended_video_player';
  5. import ImageLoader from 'react-imageloader';
  6. import { defineMessages, injectIntl } from 'react-intl';
  7. import IconButton from '../../../components/icon_button';
  8. const messages = defineMessages({
  9. close: { id: 'lightbox.close', defaultMessage: 'Close' }
  10. });
  11. const leftNavStyle = {
  12. position: 'absolute',
  13. background: 'rgba(0, 0, 0, 0.5)',
  14. padding: '30px 15px',
  15. cursor: 'pointer',
  16. fontSize: '24px',
  17. top: '0',
  18. left: '-61px',
  19. boxSizing: 'border-box',
  20. height: '100%',
  21. display: 'flex',
  22. alignItems: 'center'
  23. };
  24. const rightNavStyle = {
  25. position: 'absolute',
  26. background: 'rgba(0, 0, 0, 0.5)',
  27. padding: '30px 15px',
  28. cursor: 'pointer',
  29. fontSize: '24px',
  30. top: '0',
  31. right: '-61px',
  32. boxSizing: 'border-box',
  33. height: '100%',
  34. display: 'flex',
  35. alignItems: 'center'
  36. };
  37. const closeStyle = {
  38. position: 'absolute',
  39. top: '4px',
  40. right: '4px'
  41. };
  42. const MediaModal = React.createClass({
  43. propTypes: {
  44. media: ImmutablePropTypes.list.isRequired,
  45. index: React.PropTypes.number.isRequired,
  46. onClose: React.PropTypes.func.isRequired,
  47. intl: React.PropTypes.object.isRequired
  48. },
  49. getInitialState () {
  50. return {
  51. index: null
  52. };
  53. },
  54. mixins: [PureRenderMixin],
  55. handleNextClick () {
  56. this.setState({ index: (this.getIndex() + 1) % this.props.media.size});
  57. },
  58. handlePrevClick () {
  59. this.setState({ index: (this.getIndex() - 1) % this.props.media.size});
  60. },
  61. handleKeyUp (e) {
  62. switch(e.key) {
  63. case 'ArrowLeft':
  64. this.handlePrevClick();
  65. break;
  66. case 'ArrowRight':
  67. this.handleNextClick();
  68. break;
  69. }
  70. },
  71. componentDidMount () {
  72. window.addEventListener('keyup', this.handleKeyUp, false);
  73. },
  74. componentWillUnmount () {
  75. window.removeEventListener('keyup', this.handleKeyUp);
  76. },
  77. getIndex () {
  78. return this.state.index !== null ? this.state.index : this.props.index;
  79. },
  80. render () {
  81. const { media, intl, onClose } = this.props;
  82. const index = this.getIndex();
  83. const attachment = media.get(index);
  84. const url = attachment.get('url');
  85. let leftNav, rightNav, content;
  86. leftNav = rightNav = content = '';
  87. if (media.size > 1) {
  88. leftNav = <div role='button' tabIndex='0' style={leftNavStyle} className='modal-container__nav' onClick={this.handlePrevClick}><i className='fa fa-fw fa-chevron-left' /></div>;
  89. rightNav = <div role='button' tabIndex='0' style={rightNavStyle} className='modal-container__nav' onClick={this.handleNextClick}><i className='fa fa-fw fa-chevron-right' /></div>;
  90. }
  91. if (attachment.get('type') === 'image') {
  92. content = <ImageLoader src={url} imgProps={{ style: { display: 'block' } }} />;
  93. } else if (attachment.get('type') === 'gifv') {
  94. content = <ExtendedVideoPlayer src={url} muted={true} controls={false} />;
  95. }
  96. return (
  97. <div className='modal-root__modal media-modal'>
  98. {leftNav}
  99. <div>
  100. <IconButton title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} style={closeStyle} />
  101. {content}
  102. </div>
  103. {rightNav}
  104. </div>
  105. );
  106. }
  107. });
  108. export default injectIntl(MediaModal);