/* global Counter */
/* eslint-disable import/no-absolute-path */
/* eslint-disable react/jsx-indent-props */
/* eslint-disable react/jsx-indent */
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { ButtonGroup, Row, Col, Checkbox } from 'react-bootstrap';
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { withTracker } from 'meteor/react-meteor-data';
import { Helmet } from 'react-helmet';
import { Trans, translate } from 'react-i18next';
import { Map } from 'react-leaflet';
import Control from 'react-leaflet-control';
import LoadingBar from '/imports/ui/components/Loading/LoadingBar';
import _ from 'lodash';
import store from 'store';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import 'leaflet-graphicscale/dist/Leaflet.GraphicScale.min.css';
import 'leaflet-graphicscale/dist/Leaflet.GraphicScale.min.js';
import 'leaflet-sleep/Leaflet.Sleep.js';
import geolocation from '/imports/startup/client/geolocation';
import CenterInMyPosition from '/imports/ui/components/CenterInMyPosition/CenterInMyPosition';
import FireList from '/imports/ui/components/Maps/FireList';
import subsUnion from '/imports/ui/components/Maps/SubsUnion/SubsUnion';
import DefMapLayers from '/imports/ui/components/Maps/DefMapLayers';
import FromNow from '/imports/ui/components/FromNow/FromNow';
import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires';
import FireAlertsCollection from '/imports/api/FireAlerts/FireAlerts';
import IndustriesCollection, { industriesRemap } from '/imports/api/Industries/Industries';
import FalsePositivesCollection, { falsePositivesRemap } from '/imports/api/FalsePositives/FalsePositives';
import SiteSettings from '/imports/api/SiteSettings/SiteSettings';
import { isNotHomeAndMobile, isChrome, isAnyMobile } from '/imports/ui/components/Utils/isMobile';
import { isHome } from '/imports/ui/components/Utils/location';
import ShareIt from '/imports/ui/components/ShareIt/ShareIt';
import FullScreenMap from '/imports/ui/components/Maps/FullScreenMap';
import LocationAutocomplete from '/imports/ui/components/LocationAutocomplete/LocationAutocomplete';
import Gkeys from '/imports/startup/client/Gkeys';
import './FiresMap.scss';
const MAXZOOM = 6;
const MAXZOOMREACTIVE = 6;
const DEFZOOM = 8;
const zoom = new ReactiveVar(DEFZOOM);
// https://en.wikipedia.org/wiki/Geographical_midpoint_of_Europe
const center = new ReactiveVar([53.5775, 3.106111]);
const mapSize = new ReactiveVar();
const marks = new ReactiveVar(true);
const showUnion = new ReactiveVar(true);
// Remove map in subscription
class FiresMap extends React.Component {
constructor(props) {
super(props);
this.state = {
viewport: {
center: props.center,
zoom: props.zoom
},
init: false,
useMarkers: props.marks,
scaleAdded: false,
moving: false,
showSubsUnion: props.showUnion
};
const self = this;
// viewportchange
// https://stackoverflow.com/questions/23123138/perform-debounce-in-react-js
this.debounceView = _.debounce((viewport) => {
self.handleViewportChange(viewport);
}, 1500);
this.onViewportChanged = this.onViewportChanged.bind(this);
this.onMoveEnd = this.onMoveEnd.bind(this);
this.onMoveStart = this.onMoveStart.bind(this);
this.fireStats = this.fireStats.bind(this);
}
componentDidMount() {
const self = this;
Gkeys.load(() => {
self.setState({ init: true });
});
}
/* shouldComponentUpdate(nextProps, nextState) {
* const notMoving = !nextState.moving;
* const markersChanged = this.state.useMarkers !== nextState.useMarkers;
* const unionChanged = this.state.showSubsUnion !== nextState.showSubsUnion;
* const otherViewport = this.state.viewport !== nextState.viewport;
* // const init = nextState.viewport.center === [0, 0];
* // console.log(notMoving ? 'Not moving map' : 'Moving map');
* // console.log(otherViewport ? 'Other viewport' : 'Not other viewport');
* console.log(`${otherViewport ? 'OTHER' : 'Not other'} viewport ${nextState.viewport.center} zoom: ${nextState.viewport.zoom}`);
* return this.state.init || (notMoving && otherViewport && this.state.moved) || unionChanged || markersChanged;
* }
*/
shouldComponentUpdate(nextProps, nextState) {
const notMoving = !nextState.moving;
return notMoving;
}
onMoveStart() {
// this.setState({ moving: true });
this.state.moving = true;
}
onMoveEnd() {
// this.setState({ moving: false });
this.state.moving = false;
}
onViewportChanged(viewport) {
this.debounceView.cancel();
this.debounceView(viewport);
}
onAutocompleteChange(value) {
this.handleViewportChange({ center: [value.lat, value.lng], zoom: DEFZOOM - 1 });
}
getMap() {
return this.fireMap.leafletElement;
}
setShowSubsUnion(showSubsUnion) {
this.setState({ showSubsUnion });
store.set('firesmap_showunion', showSubsUnion);
}
componentDidUnMount() {
// this.setState({ init: true });
}
handleViewportChange(viewport) {
console.log(`Viewport changed: ${JSON.stringify(viewport)}`);
if (this.fireMap) {
const bounds = this.getMap().getBounds();
// console.log(bounds);
mapSize.set([bounds.getNorthEast(), bounds.getSouthWest()]);
store.set('firesmap_center', viewport.center);
store.set('firesmap_zoom', viewport.zoom);
if (viewport.zoom > this.state.viewport.zoom) {
this.state.viewport = viewport;
if (Meteor.isDevelopment) console.log('Don\'t query we are in the same point');
return;
}
zoom.set(viewport.zoom);
center.set(viewport.center);
this.state.viewport = viewport;
}
}
centerOnUserLocation(viewport) {
this.setState({ viewport: { center: viewport.center, zoom: 10 } });
}
useMarkers(use) {
this.setState({ useMarkers: use });
store.set('firesmap_marks', use);
marks.set(use);
}
addScale(map) {
// https://www.npmjs.com/package/leaflet-graphicscale
const options = {
fill: 'fill',
showSubunits: true
};
L.control.graphicScale([options]).addTo(map);
}
handleLeafletLoad(map) {
if (map && map.leafletElement && !this.state.moving) {
const lmap = map.leafletElement;
try {
const bounds = lmap.getBounds();
mapSize.set([bounds.getNorthEast(), bounds.getSouthWest()]);
if (!this.state.scaleAdded) {
this.addScale(lmap);
this.state.scaleAdded = true;
}
} catch (e) {
console.warn('Failed to set map bounds and scale');
}
this.state.union = subsUnion(this.state.union, {
map,
subs: this.props.userSubs,
show: this.state.showSubsUnion,
bounds: this.props.userSubsBounds,
fromServer: true,
fit: false
});
}
}
fireStats() {
return (
{ (this.props.activefires.length + this.props.firealerts.length) === 0 ?
{ this.state.viewport.zoom >= MAXZOOMREACTIVE ?
(*)