2017-07-12 11:02:51 +03:00
|
|
|
// Package imports //
|
2017-05-03 03:04:16 +03:00
|
|
|
import React from 'react';
|
2016-09-05 21:38:31 +03:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2017-04-21 21:05:35 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-01-16 03:26:03 +02:00
|
|
|
|
2017-07-12 11:02:51 +03:00
|
|
|
// Mastodon imports //
|
|
|
|
import { isIOS } from '../../../../mastodon/is_mobile';
|
2016-11-23 12:23:32 +02:00
|
|
|
|
2017-07-12 11:02:51 +03:00
|
|
|
export default class StatusGalleryItem extends React.PureComponent {
|
2017-03-04 23:17:10 +02:00
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
static propTypes = {
|
|
|
|
attachment: ImmutablePropTypes.map.isRequired,
|
|
|
|
index: PropTypes.number.isRequired,
|
|
|
|
size: PropTypes.number.isRequired,
|
2017-06-30 12:15:18 +03:00
|
|
|
letterbox: PropTypes.bool,
|
2017-05-12 15:44:10 +03:00
|
|
|
onClick: PropTypes.func.isRequired,
|
2017-05-20 18:31:47 +03:00
|
|
|
autoPlayGif: PropTypes.bool.isRequired,
|
2017-05-12 15:44:10 +03:00
|
|
|
};
|
|
|
|
|
2017-09-20 12:19:36 +03:00
|
|
|
handleMouseEnter = (e) => {
|
|
|
|
if (this.hoverToPlay()) {
|
|
|
|
e.target.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseLeave = (e) => {
|
|
|
|
if (this.hoverToPlay()) {
|
|
|
|
e.target.pause();
|
|
|
|
e.target.currentTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hoverToPlay () {
|
|
|
|
const { attachment, autoPlayGif } = this.props;
|
|
|
|
return !autoPlayGif && attachment.get('type') === 'gifv';
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
handleClick = (e) => {
|
2017-03-04 23:17:10 +02:00
|
|
|
const { index, onClick } = this.props;
|
|
|
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
e.preventDefault();
|
|
|
|
onClick(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-03-04 23:17:10 +02:00
|
|
|
|
|
|
|
render () {
|
2017-06-30 12:15:18 +03:00
|
|
|
const { attachment, index, size, letterbox } = this.props;
|
2017-03-04 23:17:10 +02:00
|
|
|
|
|
|
|
let width = 50;
|
|
|
|
let height = 100;
|
|
|
|
let top = 'auto';
|
|
|
|
let left = 'auto';
|
|
|
|
let bottom = 'auto';
|
|
|
|
let right = 'auto';
|
|
|
|
|
|
|
|
if (size === 1) {
|
|
|
|
width = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size === 4 || (size === 3 && index > 0)) {
|
|
|
|
height = 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size === 2) {
|
|
|
|
if (index === 0) {
|
|
|
|
right = '2px';
|
|
|
|
} else {
|
|
|
|
left = '2px';
|
|
|
|
}
|
|
|
|
} else if (size === 3) {
|
|
|
|
if (index === 0) {
|
|
|
|
right = '2px';
|
|
|
|
} else if (index > 0) {
|
|
|
|
left = '2px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === 1) {
|
|
|
|
bottom = '2px';
|
|
|
|
} else if (index > 1) {
|
|
|
|
top = '2px';
|
|
|
|
}
|
|
|
|
} else if (size === 4) {
|
|
|
|
if (index === 0 || index === 2) {
|
|
|
|
right = '2px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === 1 || index === 3) {
|
|
|
|
left = '2px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 2) {
|
|
|
|
bottom = '2px';
|
|
|
|
} else {
|
|
|
|
top = '2px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let thumbnail = '';
|
|
|
|
|
|
|
|
if (attachment.get('type') === 'image') {
|
2017-06-27 14:46:37 +03:00
|
|
|
const previewUrl = attachment.get('preview_url');
|
|
|
|
const previewWidth = attachment.getIn(['meta', 'small', 'width']);
|
|
|
|
|
|
|
|
const originalUrl = attachment.get('url');
|
|
|
|
const originalWidth = attachment.getIn(['meta', 'original', 'width']);
|
|
|
|
|
|
|
|
const srcSet = `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w`;
|
|
|
|
const sizes = `(min-width: 1025px) ${320 * (width / 100)}px, ${width}vw`;
|
|
|
|
|
2017-03-04 23:17:10 +02:00
|
|
|
thumbnail = (
|
2017-06-27 14:46:37 +03:00
|
|
|
<a
|
2017-04-23 05:26:55 +03:00
|
|
|
className='media-gallery__item-thumbnail'
|
2017-06-27 14:46:37 +03:00
|
|
|
href={attachment.get('remote_url') || originalUrl}
|
2017-03-04 23:17:10 +02:00
|
|
|
onClick={this.handleClick}
|
|
|
|
target='_blank'
|
2017-06-27 14:46:37 +03:00
|
|
|
>
|
2017-10-11 23:02:24 +03:00
|
|
|
<img
|
|
|
|
className={letterbox ? 'letterbox' : ''}
|
|
|
|
src={previewUrl} srcSet={srcSet}
|
|
|
|
sizes={sizes}
|
|
|
|
alt={attachment.get('description')}
|
|
|
|
title={attachment.get('description')}
|
|
|
|
/>
|
2017-06-27 14:46:37 +03:00
|
|
|
</a>
|
2017-03-04 23:17:10 +02:00
|
|
|
);
|
|
|
|
} else if (attachment.get('type') === 'gifv') {
|
2017-04-18 02:57:50 +03:00
|
|
|
const autoPlay = !isIOS() && this.props.autoPlayGif;
|
|
|
|
|
|
|
|
thumbnail = (
|
2017-04-23 05:26:55 +03:00
|
|
|
<div className={`media-gallery__gifv ${autoPlay ? 'autoplay' : ''}`}>
|
2017-04-18 02:57:50 +03:00
|
|
|
<video
|
2017-06-30 12:15:18 +03:00
|
|
|
className={`media-gallery__item-gifv-thumbnail${letterbox ? ' letterbox' : ''}`}
|
2017-04-23 05:26:55 +03:00
|
|
|
role='application'
|
2017-04-18 02:57:50 +03:00
|
|
|
src={attachment.get('url')}
|
|
|
|
onClick={this.handleClick}
|
2017-09-20 12:19:36 +03:00
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
2017-04-18 02:57:50 +03:00
|
|
|
autoPlay={autoPlay}
|
2017-06-06 14:20:07 +03:00
|
|
|
loop
|
|
|
|
muted
|
2017-04-18 02:57:50 +03:00
|
|
|
/>
|
|
|
|
|
|
|
|
<span className='media-gallery__gifv__label'>GIF</span>
|
|
|
|
</div>
|
|
|
|
);
|
2017-03-04 23:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-04-23 05:26:55 +03:00
|
|
|
<div className='media-gallery__item' key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
|
2017-03-04 23:17:10 +02:00
|
|
|
{thumbnail}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|