Skip to content

Commit

Permalink
Merge pull request #215 from JeroenDeDauw/leaflet-layers
Browse files Browse the repository at this point in the history
Add Leaflet layers
  • Loading branch information
JeroenDeDauw committed Sep 19, 2016
2 parents 684184c + 7470a25 commit 9a35aad
Show file tree
Hide file tree
Showing 32 changed files with 2,146 additions and 37 deletions.
62 changes: 61 additions & 1 deletion Maps_Settings.php
Expand Up @@ -250,7 +250,7 @@
'osm-cyclemap'
];

// The difinitions for the layers that should be available for the user.
// The definitions for the layers that should be available for the user.
$GLOBALS['egMapsOLAvailableLayers'] = [
//'google' => array( 'OpenLayers.Layer.Google("Google Streets")' ),

Expand Down Expand Up @@ -301,5 +301,65 @@
// user does not provide one.
$GLOBALS['egMapsLeafletZoom'] = 14;

// String. The default layer for Leaflet. This value will only be
// used when the user does not provide one.
$GLOBALS['egMapsLeafletLayer'] = 'OpenStreetMap';

$GLOBALS['egMapsLeafletOverlayLayers'] = [

];

// The definitions for the layers that should be available for the user.
$GLOBALS['egMapsLeafletAvailableLayers'] = [
'OpenStreetMap' => true,
'OpenStreetMap.DE' => true,
'OpenStreetMap.BlackAndWhite' => true,
'OpenStreetMap.HOT' => true,
'Thunderforest.OpenCycleMap' => true,
'Thunderforest.Transport' => true,
'Thunderforest.Landscape' => true,
'Hydda.Full' => true,
//'MapBox' => false, // todo: implement setting api key
'Stamen.Toner' => true,
'Stamen.Terrain' => true,
'Stamen.Watercolor' => true,
'Esri.WorldStreetMap' => true,
'Esri.DeLorme' => true,
'Esri.WorldTopoMap' => true,
'Esri.WorldImagery' => true,
'Esri.WorldTerrain' => true,
'Esri.WorldShadedRelief' => true,
'Esri.WorldPhysical' => true,
'Esri.OceanBasemap' => true,
'Esri.NatGeoWorldMap' => true,
'Esri.WorldGrayCanvas' => true,
'MapQuestOpen' => true,
];

$GLOBALS['egMapsLeafletAvailableOverlayLayers'] = [
'OpenSeaMap' => true,
'OpenWeatherMap.Clouds' => true,
'OpenWeatherMap.CloudsClassic' => true,
'OpenWeatherMap.Precipitation' => true,
'OpenWeatherMap.PrecipitationClassic' => true,
'OpenWeatherMap.Rain' => true,
'OpenWeatherMap.RainClassic' => true,
'OpenWeatherMap.Pressure' => true,
'OpenWeatherMap.PressureContour' => true,
'OpenWeatherMap.Wind' => true,
'OpenWeatherMap.Temperature' => true,
'OpenWeatherMap.Snow' => true,
];

$GLOBALS['egMapsLeafletLayersApiKeys'] = [
'MapBox' => '',
'MapQuestOpen' => '',
];

// Layer dependencies
$GLOBALS['egMapsLeafletLayerDependencies'] = [
'MapQuestOpen' => 'https://open.mapquestapi.com/sdk/leaflet/v2.2/mq-map.js?key=',
];


$GLOBALS['egMapsGlobalJSVars'] = [];
2 changes: 2 additions & 0 deletions i18n/en.json
Expand Up @@ -119,6 +119,8 @@
"maps_googlemaps3": "Google Maps v3",
"maps_leaflet": "Leaflet",
"maps-leaflet-par-defzoom": "Allows setting the default zoom level of the map.",
"maps-leaflet-par-layer": "The layer that will be shown when the map loads.",
"maps-leaflet-par-overlaylayers": "The overlay layers that will be shown when the map loads.",
"maps-leaflet-par-maxclusterradius": "The maximum radius that a cluster will cover from the central marker (in pixels).",
"maps-leaflet-par-clusterspiderfy": "When you click a cluster at the bottom zoom level we spiderfy it so you can see all of its markers.",
"maps_openlayers": "OpenLayers",
Expand Down
2 changes: 2 additions & 0 deletions i18n/qqq.json
Expand Up @@ -129,6 +129,8 @@
"maps_googlemaps3": "Lable for a result format on SMW's special page \"Ask\".\n\n{{optional}}",
"maps_leaflet": "This is a field label.\n\n{{optional}}",
"maps-leaflet-par-defzoom": "{{maps-par|leaflet|defzoom}}",
"maps-leaflet-par-layer": "{{maps-par|leaflet|layer}}",
"maps-leaflet-par-overlaylayers": "{{maps-par|leaflet|overlaylayers}}",
"maps-leaflet-par-maxclusterradius": "{{maps-par|leaflet|maxclusterradius}}",
"maps-leaflet-par-clusterspiderfy": "{{maps-par|leaflet|clusterspiderfy}}",
"maps_openlayers": "Lable for a result format on SMW's special page \"Ask\".\n\n{{optional}}",
Expand Down
42 changes: 27 additions & 15 deletions includes/Maps_DisplayMapRenderer.php
Expand Up @@ -88,6 +88,10 @@ public final function renderMap( array $params, Parser $parser ) {

$configVars = Skin::makeVariablesScript( $this->service->getConfigVariables() );

$this->service->addHtmlDependencies(
self::getLayerDependencies( $params['mappingservice'], $params )
);

$this->service->addDependencies( $parser );
$parser->getOutput()->addHeadItem( $configVars );

Expand Down Expand Up @@ -225,28 +229,36 @@ public static function evilOpenLayersHack( $layers ) {
}
}
}

MapsMappingServices::getServiceInstance( 'openlayers' )->addLayerDependencies( self::getLayerDependencies( $layerNames ) );

return $layerDefs;
}

/**
* FIXME
* @see evilOpenLayersHack
*/
private static function getLayerDependencies( array $layerNames ) {
global $egMapsOLLayerDependencies, $egMapsOLAvailableLayers;
public static function getLayerDependencies( $service, $params ) {
global $egMapsOLLayerDependencies, $egMapsOLAvailableLayers,
$egMapsLeafletLayerDependencies, $egMapsLeafletAvailableLayers,
$egMapsLeafletLayersApiKeys;

$layerDependencies = [];

foreach ( $layerNames as $layerName ) {
if ( array_key_exists( $layerName, $egMapsOLAvailableLayers ) // The layer must be defined in php
&& is_array( $egMapsOLAvailableLayers[$layerName] ) // The layer must be an array...
&& count( $egMapsOLAvailableLayers[$layerName] ) > 1 // ...with a second element...
&& array_key_exists( $egMapsOLAvailableLayers[$layerName][1], $egMapsOLLayerDependencies ) ) { //...that is a dependency.
$layerDependencies[] = $egMapsOLLayerDependencies[$egMapsOLAvailableLayers[$layerName][1]];
if ( $service === 'leaflet' ) {
$layerName = $params['layer'];
if ( array_key_exists( $layerName, $egMapsLeafletAvailableLayers )
&& $egMapsLeafletAvailableLayers[$layerName]
&& array_key_exists( $layerName, $egMapsLeafletLayersApiKeys )
&& array_key_exists( $layerName, $egMapsLeafletLayerDependencies ) ) {
$layerDependencies[] = '<script src="' . $egMapsLeafletLayerDependencies[$layerName] .
$egMapsLeafletLayersApiKeys[$layerName] . '"></script>';
}
} else if ( $service === 'openlayers' ) {
$layerNames = $params['layers'];
foreach ( $layerNames as $layerName ) {
if ( array_key_exists( $layerName, $egMapsOLAvailableLayers ) // The layer must be defined in php
&& is_array( $egMapsOLAvailableLayers[$layerName] ) // The layer must be an array...
&& count( $egMapsOLAvailableLayers[$layerName] ) > 1 // ...with a second element...
&& array_key_exists( $egMapsOLAvailableLayers[$layerName][1], $egMapsOLLayerDependencies ) ) { //...that is a dependency.
$layerDependencies[] = $egMapsOLLayerDependencies[$egMapsOLAvailableLayers[$layerName][1]];
}
}

}

return array_unique( $layerDependencies );
Expand Down
19 changes: 15 additions & 4 deletions includes/Maps_MappingService.php
Expand Up @@ -239,14 +239,25 @@ public function addResourceModules( $modules ) {
}

/**
* @see iMappingService::addDependency
*
* @since 0.6.3
*
* @param $dependencyHtml
*/
public final function addDependency( $dependencyHtml ) {
public final function addHtmlDependency( $dependencyHtml ) {
$this->dependencies[] = $dependencyHtml;
}

/**
* Adds dependencies.
*
* @param array $dependencies
*/
public function addHtmlDependencies(array $dependencies ) {
foreach ( $dependencies as $dependency ) {
$this->addHtmlDependency( $dependency );
}
}

/**
* @see iMappingService::getEarthZoom
*
Expand All @@ -256,4 +267,4 @@ public function getEarthZoom() {
return 1;
}

}
}
13 changes: 13 additions & 0 deletions includes/services/Leaflet/Leaflet.php
Expand Up @@ -77,6 +77,19 @@
'MarkerCluster.css',
],
];

$wgResourceModules['ext.maps.leaflet.providers'] = [
'localBasePath' => __DIR__ . '/leaflet-providers',
'remoteExtPath' => end( $pathParts ) . '/leaflet-providers',
'group' => 'ext.maps',
'targets' => [
'mobile',
'desktop'
],
'scripts' => [
'leaflet-providers.js',
],
];
} );

/**
Expand Down
17 changes: 17 additions & 0 deletions includes/services/Leaflet/Maps_Leaflet.php
Expand Up @@ -25,6 +25,8 @@ public function __construct( $serviceName ) {
* @since 3.0
*/
public function addParameterInfo( array &$params ) {
global $GLOBALS;

$params['zoom'] = [
'type' => 'integer',
'range' => [ 0, 20 ],
Expand All @@ -39,6 +41,21 @@ public function addParameterInfo( array &$params ) {
'message' => 'maps-leaflet-par-defzoom'
];

$params['layer'] = [
'type' => 'string',
'values' => array_keys( $GLOBALS['egMapsLeafletAvailableLayers'], true, true ),
'default' => $GLOBALS['egMapsLeafletLayer'],
'message' =>'maps-leaflet-par-layer',
];

$params['overlaylayers'] = [
'type' => 'string',
'values' => array_keys( $GLOBALS['egMapsLeafletAvailableOverlayLayers'], true, true ),
'default' => $GLOBALS['egMapsLeafletOverlayLayers'],
'message' =>'maps-leaflet-par-overlaylayers',
'islist' => true,
];

$params['resizable'] = [
'type' => 'boolean',
'default' => $GLOBALS['egMapsResizableByDefault'],
Expand Down
22 changes: 18 additions & 4 deletions includes/services/Leaflet/jquery.leaflet.js
Expand Up @@ -6,7 +6,7 @@
* @author Peter Grassberger < petertheone@gmail.com >
*/

(function ($, mw, L) {
(function ($, mw, L, MQ) {
$.fn.leafletmaps = function ( options ) {
var _this = this;
this.map = null;
Expand Down Expand Up @@ -255,9 +255,20 @@
this.map = map;

// add an OpenStreetMap tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
var layerOptions = {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
};
if (options.layer === 'OpenStreetMap') {
new L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', layerOptions).addTo(map);
} else if (options.layer === 'MapQuestOpen') {
new MQ.TileLayer(layerOptions).addTo(map);
} else {
new L.tileLayer.provider(options.layer, layerOptions).addTo(map);
}

$.each(options.overlaylayers, function(index, overlaylayer) {
L.tileLayer.provider(overlaylayer).addTo(_this.map);
});

if (options.resizable) {
//TODO: Fix moving map when resized
Expand Down Expand Up @@ -337,6 +348,9 @@

this.getDependencies = function ( options ) {
var dependencies = [];
if (options.layer !== 'OpenStreetMap' || options.overlaylayers.length > 0) {
dependencies.push( 'ext.maps.leaflet.providers' );
}
if (options.enablefullscreen) {
dependencies.push( 'ext.maps.leaflet.fullscreen' );
}
Expand All @@ -356,4 +370,4 @@
return this;

};
})(jQuery, window.mediaWiki, L);
})(jQuery, window.mediaWiki, L, window.MQ);
39 changes: 39 additions & 0 deletions includes/services/Leaflet/leaflet-providers/.eslintrc
@@ -0,0 +1,39 @@
{
"rules": {
"camelcase": 2,
"quotes": [2, "single", "avoid-escape"],
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
"space-before-function-paren": 2,
"space-in-parens": 2,
"object-curly-spacing": 2,
"array-bracket-spacing": 2,
"computed-property-spacing": 2,
"space-before-blocks": 2,
"keyword-spacing": 2,
"no-lonely-if": 2,
"comma-style": 2,
"no-underscore-dangle": 0,
"no-constant-condition": 0,
"no-multi-spaces": 0,
"strict": 0,
"key-spacing": 0,
"no-shadow": 0,
"no-unused-vars": 2,
"eqeqeq": 2
},
"globals": {
"L": true,
"module": false,
"define": false,
"require": true
},
"plugins": [
"html"
],
"settings": {
"html/report-bad-indent": 2
},
"env": {
"browser": true
}
}
1 change: 1 addition & 0 deletions includes/services/Leaflet/leaflet-providers/.gitignore
@@ -0,0 +1 @@
leaflet-providers.min.js
8 changes: 8 additions & 0 deletions includes/services/Leaflet/leaflet-providers/.mversionrc
@@ -0,0 +1,8 @@
{
"commitMessage": "Bumped version to %s",
"tagName": "%s",
"scripts": {
"preupdate": "npm run min",
"postupdate": "git push && git push --tags && npm publish"
}
}
8 changes: 8 additions & 0 deletions includes/services/Leaflet/leaflet-providers/.travis.yml
@@ -0,0 +1,8 @@
---
language: node_js
node_js:
- "0.10"
notifications:
email: false
git:
depth: 10
46 changes: 46 additions & 0 deletions includes/services/Leaflet/leaflet-providers/CHANGELOG.md
@@ -0,0 +1,46 @@

# Leaflet-providers changelog

## 1.1.14 (2016-07-15)
- Remove MapQuest, fixes #219
- Accidently skipped v1.1.12 and v1.1.13

## 1.1.11 (2016-06-04)
- Added protocol relativity to OSM FR, OSM HOT and Hydda providers (#214, #215).

## 1.1.9 (2016-03-23)
- Re-added HERE layers #209, discussion in #206.

## 1.1.8 (2016-03-22)
- Removed HERE layers #206

## 1.1.7 (2015-12-16)
- Removed Acetate tile layers #198

## 1.1.6 (2015-11-03)
- Removed most of the NLS layers per NLS request #193, fixes #178
- Added new variants to the HERE provider #183 by [@andreaswc](https://github.com/andreaswc)
- Added some tests to make sure all the placeholders in the url template are replaced #188

## 1.1.5 (2015-10-01)
- Improvements for the NLS layers #182 by [@tomhughes](https://github.com/tomhughes)
- Check for valid bounds before fitting the preview map to undefined (fixes #185)
- Add bounds for FreeMapSK (fixes #184)
- Fix Stamen layers with `.jpg` extension (#187, fixes #184)

## 1.1.4 (2015-09-27)
- Only include the interesting files in the npm package #180
- Add GSGS_Ireland to NLS provider with `tms:true` to invert y-axis #181

## 1.1.3 (2015-09-26)
- Add various historical layers of the Natioanal library of Scotland (NLS) #179
- Add a page to visually check bounds #179

## 1.1.2 (2015-09-05)
- Add CartoDB labels-only styles #170 by [@almccon](https://github.com/almccon)
- Implement commonjs module #172
- Added retina URL option #177, [@routexl](https://github.com/routexl)

## 1.1.1 (2015-06-22)
- Update Mapbox API to v4 #167 by [@gutenye](https://github.com/gutenye)
- Started maintaining a changelog in CHANGELOG.md.

0 comments on commit 9a35aad

Please sign in to comment.