// @flow import React from "react"; import { Map, ImageOverlay, Marker, LayersControl } from "react-leaflet"; import { CRS, point, divIcon } from "leaflet"; import map from "lodash/map"; import filter from "lodash/filter"; import reduce from "lodash/reduce"; import MqttContext from "mqtt/context"; import type { Controls, Control, UIControl, ControlUI } from "config/flowtypes"; import { renderToString } from 'react-dom/server' export type Point = [number, number]; const convertPoint = ([y, x]: Point): Point => [-x, y]; export type ControlMapProps = { width: number, height: number, zoom: number, layers: Array, controls: Controls, search: string, onChangeControl: (control: Control) => void }; const center = (props: ControlMapProps): Point => convertPoint([ props.width / 2, props.height / 2 ]); const createLeafletIcon = (control: Control, state: State) => { return divIcon({ iconSize: point(36, 36), iconAnchor: point(18, 18), html: renderToString(control.icon.render(state)) }); }; const renderMarker = (props: ControlMapProps) => (control: Control, key: string) => ( {({ state }) => ( props.onChangeControl(control)} > )} ); const safeIncludes = (o: {+type?: string, +text?: string, +topic?: string}, s: string) => { if (o.type != null) { if (o.type.toLowerCase().includes(s)) { return true; } } if (o.text != null) { if (o.text.toLowerCase().includes(s)) { return true; } } if (o.topic != null) { if (o.topic.toLowerCase().includes(s)) { return true; } } return false; }; const isVisible = (props: ControlMapProps) => (c: UIControl & {ui?: Array}) => { if (safeIncludes(c, props.search.toLowerCase())) { return true; } if (c.ui != null) { return reduce(c.ui, (b, e) => b || safeIncludes(e, props.search.toLowerCase()), false); } return false; }; const renderMarkers = (props: ControlMapProps) => map(filter(props.controls, isVisible(props)), renderMarker(props)); const renderLayer = (layer: Layer) => { const LayersControlType = layer.baseLayer ? LayersControl.BaseLayer : LayersControl.Overlay; return ( {}} // eslint-disable-line fp/no-nil removeLayerControl={(_layer) => {}} // eslint-disable-line fp/no-nil // eslint-disable-next-line fp/no-nil addOverlay={(_layer, _name, _checked) => {}} // eslint-disable-next-line fp/no-nil addBaseLayer={(_layer, _name, _checked) => {}}> ); }; const renderLayers = (props: ControlMapProps) => ( {props.layers.map(renderLayer)} ); const ControlMap = (props: ControlMapProps) => ( {renderMarkers(props)} {renderLayers(props)} ); export default ControlMap;