2017-07-12 08:02:51 +00:00
|
|
|
// Package imports //
|
2017-05-03 00:04:16 +00:00
|
|
|
import React from 'react';
|
2016-09-05 18:38:31 +00:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2017-04-21 18:05:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-01-16 01:26:03 +00:00
|
|
|
|
2017-07-12 08:02:51 +00:00
|
|
|
// Mastodon imports //
|
|
|
|
import { isIOS } from '../../../../mastodon/is_mobile';
|
2016-11-23 10:23:32 +00:00
|
|
|
|
2017-07-12 08:02:51 +00:00
|
|
|
export default class StatusGalleryItem extends React.PureComponent {
|
2017-03-04 21:17:10 +00:00
|
|
|
|
2017-05-12 12:44:10 +00:00
|
|
|
static propTypes = {
|
|
|
|
attachment: ImmutablePropTypes.map.isRequired,
|
|
|
|
index: PropTypes.number.isRequired,
|
|
|
|
size: PropTypes.number.isRequired,
|
2017-06-30 09:15:18 +00:00
|
|
|
letterbox: PropTypes.bool,
|
2017-05-12 12:44:10 +00:00
|
|
|
onClick: PropTypes.func.isRequired,
|
2017-05-20 15:31:47 +00:00
|
|
|
autoPlayGif: PropTypes.bool.isRequired,
|
2017-05-12 12:44:10 +00:00
|
|
|
};
|
|
|
|
|
2017-09-20 09:19:36 +00: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 12:44:10 +00:00
|
|
|
handleClick = (e) => {
|
2017-03-04 21:17:10 +00:00
|
|
|
const { index, onClick } = this.props;
|
|
|
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
e.preventDefault();
|
|
|
|
onClick(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2017-03-04 21:17:10 +00:00
|
|
|
|
|
|
|
render () {
|
2017-06-30 09:15:18 +00:00
|
|
|
const { attachment, index, size, letterbox } = this.props;
|
2017-03-04 21:17:10 +00: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 11:46:37 +00: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 21:17:10 +00:00
|
|
|
thumbnail = (
|
2017-06-27 11:46:37 +00:00
|
|
|
<a
|
2017-04-23 02:26:55 +00:00
|
|
|
className='media-gallery__item-thumbnail'
|
2017-06-27 11:46:37 +00:00
|
|
|
href={attachment.get('remote_url') || originalUrl}
|
2017-03-04 21:17:10 +00:00
|
|
|
onClick={this.handleClick}
|
|
|
|
target='_blank'
|
2017-06-27 11:46:37 +00:00
|
|
|
>
|
2017-10-11 20:02:24 +00:00
|
|
|
<img
|
|
|
|
className={letterbox ? 'letterbox' : ''}
|
|
|
|
src={previewUrl} srcSet={srcSet}
|
|
|
|
sizes={sizes}
|
|
|
|
alt={attachment.get('description')}
|
|
|
|
title={attachment.get('description')}
|
|
|
|
/>
|
2017-06-27 11:46:37 +00:00
|
|
|
</a>
|
2017-03-04 21:17:10 +00:00
|
|
|
);
|
|
|
|
} else if (attachment.get('type') === 'gifv') {
|
2017-04-17 23:57:50 +00:00
|
|
|
const autoPlay = !isIOS() && this.props.autoPlayGif;
|
|
|
|
|
|
|
|
thumbnail = (
|
2017-04-23 02:26:55 +00:00
|
|
|
<div className={`media-gallery__gifv ${autoPlay ? 'autoplay' : ''}`}>
|
2017-04-17 23:57:50 +00:00
|
|
|
<video
|
2017-06-30 09:15:18 +00:00
|
|
|
className={`media-gallery__item-gifv-thumbnail${letterbox ? ' letterbox' : ''}`}
|
2017-04-23 02:26:55 +00:00
|
|
|
role='application'
|
2017-04-17 23:57:50 +00:00
|
|
|
src={attachment.get('url')}
|
|
|
|
onClick={this.handleClick}
|
2017-09-20 09:19:36 +00:00
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
2017-04-17 23:57:50 +00:00
|
|
|
autoPlay={autoPlay}
|
2017-06-06 11:20:07 +00:00
|
|
|
loop
|
|
|
|
muted
|
2017-04-17 23:57:50 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<span className='media-gallery__gifv__label'>GIF</span>
|
|
|
|
</div>
|
|
|
|
);
|
2017-03-04 21:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-04-23 02:26:55 +00: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 21:17:10 +00:00
|
|
|
{thumbnail}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|