summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/resources/sm.common.js
blob: 373c1dd9c1d52ddd9f83de527dafc69d3bca07d1 (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
/**
 * JavaScript the Semantic Maps extension.
 * @see https://www.mediawiki.org/wiki/Extension:Semantic_Maps
 *
 * @licence GNU GPL v2++
 * @author Peter Grassberger < petertheone@gmail.com >
 */
window.sm = new ( function( $, mw ) {

	this.buildQueryString = function( query, ajaxcoordproperty, top, right, bottom, left ) {
		var isCompoundQuery = query.indexOf( '|' ) > -1;
		var query = query.split( '|' );
		$.each( query, function( index ) {
			query[index] += ' [[' + ajaxcoordproperty + '::+]] ';
			query[index] += '[[' + ajaxcoordproperty + '::>' + bottom + '°, ' + left + '°]] ';
			query[index] += '[[' + ajaxcoordproperty + '::<' + top + '°, ' + right + '°]]';
			if( !isCompoundQuery ) {
				query[index] += '|?' + ajaxcoordproperty;
			} else {
				query[index] += ';?' + ajaxcoordproperty;
			}
		} );
		return query.join( ' | ' );
	};

	/**
	 * Detects semicolons `;` not in square brackets `[]`.
	 *
	 * @param string
	 * @returns {boolean}
	 */
	this.hasCompoundQuerySemicolon = function( string ) {
		return /;(?![^[]*])/g.test( string );
	};

	this.sendQuery = function( query ) {
		var action = this.hasCompoundQuerySemicolon( query ) ? 'compoundquery' : 'ask';
		return $.ajax( {
			method: 'GET',
			url: mw.util.wikiScript( 'api' ),
			data: {
				'action': action,
				'query': query,
				'format': 'json'
			},
			dataType: 'json'
		} );
	};

	this.ajaxUpdateMarker = function( map, query, icon ) {
		return this.sendQuery( query ).done( function( data ) {
			if( !data.hasOwnProperty( 'query' ) ||
				!data.query.hasOwnProperty( 'results' ) ) {
				return;
			}
			// todo: don't remove and recreate all markers..
			// only add new ones.
			map.removeMarkers();
			for( var property in data.query.results ) {
				if( data.query.results.hasOwnProperty( property ) ) {
					var location = data.query.results[property];
					var coordinates = location.printouts[map.options.ajaxcoordproperty][0];
					var markerOptions = {
						lat: coordinates.lat,
						lon: coordinates.lon,
						title: location.fulltext,
						text: '<b><a href="' + location.fullurl + '">' + location.fulltext + '</a></b>',
						icon: icon
					};
					map.addMarker( markerOptions );
				}
			}
		} );
	};

} )( jQuery, mediaWiki );