import { Autocomplete, GoogleMap, GoogleMapProps, InfoWindow, Marker } from '@react-google-maps/api'; import { action, computed, IReactionDisposer, observable, ObservableMap } from 'mobx'; import { observer } from "mobx-react"; import * as React from "react"; import { DataSym, Doc, DocListCast, FieldsSym, Opt, WidthSym } from '../../../../fields/Doc'; import { documentSchema } from '../../../../fields/documentSchemas'; import { makeInterface } from '../../../../fields/Schema'; import { NumCast, StrCast } from '../../../../fields/Types'; import { emptyFunction, OmitKeys, returnFalse, returnOne, setupMoveUpEvents, Utils } from '../../../../Utils'; import { Docs } from '../../../documents/Documents'; import { DragManager } from '../../../util/DragManager'; import { ViewBoxAnnotatableComponent, ViewBoxAnnotatableProps } from '../../DocComponent'; import { AnchorMenu } from '../../pdf/AnchorMenu'; import { SidebarAnnos } from '../../SidebarAnnos'; import { StyleProp } from '../../StyleProvider'; import { FieldView, FieldViewProps } from '../FieldView'; import "./MapBox.scss"; import { MapMarker } from './MapMarker'; import { DocumentType } from '../../../documents/DocumentTypes'; import { identity } from 'lodash'; import { Id } from '../../../../fields/FieldSymbols'; import { Colors } from '../../global/globalEnums'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { TraceMobx } from '../../../../fields/util'; import { SnappingManager } from '../../../util/SnappingManager'; import { InkTool } from '../../../../fields/InkField'; import { CurrentUserUtils } from '../../../util/CurrentUserUtils'; import { CollectionFreeFormView } from '../../collections/collectionFreeForm'; import { MarqueeAnnotator } from '../../MarqueeAnnotator'; import { Annotation } from '../../pdf/Annotation'; type MapDocument = makeInterface<[typeof documentSchema]>; const MapDocument = makeInterface(documentSchema); export type Coordinates = { lat: number, lng: number, } export type LocationData = { id: string; pos: Coordinates; }; const mapContainerStyle = { height: '100%', }; const defaultCenter = { lat: 38.685, lng: -115.234, }; const mapOptions = { fullscreenControl: false, } const drawingManager = new google.maps.drawing.DrawingManager({ drawingControl: true, drawingControlOptions: { position: google.maps.ControlPosition.TOP_RIGHT, drawingModes: [ google.maps.drawing.OverlayType.MARKER, // currently we are not supporting the following drawing mode on map, a thought for future development // google.maps.drawing.OverlayType.CIRCLE, // google.maps.drawing.OverlayType.POLYLINE, ], }, }); const options = { fields: ["formatted_address", "geometry", "name"], // note: level of details is charged by item per retrieval, not recommended to return all fields strictBounds: false, types: ["establishment"], // type pf places, subject of change according to user need } as google.maps.places.AutocompleteOptions; @observer export class MapBox extends ViewBoxAnnotatableComponent, MapDocument>(MapDocument) { private _dropDisposer?: DragManager.DragDropDisposer; private _disposers: { [name: string]: IReactionDisposer } = {}; private _annotationLayer: React.RefObject = React.createRef(); @observable private _overlayAnnoInfo: Opt; showInfo = action((anno: Opt) => this._overlayAnnoInfo = anno); public static LayoutString(fieldKey: string) { return FieldView.LayoutString(MapBox, fieldKey); } public get SidebarKey() { return this.fieldKey + "-sidebar"; } private _setPreviewCursor: undefined | ((x: number, y: number, drag: boolean, hide: boolean) => void); @computed get inlineTextAnnotations() { return this.allMapMarkers.filter(a => a.textInlineAnnotations); } @observable private _map: google.maps.Map = null as unknown as google.maps.Map; @observable private selectedPlace: MapMarker | Doc | undefined; @observable private markerMap: { [id: string]: google.maps.Marker } = {}; @observable private center = navigator.geolocation ? navigator.geolocation.getCurrentPosition : defaultCenter; @observable private zoom = 2.5; @observable private infoWindowOpen = false; @observable private _marqueeing: number[] | undefined; @observable private _isAnnotating = false; @observable private bounds = new window.google.maps.LatLngBounds(); @observable private inputRef = React.createRef(); @observable private searchMarkers: google.maps.Marker[] = []; @observable private searchBox = new window.google.maps.places.Autocomplete(this.inputRef.current!, options); @observable private _savedAnnotations = new ObservableMap(); @computed get allSidebarDocs() { return DocListCast(this.dataDoc[this.SidebarKey]); }; @computed get allMapMarkers() { return DocListCast(this.dataDoc[this.annotationKey]); }; // method to add MapMarker to allMapMarkers //TODO: change all markers to a filter function to change @observable private toggleAddMarker = false; private _mainCont: React.RefObject = React.createRef(); @observable _showSidebar = false; @computed get SidebarShown() { return this._showSidebar || this.layoutDoc._showSidebar ? true : false; } static _canAnnotate = true; static _hadSelection: boolean = false; private _sidebarRef = React.createRef(); private _ref: React.RefObject = React.createRef(); constructor(props: any) { super(props); } @action private setSearchBox = (searchBox: any) => { this.searchBox = searchBox; } // iterate allMarkers to size, center, and zoom map to contain all markers private fitBounds = (map: google.maps.Map) => { console.log('map bound is:' + this.bounds); this.allMapMarkers.map(place => { this.bounds.extend({ lat: NumCast(place.lat), lng: NumCast(place.lng) }); return place._markerId; }); map.fitBounds(this.bounds) } private hasGeolocation = (doc: Doc) => { return doc.type === DocumentType.IMG } /** * Custom control for add marker button * @param controlDiv * @param map */ private CenterControl = (controlDiv: Element) => { // Set CSS for the control border. const controlUI = document.createElement("div"); controlUI.style.backgroundColor = "#fff"; controlUI.style.border = "2px solid #fff"; controlUI.style.borderRadius = "3px"; controlUI.style.cursor = "pointer"; controlUI.style.marginTop = "8px"; controlUI.style.marginBottom = "22px"; controlUI.style.textAlign = "center"; controlUI.title = "Click to add marker to the location your pointer is at"; controlDiv.appendChild(controlUI); // Set CSS for the control interior. const controlText = document.createElement("div"); controlText.style.color = "rgb(25,25,25)"; controlText.style.fontFamily = "Roboto,Arial,sans-serif"; controlText.style.fontSize = "16px"; controlText.style.lineHeight = "38px"; controlText.style.paddingLeft = "5px"; controlText.style.paddingRight = "5px"; controlText.innerHTML = "Add Marker"; controlUI.appendChild(controlText); // Setup the click event listeners controlUI.addEventListener("click", () => { if (this.toggleAddMarker == true) { this.toggleAddMarker = false; console.log("add marker button status:" + this.toggleAddMarker); controlUI.style.backgroundColor = "#fff"; controlText.style.color = "rgb(25,25,25)"; } else { this.toggleAddMarker = true; console.log("add marker button status:" + this.toggleAddMarker); controlUI.style.backgroundColor = "#4476f7"; controlText.style.color = "rgb(255,255,255)"; }; }); } /** * Place the marker on google maps & store the empty marker as a MapMarker Document in allMarkers list * @param position - the LatLng position where the marker is placed * @param map */ @action private placeMarker = (position: google.maps.LatLng, map: google.maps.Map) => { const marker = new google.maps.Marker({ position: position, map: map }); map.panTo(position); const mapMarker = Docs.Create.MapMarkerDocument(NumCast(position.lat()), NumCast(position.lng()), [], {}); this.addDocument(mapMarker, this.annotationKey); // Doc.AddDocToList(this.dataDoc, this.annotationKey, mapMarker); } /** * A function that examines allMapMarkers docs in the map node and form MapMarkers */ // private fillMarkers = () => { // // console.log("allSidebarDocs:"); // // console.log(this.allSidebarDocs); // this.allSidebarDocs?.forEach(doc => { // console.log(doc); // // search for if the map marker exists, else create marker // if (doc.lat !== undefined && doc.lng !== undefined) { // console.log("image found! loading into marker document...") // const marker = Docs.Create.MapMarkerDocument(NumCast(doc.lat), NumCast(doc.lng), [doc], {}) // Doc.AddDocToList(this.dataDoc, this.annotationKey, marker) // add marker to annotation key // } // }); // // console.log("allMarkers:") // // console.log(this.allMarkers); // } /** * store a reference to google map instance * setup the drawing manager on the top right corner of map * fit map bounds to contain all markers * @param map */ @action private loadHandler = (map: google.maps.Map) => { this._map = map; const centerControlDiv = document.createElement("div"); this.CenterControl(centerControlDiv); map.controls[google.maps.ControlPosition.TOP_RIGHT].push(centerControlDiv); //drawingManager.setMap(map); if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position: GeolocationPosition) => { const pos = { lat: position.coords.latitude, lng: position.coords.longitude, }; this._map.setCenter(pos); } ); } else { alert("Your geolocation is not supported by browser.") }; console.log("all sidebar docs during map loading is:") console.log(this.allSidebarDocs); this.fitBounds(map); // this.fillMarkers(); // listener to addmarker event this._map.addListener('click', (e) => { console.log("add marker map status:" + this.toggleAddMarker); if (this.toggleAddMarker == true) { this.placeMarker(e.latLng, map) console.log(this.allMapMarkers) } }) // this._map.addListener(drawingManager, 'markercomplete', this.addMarker) } // @action // private addMarkerToMap = (latLng: google.maps.LatLng) => { // const marker = new google.maps.Marker({ // map: this._map, // position: latLng, // draggable: true // }); // const newMapMarker = Docs.Create.MapMarkerDocument(NumCast(latLng.lat()), NumCast(latLng.lng()), [], {}); // this.allMarkers.push(newMapMarker); // } //TODO: Is this a valid way for adding marker from drawing manager..? If so, how do we update the allMarkers & render so info window appears // @action // private addMarker = (marker: google.maps.Marker) => { // const markerPosition = marker.getPosition(); // const newMapMarker = Docs.Create.MapMarkerDocument(NumCast(markerPosition?.lat()), NumCast(markerPosition?.lng()), [], {}) // this.allMarkers.push(newMapMarker) // } @action private markerLoadHandler = (marker: google.maps.Marker, place: Doc) => { place[Id] ? this.markerMap[place[Id]] = marker : null; } @action private markerClickHandler = (e: MouseEvent, place: Doc) => { // set which place was clicked this.selectedPlace = place; console.log("you have selected this location:"); console.log(this.selectedPlace); // used so clicking a second marker works if (this.infoWindowOpen) { this.infoWindowOpen = false; console.log("closeinfowindow") } this.infoWindowOpen = true; console.log("open infowindow") } /** * Called when dragging documents into map sidebar * @param doc * @param sidebarKey * @returns */ sidebarAddDocument = (doc: Doc | Doc[], sidebarKey?: string) => { console.log("print all sidebar Docs"); console.log(this.allSidebarDocs); if (!this.layoutDoc._showSidebar) this.toggleSidebar(); const docs = doc instanceof Doc ? [doc] : doc docs.forEach(doc => { if (doc.lat !== undefined && doc.lng !== undefined) { const marker = Docs.Create.MapMarkerDocument(NumCast(doc.lat), NumCast(doc.lng), [], {}) this.addDocument(marker, this.annotationKey) } }) //add to annotation list console.log("sidebaraddDocument"); console.log(doc); return this.addDocument(doc, sidebarKey); // add to sidebar list } /** * Toggle sidebar onclick the tiny comment button on the top right corner * @param e */ sidebarBtnDown = (e: React.PointerEvent) => { setupMoveUpEvents(this, e, (e, down, delta) => { const localDelta = this.props.ScreenToLocalTransform().scale(this.props.scaling?.() || 1).transformDirection(delta[0], delta[1]); const nativeWidth = NumCast(this.layoutDoc[this.fieldKey + "-nativeWidth"]); const curNativeWidth = NumCast(this.layoutDoc.nativeWidth, nativeWidth); const ratio = (curNativeWidth + localDelta[0] / (this.props.scaling?.() || 1)) / nativeWidth; if (ratio >= 1) { this.layoutDoc.nativeWidth = nativeWidth * ratio; this.layoutDoc._width = this.layoutDoc[WidthSym]() + localDelta[0]; this.layoutDoc._showSidebar = nativeWidth !== this.layoutDoc._nativeWidth; } return false; }, emptyFunction, this.toggleSidebar); } sidebarWidth = () => Number(this.sidebarWidthPercent.substring(0, this.sidebarWidthPercent.length - 1)) / 100 * this.props.PanelWidth(); @computed get sidebarWidthPercent() { return StrCast(this.layoutDoc._sidebarWidthPercent, "0%"); } @computed get sidebarColor() { return StrCast(this.layoutDoc.sidebarColor, StrCast(this.layoutDoc[this.props.fieldKey + "-backgroundColor"], "#e4e4e4")); } /** * function that reads the place inputed from searchbox, then zoom in on the location that's been autocompleted; * add a customized temporary marker on the map */ @action private handlePlaceChanged = () => { console.log(this.searchBox); const place = this.searchBox.getPlace(); if (!place.geometry || !place.geometry.location) { // user entered the name of a place that wasn't suggested & pressed the enter key, or place details request failed window.alert("No details available for input: '" + place.name + "'"); return; } // zoom in on the location of the search result if (place.geometry.viewport) { console.log(this._map); this._map.fitBounds(place.geometry.viewport); } else { console.log(this._map); this._map.setCenter(place.geometry.location); this._map.setZoom(17); } // customize icon => customized icon for the nature of the location selected const icon = { url: place.icon as string, size: new google.maps.Size(71, 71), origin: new google.maps.Point(0, 0), anchor: new google.maps.Point(17, 34), scaledSize: new google.maps.Size(25, 25), }; // put temporary cutomized marker on searched location this.searchMarkers.forEach((marker) => { marker.setMap(null); }); this.searchMarkers = []; this.searchMarkers.push( new window.google.maps.Marker({ map: this._map, icon, title: place.name, position: place.geometry.location, }) ) } @action private handleInfoWindowClose = () => { if (this.infoWindowOpen) { this.infoWindowOpen = false; } this.infoWindowOpen = false; this.selectedPlace = undefined; } @computed get sidebarHandle() { TraceMobx(); const annotated = DocListCast(this.dataDoc[this.SidebarKey]).filter(d => d?.author).length; const color = !annotated ? Colors.WHITE : Colors.BLACK; const backgroundColor = !annotated ? this.sidebarWidth() ? Colors.MEDIUM_BLUE : Colors.BLACK : this.props.styleProvider?.(this.rootDoc, this.props as any, StyleProp.WidgetColor + (annotated ? ":annotated" : "")); return (!annotated) ? (null) :
; // return (!annotated) ? (null) :
; } @action toggleSidebar = () => { const prevWidth = this.sidebarWidth(); this.layoutDoc._showSidebar = ((this.layoutDoc._sidebarWidthPercent = StrCast(this.layoutDoc._sidebarWidthPercent, "0%") === "0%" ? "50%" : "0%")) !== "0%"; this.layoutDoc._width = this.layoutDoc._showSidebar ? NumCast(this.layoutDoc._width) * 2 : Math.max(20, NumCast(this.layoutDoc._width) - prevWidth); } sidebarDown = (e: React.PointerEvent) => { setupMoveUpEvents(this, e, this.sidebarMove, emptyFunction, () => setTimeout(this.toggleSidebar), false); } sidebarMove = (e: PointerEvent, down: number[], delta: number[]) => { const bounds = this._ref.current!.getBoundingClientRect(); this.layoutDoc._sidebarWidthPercent = "" + 100 * Math.max(0, (1 - (e.clientX - bounds.left) / bounds.width)) + "%"; this.layoutDoc._showSidebar = this.layoutDoc._sidebarWidthPercent !== "0%"; e.preventDefault(); return false; } setPreviewCursor = (func?: (x: number, y: number, drag: boolean, hide: boolean) => void) => this._setPreviewCursor = func; @action onMarqueeDown = (e: React.PointerEvent) => { if (!e.altKey && e.button === 0 && this.props.isContentActive(true) && ![InkTool.Highlighter, InkTool.Pen].includes(CurrentUserUtils.SelectedTool)) { setupMoveUpEvents(this, e, action(e => { MarqueeAnnotator.clearAnnotations(this._savedAnnotations); this._marqueeing = [e.clientX, e.clientY]; return true; }), returnFalse, () => MarqueeAnnotator.clearAnnotations(this._savedAnnotations), false); } } @action finishMarquee = (x?: number, y?: number) => { this._marqueeing = undefined; this._isAnnotating = false; x !== undefined && y !== undefined && this._setPreviewCursor?.(x, y, false, false); } addDocumentWrapper = (doc: Doc | Doc[], annotationKey?: string) => { return this.addDocument(doc, annotationKey); } getAnchor = () => { const anchor = AnchorMenu.Instance?.GetAnchor(this._savedAnnotations) ?? this.rootDoc // if anchormenu pops up else return rootDoc (map) // Docs.Create.MapMarkerDocument(this.allMapMarkers, { // title: StrCast(this.rootDoc.title + " " + this.layoutDoc._scrollTop), // annotationOn: this.rootDoc, // y: NumCast(this.layoutDoc._scrollTop), // }); // this.addDocument(anchor); return anchor; } // create marker prop --> func that private renderMarkers = () => { return this.allMapMarkers.map(place => ( this.markerLoadHandler(marker, place)} onClick={e => this.markerClickHandler(e, place)} /> )) } private renderInfoWindow = () => { return this.infoWindowOpen && this.selectedPlace && (
{/* {// TODO need to figure out how to render these childDocs of the MapMarker in InfoWindow marker.childDocs} */}




) } panelWidth = () => this.props.PanelWidth() / (this.props.scaling?.() || 1) - this.sidebarWidth(); // (this.Document.scrollHeight || Doc.NativeHeight(this.Document) || 0); panelHeight = () => this.props.PanelHeight() / (this.props.scaling?.() || 1); // () => this._pageSizes.length && this._pageSizes[0] ? this._pageSizes[0].width : Doc.NativeWidth(this.Document); scrollXf = () => this.props.ScreenToLocalTransform().translate(0, NumCast(this.layoutDoc._scrollTop)); transparentFilter = () => [...this.props.docFilters(), Utils.IsTransparentFilter()]; opaqueFilter = () => [...this.props.docFilters(), Utils.IsOpaqueFilter()]; anchorMenuClick = () => this._sidebarRef.current?.anchorMenuClick; @computed get annotationLayer() { TraceMobx(); return
{this.inlineTextAnnotations.sort((a, b) => NumCast(a.y) - NumCast(b.y)).map(anno => ) }
; } render() { const renderAnnotations = (docFilters?: () => string[]) => ; return
{/* // {/* */}
e.stopPropagation()} onPointerDown={e => (e.button === 0 && !e.ctrlKey) && e.stopPropagation()} style={{ width: `calc(100% - ${this.sidebarWidthPercent})` }}>
{renderAnnotations(this.transparentFilter)}
{renderAnnotations(this.opaqueFilter)} {SnappingManager.GetIsDragging() ? (null) : renderAnnotations()} {this.annotationLayer} this.loadHandler(map)} options={mapOptions} > {this.renderMarkers()} {this.renderInfoWindow()} {!this._marqueeing || !this._mainCont.current || !this._annotationLayer.current ? (null) : }
{/* {/*
*/}
{this.sidebarHandle}
; } }