aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionMapView.tsx
blob: 64946e59e80982d63c8d7ff67771cfc08831fa01 (plain)
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
import { GoogleApiWrapper, Map as GeoMap, MapProps, Marker } from "google-maps-react";
import { observer } from "mobx-react";
import { Doc, Opt, DocListCast, FieldResult } from "../../../new_fields/Doc";
import { documentSchema } from "../../../new_fields/documentSchemas";
import { Id } from "../../../new_fields/FieldSymbols";
import { makeInterface } from "../../../new_fields/Schema";
import { Cast, NumCast, ScriptCast, StrCast } from "../../../new_fields/Types";
import "./CollectionMapView.scss";
import { CollectionSubView } from "./CollectionSubView";
import React = require("react");
import { DocumentManager } from "../../util/DocumentManager";
import { UndoManager, undoBatch } from "../../util/UndoManager";
import { IReactionDisposer, reaction, computed, runInAction } from "mobx";
import requestPromise = require("request-promise");

type MapSchema = makeInterface<[typeof documentSchema]>;
const MapSchema = makeInterface(documentSchema);

export type LocationData = google.maps.LatLngLiteral & {
    address?: string
    resolvedAddress?: string;
    zoom?: number;
};

// Nowhere, Oklahoma
const defaultLocation = { lat: 35.1592238, lng: -98.444512, zoom: 15 };

const query = async (data: string | google.maps.LatLngLiteral) => {
    const contents = typeof data === "string" ? `address=${data.replace(/\s+/g, "+")}` : `latlng=${data.lat},${data.lng}`;
    const target = `https://maps.googleapis.com/maps/api/geocode/json?${contents}&key=${process.env.GOOGLE_MAPS_GEO}`;
    try {
        return JSON.parse(await requestPromise.get(target));
    } catch {
        return undefined;
    }
};

@observer
class CollectionMapView extends CollectionSubView<MapSchema, Partial<MapProps> & { google: any }>(MapSchema) {

    private _cancelAddrReq = new Map<string, boolean>();
    private _cancelLocReq = new Map<string, boolean>();
    private _initialLookupPending = new Map<string, boolean>();
    private addressUpdaters: IReactionDisposer[] = [];
    private latlngUpdaters: IReactionDisposer[] = [];

    /**
     * Note that all the uses of runInAction below are not included
     * as a way to update observables (documents handle this already
     * in their property setters), but rather to create a single bulk
     * update and thus prevent uneeded invocations of the location-
     * and address–updating reactions. 
     */

    private getLocation = (doc: Opt<Doc>, fieldKey: string): Opt<LocationData> => {
        if (doc) {
            const lat: Opt<number> = Cast(doc[fieldKey + "-lat"], "number", null) || (Cast(doc[fieldKey + "-lat"], "string", null) && Number(Cast(doc[fieldKey + "-lat"], "string", null))) || undefined;
            const lng: Opt<number> = Cast(doc[fieldKey + "-lng"], "number", null) || (Cast(doc[fieldKey + "-lng"], "string", null) && Number(Cast(doc[fieldKey + "-lng"], "string", null))) || undefined;
            const zoom: Opt<number> = Cast(doc[fieldKey + "-zoom"], "number", null) || (Cast(doc[fieldKey + "-zoom"], "string", null) && Number(Cast(doc[fieldKey + "-zoom"], "string", null))) || undefined;
            const address: Opt<string> = Cast(doc[fieldKey + "-address"], "string", null);
            if (lat !== undefined && lng !== undefined) {
                return ({ lat, lng, zoom });
            } else if (address) {
                const id = doc[Id];
                if (!this._initialLookupPending.get(id)) {
                    this._initialLookupPending.set(id, true);
                    setTimeout(() => {
                        this.respondToAddressChange(address, doc).then(() => this._initialLookupPending.delete(id));
                    });
                }
                return defaultLocation;
            }
        }
        return undefined;
    }

    private markerClick = async (layout: Doc, { lat, lng, zoom }: LocationData) => {
        const batch = UndoManager.StartBatch("marker click");
        runInAction(() => {
            this.layoutDoc[this.props.fieldKey + "-mapCenter-lat"] = lat;
            this.layoutDoc[this.props.fieldKey + "-mapCenter-lng"] = lng;
            zoom && (this.layoutDoc[this.props.fieldKey + "-mapCenter-zoom"] = zoom);
        });
        if (layout.isLinkButton && DocListCast(layout.links).length) {
            await DocumentManager.Instance.FollowLink(undefined, layout, (doc: Doc, where: string, finished?: () => void) => {
                this.props.addDocTab(doc, where);
                finished?.();
            }, false, this.props.ContainingCollectionDoc, batch.end, undefined);
        } else {
            ScriptCast(layout.onClick)?.script.run({ this: layout, self: Cast(layout.rootDocument, Doc, null) || layout });
            batch.end();
        }
    }

    private renderMarkerIcon = (layout: Doc) => {
        const iconUrl = StrCast(this.props.Document.mapIconUrl, null);
        if (iconUrl) {
            const iconWidth = NumCast(layout["mapLocation-iconWidth"], 45);
            const iconHeight = NumCast(layout["mapLocation-iconHeight"], 45);
            const iconSize = new google.maps.Size(iconWidth, iconHeight);
            return {
                size: iconSize,
                scaledSize: iconSize,
                url: iconUrl
            };
        }
    }

    private renderMarker = (layout: Doc) => {
        const location = this.getLocation(layout, "mapLocation");
        return !location ? (null) :
            <Marker
                key={layout[Id]}
                label={StrCast(layout.title)}
                position={location}
                onClick={() => this.markerClick(layout, location)}
                icon={this.renderMarkerIcon(layout)}
            />;
    }

    private respondToAddressChange = async (newAddress: string, doc: Doc) => {
        const { results } = await query(newAddress);
        if (!results?.length) {
            return;
        }
        const { geometry, formatted_address } = results[0];
        const { lat, lng } = geometry.location;
        runInAction(() => {
            if (doc["mapLocation-lat"] !== lat || doc["mapLocation-lng"] !== lng) {
                this._cancelLocReq.set(doc[Id], true);
                Doc.SetInPlace(doc, "mapLocation-lat", lat, true);
                Doc.SetInPlace(doc, "mapLocation-lng", lng, true);
            }
            if (formatted_address !== newAddress) {
                this._cancelAddrReq.set(doc[Id], true);
                Doc.SetInPlace(doc, "mapLocation-address", formatted_address, true);
            }
        });
    }

    private respondToLocationChange = async (newLat: FieldResult, newLng: FieldResult, doc: Doc) => {
        const { results } = await query({ lat: NumCast(newLat), lng: NumCast(newLng) });
        if (!results?.length) {
            return;
        }
        const { formatted_address } = results[0];
        if (formatted_address !== doc["mapLocation-address"]) {
            this._cancelAddrReq.set(doc[Id], true);
            Doc.SetInPlace(doc, "mapLocation-address", formatted_address, true);
        }
    }

    @computed get contents() {
        this.addressUpdaters.forEach(disposer => disposer());
        this.addressUpdaters = [];
        this.latlngUpdaters.forEach(disposer => disposer());
        this.latlngUpdaters = [];
        return this.childLayoutPairs.map(({ layout }) => {
            this.addressUpdaters.push(reaction(
                () => ({ lat: layout["mapLocation-lat"], lng: layout["mapLocation-lng"] }),
                ({ lat, lng }) => {
                    if (this._cancelLocReq.get(layout[Id])) {
                        this._cancelLocReq.set(layout[Id], false);
                    } else if (lat !== undefined && lng !== undefined) {
                        this.respondToLocationChange(lat, lng, layout);
                    }
                }
            ));
            this.latlngUpdaters.push(reaction(
                () => ({ address: Cast(layout["mapLocation-address"], "string", null) }),
                ({ address }) => {
                    if (this._cancelAddrReq.get(layout[Id])) {
                        this._cancelAddrReq.set(layout[Id], false);
                    } else if (address?.length) {
                        this.respondToAddressChange(address, layout);
                    }
                }
            ));
            return this.renderMarker(layout);
        });
    }

    render() {
        const { childLayoutPairs } = this;
        const { Document, fieldKey, active, google } = this.props;
        let center = this.getLocation(Document, fieldKey + "-mapCenter");
        if (center === undefined) {
            center = childLayoutPairs.map(pair => this.getLocation(pair.layout, "mapLocation")).find(layout => layout);
            if (center === undefined) {
                center = defaultLocation;
            }
        }
        return <div className="collectionMapView" ref={this.createDashEventsTarget}>
            <div className={"collectionMapView-contents"}
                style={{ pointerEvents: active() ? undefined : "none" }}
                onWheel={e => e.stopPropagation()}
                onPointerDown={e => (e.button === 0 && !e.ctrlKey) && e.stopPropagation()} >
                <GeoMap
                    google={google}
                    zoom={center.zoom || 10}
                    initialCenter={center}
                    center={center}
                    onDragend={undoBatch((_props: MapProps, map: google.maps.Map) => {
                        const { lat, lng } = map.getCenter();
                        runInAction(() => {
                            Document[fieldKey + "-mapCenter-lat"] = lat();
                            Document[fieldKey + "-mapCenter-lng"] = lng();
                        });
                    })}
                >
                    {this.contents}
                </GeoMap>
            </div>
        </div>;
    }

}

export default GoogleApiWrapper({
    apiKey: process.env.GOOGLE_MAPS!,
    LoadingContainer: () => (
        <div className={"loadingWrapper"}>
            <img className={"loadingGif"} src={"/assets/loading.gif"} />
        </div>
    )
})(CollectionMapView) as any;