-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
395 lines (363 loc) · 14.1 KB
/
index.js
File metadata and controls
395 lines (363 loc) · 14.1 KB
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
/**
* Copyright (c) Institut national de l'information géographique et forestière
*
* This program and the accompanying materials are made available under the terms of the GPL License, Version 3.0.
*/
import maplibregl from "maplibre-gl";
import Globals from "./globals";
import MapButtonsListeners from "./map-buttons-listeners";
import MapListeners from "./map-listeners";
import EventListeners from "./event-listeners";
import LayerManager from "./layer-manager/layer-manager";
import LayersConfig from "./layer-manager/layer-config";
import Controls from "./controls";
import RecentSearch from "./search-recent";
import MenuNavigation from "./nav";
import InteractivityIndicator from "./map-interactivity/interactivity-indicator";
import StatusPopups from "./status-popups";
import DOM from "./dom";
import { Capacitor } from "@capacitor/core";
import { SplashScreen } from "@capacitor/splash-screen";
import { StatusBar, Style } from "@capacitor/status-bar";
import { SafeAreaController } from "@aashu-dubey/capacitor-statusbar-safe-area";
import { TextZoom } from "@capacitor/text-zoom";
import { Device } from "@capacitor/device";
import { App } from "@capacitor/app";
import { Toast } from "@capacitor/toast";
import { Protocol } from "pmtiles";
import PinchZoom from "pinch-zoom-js";
// import CSS
import "@maplibre/maplibre-gl-compare/dist/maplibre-gl-compare.css";
import "maplibre-gl/dist/maplibre-gl.css";
import "../css/app.scss";
// Affichage des éléments PWA en mode WE (Toast et ActionSheet)
import { defineCustomElements } from "@ionic/pwa-elements/loader";
defineCustomElements(window);
/**
* Fonction définissant l'application
*/
function app() {
// Ajout du protocole PM Tiles
let protocol = new Protocol();
maplibregl.addProtocol("pmtiles", protocol.tile);
// Ecouteur sur le chargement total des contrôles
window.addEventListener("controlsloaded", async () => {
// Ajout d'autres ecouteurs
EventListeners.addListeners();
SplashScreen.hide();
StatusPopups.getNetworkPopup(map);
StatusPopups.getEditoPopup(map);
App.getLaunchUrl().then( (url) => {
if (url && url.url) {
if (url.url.split("://")[0] === "https") {
const urlParams = new URLSearchParams(url.url.split("?")[1]);
if (urlParams.get("lng") && urlParams.get("lat")) {
const center = { lng: parseFloat(urlParams.get("lng")), lat: parseFloat(urlParams.get("lat")) };
const zoom = parseFloat(urlParams.get("z")) || map.getZoom();
map.setCenter(center);
map.setZoom(zoom);
if (urlParams.get("l1") && urlParams.get("l2") && urlParams.get("m") && urlParams.get("title") && urlParams.get("color")) {
const feature = {
type: "Feature",
id: -1,
geometry: {
type: "Point",
coordinates: [center.lng, center.lat],
},
properties: {
accroche: urlParams.get("title").replace(/%20/g, " "),
theme: urlParams.get("title").replace(/%20/g, " "),
text: urlParams.get("text").replace(/%20/g, " "),
zoom: zoom,
color: urlParams.get("color"),
icon: `compare-landmark-${urlParams.get("color")}`,
layer1: urlParams.get("l1"),
layer2: urlParams.get("l2"),
mode: urlParams.get("m"),
visible: true,
}
};
Globals.myaccount.addCompareLandmark(feature);
Toast.show({
duration: "long",
text: `Point de repère Comparer "${urlParams.get("title").replace(/%20/g, " ")}" ajouté à 'Enregistrés' et à la carte`,
position: "bottom",
});
} else {
map.once("moveend", () => {
const params = { lngLat: center };
if (urlParams.get("titre")) {
params.text = urlParams.get("titre").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
if (urlParams.get("description")) {
params.html = `<p>${urlParams.get("description").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'")}</p>`;
}
}
Globals.position.compute(params).then(() => {
Globals.menu.open("position");
});
if (Globals.searchResultMarker != null) {
Globals.searchResultMarker.remove();
Globals.searchResultMarker = null;
}
Globals.searchResultMarker = new maplibregl.Marker({element: Globals.searchResultIcon, anchor: "bottom"})
.setLngLat(center)
.addTo(map);
});
}
} else if (urlParams.get("newsid")) {
if (!document.querySelector("#newsfeed").classList.contains("d-none")) {
Globals.menu.open("newsfeed");
const element = document.getElementById("newsfeedItem-" + urlParams.get("newsid"));
if (element) {
setTimeout( () => {
element.scrollIntoView(false, {
behavior: "smooth",
});
}, 2000);
}
}
}
}
}
});
// INFO: BUG https://github.com/ionic-team/capacitor-plugins/issues/1160
setTimeout( async () => {
SafeAreaController.injectCSSVariables();
if (Capacitor.isPluginAvailable("StatusBar")) {
const info = await Device.getInfo();
if (!(info.platform === "android" && info.androidSDKVersion < 29)) {
StatusBar.setOverlaysWebView({ overlay: true });
}
StatusBar.setStyle({ style: Style.Light });
}
}, 500);
if (Capacitor.isNativePlatform()) {
TextZoom.getPreferred().then(value => {
const newValue = Math.min(1.5, value.value);
TextZoom.set({
value: newValue
});
document.documentElement.style.fontSize = `calc(13px * ${newValue})`;
document.documentElement.style.setProperty("--text-zoom", newValue);
});
}
if (!localStorage.getItem("hasBeenLaunched")) {
document.getElementById("geolocateBtn").click();
localStorage.setItem("hasBeenLaunched", true);
}
const tempLayers = LayersConfig.getTempLayers();
if (tempLayers.length > 0) {
const layers = tempLayers;
let tempOnboarding = false;
for (let i = 0; i < layers.length; i++) {
const layer = layers[i];
if (layer.onBoardingCfg) {
StatusPopups.getOnboardingModal(layer.id, layer.onBoardingCfg.html);
tempOnboarding = true;
break;
}
}
if (!tempOnboarding) {
StatusPopups.getOnboardingModal();
}
} else {
StatusPopups.getOnboardingModal();
}
// Pour charger un fichier partagé depuis une autre app au démarrage de l'appli
if (Capacitor.getPlatform() === "android") {
if (window.AndroidInterface && window.AndroidInterface.getSharedFileUrl) {
window.dispatchEvent(new CustomEvent("sendIntentReceived", {detail: {url: window.AndroidInterface.getSharedFileUrl()}}));
}
}
// Mise en place du bouton évènements
if (tempLayers.length > 0) {
let layer;
for (let i = 0; i < tempLayers.length; i++) {
layer = tempLayers[i];
if (layer.mainScreenBtn) {
break;
}
}
if (layer.mainScreenBtn) {
const eventButton = document.getElementById("eventMapBtn");
eventButton.classList.remove("d-none");
eventButton.title = layer.mainScreenBtn.title;
eventButton.style.backgroundImage = `url(${layer.mainScreenBtn.iconUrl})`;
eventButton.addEventListener("click", () => {
document.querySelector(`#${layer.id}`).click();
});
}
if (layer.colors) {
document.documentElement.style.setProperty("--event-main", layer.colors.main);
document.documentElement.style.setProperty("--event-light", layer.colors.light);
document.documentElement.style.setProperty("--event-dark", layer.colors.dark);
}
}
});
// Définition des icones
Globals.myPositionIcon = document.createElement("div");
Globals.myPositionIcon.className = "myPositionIcon";
Globals.myPositionIconGrey = document.createElement("div");
Globals.myPositionIconGrey.className = "myPositionIconGrey";
Globals.searchResultIcon = document.createElement("div");
Globals.searchResultIcon.className = "searchResultIcon";
// Main map
const map = new maplibregl.Map({
container: "map",
zoom: 5,
center: [2.0, 47.33],
attributionControl: false,
maxZoom: 21,
locale: "fr",
maxPitch: 60,
crossSourceCollisions: false,
});
// Secondary maps for RLT
const mapRLT1 = new maplibregl.Map({
container: "mapRLT1",
zoom: 5,
center: [2.0, 47.33],
attributionControl: false,
maxZoom: 21,
locale: "fr",
maxPitch: 0,
touchPitch: false,
});
// disable map rotation using right click + drag
mapRLT1.dragRotate.disable();
// disable map rotation using touch rotation gesture
mapRLT1.touchZoomRotate.disableRotation();
const mapRLT2 = new maplibregl.Map({
container: "mapRLT2",
zoom: 5,
center: [2.0, 47.33],
attributionControl: false,
maxZoom: 21,
locale: "fr",
maxPitch: 0,
touchPitch: false,
});
// disable map rotation using right click + drag
mapRLT2.dragRotate.disable();
// disable map rotation using touch rotation gesture
mapRLT2.touchZoomRotate.disableRotation();
// Enregistrement de la carte
Globals.map = map;
Globals.mapRLT1 = mapRLT1;
Globals.mapRLT2 = mapRLT2;
window.scroll({
top: 0,
left: 0,
behavior: "smooth"
});
Globals.currentScrollIndex = 0;
// Ajout des contrôles
Controls.addControls();
// HACK: déplacement de l'échelle hors de la div map pour qu'elle bouge librement
var mapLibreControls = document.querySelectorAll(".maplibregl-ctrl-bottom-left")[2];
var mapLibreFullscreenControl = document.querySelectorAll(".maplibregl-ctrl-bottom-right")[2];
var parent = document.getElementById("bottomButtons");
DOM.$mapScale = mapLibreControls;
parent.appendChild(mapLibreControls);
parent.appendChild(mapLibreFullscreenControl);
// Ajout des ecouteurs des boutons de la carte
MapButtonsListeners.addListeners();
// Ajout des ecouteurs de la carte
MapListeners.addListeners();
// Ajout des recherches recentes issues du localStorage
RecentSearch.create();
// Ajout des sources definies dans la configuration à la carte
// (les couches de fonds, rlt et thématiques sont pre chargées)
// Les sources des couches tuiles vectorielles ne sont pas pré chargées
// car on ne connait pas la liste des sources disponible dans le fichier de style.
for (let layer in LayersConfig.baseLayerSources) {
var source = LayersConfig.baseLayerSources[layer];
if (source.type !== "vector") {
map.addSource(layer, source);
}
}
for (let layer in LayersConfig.rltLayerSources) {
source = LayersConfig.rltLayerSources[layer];
if (source.type !== "vector") {
mapRLT1.addSource(layer, source);
mapRLT2.addSource(layer, source);
}
}
for (let layer in LayersConfig.thematicLayerSources) {
source = LayersConfig.thematicLayerSources[layer];
if (source.type !== "vector") {
map.addSource(layer, source);
}
}
for (let layer in LayersConfig.tempLayerSources) {
source = LayersConfig.tempLayerSources[layer];
map.addSource(layer, source);
const imageUrl = LayersConfig.getTempLayers().filter((config) => config.id === layer)[0].iconUrl;
map.loadImage(imageUrl).then((image) => {
map.addImage(layer, image.data);
});
}
// Ajout de la source pour le cercle de précision
map.addSource("location-precision", {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": []
},
});
// Chargement de la position précédente
if (localStorage.getItem("lastMapLat") && localStorage.getItem("lastMapLng") && localStorage.getItem("lastMapZoom")) {
map.setCenter([localStorage.getItem("lastMapLng"), localStorage.getItem("lastMapLat")]);
map.setZoom(localStorage.getItem("lastMapZoom") || map.getZoom());
}
// Chargement des couches par defaut dans le localStorage
// En premier lieu : on ne garde que les layers bien présents dans l'appli (peut arriver lors d'un màj si on supprime ou remplace une couche)
const newLayersDisplayed = [];
Globals.layersDisplayed.forEach( (layer) => {
if ((layer.id in LayersConfig.thematicLayerSources) || (layer.id in LayersConfig.baseLayerSources) || (layer.id in LayersConfig.tempLayerSources)) {
newLayersDisplayed.push(layer);
}
});
if (!newLayersDisplayed.length) {
newLayersDisplayed.push(
{
id: "PLAN.IGN.INTERACTIF$TMS",
opacity: 100,
visible: true,
gray: false,
}
);
}
Globals.layersDisplayed = newLayersDisplayed;
Globals.manager = new LayerManager({
layers : Globals.layersDisplayed,
});
// INFO
// Indicateur d'activité du Plan IGN interactif sur la carte
// (il doit être placé après le LayerManager afin de connaitre les couches ajoputées par défaut !)
Globals.interactivityIndicator = new InteractivityIndicator(map, {});
// Initialisation du menu de navigation
Globals.menu = new MenuNavigation();
Globals.menu.show();
// HACK: Nécessaire pour iOS qui ne met pas à jour la taille de l'écran au lancement...
if (Capacitor.getPlatform() === "ios") {
map.once("load", () => {
Globals.map.resize();
});
}
setTimeout(() => {
if (!Globals.mapLoaded) {
map.flyTo(map.getCenter());
StatusPopups.getNetworkPopup(map);
}
setTimeout(() => {
if (!Globals.mapLoaded) {
SplashScreen.hide();
StatusPopups.getEditoPopup(map);
}
}, 2000);
}, 4000);
// Pich zoom sur img overlay
new PinchZoom(document.getElementById("imgOverlayImage"));
}
app();