summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/resources/leaflet/ext.sm.leafletajax.js
blob: fb9f7341396f50889a725bae7253e3d8c0a96e41 (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
/**
 * JavaScript for Leaflet in the Semantic Maps extension.
 * @see https://www.mediawiki.org/wiki/Extension:Semantic_Maps
 *
 * @licence GNU GPL v2+
 * @author Peter Grassberger < petertheone@gmail.com >
 */


(function( $, sm ) {
	var ajaxRequest = null;

	var mapEvents = ['dragend', 'zoomend'];

	$( document ).ready( function() {
		// todo: find a way to remove setTimeout.
		setTimeout( function() {
			$( window.maps.leafletList ).each( function( index, map ) {
				if( !map.options.ajaxquery || !map.options.ajaxcoordproperty ) {
					return;
				}
				map.map.on( mapEvents.join( ' ' ), function() {
					var bounds = map.map.getBounds();
					var query = sm.buildQueryString(
						decodeURIComponent( map.options.ajaxquery.replace( /\+/g, ' ' ) ),
						map.options.ajaxcoordproperty,
						bounds.getNorthEast().lat,
						bounds.getNorthEast().lng,
						bounds.getSouthWest().lat,
						bounds.getSouthWest().lng
					);

					if( ajaxRequest !== null ) {
						ajaxRequest.abort();
					}
					ajaxRequest = sm.ajaxUpdateMarker( map, query, map.options.icon ).done( function() {
						map.createMarkerCluster();
						ajaxRequest = null;
					} );
				} );
			} );
		}, 1000 );
	} );
})( window.jQuery, window.sm );