/* global L 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 _ from 'lodash'; import store from 'store'; 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 Loading from '/imports/ui/components/Loading/Loading'; import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires'; import FireAlertsCollection from '/imports/api/FireAlerts/FireAlerts'; import FalsePositivesCollection from '/imports/api/FalsePositives/FalsePositives'; import SiteSettings from '/imports/api/SiteSettings/SiteSettings'; import UserSubsToFiresCollection from '/imports/api/Subscriptions/Subscriptions'; import { isNotHomeAndMobile, isChrome } from '/imports/ui/components/Utils/isMobile'; import { isHome } from '/imports/ui/components/Utils/location'; import ShareIt from '/imports/ui/components/ShareIt/ShareIt'; import './FiresMap.scss'; const MAXZOOM = 6; const MAXZOOMREACTIVE = 6; const zoom = new ReactiveVar(8); const center = new ReactiveVar([0, 0]); const mapSize = new ReactiveVar(); const marks = new ReactiveVar(false); 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: true, 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); } componentDidMount() { } /* 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); } 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.center === this.state.viewport.center && viewport.zoom === this.state.viewport.zoom) { // Do nothing, in same point return; } zoom.set(viewport.zoom); center.set(viewport.center); // this.setState({ viewport }); this.state.viewport = viewport; } } centerOnUserLocation(viewport) { this.setState({ viewport }); } useMarkers(use) { this.setState({ useMarkers: use }); store.set('firesmap_marks', 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, fit: false }); } } render() { const { t } = this.props; console.log(`Rendering ${this.props.loading ? 'loading' : 'LOADED'} map ${this.props.activefires.length + this.props.firealerts.length} of ${this.props.activefirestotal} total. False positives: ${this.props.falsePositives.length}. Subs users ready ${this.props.subsready} (${this.props.userSubs.length}), reactive ${this.state.viewport.zoom >= MAXZOOMREACTIVE}`); const title = `${t('AppName')}: ${t('Fuegos activos')}`; if (Meteor.isDevelopment) { console.log(`False positives total: ${this.props.falsePositivesTotal}`); } return ( /* Large number of markers: https://stackoverflow.com/questions/43015854/large-dataset-of-markers-or-dots-in-leaflet/43019740#43019740 */
{ this.divElement = divElement; }} > { !isHome() && {title} } {this.props.loading || !this.props.subsready ? : ''}

Fuegos activos

{ (this.props.activefires.length + this.props.firealerts.length) === 0 ? No hay fuegos activos en esta zona del mapa. Hay un total de {{ countTotal: this.props.activefirestotal }} fuegos activos detectados en todo el mundo. Datos actualizados . : En rojo, {{ count: this.props.activefires.length + this.props.firealerts.length }} fuegos activos en el mapa. Hay un total de {{ countTotal: this.props.activefirestotal }} fuegos activos detectados en todo el mundo por la NASA. Datos actualizados . }

{isNotHomeAndMobile &&

En naranja, los fuegos notificados por nuestros usuarios/as recientemente.

} {isNotHomeAndMobile && this.setShowSubsUnion(e.target.checked)}> Resaltar en verde el área vigilada por nuestros usuarios/as (*) {(this.state.viewport.zoom >= MAXZOOM) && this.useMarkers(e.target.checked)}> Resaltar los fuegos con un marcador } }

{ this.state.viewport.zoom >= MAXZOOMREACTIVE ? Los fuegos activos se actualizan en tiempo real. : Haga zoom en una zona de su interés si quiere que los fuegos se actualicen en tiempo real. }

{/* https://github.com/CliffCloud/Leaflet.Sleep */} { this.fireMap = map; this.handleLeafletLoad(map); }} className="firesmap-leaflet-container" animate minZoom={5} center={this.state.viewport.center} zoom={this.state.viewport.zoom} preferCanvas viewport={this.state.viewport} onViewportChanged={this.onViewportChanged} sleep={isHome() && !isChrome} onMoveend={this.onMoveEnd} onMovestart={this.onMoveStart} onZoomend={this.onMoveEnd} onZoomstart={this.onMoveStart} sleepTime={10750} wakeTime={750} sleepNote hoverToWake={false} wakeMessage={this.props.t('Pulsa para activar')} wakeMessageTouch={this.props.t('Pulsa para activar')} sleepOpacity={0.6} > {/* http://wiki.openstreetmap.org/wiki/Tile_servers */} {!this.props.loading && = MAXZOOM} useMarkers={this.state.useMarkers} nasa={false} falsePositives /> = MAXZOOM} useMarkers={this.state.useMarkers} nasa falsePositives={false} /> } this.centerOnUserLocation(viewport)} onlyIcon {... this.props} />

(*)Para preservar la privacidad de nuestros usuarios/as, los datos reflejados están aleatoriamente alterados y son solo orientativos.

{ !isHome() && }
); } } FiresMap.propTypes = { loading: PropTypes.bool.isRequired, subsready: PropTypes.bool.isRequired, userSubs: PropTypes.arrayOf(PropTypes.object).isRequired, activefires: PropTypes.arrayOf(PropTypes.object).isRequired, firealerts: PropTypes.arrayOf(PropTypes.object).isRequired, falsePositives: PropTypes.arrayOf(PropTypes.object).isRequired, falsePositivesTotal: PropTypes.number.isRequired, lastCheck: PropTypes.instanceOf(Date), activefirestotal: PropTypes.number.isRequired, center: PropTypes.arrayOf(PropTypes.number), zoom: PropTypes.number, marks: PropTypes.bool.isRequired, showUnion: PropTypes.bool.isRequired, history: PropTypes.object.isRequired, t: PropTypes.func.isRequired }; let geoInit = true; export default translate([], { wait: true })(withTracker(() => { let subscription; const centerStored = store.get('firesmap_center'); const zoomStored = store.get('firesmap_zoom'); const marksStored = store.get('firesmap_marks'); const showUnionStored = store.get('firesmap_showunion'); zoom.set(zoomStored || 8); if (typeof marksStored === 'boolean') { marks.set(marksStored); } if (typeof showUnionStored === 'boolean') { showUnion.set(showUnionStored); } Meteor.autorun(() => { if ((centerStored || geolocation.get()) && geoInit) { center.set(centerStored || geolocation.get()); // console.log(`Geolocation ${geolocation.get()}`); geoInit = false; } if (mapSize.get() && mapSize.get()[0].lng && mapSize.get()[1].lat) { subscription = Meteor.subscribe( 'activefiresmyloc', mapSize.get()[0].lng, mapSize.get()[0].lat, mapSize.get()[1].lng, mapSize.get()[1].lat ); Meteor.subscribe( 'fireAlerts', mapSize.get()[0].lng, mapSize.get()[0].lat, mapSize.get()[1].lng, mapSize.get()[1].lat ); Meteor.subscribe( 'falsePositivesMyloc', mapSize.get()[0].lng, mapSize.get()[0].lat, mapSize.get()[1].lng, mapSize.get()[1].lat ); } }); Meteor.subscribe('activefirestotal'); Meteor.subscribe('falsePositivesTotal'); Meteor.subscribe('settings'); const userSubs = Meteor.subscribe('userSubsToFires'); const lastCheck = SiteSettings.findOne({ name: 'last-fire-check' }); const fireAlerts = FireAlertsCollection.find().fetch(); const falsePositives = FalsePositivesCollection.find().fetch(); return { loading: !subscription ? true : !subscription.ready(), userSubs: UserSubsToFiresCollection.find().fetch(), subsready: userSubs.ready(), // Not reactive query depending on zoom level activefires: ActiveFiresCollection.find({}, { reactive: zoom.get() >= MAXZOOMREACTIVE }).fetch(), activefirestotal: Counter.get('countActiveFires') + fireAlerts.length, falsePositivesTotal: Counter.get('countFalsePositives') + fireAlerts.length, firealerts: fireAlerts, falsePositives, lastCheck: lastCheck ? lastCheck.value : null, center: center.get(), marks: marks.get(), showUnion: showUnion.get(), zoom: zoom.get() }; })(FiresMap));