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.

93 lines
2.4 KiB

  1. import ImmutablePropTypes from 'react-immutable-proptypes';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. const MediaGallery = React.createClass({
  4. propTypes: {
  5. media: ImmutablePropTypes.list.isRequired,
  6. height: React.PropTypes.number.isRequired,
  7. onOpenMedia: React.PropTypes.func.isRequired
  8. },
  9. mixins: [PureRenderMixin],
  10. handleClick (url, e) {
  11. if (e.button === 0) {
  12. e.preventDefault();
  13. this.props.onOpenMedia(url);
  14. }
  15. e.stopPropagation();
  16. },
  17. render () {
  18. var children = this.props.media.take(4);
  19. var size = children.size;
  20. children = children.map((attachment, i) => {
  21. let width = 50;
  22. let height = 100;
  23. let top = 'auto';
  24. let left = 'auto';
  25. let bottom = 'auto';
  26. let right = 'auto';
  27. if (size === 1) {
  28. width = 100;
  29. }
  30. if (size === 4 || (size === 3 && i > 0)) {
  31. height = 50;
  32. }
  33. if (size === 2) {
  34. if (i === 0) {
  35. right = '2px';
  36. } else {
  37. left = '2px';
  38. }
  39. } else if (size === 3) {
  40. if (i === 0) {
  41. right = '2px';
  42. } else if (i > 0) {
  43. left = '2px';
  44. }
  45. if (i === 1) {
  46. bottom = '2px';
  47. } else if (i > 1) {
  48. top = '2px';
  49. }
  50. } else if (size === 4) {
  51. if (i === 0 || i === 2) {
  52. right = '2px';
  53. }
  54. if (i === 1 || i === 3) {
  55. left = '2px';
  56. }
  57. if (i < 2) {
  58. bottom = '2px';
  59. } else {
  60. top = '2px';
  61. }
  62. }
  63. return (
  64. <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}%` }}>
  65. <a href={attachment.get('url')} onClick={this.handleClick.bind(this, attachment.get('url'))} 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' }} />
  66. </div>
  67. );
  68. });
  69. return (
  70. <div style={{ marginTop: '8px', overflow: 'hidden', width: '100%', height: `${this.props.height}px`, boxSizing: 'border-box' }}>
  71. {children}
  72. </div>
  73. );
  74. }
  75. });
  76. export default MediaGallery;