2020-10-02 19:00:24 +02:00
|
|
|
import * as L from "leaflet";
|
2021-07-23 15:56:22 +02:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
2020-11-05 12:28:02 +01:00
|
|
|
import Svg from "../../Svg";
|
2021-01-03 00:19:42 +01:00
|
|
|
import Img from "../../UI/Base/Img";
|
2021-07-23 15:56:22 +02:00
|
|
|
import {LocalStorageSource} from "../Web/LocalStorageSource";
|
|
|
|
import {VariableUiElement} from "../../UI/Base/VariableUIElement";
|
2021-08-07 23:11:34 +02:00
|
|
|
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
|
2021-08-26 12:15:26 +02:00
|
|
|
import {QueryParameters} from "../Web/QueryParameters";
|
2020-06-28 02:42:22 +02:00
|
|
|
|
2021-06-28 00:45:49 +02:00
|
|
|
export default class GeoLocationHandler extends VariableUiElement {
|
2021-07-23 15:56:22 +02:00
|
|
|
/**
|
|
|
|
* Wether or not the geolocation is active, aka the user requested the current location
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly _isActive: UIEventSource<boolean>;
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-24 02:32:33 +02:00
|
|
|
/**
|
|
|
|
* Wether or not the geolocation is locked, aka the user requested the current location and wants the crosshair to follow the user
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly _isLocked: UIEventSource<boolean>;
|
2021-07-13 14:39:50 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
/**
|
|
|
|
* The callback over the permission API
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly _permission: UIEventSource<string>;
|
|
|
|
/***
|
|
|
|
* The marker on the map, in order to update it
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _marker: L.Marker;
|
|
|
|
/**
|
|
|
|
* Literally: _currentGPSLocation.data != undefined
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly _hasLocation: UIEventSource<boolean>;
|
|
|
|
private readonly _currentGPSLocation: UIEventSource<{
|
|
|
|
latlng: any;
|
|
|
|
accuracy: number;
|
|
|
|
}>;
|
|
|
|
/**
|
|
|
|
* Kept in order to update the marker
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly _leafletMap: UIEventSource<L.Map>;
|
2021-08-19 23:41:48 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
/**
|
|
|
|
* The date when the user requested the geolocation. If we have a location, it'll autozoom to it the first 30 secs
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _lastUserRequest: Date;
|
2021-08-19 23:41:48 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
/**
|
|
|
|
* A small flag on localstorage. If the user previously granted the geolocation, it will be set.
|
|
|
|
* On firefox, the permissions api is broken (probably fingerprint resistiance) and "granted + don't ask again" doesn't stick between sessions.
|
|
|
|
*
|
|
|
|
* Instead, we set this flag. If this flag is set upon loading the page, we start geolocating immediately.
|
|
|
|
* If the user denies the geolocation this time, we unset this flag
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly _previousLocationGrant: UIEventSource<string>;
|
|
|
|
private readonly _layoutToUse: UIEventSource<LayoutConfig>;
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
constructor(
|
|
|
|
currentGPSLocation: UIEventSource<{ latlng: any; accuracy: number }>,
|
|
|
|
leafletMap: UIEventSource<L.Map>,
|
|
|
|
layoutToUse: UIEventSource<LayoutConfig>
|
|
|
|
) {
|
|
|
|
const hasLocation = currentGPSLocation.map(
|
|
|
|
(location) => location !== undefined
|
|
|
|
);
|
|
|
|
const previousLocationGrant = LocalStorageSource.Get(
|
|
|
|
"geolocation-permissions"
|
|
|
|
);
|
|
|
|
const isActive = new UIEventSource<boolean>(false);
|
2021-07-24 02:32:33 +02:00
|
|
|
const isLocked = new UIEventSource<boolean>(false);
|
2021-08-19 23:41:48 +02:00
|
|
|
const permission = new UIEventSource<string>("");
|
|
|
|
const lastClick = new UIEventSource<Date>(undefined);
|
|
|
|
const lastClickWithinThreeSecs = lastClick.map(lastClick => {
|
|
|
|
if (lastClick === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const timeDiff = (new Date().getTime() - lastClick.getTime()) / 1000
|
|
|
|
return timeDiff <= 3
|
|
|
|
})
|
|
|
|
lastClick.addCallbackAndRunD(_ => {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (lastClickWithinThreeSecs.data) {
|
|
|
|
lastClick.ping()
|
|
|
|
}
|
|
|
|
}, 500)
|
|
|
|
})
|
2021-07-23 15:56:22 +02:00
|
|
|
super(
|
|
|
|
hasLocation.map(
|
2021-07-24 02:32:33 +02:00
|
|
|
(hasLocationData) => {
|
2021-08-19 23:41:48 +02:00
|
|
|
if (permission.data === "denied") {
|
|
|
|
return Svg.location_refused_svg();
|
|
|
|
}
|
2021-07-13 14:39:50 +02:00
|
|
|
|
2021-08-19 23:41:48 +02:00
|
|
|
if (!isActive.data) {
|
|
|
|
return Svg.location_empty_svg()
|
|
|
|
}
|
|
|
|
if (!hasLocationData) {
|
|
|
|
// Position not yet found but we are active: we spin to indicate activity
|
|
|
|
const icon = Svg.location_empty_svg()
|
|
|
|
icon.SetStyle("animation: spin 4s linear infinite;")
|
|
|
|
return icon;
|
|
|
|
}
|
2021-07-24 02:32:33 +02:00
|
|
|
if (isLocked.data) {
|
2021-08-19 23:41:48 +02:00
|
|
|
return Svg.location_locked_svg()
|
2021-07-23 15:56:22 +02:00
|
|
|
}
|
2021-08-19 23:41:48 +02:00
|
|
|
if (lastClickWithinThreeSecs.data) {
|
|
|
|
return Svg.location_unlocked_svg()
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a location, so we show a dot in the center
|
|
|
|
return Svg.location_svg();
|
2021-07-23 15:56:22 +02:00
|
|
|
},
|
2021-08-19 23:41:48 +02:00
|
|
|
[isActive, isLocked, permission, lastClickWithinThreeSecs]
|
2021-07-23 15:56:22 +02:00
|
|
|
)
|
|
|
|
);
|
2021-07-27 21:02:30 +02:00
|
|
|
this.SetClass("mapcontrol")
|
2021-07-23 15:56:22 +02:00
|
|
|
this._isActive = isActive;
|
2021-07-24 02:32:33 +02:00
|
|
|
this._isLocked = isLocked;
|
2021-08-19 23:41:48 +02:00
|
|
|
this._permission = permission
|
2021-07-23 15:56:22 +02:00
|
|
|
this._previousLocationGrant = previousLocationGrant;
|
|
|
|
this._currentGPSLocation = currentGPSLocation;
|
|
|
|
this._leafletMap = leafletMap;
|
|
|
|
this._layoutToUse = layoutToUse;
|
|
|
|
this._hasLocation = hasLocation;
|
|
|
|
const self = this;
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
const currentPointer = this._isActive.map(
|
|
|
|
(isActive) => {
|
|
|
|
if (isActive && !self._hasLocation.data) {
|
|
|
|
return "cursor-wait";
|
|
|
|
}
|
|
|
|
return "cursor-pointer";
|
|
|
|
},
|
|
|
|
[this._hasLocation]
|
|
|
|
);
|
|
|
|
currentPointer.addCallbackAndRun((pointerClass) => {
|
|
|
|
self.SetClass(pointerClass);
|
|
|
|
});
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-08-19 23:41:48 +02:00
|
|
|
|
2021-07-24 02:32:33 +02:00
|
|
|
this.onClick(() => {
|
2021-08-19 23:41:48 +02:00
|
|
|
/*
|
|
|
|
* If the previous click was within 3 seconds (and we have an active location), then we lock to the location
|
|
|
|
*/
|
2021-07-24 02:32:33 +02:00
|
|
|
if (self._hasLocation.data) {
|
2021-08-19 23:41:48 +02:00
|
|
|
if (isLocked.data) {
|
|
|
|
isLocked.setData(false)
|
|
|
|
} else if (lastClick.data !== undefined) {
|
|
|
|
const timeDiff = (new Date().getTime() - lastClick.data.getTime()) / 1000
|
|
|
|
if (timeDiff <= 3) {
|
|
|
|
isLocked.setData(true)
|
|
|
|
lastClick.setData(undefined)
|
|
|
|
} else {
|
|
|
|
lastClick.setData(new Date())
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
} else {
|
2021-08-19 23:41:48 +02:00
|
|
|
lastClick.setData(new Date())
|
|
|
|
}
|
2021-07-24 02:32:33 +02:00
|
|
|
}
|
2021-08-19 23:41:48 +02:00
|
|
|
|
2021-08-26 12:15:26 +02:00
|
|
|
self.init(true, true);
|
2021-07-24 02:32:33 +02:00
|
|
|
});
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-08-26 12:15:26 +02:00
|
|
|
const latLonGiven = QueryParameters.wasInitialized("lat") && QueryParameters.wasInitialized("lon")
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-08-26 12:15:26 +02:00
|
|
|
this.init(false, !latLonGiven);
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-08-19 23:41:48 +02:00
|
|
|
isLocked.addCallbackAndRunD(isLocked => {
|
|
|
|
if (isLocked) {
|
|
|
|
leafletMap.data?.dragging?.disable()
|
|
|
|
} else {
|
|
|
|
leafletMap.data?.dragging?.enable()
|
|
|
|
}
|
|
|
|
})
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-08-19 23:41:48 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
this._currentGPSLocation.addCallback((location) => {
|
|
|
|
self._previousLocationGrant.setData("granted");
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
const timeSinceRequest =
|
|
|
|
(new Date().getTime() - (self._lastUserRequest?.getTime() ?? 0)) / 1000;
|
|
|
|
if (timeSinceRequest < 30) {
|
|
|
|
self.MoveToCurrentLoction(16);
|
2021-07-24 02:32:33 +02:00
|
|
|
} else if (self._isLocked.data) {
|
|
|
|
self.MoveToCurrentLoction();
|
2021-07-23 15:56:22 +02:00
|
|
|
}
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
let color = "#1111cc";
|
|
|
|
try {
|
|
|
|
color = getComputedStyle(document.body).getPropertyValue(
|
|
|
|
"--catch-detail-color"
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
const icon = L.icon({
|
2021-08-19 23:41:48 +02:00
|
|
|
iconUrl: Img.AsData(Svg.location.replace(/#000000/g, color).replace(/#000/g, color)),
|
2021-07-23 15:56:22 +02:00
|
|
|
iconSize: [40, 40], // size of the icon
|
|
|
|
iconAnchor: [20, 20], // point of the icon which will correspond to marker's location
|
|
|
|
});
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-24 02:32:33 +02:00
|
|
|
const map = self._leafletMap.data;
|
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
const newMarker = L.marker(location.latlng, {icon: icon});
|
|
|
|
newMarker.addTo(map);
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
if (self._marker !== undefined) {
|
|
|
|
map.removeLayer(self._marker);
|
|
|
|
}
|
|
|
|
self._marker = newMarker;
|
|
|
|
});
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-08-26 12:15:26 +02:00
|
|
|
private init(askPermission: boolean, forceZoom: boolean) {
|
2021-07-24 02:32:33 +02:00
|
|
|
const self = this;
|
2021-07-13 14:39:50 +02:00
|
|
|
|
2021-07-24 02:32:33 +02:00
|
|
|
if (self._isActive.data) {
|
|
|
|
self.MoveToCurrentLoction(16);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-13 14:39:50 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
try {
|
|
|
|
navigator?.permissions
|
|
|
|
?.query({name: "geolocation"})
|
|
|
|
?.then(function (status) {
|
|
|
|
console.log("Geolocation is already", status);
|
|
|
|
if (status.state === "granted") {
|
2021-08-26 12:15:26 +02:00
|
|
|
self.StartGeolocating(forceZoom);
|
2021-07-23 15:56:22 +02:00
|
|
|
}
|
|
|
|
self._permission.setData(status.state);
|
|
|
|
status.onchange = function () {
|
|
|
|
self._permission.setData(status.state);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2021-05-18 19:48:20 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
if (askPermission) {
|
2021-08-26 12:15:26 +02:00
|
|
|
self.StartGeolocating(forceZoom);
|
2021-07-23 15:56:22 +02:00
|
|
|
} else if (this._previousLocationGrant.data === "granted") {
|
|
|
|
this._previousLocationGrant.setData("");
|
2021-08-26 12:15:26 +02:00
|
|
|
self.StartGeolocating(forceZoom);
|
2021-07-23 15:56:22 +02:00
|
|
|
}
|
2021-07-19 16:23:13 +02:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:46:54 +02:00
|
|
|
private MoveToCurrentLoction(targetZoom?: number) {
|
2021-07-23 15:56:22 +02:00
|
|
|
const location = this._currentGPSLocation.data;
|
|
|
|
this._lastUserRequest = undefined;
|
2021-07-19 16:23:13 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
if (
|
|
|
|
this._currentGPSLocation.data.latlng[0] === 0 &&
|
|
|
|
this._currentGPSLocation.data.latlng[1] === 0
|
|
|
|
) {
|
|
|
|
console.debug("Not moving to GPS-location: it is null island");
|
|
|
|
return;
|
|
|
|
}
|
2021-01-02 21:03:40 +01:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
// We check that the GPS location is not out of bounds
|
|
|
|
const b = this._layoutToUse.data.lockLocation;
|
|
|
|
let inRange = true;
|
|
|
|
if (b) {
|
|
|
|
if (b !== true) {
|
|
|
|
// B is an array with our locklocation
|
|
|
|
inRange =
|
|
|
|
b[0][0] <= location.latlng[0] &&
|
|
|
|
location.latlng[0] <= b[1][0] &&
|
|
|
|
b[0][1] <= location.latlng[1] &&
|
|
|
|
location.latlng[1] <= b[1][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inRange) {
|
|
|
|
console.log(
|
|
|
|
"Not zooming to GPS location: out of bounds",
|
|
|
|
b,
|
|
|
|
location.latlng
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this._leafletMap.data.setView(location.latlng, targetZoom);
|
|
|
|
}
|
2021-07-19 16:23:13 +02:00
|
|
|
}
|
2021-05-29 20:30:36 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
private StartGeolocating(zoomToGPS = true) {
|
|
|
|
const self = this;
|
|
|
|
console.log("Starting geolocation");
|
2021-05-29 20:30:36 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
this._lastUserRequest = zoomToGPS ? new Date() : new Date(0);
|
|
|
|
if (self._permission.data === "denied") {
|
|
|
|
self._previousLocationGrant.setData("");
|
2021-08-19 23:41:48 +02:00
|
|
|
self._isActive.setData(false)
|
2021-07-23 15:56:22 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (this._currentGPSLocation.data !== undefined) {
|
|
|
|
this.MoveToCurrentLoction(16);
|
|
|
|
}
|
2021-05-29 20:30:36 +02:00
|
|
|
|
2021-07-23 15:56:22 +02:00
|
|
|
console.log("Searching location using GPS");
|
2021-05-18 19:48:20 +02:00
|
|
|
|
2021-07-24 02:32:33 +02:00
|
|
|
if (self._isActive.data) {
|
|
|
|
return;
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
2021-07-24 02:32:33 +02:00
|
|
|
self._isActive.setData(true);
|
2020-07-31 01:45:54 +02:00
|
|
|
|
2021-07-24 02:32:33 +02:00
|
|
|
navigator.geolocation.watchPosition(
|
|
|
|
function (position) {
|
|
|
|
self._currentGPSLocation.setData({
|
|
|
|
latlng: [position.coords.latitude, position.coords.longitude],
|
|
|
|
accuracy: position.coords.accuracy,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
console.warn("Could not get location with navigator.geolocation");
|
2021-07-26 09:46:54 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
enableHighAccuracy: true
|
2021-07-24 02:32:33 +02:00
|
|
|
}
|
|
|
|
);
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
2021-07-19 16:23:13 +02:00
|
|
|
}
|