Skip to content

Commit e84628c

Browse files
author
ben
committed
v0.2.0
1 parent 89ef8fd commit e84628c

File tree

4 files changed

+74
-60
lines changed

4 files changed

+74
-60
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# react-grid-gallery
22

3+
### v0.2.0 / 2016-09-03
4+
5+
* Construction of thumbnail images and image rows removed from render. Thumbnails and rows now only rebuilt when container size changes.
6+
7+
* `selectedImages` state now set via props change.
8+
9+
* `onSelectedImagesChange` callback now called directly from `onToggleSelected` rather than `componentWillUpdate`. Perviously, a combination of setting `selectedImages` state and triggering `onSelectedImagesChange` when `componentWillUpdate` due to that state change caused a double render.
10+
11+
* Internal image access now via state instead of props.
12+
13+
* Thumbnail generation now atomic function rather than whole array at once.
14+
315
### v0.1.14 / 2016-08-22
416

517
* `selectedImages` state set on `componentWillReceiveProps` allowing selections from outside component to trigger state update.

lib/Gallery.js

+49-47
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
2222

2323
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
2424

25-
var update = require('react-addons-update');
26-
2725
var Gallery = function (_Component) {
2826
_inherits(Gallery, _Component);
2927

@@ -33,6 +31,8 @@ var Gallery = function (_Component) {
3331
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Gallery).call(this, props));
3432

3533
_this.state = {
34+
images: _this.props.images,
35+
thumbnails: [],
3636
lightboxIsOpen: _this.props.isOpen,
3737
selectedImages: _this.props.selectedImages,
3838
currentImage: _this.props.currentImage,
@@ -63,26 +63,28 @@ var Gallery = function (_Component) {
6363
selectedImages: np.selectedImages
6464
});
6565
}
66-
}
67-
}, {
68-
key: 'componentWillUpdate',
69-
value: function componentWillUpdate(np, ns) {
70-
if (np.selectedImages != ns.selectedImages) {
71-
if (this.props.onSelectedImagesChange) this.props.onSelectedImagesChange(ns.selectedImages);
66+
67+
if (this.state.images != np.images) {
68+
this.setState({
69+
images: np.images
70+
});
7271
}
7372
}
7473
}, {
7574
key: 'componentDidUpdate',
7675
value: function componentDidUpdate() {
76+
if (!this._gallery) return;
7777
if (this._gallery.clientWidth !== this.state.containerWidth) {
7878
this.handleResize();
7979
}
8080
}
8181
}, {
8282
key: 'handleResize',
8383
value: function handleResize() {
84+
if (!this._gallery) return;
8485
this.setState({
85-
containerWidth: Math.floor(this._gallery.clientWidth)
86+
containerWidth: Math.floor(this._gallery.clientWidth),
87+
thumbnails: this.renderThumbs(this._gallery.clientWidth)
8688
});
8789
}
8890
}, {
@@ -127,13 +129,10 @@ var Gallery = function (_Component) {
127129
value: function onToggleSelected(index, event) {
128130
event.preventDefault();
129131
var i = this.state.selectedImages.indexOf(index);
130-
if (i == -1) {
131-
this.setState({ selectedImages: update(this.state.selectedImages, { $push: [index] }) });
132-
} else {
133-
this.setState({
134-
selectedImages: update(this.state.selectedImages, { $splice: [[i, 1]] })
135-
});
136-
}
132+
var selectedImages = this.state.selectedImages.slice();
133+
if (i == -1) selectedImages.push(index);else selectedImages.splice(i, 1);
134+
135+
if (this.props.onSelectedImagesChange) this.props.onSelectedImagesChange(selectedImages);
137136
}
138137
}, {
139138
key: 'getOnClickThumbnailFunc',
@@ -166,17 +165,17 @@ var Gallery = function (_Component) {
166165
}
167166
}, {
168167
key: 'buildImageRow',
169-
value: function buildImageRow(items) {
168+
value: function buildImageRow(items, containerWidth) {
170169
var row = [];
171170
var len = 0;
172171
var imgMargin = 2 * this.props.margin;
173-
while (items.length > 0 && len < this.state.containerWidth) {
172+
while (items.length > 0 && len < containerWidth) {
174173
var item = items.shift();
175174
row.push(item);
176175
len += item.scaletwidth + imgMargin;
177176
}
178177

179-
var delta = len - this.state.containerWidth;
178+
var delta = len - containerWidth;
180179
if (row.length > 0 && delta > 0) {
181180
var cutoff = this.calculateCutOff(len, delta, row);
182181
for (var i in row) {
@@ -186,49 +185,39 @@ var Gallery = function (_Component) {
186185
item.vwidth = item.scaletwidth - pixelsToRemove;
187186
}
188187
} else {
189-
for (var i in row) {
190-
item = row[i];
188+
for (var j in row) {
189+
item = row[j];
191190
item.marginLeft = 0;
192191
item.vwidth = item.scaletwidth;
193192
}
194193
}
195194
return row;
196195
}
197196
}, {
198-
key: 'scaleThumbs',
199-
value: function scaleThumbs(items) {
200-
for (var i in items) {
201-
items[i].scaletwidth = Math.floor(this.props.rowHeight * (items[i].thumbnailWidth / items[i].thumbnailHeight));
202-
}
203-
return items;
197+
key: 'setThumbScale',
198+
value: function setThumbScale(item) {
199+
item.scaletwidth = Math.floor(this.props.rowHeight * (item.thumbnailWidth / item.thumbnailHeight));
204200
}
205201
}, {
206-
key: 'renderImages',
207-
value: function renderImages() {
208-
if (!this.props.images) return;
209-
if (this.state.containerWidth == 0) return;
210-
var items = this.scaleThumbs(this.props.images.slice());
202+
key: 'renderThumbs',
203+
value: function renderThumbs(containerWidth) {
204+
if (!this.state.images) return [];
205+
if (containerWidth == 0) return [];
206+
207+
var items = this.state.images.slice();
208+
for (var t in items) {
209+
this.setThumbScale(items[t]);
210+
}
211+
211212
var images = [];
212213
var rows = [];
213214
while (items.length > 0) {
214-
rows.push(this.buildImageRow(items));
215+
rows.push(this.buildImageRow(items, containerWidth));
215216
}
216-
217-
var idx = 0;
218217
for (var r in rows) {
219218
for (var i in rows[r]) {
220219
var item = rows[r][i];
221-
images.push(_react2.default.createElement(_Image2.default, {
222-
key: "Image-" + idx,
223-
item: item,
224-
index: idx,
225-
margin: this.props.margin,
226-
height: this.props.rowHeight,
227-
isSelectable: this.props.enableImageSelection,
228-
isSelected: this.state.selectedImages.indexOf(idx) > -1 ? true : false,
229-
onClick: this.getOnClickThumbnailFunc(),
230-
onToggleSelected: this.onToggleSelected }));
231-
idx++;
220+
images.push(item);
232221
}
233222
}
234223
return images;
@@ -238,12 +227,25 @@ var Gallery = function (_Component) {
238227
value: function render() {
239228
var _this2 = this;
240229

230+
var images = this.state.thumbnails.map(function (item, idx) {
231+
return _react2.default.createElement(_Image2.default, {
232+
key: "Image-" + idx,
233+
item: item,
234+
index: idx,
235+
margin: _this2.props.margin,
236+
height: _this2.props.rowHeight,
237+
isSelectable: _this2.props.enableImageSelection,
238+
isSelected: _this2.state.selectedImages.indexOf(idx) > -1 ? true : false,
239+
onClick: _this2.getOnClickThumbnailFunc(),
240+
onToggleSelected: _this2.onToggleSelected });
241+
});
242+
241243
return _react2.default.createElement(
242244
'div',
243245
{ id: 'Gallery', ref: function ref(c) {
244246
return _this2._gallery = c;
245247
} },
246-
this.renderImages(),
248+
images,
247249
_react2.default.createElement(_reactImages2.default, {
248250
images: this.props.images,
249251
backdropClosesModal: this.props.backdropClosesModal,

lib/react-grid-gallery.bundle.min.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-grid-gallery",
3-
"version": "0.1.14",
4-
"description": "Justified gallery component for React.",
3+
"version": "0.2.0",
4+
"description": "Justified gallery component for ReactJS.",
55
"main": "lib/Gallery.js",
66
"dependencies": {
77
"react-images": "^0.4.11"
@@ -20,9 +20,8 @@
2020
"gulp-gh-pages": "^0.5.4",
2121
"gulp-if": "^2.0.1",
2222
"gulp-uglify": "^2.0.0",
23-
"react": "^15.3.0",
24-
"react-addons-update": "^15.3.0",
25-
"react-dom": "^15.3.0",
23+
"react": "^15.3.1",
24+
"react-dom": "^15.3.1",
2625
"run-sequence": "^1.2.2",
2726
"vinyl-buffer": "^1.0.0",
2827
"vinyl-source-stream": "^1.1.0",
@@ -38,6 +37,7 @@
3837
"lightbox",
3938
"images",
4039
"gallery",
40+
"selectable",
4141
"justified"
4242
],
4343
"author": "Ben Howell",

0 commit comments

Comments
 (0)