summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/resources/maps.services.js
blob: a23ff0c880c4d6ac594b2fbb1fee80d93e7b28ec (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
/*global jQuery, mediaWiki, maps */
/*global confirm */
( function ( $, mw, maps ) {
	'use strict';

	/**
	 * @since 3.5
	 *
	 * @param {object} container
	 * @return {this}
	 */
	var services = function ( container ) {

		if ( $.type( container ) !== 'object' ) {
			throw new Error( 'The container is not of the correct type ' + $.type( container ) );
		}

		this.container = container;

		return this;
	};

	/* Public methods */

	services.prototype = {

		constructor: services,

		/**
		 * @since 3.5
		 *
		 * @param {string} service
		 */
		render: function( service ) {
			if ( service === 'googlemaps' || service === 'maps' || service === 'googlemaps3' ) {
				this.google();
			}

			if ( service === 'leaflet' || service === 'leafletmaps' ) {
				this.leaflet();
			}
		},

		/**
		 * Google service
		 *
		 * @since 3.5
		 */
		google: function() {

			var self = this;

			// https://www.mediawiki.org/wiki/ResourceLoader/Modules#mw.loader.using
			mw.loader.using( 'ext.maps.googlemaps3' ).done( function () {

				if ( typeof google === 'undefined' ) {
					throw new Error( 'The google map service is unknown, please ensure that the API or module is loaded correctly.' );
				}

				self.container.find( '.maps-googlemaps3' ).each( function() {
					var $this = $( this );
					$this.googlemaps( $.parseJSON( $this.find( 'div').text() ) );
				} );
			} );
		},

		/**
		 * Leaflet service
		 *
		 * @since 3.5
		 */
		leaflet: function() {
			mw.loader.using( 'ext.maps.leaflet' ).done( function () {
				$( '.maps-leaflet' ).each( function() {
					var $this = $( this );
					maps.leafletList.push(
						$this.leafletmaps( $.parseJSON( $this.find( 'div').text() ) )
					);
				} );
			} );
		}
	};

	maps.services = services;

}( jQuery, mediaWiki, maps ) );