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