1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
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, WidthSym } from '../../../../fields/Doc';
import { documentSchema } from '../../../../fields/documentSchemas';
import { makeInterface } from '../../../../fields/Schema';
import { NumCast, StrCast } from '../../../../fields/Types';
import { emptyFunction, setupMoveUpEvents } 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';
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<ViewBoxAnnotatableProps & FieldViewProps & Partial<GoogleMapProps>, MapDocument>(MapDocument) {
private _dropDisposer?: DragManager.DragDropDisposer;
private _disposers: { [name: string]: IReactionDisposer } = {};
public static LayoutString(fieldKey: string) { return FieldView.LayoutString(MapBox, fieldKey); }
@observable private _map: google.maps.Map = null as unknown as google.maps.Map;
@observable private selectedPlace: MapMarker | 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 bounds = new window.google.maps.LatLngBounds();
@observable private inputRef = React.createRef<HTMLInputElement>();
@observable private searchMarkers: google.maps.Marker[] = [];
@observable private searchBox = new window.google.maps.places.Autocomplete(this.inputRef.current!, options);
@observable private _savedAnnotations = new ObservableMap<number, HTMLDivElement[]>();
@computed get allSidebarDocs() { return DocListCast(this.dataDoc[this.SidebarKey]); };
@computed get allMapMarkers() { return DocListCast(this.dataDoc[this.annotationKey]); };
@observable private allMarkers: Doc[] = [];
//TODO: change all markers to a filter function to change
@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<SidebarAnnos>();
private _ref: React.RefObject<HTMLDivElement> = 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.allMarkers.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
}
/**
* A function that examines allMapMarkers docs in the map node and form MapMarkers
*/
private fillMarkers = () => {
this.allMapMarkers?.forEach(doc => {
// search for if the map marker exists, else create marker
if (doc.lat !== undefined && doc.lng !== undefined) {
const marker = Docs.Create.MapMarkerDocument(NumCast(doc.lat), NumCast(doc.lng), [doc], {})
this.allMarkers.push(marker)
}
})
}
// TODO: things to ask & think about when designing
// 1. All markers are stored in allMarkers[], when adding a new marker (from a button, ideally not using drawManager),
// the new marker will be stored in allMarkers[]
// currently markerloadhandler only gets called when the map is reloaded, but we want it to be update on the GUI in real time
// TODO ** core issue --> real time updates **
/**
* 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;
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.")
}
this.fitBounds(map);
this.fillMarkers();
// this._map.addListener(drawingManager, 'markercomplete', this.addMarker)
}
//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: any) => {
// set which place was clicked
this.selectedPlace = place;
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) => {
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
return this.addDocument(doc, sidebarKey); // add to sidebar list
}
/**
* What does this do exactly? How to operate on sidebar?
* @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;
}
public get SidebarKey() { return this.fieldKey + "-sidebar"; }
@computed get sidebarHandle() {
const annotated = DocListCast(this.dataDoc[this.SidebarKey]).filter(d => d?.author).length;
//&& !this.isContentActive()
return (!annotated) ? (null) : <div className="mapBox-sidebar-handle" onPointerDown={this.sidebarDown}
style={{
left: `max(0px, calc(100% - ${this.sidebarWidthPercent} ${this.sidebarWidth() ? "- 5px" : "- 10px"}))`,
background: this.props.styleProvider?.(this.rootDoc, this.props as any, StyleProp.WidgetColor + (annotated ? ":annotated" : ""))
}} />;
}
@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;
}
// TODO what's the difference between savedAnnotations & allMapMarkers?
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;
}
private saveMarkerInfo = () => {
}
// create marker prop --> func that
private renderMarkers = () => {
return this.allMarkers.map(place => (
<Marker
key={place[Id]}
position={{ lat: NumCast(place.lat), lng: NumCast(place.lng) }}
onLoad={marker => this.markerLoadHandler(marker, place)}
onClick={e => this.markerClickHandler(e, place)}
/>
))
}
private renderInfoWindow = () => {
return this.infoWindowOpen && this.selectedPlace && (
<InfoWindow
anchor={this.markerMap[this.selectedPlace._markerId!]}
onCloseClick={this.handleInfoWindowClose}
>
<div style={{ backgroundColor: 'white', opacity: 0.75, padding: 12 }}>
<div style={{ fontSize: 16 }}>
<div>
{/* {// TODO need to figure out how to render these childDocs of the MapMarker in InfoWindow
marker.childDocs} */}
<hr />
<form>
<label>Title: </label><br />
<input type="text" id="title" name="title"></input><br />
<label>Desription: </label><br />
<textarea style={{ height: 150 }} id="description" name="description" placeholder="Notes, a short description of this location, a brief comment, etc."></textarea>
<button type="submit">Save</button>
</form>
<hr />
<div>
<button>New link+</button>
</div>
</div>
</div>
</div>
</InfoWindow>
)
}
render() {
return <div className="mapBox" ref={this._ref}
// style={{ pointerEvents: this.isContentActive() ? undefined : "none" }}
>
{/* // {/* <LoadScript
// googleMapsApiKey={process.env.GOOGLE_MAPS!}
// libraries={['places', 'drawing']}
// > */}
<div className="mapBox-wrapper"
onWheel={e => e.stopPropagation()}
onPointerDown={e => (e.button === 0 && !e.ctrlKey) && e.stopPropagation()}
style={{ width: `calc(100% - ${this.sidebarWidthPercent})` }}>
<GoogleMap
mapContainerStyle={mapContainerStyle}
zoom={this.zoom}
// center={this.center}
onLoad={map => this.loadHandler(map)}
options={mapOptions}
>
<Autocomplete
onLoad={this.setSearchBox}
onPlaceChanged={this.handlePlaceChanged}>
<input ref={this.inputRef} className="searchbox" type="text" placeholder="Search anywhere:" />
</Autocomplete>
{this.renderMarkers()}
{this.renderInfoWindow()}
</GoogleMap>
</div>
{/* {/* </LoadScript > */}
<div className="mapBox-sidebar"
style={{ width: `${this.sidebarWidthPercent}`, backgroundColor: `${this.sidebarColor}` }}>
<SidebarAnnos ref={this._sidebarRef}
{...this.props}
fieldKey={this.annotationKey}
rootDoc={this.rootDoc}
layoutDoc={this.layoutDoc}
dataDoc={this.dataDoc}
showSidebar={this.SidebarShown}
nativeWidth={NumCast(this.layoutDoc._nativeWidth)}
whenChildContentsActiveChanged={this.whenChildContentsActiveChanged}
PanelWidth={this.sidebarWidth}
sidebarAddDocument={this.sidebarAddDocument}
moveDocument={this.moveDocument}
removeDocument={this.removeDocument}
//isContentActive={this.isContentActive}
/>
</div>
<div className="mapBox-overlayButton-sidebar" key="sidebar" title="Toggle Sidebar"
style={{
display: !this.props.isContentActive() ? "none" : undefined,
top: StrCast(this.rootDoc._showTitle) === "title" ? 20 : 5,
backgroundColor: this.SidebarShown ? Colors.MEDIUM_BLUE : Colors.BLACK
}}
onPointerDown={this.sidebarBtnDown} >
<FontAwesomeIcon style={{ color: Colors.WHITE }} icon={"comment-alt"} size="sm" />
</div>
{this.sidebarHandle}
</div>;
}
}
|