summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/resources/geoJsonPage.js
blob: 8c6c7c14d082d38c86f181d8bc2e9aff775dd615 (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
(function( $, mw, maps ) {

	function hideLoadingMessage(map, $content) {
		map.on(
			'load',
			function() {
				$content.find('div.maps-loading-message').hide();
			}
		);
	}

	function addZoomControl(map) {
		map.addControl(new L.Control.Zoom());
	}

	function addTitleLayer(map) {
		L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
			attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
		}).addTo(map);
	}

	function fitContent(map, geoJsonLayer) {
		map.fitWorld();
		let bounds = geoJsonLayer.getBounds();

		if (bounds.isValid()) {
			if (bounds.getNorthEast().equals(bounds.getSouthWest())) {
				map.setView(
					bounds.getCenter(),
					14
				);
			}
			else {
				map.fitBounds(bounds);
			}
		}
	}

	function initializeWithEditor(map) {
		let editor = maps.leaflet.LeafletEditor(
			map,
			new maps.MapSaver(mw.config.get('wgPageName'))
		);

		editor.onSaved(function() {
			alert(mw.msg('maps-json-editor-changes-saved'));
		});

		editor.initialize(window.GeoJson);

		fitContent(map, editor.getLayer());
	}

	function initializePlainMap(map) {
		fitContent(
			map,
			maps.leaflet.GeoJson.newGeoJsonLayer(L, window.GeoJson).addTo(map)
		);
	}

	function initializeGeoJsonAndEditorUi(map) {
		if (mw.config.get('wgCurRevisionId') === mw.config.get('wgRevisionId')) {

			maps.api.canEditPage(mw.config.get('wgPageName')).done(
				function(canEdit) {
					if (canEdit) {
						initializeWithEditor(map);
					}
					else {
						initializePlainMap(map);
					}
				}
			);
		}
		else {
			initializePlainMap(map);
		}
	}

	mw.hook( 'wikipage.content' ).add( function ( $content ) {
		let map = L.map(
			'GeoJsonMap',
			{
				fullscreenControl: true,
				fullscreenControlOptions: {position: 'topright'},
				zoomControl: false
			}
		);

		hideLoadingMessage(map, $content);
		addZoomControl(map);
		addTitleLayer(map);
		initializeGeoJsonAndEditorUi(map);
	} );

})( window.jQuery, window.mediaWiki, window.maps );