Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Added react-masonry-plugin locally and converted to es6

VictorSff74a46bf595
diff --git a/package-lock.json b/package-lock.json
index f36d52935..515655978 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -25,13 +25,16 @@
"compression": "^1.7.4",
"coordinate-parser": "^1.0.4",
"copy-to-clipboard": "^3.3.1",
+ "element-resize-detector": "^1.2.4",
"elm-pep": "^1.0.6",
"file-saver": "^2.0.2",
"geographiclib": "^1.50.0",
"history": "^4.7.2",
+ "imagesloaded": "^5.0.0",
"immutability-helper": "^3.0.1",
"jsdom-worker": "^0.3.0",
"lodash": "^4.17.21",
+ "masonry-layout": "^4.2.2",
"moment": "^2.29.4",
"moment-locales-webpack-plugin": "^1.2.0",
"node-dir": "^0.1.17",
@@ -51,7 +54,6 @@
"react-image-crop": "^9.1.1",
"react-infinite-scroller": "^1.2.4",
"react-joyride": "^2.3.0",
- "react-masonry-component": "^6.2.1",
"react-mobile-datepicker": "^4.0.1",
"react-portal": "^4.2.1",
"react-redux": "^8.0.5",
@@ -6269,15 +6271,6 @@
"node": ">=10.0.0"
}
},
- "node_modules/create-react-class": {
- "version": "15.7.0",
- "resolved": "https://registry.npmjs.org/create-react-class/-/create-react-class-15.7.0.tgz",
- "integrity": "sha512-QZv4sFWG9S5RUvkTYWbflxeZX+JG7Cz0Tn33rQBJ+WFQTqTfUTjMjiv9tnfXazjsO5r0KhPs+AqCjyrQX6h2ng==",
- "dependencies": {
- "loose-envify": "^1.3.1",
- "object-assign": "^4.1.1"
- }
- },
"node_modules/cross-env": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
@@ -10046,13 +10039,18 @@
}
},
"node_modules/imagesloaded": {
- "version": "4.1.4",
- "resolved": "https://registry.npmjs.org/imagesloaded/-/imagesloaded-4.1.4.tgz",
- "integrity": "sha512-ltiBVcYpc/TYTF5nolkMNsnREHW+ICvfQ3Yla2Sgr71YFwQ86bDwV9hgpFhFtrGPuwEx5+LqOHIrdXBdoWwwsA==",
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/imagesloaded/-/imagesloaded-5.0.0.tgz",
+ "integrity": "sha512-/0JGSubc1MTCoDKVmonLHgbifBWHdyLkun+R/151E1c5n79hiSxcd7cB7mPXFgojYu8xnRZv7GYxzKoxW8BetQ==",
"dependencies": {
- "ev-emitter": "^1.0.0"
+ "ev-emitter": "^2.1.2"
}
},
+ "node_modules/imagesloaded/node_modules/ev-emitter": {
+ "version": "2.1.2",
+ "resolved": "https://registry.npmjs.org/ev-emitter/-/ev-emitter-2.1.2.tgz",
+ "integrity": "sha512-jQ5Ql18hdCQ4qS+RCrbLfz1n+Pags27q5TwMKvZyhp5hh2UULUYZUy1keqj6k6SYsdqIYjnmz7xyyEY0V67B8Q=="
+ },
"node_modules/immediate": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",
@@ -16790,22 +16788,6 @@
"resolved": "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz",
"integrity": "sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA=="
},
- "node_modules/react-masonry-component": {
- "version": "6.3.0",
- "resolved": "https://registry.npmjs.org/react-masonry-component/-/react-masonry-component-6.3.0.tgz",
- "integrity": "sha512-4ZI78nxMfCpU5yQiS6Rup07Ju++YzcOGAyvbMcl2GfpZTw8GRwT548lkKr0PJarNicRV1qE2D/NiT26UPg1T7A==",
- "dependencies": {
- "create-react-class": "^15.6.2",
- "element-resize-detector": "^1.1.9",
- "imagesloaded": "^4.0.0",
- "lodash": "^4.17.4",
- "masonry-layout": "^4.2.0",
- "prop-types": "^15.5.8"
- },
- "peerDependencies": {
- "react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0 || ^17.0.0"
- }
- },
"node_modules/react-mobile-datepicker": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/react-mobile-datepicker/-/react-mobile-datepicker-4.0.2.tgz",
diff --git a/package.json b/package.json
index 4c56bde1e..0e54cf0ba 100644
--- a/package.json
+++ b/package.json
@@ -167,13 +167,16 @@
"compression": "^1.7.4",
"coordinate-parser": "^1.0.4",
"copy-to-clipboard": "^3.3.1",
+ "element-resize-detector": "^1.2.4",
"elm-pep": "^1.0.6",
"file-saver": "^2.0.2",
"geographiclib": "^1.50.0",
"history": "^4.7.2",
+ "imagesloaded": "^5.0.0",
"immutability-helper": "^3.0.1",
"jsdom-worker": "^0.3.0",
"lodash": "^4.17.21",
+ "masonry-layout": "^4.2.2",
"moment": "^2.29.4",
"moment-locales-webpack-plugin": "^1.2.0",
"node-dir": "^0.1.17",
@@ -193,7 +196,6 @@
"react-image-crop": "^9.1.1",
"react-infinite-scroller": "^1.2.4",
"react-joyride": "^2.3.0",
- "react-masonry-component": "^6.2.1",
"react-mobile-datepicker": "^4.0.1",
"react-portal": "^4.2.1",
"react-redux": "^8.0.5",
diff --git a/web/js/components/layer/product-picker/browse/category-grid.js b/web/js/components/layer/product-picker/browse/category-grid.js
index 091096a38..3c965d2ef 100644
--- a/web/js/components/layer/product-picker/browse/category-grid.js
+++ b/web/js/components/layer/product-picker/browse/category-grid.js
@@ -2,7 +2,7 @@ import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import lodashOrderBy from 'lodash/orderBy';
-import Masonry from 'react-masonry-component';
+import MasonryComponent from '../../../../util/masonry';
import CategoryCell from './category-cell';
import {
showMeasurements as showMeasurementsAction,
@@ -39,7 +39,7 @@ function CategoryGrid(props) {
['asc'],
);
return (
- <Masonry className="category-masonry-case" options={masonryOptions}>
+ <MasonryComponent className="category-masonry-case" options={masonryOptions}>
{orderedCategories.map((category) => (
<CategoryCell
key={category.id}
@@ -50,7 +50,7 @@ function CategoryGrid(props) {
hasMeasurementSource={hasMeasurementSource}
/>
))}
- </Masonry>
+ </MasonryComponent>
);
}
const mapDispatchToProps = (dispatch) => ({
diff --git a/web/js/util/masonry.js b/web/js/util/masonry.js
new file mode 100644
index 000000000..58a121cc6
--- /dev/null
+++ b/web/js/util/masonry.js
@@ -0,0 +1,257 @@
+import React, { Component } from 'react';
+import PropTypes from 'prop-types';
+import MasonryLayout from 'masonry-layout';
+import imagesloaded from 'imagesloaded';
+import elementResizeDetectorMaker from 'element-resize-detector';
+import debounce from 'lodash/debounce';
+import omit from 'lodash/omit';
+
+const propTypes = {
+ enableResizableChildren: PropTypes.bool,
+ disableImagesLoaded: PropTypes.bool,
+ onImagesLoaded: PropTypes.func,
+ updateOnEachImageLoad: PropTypes.bool,
+ options: PropTypes.object,
+ imagesLoadedOptions: PropTypes.object,
+ elementType: PropTypes.string,
+ onLayoutComplete: PropTypes.func,
+ onRemoveComplete: PropTypes.func
+}
+
+class MasonryComponent extends Component {
+ constructor(props) {
+ super(props);
+ this.enableResizableChildren = false;
+ this.disableImagesLoaded = false;
+ this.updateOnEachImageLoad = false,
+ this.options = {},
+ this.imagesLoadedOptions = {},
+ this.className = '',
+ this.elementType = 'div',
+ this.onLayoutComplete = () => {},
+ this.onRemoveComplete = () => {}
+ this.masonryContainer = null;
+ this.masonry = null;
+ this.latestKnownDomChildren = [];
+ this.imagesLoadedCancelRef = null;
+ this.erd = null;
+ }
+
+ initializeMasonry(force) {
+ if (!this.masonry || force) {
+ this.masonry = new MasonryLayout(this.masonryContainer, this.props.options);
+
+ if (this.props.onLayoutComplete) {
+ this.masonry.on('layoutComplete', this.props.onLayoutComplete);
+ }
+
+ if (this.props.onRemoveComplete) {
+ this.masonry.on('removeComplete', this.props.onRemoveComplete);
+ }
+
+ this.latestKnownDomChildren = this.getCurrentDomChildren();
+ }
+ }
+
+ getCurrentDomChildren() {
+ const node = this.masonryContainer;
+ const children = this.props.options.itemSelector ? node.querySelectorAll(this.props.options.itemSelector) : node.children;
+ return Array.prototype.slice.call(children);
+ }
+
+ diffDomChildren() {
+ let forceItemReload = false;
+
+ const knownChildrenStillAttached = this.latestKnownDomChildren.filter((element) => {
+ return !!element.parentNode;
+ });
+
+ if (knownChildrenStillAttached.length !== this.latestKnownDomChildren.length) {
+ forceItemReload = true;
+ }
+
+ const currentDomChildren = this.getCurrentDomChildren();
+
+ const removed = knownChildrenStillAttached.filter((attachedKnownChild) => {
+ return !currentDomChildren.includes(attachedKnownChild);
+ });
+
+ const newDomChildren = currentDomChildren.filter((currentChild) => {
+ return !knownChildrenStillAttached.includes(currentChild);
+ });
+
+ let beginningIndex = 0;
+
+ const prepended = newDomChildren.filter((newChild) => {
+ const prepend = (beginningIndex === currentDomChildren.indexOf(newChild));
+
+ if (prepend) {
+ beginningIndex++;
+ }
+
+ return prepend;
+ });
+
+ const appended = newDomChildren.filter((el) => {
+ return !prepended.includes(el);
+ });
+
+ let moved = [];
+
+ /*
+ * This would always be true (see above about the logic for "removed")
+ */
+ if (removed.length === 0) {
+ moved = knownChildrenStillAttached.filter((child, index) => {
+ return index !== currentDomChildren.indexOf(child);
+ });
+ }
+
+ this.latestKnownDomChildren = currentDomChildren;
+
+ return {
+ old: knownChildrenStillAttached, // Not used
+ new: currentDomChildren, // Not used
+ removed: removed,
+ appended: appended,
+ prepended: prepended,
+ moved: moved,
+ forceItemReload: forceItemReload
+ };
+ }
+
+ performLayout() {
+ const diff = this.diffDomChildren();
+ let reloadItems = diff.forceItemReload || diff.moved.length > 0;
+
+ // Would never be true. (see comments of 'diffDomChildren' about 'removed')
+ if (diff.removed.length > 0) {
+ if (this.props.enableResizableChildren) {
+ diff.removed.forEach(this.erd.removeAllListeners, this.erd);
+ }
+ this.masonry.remove(diff.removed);
+ reloadItems = true;
+ }
+
+ if (diff.appended.length > 0) {
+ this.masonry.appended(diff.appended);
+
+ if (diff.prepended.length === 0) {
+ reloadItems = true;
+ }
+
+ if (this.props.enableResizableChildren) {
+ diff.appended.forEach(this.listenToElementResize, this);
+ }
+ }
+
+ if (diff.prepended.length > 0) {
+ this.masonry.prepended(diff.prepended);
+
+ if (this.props.enableResizableChildren) {
+ diff.prepended.forEach(this.listenToElementResize, this);
+ }
+ }
+
+ if (reloadItems) {
+ this.masonry.reloadItems();
+ }
+
+ this.masonry.layout();
+ }
+
+ derefImagesLoaded() {
+ this.imagesLoadedCancelRef();
+ this.imagesLoadedCancelRef = null;
+ }
+
+ imagesLoaded() {
+ if (this.props.disableImagesLoaded) { return; }
+
+ if (this.imagesLoadedCancelRef) {
+ this.derefImagesLoaded();
+ }
+
+ const event = this.props.updateOnEachImageLoad ? 'progress' : 'always';
+ const handler = debounce(
+ function(instance) {
+ if (this.props.onImagesLoaded) {
+ this.props.onImagesLoaded(instance);
+ }
+ this.masonry.layout();
+ }.bind(this), 100);
+
+ const imgLoad = imagesloaded(this.masonryContainer, this.props.imagesLoadedOptions).on(event, handler);
+
+ this.imagesLoadedCancelRef = function() {
+ imgLoad.off(event, handler);
+ handler.cancel();
+ };
+ }
+
+ initializeResizableChildren() {
+ if (!this.props.enableResizableChildren) { return; }
+
+ this.erd = elementResizeDetectorMaker({
+ strategy: 'scroll'
+ });
+
+ this.latestKnownDomChildren.forEach(this.listenToElementResize, this);
+ }
+
+ listenToElementResize(el) {
+ this.erd.listenTo(el, function() {
+ this.masonry.layout()
+ }.bind(this))
+ }
+
+ destroyErd() {
+ if (this.erd) {
+ this.latestKnownDomChildren.forEach(this.erd.uninstall, this.erd);
+ }
+ }
+
+ componentDidMount() {
+ this.initializeMasonry();
+ this.initializeResizableChildren();
+ this.imagesLoaded();
+ }
+
+ componentDidUpdate() {
+ this.performLayout();
+ this.imagesLoaded();
+ }
+
+ componentWillUnmount() {
+ this.destroyErd();
+
+ // unregister events
+ if (this.props.onLayoutComplete) {
+ this.masonry.off('layoutComplete', this.props.onLayoutComplete);
+ }
+
+ if (this.props.onRemoveComplete) {
+ this.masonry.off('removeComplete', this.props.onRemoveComplete);
+ }
+
+ if (this.imagesLoadedCancelRef) {
+ this.derefImagesLoaded();
+ }
+ this.masonry.destroy();
+ }
+
+ setRef = (n) => {
+ this.masonryContainer = n;
+ }
+
+ render() {
+ const props = omit(this.props, Object.keys(propTypes));
+ return (
+ <div {...props} ref={this.setRef}>
+ {this.props.children}
+ </div>
+ );
+ }
+}
+
+export default MasonryComponent;