forked from georchestra/sviewer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidget.js
More file actions
1596 lines (1484 loc) · 78.1 KB
/
Copy pathwidget.js
File metadata and controls
1596 lines (1484 loc) · 78.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// sViewer × Grist widget
// ---------------------------------------------------------------------------
// i18n
// ---------------------------------------------------------------------------
var I18N = {
fr: {
'loading': 'Chargement…',
'clear': '✕ Désélectionner',
'clear.title': 'Effacer la sélection',
'geom.col': 'Géométrie :',
'label.col': 'Étiquette :',
'lat.col': 'Latitude :',
'lon.col': 'Longitude :',
'auto.detected': 'Colonne géométrie détectée : ',
'auto.detected.ll': 'Colonnes lat/lon détectées : ',
'choose.col': 'Choisir la colonne géométrie',
'choose.lat': 'Choisir la colonne latitude',
'choose.lon': 'Choisir la colonne longitude',
'features': ' entités',
'skipped': ' ignorées',
'settings': 'Paramètres de la carte',
'settings.geom.mode': 'Mode géométrie',
'settings.geom.mode.auto': 'Auto (détection automatique)',
'settings.geom.mode.geojson': 'GeoJSON {"type":"Point"…}',
'settings.geom.mode.latlon': 'Lat + Lon (2 colonnes numériques)',
'settings.geom.mode.lonlat_str': 'Texte "2.35,48.85" (lon,lat)',
'settings.geom.mode.latlon_str': 'Texte "48.85,2.35" (lat,lon)',
'settings.geom.mode.wkt': 'WKT "POINT(2.35 48.85)"',
'settings.geom': 'Colonne géométrie',
'settings.lat': 'Colonne latitude',
'settings.lon': 'Colonne longitude',
'settings.label': 'Colonne étiquette',
'settings.autozoom': 'Zoom automatique sur sélection d\'une ligne',
'settings.features': 'Données',
'settings.selection': 'Ligne sélectionnée',
'settings.fill': 'Couleur',
'settings.fill.opacity': 'Opacité remplissage',
'settings.sel.fill': 'Couleur',
'settings.sel.fill.opacity': 'Opacité remplissage',
'settings.stroke.width': 'Épaisseur contour (px)',
'settings.sel.stroke.width': 'Épaisseur contour (px)',
'settings.inline.opacity': 'opacité',
'settings.inline.width': 'épaisseur',
'settings.title': 'Titre (title=)',
'settings.layers': 'Donnée via WMS',
'settings.md': 'Donnée via catalogue',
'settings.lb': 'Fond de carte',
'settings.wms.opacity': 'Opacité données WMS',
'settings.svbase': 'URL de base sViewer',
'settings.apibase': 'URL de base API Grist',
'settings.georchestra': 'URL de base geOrchestra',
'settings.section.data': 'Données',
'settings.section.advanced': 'Avancé',
'settings.section.help': 'Aide',
'settings.save': 'Appliquer',
'settings.cancel': 'Annuler',
'settings.json': 'Configuration JSON',
'settings.export': 'Exporter',
'settings.import': 'Importer',
'settings.import.apply': 'Appliquer',
'settings.import.placeholder': 'Coller le JSON exporté ici…',
'settings.export.done': 'Paramètres copiés dans le presse-papiers',
'settings.import.error': 'JSON invalide ou incompatible',
'settings.save.reminder': '⚠ Cliquez sur Enregistrer dans la barre Grist pour conserver les paramètres',
'edit.label': 'Saisir :',
'edit.type.point': 'Point',
'edit.type.line': 'Ligne',
'edit.type.polygon': 'Surface',
'edit.save': '✓ Enregistrer',
'edit.cancel': '✗ Annuler',
'edit.saved': '✓ Géométrie enregistrée — ligne ',
'edit.error': '⚠ Erreur — modifications non enregistrées',
'edit.noaccess': '⚠ Accès complet requis pour modifier — changez le niveau d\'accès dans les paramètres du widget Grist',
'edit.instr.type': 'Choisir le type de géométrie',
'edit.instr.point': 'Cliquez sur la carte pour placer le point',
'edit.instr.line': 'Cliquez pour ajouter des sommets — double-clic ou Enregistrer pour terminer',
'edit.instr.polygon': 'Cliquez pour ajouter des sommets — double-clic ou Enregistrer pour fermer',
'edit.instr.confirm': 'Tracé terminé — cliquez Enregistrer pour sauvegarder',
'edit.latlon.noline': '⚠ Mode lat/lon : édition point uniquement'
},
en: {
'loading': 'Loading…',
'clear': '✕ Deselect',
'clear.title': 'Clear selection',
'geom.col': 'Geometry:',
'label.col': 'Label:',
'lat.col': 'Latitude:',
'lon.col': 'Longitude:',
'auto.detected': 'Geometry column auto-detected: ',
'auto.detected.ll': 'Lat/lon columns auto-detected: ',
'choose.col': 'Choose geometry column',
'choose.lat': 'Choose latitude column',
'choose.lon': 'Choose longitude column',
'features': ' features',
'skipped': ' skipped',
'settings': 'Map settings',
'settings.geom.mode': 'Geometry mode',
'settings.geom.mode.auto': 'Auto (automatic detection)',
'settings.geom.mode.geojson': 'GeoJSON {"type":"Point"…}',
'settings.geom.mode.latlon': 'Lat + Lon (2 numeric columns)',
'settings.geom.mode.lonlat_str': 'Text "2.35,48.85" (lon,lat)',
'settings.geom.mode.latlon_str': 'Text "48.85,2.35" (lat,lon)',
'settings.geom.mode.wkt': 'WKT "POINT(2.35 48.85)"',
'settings.geom': 'Geometry column',
'settings.lat': 'Latitude column',
'settings.lon': 'Longitude column',
'settings.label': 'Label column',
'settings.autozoom': 'Auto-zoom on row selection',
'settings.features': 'Data',
'settings.selection': 'Selected row',
'settings.fill': 'Color',
'settings.fill.opacity': 'Fill opacity',
'settings.sel.fill': 'Color',
'settings.sel.fill.opacity': 'Fill opacity',
'settings.stroke.width': 'Stroke width (px)',
'settings.sel.stroke.width': 'Stroke width (px)',
'settings.inline.opacity': 'opacity',
'settings.inline.width': 'width',
'settings.title': 'Title (title=)',
'settings.layers': 'Data via WMS',
'settings.md': 'Data via catalogue',
'settings.lb': 'Background map',
'settings.wms.opacity': 'WMS layer opacity',
'settings.svbase': 'sViewer base URL',
'settings.apibase': 'Grist API base URL',
'settings.georchestra': 'geOrchestra base URL',
'settings.section.data': 'Data',
'settings.section.advanced': 'Advanced',
'settings.section.help': 'Help',
'settings.save': 'Apply',
'settings.cancel': 'Cancel',
'settings.json': 'JSON config',
'settings.export': 'Export',
'settings.import': 'Import',
'settings.import.apply': 'Apply',
'settings.import.placeholder': 'Paste exported JSON here…',
'settings.export.done': 'Settings copied to clipboard',
'settings.import.error': 'Invalid or incompatible JSON',
'settings.save.reminder': '⚠ Click Save in the Grist bar to persist settings',
'edit.label': 'Enter:',
'edit.type.point': 'Point',
'edit.type.line': 'Line',
'edit.type.polygon': 'Area',
'edit.save': '✓ Save',
'edit.cancel': '✗ Cancel',
'edit.saved': '✓ Geometry saved — row ',
'edit.error': '⚠ Error — changes not saved',
'edit.noaccess': '⚠ Full access required to edit — change the access level in Grist widget settings',
'edit.instr.type': 'Choose geometry type',
'edit.instr.point': 'Click on the map to place the point',
'edit.instr.line': 'Click to add vertices — double-click or Save to finish',
'edit.instr.polygon': 'Click to add vertices — double-click or Save to close',
'edit.instr.confirm': 'Drawing complete — click Save to store',
'edit.latlon.noline': '⚠ Lat/lon mode: point editing only'
},
es: {
'loading': 'Cargando…',
'clear': '✕ Deseleccionar',
'clear.title': 'Borrar selección',
'geom.col': 'Geometría:',
'label.col': 'Etiqueta:',
'lat.col': 'Latitud:',
'lon.col': 'Longitud:',
'auto.detected': 'Columna de geometría detectada: ',
'auto.detected.ll': 'Columnas lat/lon detectadas: ',
'choose.col': 'Elegir columna de geometría',
'choose.lat': 'Elegir columna de latitud',
'choose.lon': 'Elegir columna de longitud',
'features': ' entidades',
'skipped': ' omitidas',
'settings': 'Configuración del mapa',
'settings.geom.mode': 'Modo geometría',
'settings.geom.mode.auto': 'Auto (detección automática)',
'settings.geom.mode.geojson': 'GeoJSON {"type":"Point"…}',
'settings.geom.mode.latlon': 'Lat + Lon (2 columnas numéricas)',
'settings.geom.mode.lonlat_str': 'Texto "2.35,48.85" (lon,lat)',
'settings.geom.mode.latlon_str': 'Texto "48.85,2.35" (lat,lon)',
'settings.geom.mode.wkt': 'WKT "POINT(2.35 48.85)"',
'settings.geom': 'Columna de geometría',
'settings.lat': 'Columna de latitud',
'settings.lon': 'Columna de longitud',
'settings.label': 'Columna de etiqueta',
'settings.autozoom': 'Zoom automático al seleccionar fila',
'settings.features': 'Datos',
'settings.selection': 'Fila seleccionada',
'settings.fill': 'Color',
'settings.fill.opacity': 'Opacidad relleno',
'settings.sel.fill': 'Color',
'settings.sel.fill.opacity': 'Opacidad relleno',
'settings.stroke.width': 'Grosor contorno (px)',
'settings.sel.stroke.width': 'Grosor contorno (px)',
'settings.inline.opacity': 'opacidad',
'settings.inline.width': 'grosor',
'settings.title': 'Título (title=)',
'settings.layers': 'Dato via WMS',
'settings.md': 'Dato via catálogo',
'settings.lb': 'Mapa de fondo',
'settings.wms.opacity': 'Opacidad capa WMS',
'settings.svbase': 'URL base sViewer',
'settings.apibase': 'URL base API Grist',
'settings.georchestra': 'URL base geOrchestra',
'settings.section.data': 'Datos',
'settings.section.advanced': 'Avanzado',
'settings.section.help': 'Ayuda',
'settings.save': 'Aplicar',
'settings.cancel': 'Cancelar',
'settings.json': 'Configuración JSON',
'settings.export': 'Exportar',
'settings.import': 'Importar',
'settings.import.apply': 'Aplicar',
'settings.import.placeholder': 'Pegar JSON exportado aquí…',
'settings.export.done': 'Configuración copiada al portapapeles',
'settings.import.error': 'JSON inválido o incompatible',
'settings.save.reminder': '⚠ Haga clic en Guardar en la barra Grist para conservar los ajustes',
'edit.label': 'Introducir:',
'edit.type.point': 'Punto',
'edit.type.line': 'Línea',
'edit.type.polygon': 'Superficie',
'edit.save': '✓ Guardar',
'edit.cancel': '✗ Cancelar',
'edit.saved': '✓ Geometría guardada — fila ',
'edit.error': '⚠ Error — cambios no guardados',
'edit.noaccess': '⚠ Se requiere acceso completo para editar — cambie el nivel de acceso en los ajustes del widget Grist',
'edit.instr.type': 'Elegir tipo de geometría',
'edit.instr.point': 'Haga clic en el mapa para colocar el punto',
'edit.instr.line': 'Haga clic para añadir vértices — doble clic o Guardar para terminar',
'edit.instr.polygon': 'Haga clic para añadir vértices — doble clic o Guardar para cerrar',
'edit.instr.confirm': 'Trazado terminado — haga clic en Guardar para almacenar',
'edit.latlon.noline': '⚠ Modo lat/lon: solo edición de punto'
},
de: {
'loading': 'Laden…',
'clear': '✕ Auswahl aufheben',
'clear.title': 'Auswahl löschen',
'geom.col': 'Geometrie:',
'label.col': 'Beschriftung:',
'lat.col': 'Breitengrad:',
'lon.col': 'Längengrad:',
'auto.detected': 'Geometriespalte erkannt: ',
'auto.detected.ll': 'Lat/Lon-Spalten erkannt: ',
'choose.col': 'Geometriespalte wählen',
'choose.lat': 'Breitengradpalte wählen',
'choose.lon': 'Längengradpalte wählen',
'features': ' Objekte',
'skipped': ' übersprungen',
'settings': 'Karteneinstellungen',
'settings.geom.mode': 'Geometriemodus',
'settings.geom.mode.auto': 'Auto (automatische Erkennung)',
'settings.geom.mode.geojson': 'GeoJSON {"type":"Point"…}',
'settings.geom.mode.latlon': 'Lat + Lon (2 numerische Spalten)',
'settings.geom.mode.lonlat_str': 'Text "2.35,48.85" (lon,lat)',
'settings.geom.mode.latlon_str': 'Text "48.85,2.35" (lat,lon)',
'settings.geom.mode.wkt': 'WKT "POINT(2.35 48.85)"',
'settings.geom': 'Geometriespalte',
'settings.lat': 'Breitengradpalte',
'settings.lon': 'Längengradpalte',
'settings.label': 'Beschriftungsspalte',
'settings.autozoom': 'Automatischer Zoom bei Zeilenauswahl',
'settings.features': 'Daten',
'settings.selection': 'Ausgewählte Zeile',
'settings.fill': 'Farbe',
'settings.fill.opacity': 'Fülldeckkraft',
'settings.sel.fill': 'Farbe',
'settings.sel.fill.opacity': 'Fülldeckkraft',
'settings.stroke.width': 'Konturstärke (px)',
'settings.sel.stroke.width': 'Konturstärke (px)',
'settings.inline.opacity': 'Deck.',
'settings.inline.width': 'Stärke',
'settings.title': 'Titel (title=)',
'settings.layers': 'Daten via WMS',
'settings.md': 'Daten via Katalog',
'settings.lb': 'Hintergrundkarte',
'settings.wms.opacity': 'WMS-Ebene Deckkraft',
'settings.svbase': 'sViewer Basis-URL',
'settings.apibase': 'Grist API Basis-URL',
'settings.georchestra': 'geOrchestra Basis-URL',
'settings.section.data': 'Daten',
'settings.section.advanced': 'Erweitert',
'settings.section.help': 'Hilfe',
'settings.save': 'Anwenden',
'settings.cancel': 'Abbrechen',
'settings.json': 'JSON-Konfiguration',
'settings.export': 'Exportieren',
'settings.import': 'Importieren',
'settings.import.apply': 'Anwenden',
'settings.import.placeholder': 'Exportiertes JSON hier einfügen…',
'settings.export.done': 'Einstellungen in Zwischenablage kopiert',
'settings.import.error': 'Ungültiges oder inkompatibles JSON',
'settings.save.reminder': '⚠ Klicken Sie in der Grist-Leiste auf Speichern, um die Einstellungen zu behalten',
'edit.label': 'Eingabe:',
'edit.type.point': 'Punkt',
'edit.type.line': 'Linie',
'edit.type.polygon': 'Fläche',
'edit.save': '✓ Speichern',
'edit.cancel': '✗ Abbrechen',
'edit.saved': '✓ Geometrie gespeichert — Zeile ',
'edit.error': '⚠ Fehler — Änderungen nicht gespeichert',
'edit.noaccess': '⚠ Vollzugriff erforderlich — Zugriffsebene in den Grist-Widget-Einstellungen ändern',
'edit.instr.type': 'Geometrietyp wählen',
'edit.instr.point': 'Klicken Sie auf die Karte, um den Punkt zu setzen',
'edit.instr.line': 'Klicken zum Hinzufügen von Punkten — Doppelklick oder Speichern zum Beenden',
'edit.instr.polygon': 'Klicken zum Hinzufügen von Punkten — Doppelklick oder Speichern zum Schließen',
'edit.instr.confirm': 'Zeichnung abgeschlossen — Speichern zum Übernehmen klicken',
'edit.latlon.noline': '⚠ Lat/Lon-Modus: nur Punktbearbeitung'
}
};
// slice(0,2) : garde uniquement le code langue sans le sous-tag région (ex. "fr-FR" → "fr")
var lang = (navigator.language || 'en').slice(0, 2);
var t = I18N[lang] || I18N['en'];
function tr(key) { return t[key] || key; }
function applyDomTranslations() {
document.documentElement.lang = lang;
document.querySelectorAll('[data-i18n],[data-i18n-title],[data-i18n-aria],[data-i18n-placeholder]').forEach(function(el) {
var key = el.getAttribute('data-i18n');
if (key && t[key]) { el.textContent = t[key]; }
var titleKey = el.getAttribute('data-i18n-title');
if (titleKey && t[titleKey]) { el.title = t[titleKey]; }
var ariaKey = el.getAttribute('data-i18n-aria');
if (ariaKey && t[ariaKey]) { el.setAttribute('aria-label', t[ariaKey]); }
var phKey = el.getAttribute('data-i18n-placeholder');
if (phKey && t[phKey]) { el.placeholder = t[phKey]; }
});
}
// ---------------------------------------------------------------------------
// État
// ---------------------------------------------------------------------------
var GEOM_CANDIDATES = ['geometry', 'geom', 'geo', 'shape', 'wkb_geometry'];
var LABEL_CANDIDATES = ['label', 'nom', 'name', 'libelle', 'titre', 'title'];
var LAT_CANDIDATES = ['latitude', 'lat'];
var LON_CANDIDATES = ['longitude', 'lon', 'lng'];
var colGeom = null; // colonne géométrie active
var colLabel = null; // colonne étiquette active (optionnelle)
var colLat = null; // colonne latitude (mode lat/lon)
var colLon = null; // colonne longitude (mode lat/lon)
var colGeomMode = 'auto'; // mode géométrie : auto|geojson|latlon|lonlat_str|latlon_str|wkt
var initialLayers = null; // valeur de layers= à l'init — rechargement si modifiée
var initialMd = null; // valeur de md= à l'init — rechargement si modifiée
var optionsLoaded = false; // vrai une fois applyOptions() appelé au moins une fois
var widgetOptions = null; // options widget Grist (surcharges par instance)
var featureByRowId = {}; // id ligne Grist → OL Feature
var allColumns = []; // noms de colonnes du dernier onRecords
var allRecords = []; // enregistrements bruts du dernier onRecords
var svConfig = {}; // clés de configuration (widget options)
var mapReady = false; // vrai une fois SViewer.init() résolu
var gristDocId = null; // identifiant du document Grist (pour l'URL de partage)
var gristTableId = null; // identifiant de la table Grist (pour l'URL de partage)
var debounceTimer = null;
var selectedRowId = null; // id de la ligne Grist sélectionnée (pour le surlignage post-rebuild)
var lastRecordsFingerprint = null; // empreinte JSON pour éviter un rebuild si seule la sélection a changé
var viewFitted = false; // vrai une fois le premier fit de vue effectué
var layerBuilt = false; // vrai une fois les données OL passées à SViewer via loadFeatureObjects
var editMode = false; // vrai pendant l'édition de géométrie
var editRowId = null; // id de la ligne en cours d'édition
var editOrigGeom = null; // géométrie OL originale sérialisée (pour annulation)
var editDrawType = null; // type Draw en cours : 'Point'|'LineString'|'Polygon'
var editDrawInteraction = null; // instance ol.interaction.Draw active
var editDraftGeom = null; // géométrie OL dessinée (après drawend, avant confirm)
var editVertexCount = 0; // nombre de sommets placés (pour activer Terminer)
var gristAccessLevel = 'full'; // niveau d'accès accordé par Grist ('none'|'read table'|'full') — optimistic default, corrected by onOptions
// ---------------------------------------------------------------------------
// Utilitaires
// ---------------------------------------------------------------------------
function setStatus(msg) {
document.getElementById('sv-status').textContent = msg;
}
// Accepte un objet GeoJSON géométrie ou une chaîne JSON équivalente.
// Retourne l'objet géométrie, ou null si non parseable.
function parseGeom(val) {
if (!val) { return null; }
var g = (typeof val === 'string') ? (function() { try { return JSON.parse(val); } catch(e) { return null; } }()) : val;
if (g && g.type && g.coordinates) { return g; }
return null;
}
// Détecte les colonnes géométrie, lat/lon et étiquette depuis les noms de colonnes et la première ligne.
// Retourne { geom, lat, lon, label, mode } — chaque champ peut être null.
// Priorité : GeoJSON > paire lat/lon > colonne texte "lat,lon" ou "lon,lat".
function detectColumns(columns, firstRow) {
var names = columns.map(function(c) { return c.toLowerCase(); });
var geom = null, lbl = null, lat = null, lon = null, mode = 'auto';
GEOM_CANDIDATES.forEach(function(c) {
if (!geom && names.indexOf(c) !== -1) { geom = columns[names.indexOf(c)]; }
});
// Fallback : scan de la première ligne si aucun nom candidat ne correspond
if (!geom && firstRow) {
columns.forEach(function(c) {
if (!geom && parseGeom(firstRow[c])) { geom = c; }
});
}
if (geom && firstRow) {
// Name matched — check actual value to distinguish GeoJSON vs WKT
var geomVal = firstRow[geom];
if (parseGeom(geomVal)) {
mode = 'geojson';
} else if (typeof ol !== 'undefined') {
// Value is not GeoJSON — try WKT before committing
var wktProbe = new ol.format.WKT();
try { wktProbe.readGeometry(geomVal); mode = 'wkt'; } catch(e) { geom = null; }
} else {
geom = null;
}
} else if (geom) {
mode = 'geojson'; // no firstRow to probe, assume GeoJSON
}
// Si pas de colonne géométrie, chercher une paire lat/lon
if (!geom) {
LAT_CANDIDATES.forEach(function(c) { if (!lat && names.indexOf(c) !== -1) { lat = columns[names.indexOf(c)]; } });
LON_CANDIDATES.forEach(function(c) { if (!lon && names.indexOf(c) !== -1) { lon = columns[names.indexOf(c)]; } });
if (!lat || !lon) { lat = null; lon = null; }
if (lat && lon) { mode = 'latlon'; }
}
// Fallback : chercher une colonne WKT
if (!geom && !lat && firstRow && typeof ol !== 'undefined') {
var wktParser = new ol.format.WKT();
columns.forEach(function(c) {
if (geom || lat) { return; }
var val = firstRow[c];
if (typeof val !== 'string') { return; }
try { wktParser.readGeometry(val); geom = c; mode = 'wkt'; } catch(e) { /* not WKT */ }
});
}
// Fallback : chercher une colonne texte "N,N" — tente geo_point_2d (lat,lon) puis lon,lat
if (!geom && !lat && firstRow) {
columns.forEach(function(c) {
if (geom || lat) { return; }
var val = firstRow[c];
if (typeof val !== 'string') { return; }
var parts = val.split(',');
if (parts.length !== 2) { return; }
var a = parseFloat(parts[0].trim()), b = parseFloat(parts[1].trim());
if (isNaN(a) || isNaN(b)) { return; }
// lat in [-90,90], lon in [-180,180] → first value is lat → latlon_str
if (Math.abs(a) <= 90 && Math.abs(b) <= 180) { geom = c; mode = 'latlon_str'; }
// otherwise assume lon,lat
else if (Math.abs(b) <= 90 && Math.abs(a) <= 180) { geom = c; mode = 'lonlat_str'; }
});
}
LABEL_CANDIDATES.forEach(function(c) {
if (!lbl && names.indexOf(c) !== -1) { lbl = columns[names.indexOf(c)]; }
});
return { geom: geom, lat: lat, lon: lon, label: lbl, mode: mode };
}
// Remplit les selects géométrie, lat, lon et étiquette du panneau de configuration.
// Affiche la ligne géométrie OU les lignes lat/lon selon le mode actif.
function populateColumnPicker(columns) {
var selGeom = document.getElementById('sv-cfg-geom');
var selLat = document.getElementById('sv-cfg-lat');
var selLon = document.getElementById('sv-cfg-lon');
var selLbl = document.getElementById('sv-cfg-label');
var noneLbl = lang === 'fr' ? '(aucune)' : lang === 'de' ? '(keine)' : lang === 'es' ? '(ninguna)' : '(none)';
selGeom.options.length = 0;
selLat.options.length = 0;
selLon.options.length = 0;
selLbl.options.length = 0;
// Placeholder désactivé pour géométrie, lat, lon
[['sv-cfg-geom', 'choose.col'], ['sv-cfg-lat', 'choose.lat'], ['sv-cfg-lon', 'choose.lon']].forEach(function(pair) {
var sel = document.getElementById(pair[0]);
var ph = document.createElement('option');
ph.value = ''; ph.textContent = tr(pair[1]); ph.disabled = true;
sel.appendChild(ph);
});
var noneOpt = document.createElement('option');
noneOpt.value = ''; noneOpt.textContent = noneLbl;
selLbl.appendChild(noneOpt);
columns.forEach(function(col) {
function makeOpt() { var o = document.createElement('option'); o.value = col; o.textContent = col; o.title = col; return o; }
selGeom.appendChild(makeOpt());
selLat.appendChild(makeOpt());
selLon.appendChild(makeOpt());
selLbl.appendChild(makeOpt());
});
syncColumnPickerMode();
}
// Affiche les bons sélecteurs de colonnes selon le mode géométrie actif.
// latlon → lat+lon; geojson/auto/lonlat_str/latlon_str → colonne unique.
function syncColumnPickerMode() {
var modeEl = document.getElementById('sv-cfg-geom-mode');
var selGeom = document.getElementById('sv-cfg-geom');
var selLat = document.getElementById('sv-cfg-lat');
var selLon = document.getElementById('sv-cfg-lon');
var selLbl = document.getElementById('sv-cfg-label');
if (!selGeom) { return; }
var mode = modeEl ? modeEl.value : colGeomMode;
var isLatLon = (mode === 'latlon');
var isStrMode = (mode === 'lonlat_str' || mode === 'latlon_str');
var lblGeom = document.querySelector('label[for="sv-cfg-geom"]');
var lblLat = document.querySelector('label[for="sv-cfg-lat"]');
var lblLon = document.querySelector('label[for="sv-cfg-lon"]');
// geom column: visible for geojson/auto/str modes; hidden for latlon
if (lblGeom) { lblGeom.style.display = isLatLon ? 'none' : ''; }
selGeom.style.display = isLatLon ? 'none' : '';
// lat/lon columns: visible only for latlon mode
if (lblLat) { lblLat.style.display = isLatLon ? '' : 'none'; }
if (lblLon) { lblLon.style.display = isLatLon ? '' : 'none'; }
selLat.style.display = isLatLon ? '' : 'none';
selLon.style.display = isLatLon ? '' : 'none';
// sync column select values to current state (but never overwrite the mode select —
// this function is called from the change handler, so modeEl already has the user's value)
if (!isLatLon) {
selGeom.value = colGeom || '';
} else {
selLat.value = colLat || '';
selLon.value = colLon || '';
}
if (selLbl) { selLbl.value = colLabel || ''; }
}
// Retourne un ol.style.Text pour une valeur d'étiquette (null-safe).
// text !== 0 : autorise la valeur zéro comme étiquette valide.
var LABEL_MAX_RESOLUTION = 19.11; // ~zoom 13 in EPSG:3857
function makeTextStyle(text, bold, resolution) {
if (!text && text !== 0) { return null; }
if (resolution !== undefined && resolution > LABEL_MAX_RESOLUTION) { return null; }
return new ol.style.Text({
text: String(text),
font: (bold ? 'bold ' : '') + '13px sans-serif',
fill: new ol.style.Fill({ color: '#222' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 3 }),
offsetY: -14
});
}
// Construit une couleur rgba à partir d'une couleur CSS et d'une opacité (0–1).
function colorWithOpacity(color, opacity) {
var arr = ol.color.asArray(color).slice();
arr[3] = opacity;
return 'rgba(' + arr.join(',') + ')';
}
// Retourne une fonction de style OL : cercle pour Point/MultiPoint, fill+stroke pour les autres.
// Lit la propriété _label posée sur chaque feature dans rebuildLayer.
function makeFeatureStyle(cfg, selected) {
var fillColorSolid = colorWithOpacity(cfg.fillColor, 1);
var fillColorPoly = colorWithOpacity(cfg.fillColor, cfg.fillOpacity);
var strokeColor = cfg.strokeColor;
var radius = selected ? 10 : 9;
var sw = cfg.strokeWidth;
var haloWidth = sw + 2;
return function(feature, resolution) {
var geomType = feature.getGeometry() ? feature.getGeometry().getType() : '';
var isPoint = geomType === 'Point' || geomType === 'MultiPoint';
var isLine = geomType === 'LineString' || geomType === 'MultiLineString';
var text = makeTextStyle(feature.get('_label'), true, resolution);
if (isPoint) {
var pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: radius,
fill: new ol.style.Fill({ color: fillColorSolid }),
stroke: new ol.style.Stroke({ color: '#fff', width: 1.5 })
}),
text: text
});
var haloPoint = new ol.style.Style({
image: new ol.style.Circle({
radius: radius + 2,
fill: new ol.style.Fill({ color: 'rgba(0,0,0,0)' }),
stroke: new ol.style.Stroke({ color: strokeColor, width: 2 })
})
});
return selected ? [pointStyle, haloPoint] : pointStyle;
}
var colorStyle = new ol.style.Style({
fill: isLine ? null : new ol.style.Fill({ color: fillColorPoly }),
stroke: new ol.style.Stroke({ color: strokeColor, width: sw }),
text: text
});
if (haloWidth <= 0) { return colorStyle; }
return [
new ol.style.Style({ stroke: new ol.style.Stroke({ color: '#fff', width: haloWidth }) }),
colorStyle
];
};
}
// Returns geojsonStyle defaults from customConfig/hardConfig, with built-in fallbacks.
function geojsonStyleDefaults() {
var gs = (window.customConfig && window.customConfig.geojsonStyle) ||
(window.SViewer.hardConfig && window.SViewer.hardConfig.geojsonStyle) || {};
return {
color: safeColor(gs.color, '#0077bb'),
fillOpacity: gs.fillOpacity !== undefined ? gs.fillOpacity : 0.35,
strokeWidth: gs.strokeWidth !== undefined ? gs.strokeWidth : 4
};
}
// Extrait les paramètres de style depuis svConfig pour les entités normales ou sélectionnées.
function getStyleCfg(selected) {
var d = geojsonStyleDefaults();
if (selected) {
return {
fillColor: safeColor(svConfig.sel_fill_color, '#ee7733'),
fillOpacity: svConfig.sel_fill_opacity !== undefined ? parseFloat(svConfig.sel_fill_opacity) : 0.5,
strokeColor: safeColor(svConfig.sel_fill_color, '#ee7733'),
strokeWidth: svConfig.sel_stroke_width !== undefined ? parseFloat(svConfig.sel_stroke_width) : d.strokeWidth + 1
};
}
return {
fillColor: safeColor(svConfig.fill_color, d.color),
fillOpacity: svConfig.fill_opacity !== undefined ? parseFloat(svConfig.fill_opacity) : d.fillOpacity,
strokeColor: safeColor(svConfig.fill_color, d.color),
strokeWidth: svConfig.stroke_width !== undefined ? parseFloat(svConfig.stroke_width) : d.strokeWidth
};
}
// Builds a layer-level style function that renders selected feature differently.
// Uses selectedRowId + featureByRowId at render time — no per-feature setStyle().
function makeLayerStyleFn() {
var baseCfg = getStyleCfg(false);
var selCfg = getStyleCfg(true);
var baseStyleFn = makeFeatureStyle(baseCfg, false);
var selStyleFn = makeFeatureStyle(selCfg, true);
return function(feature, resolution) {
return (selectedRowId !== null && featureByRowId[selectedRowId] === feature)
? selStyleFn(feature, resolution)
: baseStyleFn(feature, resolution);
};
}
// Trigger redraw — style fn reads selectedRowId at render time, declutter preserved.
function applySelectionStyle() {
if (!layerBuilt || !mapReady) { return; }
SViewer.refreshVector();
}
// ---------------------------------------------------------------------------
// Édition de géométrie (point / ligne / polygone)
// ---------------------------------------------------------------------------
// Modes point-only : latlon cols et str formats ne peuvent pas stocker ligne/polygone
function isPointOnlyMode() {
return colGeomMode === 'latlon' || colGeomMode === 'latlon_str' || colGeomMode === 'lonlat_str';
}
function syncEditButton() {
var ids = ['sv-btn-type-point', 'sv-btn-type-line', 'sv-btn-type-polygon'];
var noAccess = gristAccessLevel !== 'full';
var lbl = document.getElementById('sv-edit-label');
if (lbl) { lbl.style.display = noAccess ? 'none' : ''; }
ids.forEach(function(id) {
var btn = document.getElementById(id);
if (!btn) { return; }
btn.style.display = noAccess ? 'none' : '';
btn.disabled = !selectedRowId || editMode;
});
// point-only mode: disable line/polygon
if (!noAccess && selectedRowId && !editMode) {
var pointOnly = isPointOnlyMode();
var lineBtn = document.getElementById('sv-btn-type-line');
var polyBtn = document.getElementById('sv-btn-type-polygon');
if (lineBtn) { lineBtn.disabled = pointOnly; }
if (polyBtn) { polyBtn.disabled = pointOnly; }
}
}
function removeDraw() {
var map = SViewer.getMap();
if (map && editDrawInteraction) {
map.removeInteraction(editDrawInteraction);
}
editDrawInteraction = null;
editDrawType = null;
editVertexCount = 0;
}
function exitEdit() {
removeDraw();
editMode = false;
editRowId = null;
editOrigGeom = null;
editDraftGeom = null;
document.getElementById('sv-btn-edit-finish').style.display = 'none';
document.getElementById('sv-btn-edit-cancel').style.display = 'none';
syncEditButton();
setStatus('');
}
// Serialize OL geometry to string for write-back to Grist.
// Returns { fields } or null if format incompatible with geom type.
function geomToFields(olGeom) {
var fmt = new ol.format.GeoJSON();
var geojsonGeom = JSON.parse(fmt.writeGeometry(olGeom, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' }));
var geomType = olGeom.getType(); // 'Point', 'LineString', 'Polygon'
var fields = {};
if (colGeomMode === 'latlon' || colGeomMode === 'latlon_str' || colGeomMode === 'lonlat_str') {
if (geomType !== 'Point') { return null; }
var coords = geojsonGeom.coordinates; // [lon, lat]
var lon = coords[0], lat = coords[1];
if (colGeomMode === 'latlon') { fields[colLat] = lat; fields[colLon] = lon; }
else if (colGeomMode === 'latlon_str') { fields[colGeom] = lat + ',' + lon; }
else { fields[colGeom] = lon + ',' + lat; }
} else if (colGeomMode === 'wkt') {
var wktFmt = new ol.format.WKT();
fields[colGeom] = wktFmt.writeGeometry(olGeom, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });
} else {
// geojson or auto
fields[colGeom] = JSON.stringify(geojsonGeom);
}
return fields;
}
// Phase 2: geometry drawn, waiting for confirm
function onDrawEnd(olGeom) {
editDraftGeom = olGeom;
removeDraw();
// Update the feature on map immediately so user sees the result
var feat = featureByRowId[editRowId];
if (feat) {
feat.setGeometry(olGeom);
applySelectionStyle();
}
var finishBtn = document.getElementById('sv-btn-edit-finish');
finishBtn.disabled = false;
finishBtn.textContent = tr('edit.save');
setStatus(tr('edit.instr.confirm'));
}
// Phase 1: activate Draw interaction for chosen type
function startDraw(olType) {
var map = SViewer.getMap();
if (!map) { return; }
removeDraw();
editDrawType = olType;
editVertexCount = 0;
var minPoints = olType === 'Point' ? 1 : olType === 'LineString' ? 2 : 3;
// Keep existing feature visible during draw — user sees original position for reference
var feat = featureByRowId[editRowId];
if (feat) { feat.setStyle(null); } // null = revert to layer style
var drawSketchStyle = new ol.style.Style({
fill: new ol.style.Fill({ color: 'rgba(255,215,0,0.3)' }),
stroke: new ol.style.Stroke({ color: '#FFD700', width: 2.5 }),
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({ color: '#FFD700' }),
stroke: new ol.style.Stroke({ color: '#000', width: 1.5 })
})
});
editDrawInteraction = new ol.interaction.Draw({
type: olType,
clickTolerance: 12,
stopClick: true,
style: drawSketchStyle
});
var finishBtn = document.getElementById('sv-btn-edit-finish');
finishBtn.disabled = true;
var instrKey = olType === 'Point' ? 'edit.instr.point' : olType === 'LineString' ? 'edit.instr.line' : 'edit.instr.polygon';
setStatus(tr(instrKey));
editDrawInteraction.on('drawstart', function() {
editVertexCount = 0;
});
// Track vertex count via geometry change on the sketch feature
editDrawInteraction.on('drawstart', function(evt) {
evt.feature.getGeometry().on('change', function() {
var geom = evt.feature.getGeometry();
var count = 0;
if (olType === 'Point') { count = 1; }
else if (olType === 'LineString') { count = geom.getCoordinates().length; }
else { count = geom.getCoordinates()[0].length - 1; } // polygon ring, last = first
editVertexCount = count;
finishBtn.disabled = count < minPoints;
});
});
editDrawInteraction.on('drawend', function(evt) {
onDrawEnd(evt.feature.getGeometry());
});
map.addInteraction(editDrawInteraction);
// Type buttons: highlight active
['Point', 'LineString', 'Polygon'].forEach(function(t) {
var id = t === 'Point' ? 'sv-btn-type-point' : t === 'LineString' ? 'sv-btn-type-line' : 'sv-btn-type-polygon';
var b = document.getElementById(id);
if (b) { b.style.fontWeight = t === olType ? 'bold' : ''; }
});
}
// Called by type buttons (Point/Ligne/Polygone) — enters edit mode for chosen type
function startEdit(olType) {
if (gristAccessLevel !== 'full') { setStatus(tr('edit.noaccess')); return; }
if (!selectedRowId) { return; }
if (editMode) { cancelEdit(); } // switch type mid-edit: cancel current draw first
editMode = true;
editRowId = selectedRowId;
editDraftGeom = null;
// Serialize original geometry for cancel restoration
var feat = featureByRowId[editRowId];
if (feat && feat.getGeometry()) {
var fmt = new ol.format.GeoJSON();
editOrigGeom = fmt.writeGeometry(feat.getGeometry());
} else {
editOrigGeom = null;
}
// Show save/cancel, disable type buttons during draw
document.getElementById('sv-btn-edit-finish').style.display = '';
document.getElementById('sv-btn-edit-cancel').style.display = '';
syncEditButton(); // disables type buttons while editMode=true
startDraw(olType);
}
function finishEdit() {
if (editDraftGeom) {
// Already drawn — confirm write-back
if (!editRowId) { exitEdit(); return; }
var fields = geomToFields(editDraftGeom);
if (!fields) { setStatus(tr('edit.latlon.noline')); exitEdit(); return; }
var rowId = editRowId;
exitEdit();
grist.selectedTable.update({ id: rowId, fields: fields }).then(function() {
setStatus(tr('edit.saved') + rowId);
setTimeout(function() { if (document.getElementById('sv-status').textContent === tr('edit.saved') + rowId) { setStatus(''); } }, 4000);
}).catch(function(e) {
console.error('[sviewer] geometry update failed:', e);
setStatus(tr('edit.error'));
});
} else if (editDrawInteraction) {
// Still drawing — trigger finish (OL will fire drawend)
editDrawInteraction.finishDrawing();
}
}
function cancelEdit() {
// Restore original geometry on map if feature exists
var feat = editRowId ? featureByRowId[editRowId] : null;
if (feat) {
if (editOrigGeom) {
var fmt = new ol.format.GeoJSON();
feat.setGeometry(fmt.readGeometry(editOrigGeom));
} else {
feat.setGeometry(null);
}
applySelectionStyle();
}
exitEdit();
}
// ---------------------------------------------------------------------------
// Widget options (persistance par instance via grist.widgetApi)
// ---------------------------------------------------------------------------
// Persiste les colonnes actives + les clés svConfig dans les options widget Grist.
function saveOptions() {
if (!grist.widgetApi || typeof grist.widgetApi.setOptions !== 'function') { return; }
var opts = {};
if (colGeom) { opts._colGeom = colGeom; }
if (colLat) { opts._colLat = colLat; }
if (colLon) { opts._colLon = colLon; }
if (colLabel) { opts._colLabel = colLabel; }
var configKeys = ['fill_color', 'fill_opacity', 'stroke_width',
'sel_fill_color', 'sel_fill_opacity', 'sel_stroke_width',
'title', 'layers', 'md', 'sviewer_base', 'grist_api_base', 'georchestra_base',
'geom_mode', 'autozoom', 'lb', 'wms_opacity'];
configKeys.forEach(function(k) { if (svConfig[k] !== undefined) { opts[k] = svConfig[k]; } });
grist.widgetApi.setOptions(opts).catch(function(e) { console.warn('[sviewer] setOptions failed:', e); });
}
// Applique les options widget : restaure svConfig + colonnes actives.
function applyOptions(opts) {
if (!opts) { return; }
widgetOptions = opts;
optionsLoaded = true;
var configKeys = ['fill_color', 'fill_opacity', 'stroke_width',
'sel_fill_color', 'sel_fill_opacity', 'sel_stroke_width',
'title', 'layers', 'md', 'sviewer_base', 'grist_api_base', 'georchestra_base',
'geom_mode', 'autozoom', 'lb', 'wms_opacity'];
configKeys.forEach(function(k) { if (opts[k] !== undefined) { svConfig[k] = opts[k]; } });
// migrate legacy keys
if (opts.feature_color && !opts.fill_color) { svConfig.fill_color = opts.feature_color; }
if (opts.feature_highlight_color && !opts.sel_fill_color) { svConfig.sel_fill_color = opts.feature_highlight_color; }
// migrate sel_fill_opacity=1 (old default) → 0.5
if (svConfig.sel_fill_opacity === 1) { svConfig.sel_fill_opacity = 0.5; }
if (opts.geom_mode) { colGeomMode = opts.geom_mode; }
if (opts._colGeom) { colGeom = opts._colGeom; colLat = null; colLon = null; }
if (opts._colLat) { colLat = opts._colLat; colGeom = null; }
if (opts._colLon) { colLon = opts._colLon; colGeom = null; }
if (opts._colLabel) { colLabel = opts._colLabel; }
}
// Returns a human-readable label for an OL layer: attribution text first, then title.
// Strips HTML tags (attributions may contain <a> markup).
function layerLabel(layer) {
if (!layer) { return ''; }
try {
var src = layer.getSource ? layer.getSource() : null;
var attrFn = src ? src.getAttributions() : null;
if (attrFn) {
var arr = typeof attrFn === 'function' ? attrFn(null) : attrFn;
if (!Array.isArray(arr)) { arr = [arr]; }
var text = arr.join(', ').replace(/<[^>]+>/g, '').trim();
if (text) { return text; }
}
} catch(e) { /* fall through */ }
return layer.get('title') || '';
}
// ---------------------------------------------------------------------------
// Panneau de configuration (onEditOptions)
// ---------------------------------------------------------------------------
function openSettings() {
var panel = document.getElementById('sv-settings');
if (!panel) { return; }
document.querySelectorAll('.sv-tab-btn').forEach(function(b) { b.classList.remove('sv-tab-active'); b.setAttribute('aria-selected', 'false'); });
document.querySelectorAll('.sv-tab-panel[data-tab]').forEach(function(fs) { fs.classList.remove('sv-tab-visible'); fs.setAttribute('aria-hidden', 'true'); });
var firstBtn = document.querySelector('.sv-tab-btn[data-tab="data"]');
var firstFs = document.querySelector('.sv-tab-panel[data-tab="data"]');
if (firstBtn) { firstBtn.classList.add('sv-tab-active'); firstBtn.setAttribute('aria-selected', 'true'); }
if (firstFs) { firstFs.classList.add('sv-tab-visible'); firstFs.setAttribute('aria-hidden', 'false'); }
var modeElOs = document.getElementById('sv-cfg-geom-mode');
if (modeElOs) { modeElOs.value = colGeomMode; }
syncColumnPickerMode();
var d = geojsonStyleDefaults();
document.getElementById('sv-cfg-fill-color').value = safeColor(svConfig.fill_color, d.color);
document.getElementById('sv-cfg-fill-opacity').value = svConfig.fill_opacity !== undefined ? svConfig.fill_opacity : d.fillOpacity;
document.getElementById('sv-cfg-stroke-width').value = svConfig.stroke_width !== undefined ? svConfig.stroke_width : d.strokeWidth;
document.getElementById('sv-cfg-sel-fill-color').value = safeColor(svConfig.sel_fill_color, '#ee7733');
document.getElementById('sv-cfg-sel-fill-opacity').value = svConfig.sel_fill_opacity !== undefined ? svConfig.sel_fill_opacity : 0.5;
document.getElementById('sv-cfg-sel-stroke-width').value = svConfig.sel_stroke_width !== undefined ? svConfig.sel_stroke_width : d.strokeWidth + 1;
document.getElementById('sv-cfg-md').value = svConfig.md || '';
document.getElementById('sv-cfg-layers').value = svConfig.layers || '';
document.getElementById('sv-cfg-md').disabled = !!svConfig.layers;
document.getElementById('sv-cfg-layers').disabled = !!svConfig.md;
document.getElementById('sv-cfg-autozoom').checked = svConfig.autozoom !== false;
var defaultSvBase = svConfig.sviewer_base || (function() {
var loc = window.location;
var dir = loc.pathname.replace(/\/[^\/]*$/, '/');
return loc.origin + dir.replace(/ext\/grist\/$/, '');
}());
document.getElementById('sv-cfg-svbase').value = defaultSvBase;
var defaultApiBase = svConfig.grist_api_base || (function() {
try { return document.referrer ? new URL(document.referrer).origin : ''; } catch(e) { return ''; }
}());
document.getElementById('sv-cfg-apibase').value = defaultApiBase;
document.getElementById('sv-cfg-georchestra').value = svConfig.georchestra_base ||
(window.SViewer.hardConfig && window.SViewer.hardConfig.geOrchestraBaseUrl) ||
(window.customConfig && window.customConfig.geOrchestraBaseUrl) || '';
(function() {
var opVal = svConfig.wms_opacity !== undefined ? svConfig.wms_opacity : 1;
var opPct = Math.round(opVal * 100) + '%';
var sl = document.getElementById('sv-cfg-wms-opacity');
var lb = document.getElementById('sv-cfg-wms-opacity-val');
if (sl) { sl.value = opVal; sl.setAttribute('aria-valuetext', opPct); }
if (lb) { lb.textContent = opPct; }
}());
// Background selector — populate from backgroundPresets if available
(function() {
var presets = (window.customConfig && window.customConfig.backgroundPresets) ||
(window.SViewer.hardConfig && window.SViewer.hardConfig.backgroundPresets) || [];
var sel = document.getElementById('sv-cfg-lb');
var lbl = document.getElementById('sv-cfg-lb-label');
var sep = document.getElementById('sv-cfg-lb-sep');
var show = presets.length > 1;
sel.style.display = lbl.style.display = sep.style.display = show ? '' : 'none';
if (show) {
sel.innerHTML = '';
presets.forEach(function(p, i) {
var opt = document.createElement('option');
opt.value = i;
opt.textContent = p.title || ('Fond ' + i);
sel.appendChild(opt);
});
sel.value = svConfig.lb !== undefined ? svConfig.lb : 0;
}
}());