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.

79 lines
2.0 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. },
  8. mixins: [PureRenderMixin],
  9. render () {
  10. var children = this.props.media.take(4);
  11. var size = children.size;
  12. children = children.map((attachment, i) => {
  13. let width = 50;
  14. let height = 100;
  15. let top = 'auto';
  16. let left = 'auto';
  17. let bottom = 'auto';
  18. let right = 'auto';
  19. if (size === 1) {
  20. width = 100;
  21. }
  22. if (size === 4 || (size === 3 && i > 0)) {
  23. height = 50;
  24. }
  25. if (size === 2) {
  26. if (i === 0) {
  27. right = '2px';
  28. } else {
  29. left = '2px';
  30. }
  31. } else if (size === 3) {
  32. if (i === 0) {
  33. right = '2px';
  34. } else if (i > 0) {
  35. left = '2px';
  36. }
  37. if (i === 1) {
  38. bottom = '2px';
  39. } else if (i > 1) {
  40. top = '2px';
  41. }
  42. } else if (size === 4) {
  43. if (i === 0 || i === 2) {
  44. right = '2px';
  45. }
  46. if (i === 1 || i === 3) {
  47. left = '2px';
  48. }
  49. if (i < 2) {
  50. bottom = '2px';
  51. } else {
  52. top = '2px';
  53. }
  54. }
  55. return <a key={attachment.get('id')} href={attachment.get('url')} target='_blank' style={{ boxSizing: 'border-box', position: 'relative', left: left, top: top, right: right, bottom: bottom, float: 'left', textDecoration: 'none', border: 'none', display: 'block', width: `${width}%`, height: `${height}%`, background: `url(${attachment.get('preview_url')}) no-repeat center`, backgroundSize: 'cover', cursor: 'zoom-in' }} />;
  56. });
  57. return (
  58. <div style={{ marginTop: '8px', overflow: 'hidden', width: '100%', height: `${this.props.height}px`, boxSizing: 'border-box' }}>
  59. {children}
  60. </div>
  61. );
  62. }
  63. });
  64. export default MediaGallery;