Skip to content

Commit 383c4e4

Browse files
Merge pull request #89 from TobitSoftware/feature/updateGallery
Feature/update gallery
2 parents 99ad9ac + 4941c12 commit 383c4e4

File tree

3 files changed

+75
-31
lines changed

3 files changed

+75
-31
lines changed

src/react-chayns-gallery/component/Gallery.jsx

+54-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3+
import classNames from 'classnames';
34
import ImageContainer from './ImageContainer';
45

56
export default class Gallery extends Component {
@@ -22,13 +23,37 @@ export default class Gallery extends Component {
2223

2324
constructor() {
2425
super();
25-
this.openImage = this.openGallery.bind(this);
26+
this.openGallery = this.openGallery.bind(this);
27+
}
28+
29+
static getScaledImageUrl(url, shortEdgeSize) {
30+
if (url.indexOf('tsimg.space') >= 0) {
31+
if (url.indexOf('jpg') >= 0) {
32+
return url.replace('.jpg', `_s${shortEdgeSize}-mshortedgescale.jpg`);
33+
}
34+
if (url.indexOf('jpeg') >= 0) {
35+
return url.replace('.jpeg', `_s${shortEdgeSize}-mshortedgescale.jpeg`);
36+
}
37+
if (url.indexOf('png') >= 0) {
38+
return url.replace('.png', `_s${shortEdgeSize}-mshortedgescale.png`);
39+
}
40+
}
41+
return url;
42+
}
43+
44+
static getBigImages(urls) {
45+
const bigUrls = [];
46+
urls.forEach((url) => {
47+
// eslint-disable-next-line no-restricted-globals
48+
bigUrls.push(Gallery.getScaledImageUrl(url, screen.availHeight * 0.9));
49+
});
50+
return bigUrls;
2651
}
2752

2853
openGallery(start) {
2954
const { onClick, urls, deleteMode } = this.props;
30-
if(!deleteMode) {
31-
onClick(urls, start);
55+
if (!deleteMode) {
56+
onClick(Gallery.getBigImages(urls), start);
3257
}
3358
}
3459

@@ -40,34 +65,48 @@ export default class Gallery extends Component {
4065
onDelete,
4166
deleteMode,
4267
} = this.props;
68+
let styleHeight;
4369
const style = {};
44-
if (width) {
45-
style.width = `${width}px`;
46-
}
4770
if (!deleteMode) {
48-
if(height) {
49-
style.height = `${height}px`;
50-
} else if(chayns.env.mobile) {
51-
style.height = '256px';
71+
if (height) {
72+
styleHeight = height;
73+
} else if (chayns.env.mobile) {
74+
styleHeight = 256;
5275
} else {
53-
style.height = '428px';
76+
styleHeight = 428;
5477
}
78+
style.height = `${styleHeight}px`;
5579
}
80+
if (width) {
81+
style.width = width;
82+
}
83+
const numberOfImages = urls.length;
5684
return (
5785
<div className="chayns-gallery" style={style}>
58-
<div className="gallery-grid">
86+
<div className={classNames('gallery-grid', { 'delete-mode': deleteMode })}>
5987
{
6088
urls.map((url, index) => {
6189
if (index <= 3 || deleteMode) {
90+
let shortEdge = window.innerWidth;
91+
if (!deleteMode) {
92+
if (index <= 2 && numberOfImages <= 2) {
93+
shortEdge = styleHeight;
94+
} else if (index === 0) {
95+
shortEdge = parseInt((styleHeight * 2) / 3, 10);
96+
} else if (index < 4) {
97+
shortEdge = parseInt(styleHeight / 3, 10);
98+
}
99+
}
100+
62101
return (
63102
<ImageContainer
64103
key={url}
65-
url={url}
104+
url={Gallery.getScaledImageUrl(url, shortEdge)}
66105
index={index}
67-
openImage={this.openImage}
106+
openImage={this.openGallery}
68107
onDelete={onDelete}
69108
deleteMode={deleteMode}
70-
moreImages={(index === 3 && !deleteMode) ? urls.length - 1 - index : 0}
109+
moreImages={(index === 3 && !deleteMode) ? numberOfImages - 1 - index : 0}
71110
/>
72111
);
73112
}

src/react-chayns-gallery/component/ImageContainer.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default class ImageContainer extends PureComponent {
4949
} = this.props;
5050

5151
return (
52-
<div className={classNames('gallery_item', { 'delete-mode': deleteMode })}>
52+
<div className="gallery_item">
5353
<div
5454
className={classNames('gallery_item_inner', { 'more-images': moreImages > 0 })}
5555
style={{ backgroundImage: `url(${url})` }}

src/react-chayns-gallery/index.scss

+20-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
flex-flow: row;
77
flex-wrap: wrap;
88
height: 100%;
9+
&.delete-mode {
10+
height: unset !important;
11+
position: relative;
12+
.gallery_item {
13+
background-color: #f0f0f00a;
14+
width: calc(33.33% - 2px) !important;
15+
padding-top: calc(33.33% - 2px) !important;
16+
height: 0;
17+
position: relative;
18+
margin: 1px;
19+
.gallery_item_inner {
20+
position: absolute;
21+
top: 0;
22+
left: 0;
23+
bottom: 0;
24+
right: 0;
25+
}
26+
}
27+
}
928
.gallery_item {
1029
padding: 1px;
1130
transition: opacity 0.2s ease;
@@ -33,24 +52,10 @@
3352
font-size: 16px;
3453
}
3554
}
36-
&.delete-mode {
37-
background-color: red;
38-
width: calc(33.33% - 2px);
39-
padding-top: calc(33.33% - 2px);
40-
position: relative;
41-
margin: 1px;
42-
.gallery_item_inner {
43-
position: absolute;
44-
top: 0;
45-
left: 0;
46-
bottom: 0;
47-
right: 0;
48-
}
49-
}
5055
&:hover {
5156
opacity: 0.9;
5257
}
53-
&:not(.delete-mode) {
58+
&:not(.delete-mode &) {
5459
&:nth-child(1) {
5560
&:nth-last-child(1) {
5661
//Image 1 of 1

0 commit comments

Comments
 (0)