2016-09-06 03:38:31 +09:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-11-23 19:23:32 +09:00
|
|
|
import PureRenderMixin from 'react-addons-pure-render-mixin';
|
2017-01-16 10:26:03 +09:00
|
|
|
import IconButton from './icon_button';
|
|
|
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' }
|
|
|
|
});
|
2016-11-23 19:23:32 +09:00
|
|
|
|
|
|
|
const outerStyle = {
|
|
|
|
marginTop: '8px',
|
|
|
|
overflow: 'hidden',
|
|
|
|
width: '100%',
|
2017-01-16 10:26:03 +09:00
|
|
|
boxSizing: 'border-box',
|
|
|
|
position: 'relative'
|
2016-11-23 19:23:32 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const spoilerStyle = {
|
|
|
|
textAlign: 'center',
|
|
|
|
height: '100%',
|
2016-11-24 02:53:23 +09:00
|
|
|
cursor: 'pointer',
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
flexDirection: 'column'
|
2016-11-23 19:23:32 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const spoilerSpanStyle = {
|
|
|
|
display: 'block',
|
|
|
|
fontSize: '14px',
|
|
|
|
};
|
|
|
|
|
|
|
|
const spoilerSubSpanStyle = {
|
2016-11-24 02:53:23 +09:00
|
|
|
display: 'block',
|
2016-11-23 19:23:32 +09:00
|
|
|
fontSize: '11px',
|
|
|
|
fontWeight: '500'
|
|
|
|
};
|
2016-09-06 03:38:31 +09:00
|
|
|
|
2017-01-16 10:26:03 +09:00
|
|
|
const spoilerButtonStyle = {
|
|
|
|
position: 'absolute',
|
|
|
|
top: '6px',
|
|
|
|
left: '8px',
|
|
|
|
zIndex: '100'
|
|
|
|
};
|
|
|
|
|
2016-09-06 03:38:31 +09:00
|
|
|
const MediaGallery = React.createClass({
|
|
|
|
|
2016-11-23 19:23:32 +09:00
|
|
|
getInitialState () {
|
|
|
|
return {
|
2017-01-20 11:24:30 +09:00
|
|
|
visible: !this.props.sensitive
|
2016-11-23 19:23:32 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-06 03:38:31 +09:00
|
|
|
propTypes: {
|
2016-11-23 19:23:32 +09:00
|
|
|
sensitive: React.PropTypes.bool,
|
2016-09-18 20:03:37 +09:00
|
|
|
media: ImmutablePropTypes.list.isRequired,
|
2016-10-25 01:07:40 +09:00
|
|
|
height: React.PropTypes.number.isRequired,
|
2017-02-05 10:48:11 +09:00
|
|
|
onOpenMedia: React.PropTypes.func.isRequired,
|
|
|
|
intl: React.PropTypes.object.isRequired
|
2016-09-06 03:38:31 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
mixins: [PureRenderMixin],
|
|
|
|
|
2017-02-05 10:48:11 +09:00
|
|
|
handleClick (index, e) {
|
2016-10-25 01:07:40 +09:00
|
|
|
if (e.button === 0) {
|
|
|
|
e.preventDefault();
|
2017-02-05 10:48:11 +09:00
|
|
|
this.props.onOpenMedia(this.props.media, index);
|
2016-10-25 01:07:40 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
},
|
|
|
|
|
2016-11-23 19:23:32 +09:00
|
|
|
handleOpen () {
|
2017-01-16 10:26:03 +09:00
|
|
|
this.setState({ visible: !this.state.visible });
|
2016-11-23 19:23:32 +09:00
|
|
|
},
|
|
|
|
|
2016-09-06 03:38:31 +09:00
|
|
|
render () {
|
2017-01-16 10:26:03 +09:00
|
|
|
const { media, intl, sensitive } = this.props;
|
2016-09-06 08:26:35 +09:00
|
|
|
|
2016-11-23 19:23:32 +09:00
|
|
|
let children;
|
|
|
|
|
2017-01-20 11:24:30 +09:00
|
|
|
if (!this.state.visible) {
|
|
|
|
if (sensitive) {
|
|
|
|
children = (
|
2017-02-11 00:35:19 +09:00
|
|
|
<div style={spoilerStyle} className='media-spoiler' onClick={this.handleOpen}>
|
2017-01-20 11:24:30 +09:00
|
|
|
<span style={spoilerSpanStyle}><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
|
|
|
|
<span style={spoilerSubSpanStyle}><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
children = (
|
2017-02-11 00:35:19 +09:00
|
|
|
<div style={spoilerStyle} className='media-spoiler' onClick={this.handleOpen}>
|
2017-01-20 11:24:30 +09:00
|
|
|
<span style={spoilerSpanStyle}><FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' /></span>
|
|
|
|
<span style={spoilerSubSpanStyle}><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-11-23 19:23:32 +09:00
|
|
|
} else {
|
|
|
|
const size = media.take(4).size;
|
|
|
|
|
|
|
|
children = media.take(4).map((attachment, i) => {
|
|
|
|
let width = 50;
|
|
|
|
let height = 100;
|
|
|
|
let top = 'auto';
|
|
|
|
let left = 'auto';
|
|
|
|
let bottom = 'auto';
|
|
|
|
let right = 'auto';
|
|
|
|
|
|
|
|
if (size === 1) {
|
|
|
|
width = 100;
|
2016-09-06 08:26:35 +09:00
|
|
|
}
|
|
|
|
|
2016-11-23 19:23:32 +09:00
|
|
|
if (size === 4 || (size === 3 && i > 0)) {
|
|
|
|
height = 50;
|
2016-09-06 08:26:35 +09:00
|
|
|
}
|
|
|
|
|
2016-11-23 19:23:32 +09:00
|
|
|
if (size === 2) {
|
|
|
|
if (i === 0) {
|
|
|
|
right = '2px';
|
|
|
|
} else {
|
|
|
|
left = '2px';
|
|
|
|
}
|
|
|
|
} else if (size === 3) {
|
|
|
|
if (i === 0) {
|
|
|
|
right = '2px';
|
|
|
|
} else if (i > 0) {
|
|
|
|
left = '2px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i === 1) {
|
|
|
|
bottom = '2px';
|
|
|
|
} else if (i > 1) {
|
|
|
|
top = '2px';
|
|
|
|
}
|
|
|
|
} else if (size === 4) {
|
|
|
|
if (i === 0 || i === 2) {
|
|
|
|
right = '2px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i === 1 || i === 3) {
|
|
|
|
left = '2px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < 2) {
|
|
|
|
bottom = '2px';
|
|
|
|
} else {
|
|
|
|
top = '2px';
|
|
|
|
}
|
2016-09-06 08:26:35 +09:00
|
|
|
}
|
|
|
|
|
2016-11-23 19:23:32 +09:00
|
|
|
return (
|
|
|
|
<div key={attachment.get('id')} style={{ boxSizing: 'border-box', position: 'relative', left: left, top: top, right: right, bottom: bottom, float: 'left', border: 'none', display: 'block', width: `${width}%`, height: `${height}%` }}>
|
2017-02-05 10:48:11 +09:00
|
|
|
<a href={attachment.get('remote_url') ? attachment.get('remote_url') : attachment.get('url')} onClick={this.handleClick.bind(this, i)} target='_blank' style={{ display: 'block', width: '100%', height: '100%', background: `url(${attachment.get('preview_url')}) no-repeat center`, textDecoration: 'none', backgroundSize: 'cover', cursor: 'zoom-in' }} />
|
2016-11-23 19:23:32 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-02-05 10:48:11 +09:00
|
|
|
|
2016-09-06 03:38:31 +09:00
|
|
|
return (
|
2016-11-23 19:23:32 +09:00
|
|
|
<div style={{ ...outerStyle, height: `${this.props.height}px` }}>
|
2017-01-20 11:24:30 +09:00
|
|
|
<div style={spoilerButtonStyle} >
|
|
|
|
<IconButton title={intl.formatMessage(messages.toggle_visible)} icon={this.state.visible ? 'eye' : 'eye-slash'} onClick={this.handleOpen} />
|
|
|
|
</div>
|
2016-09-06 03:38:31 +09:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-01-16 10:26:03 +09:00
|
|
|
export default injectIntl(MediaGallery);
|