r58452 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58451‎ | r58452 | r58453 >
Date:00:34, 3 November 2009
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Modified paths:
  • /trunk/extensions/Maps/MapUtilityFunctions.js (modified) (history)
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/Maps_MapFeature.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Mapper.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Settings.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Utils.php (modified) (history)
  • /trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php (modified) (history)
  • /trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispPoint.php (modified) (history)
  • /trunk/extensions/Maps/OpenLayers/OpenLayerFunctions.js (modified) (history)
  • /trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispMap.php (modified) (history)
  • /trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispPoint.php (modified) (history)
  • /trunk/extensions/Maps/OpenStreetMaps/Maps_OSMUtils.php (modified) (history)
  • /trunk/extensions/Maps/OpenStreetMaps/OSMFunctions.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispPoint.php
@@ -66,8 +66,8 @@
6767 $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label;
6868
6969 $title = str_replace("'", "\'", $title);
70 - $label = str_replace("'", "\'", $label);
71 -
 70+ $label = str_replace("'", "\'", $label);
 71+
7272 $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : '';
7373 $markerItems[] = "getOLMarkerData($lon, $lat, '$title', '$label', '$icon')";
7474 }
@@ -77,7 +77,7 @@
7878 $this->output .= "<div id='$this->mapName' style='width: {$this->width}px; height: {$this->height}px; background-color: #cccccc;'></div>
7979 <script type='$wgJsMimeType'> /*<![CDATA[*/
8080 addOnloadHook(
81 - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString])
 81+ initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString], $this->height)
8282 );
8383 /*]]>*/ </script>";
8484 }
Index: trunk/extensions/Maps/OpenLayers/OpenLayerFunctions.js
@@ -11,7 +11,7 @@
1212 * Creates and initializes an OpenLayers map.
1313 * The resulting map is returned by the function but no further handling is required in most cases.
1414 */
15 -function initOpenLayer(mapName, lon, lat, zoom, mapTypes, controls, marker_data){
 15+function initOpenLayer(mapName, lon, lat, zoom, mapTypes, controls, marker_data, height){
1616
1717 // Create a new OpenLayers map with without any controls on it
1818 var mapOptions = {
@@ -31,6 +31,10 @@
3232
3333 // If a string is provided, find the correct name for the control, and use eval to create the object itself
3434 if (typeof controls[i] == 'string') {
 35+ if (controls[i].toLowerCase() == 'autopanzoom') {
 36+ if (height > 140) controls[i] = height > 320 ? 'panzoombar' : 'panzoom';
 37+ }
 38+
3539 control = getValidControlName(controls[i]);
3640
3741 if (control) {
Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php
@@ -58,7 +58,7 @@
5959 $this->output .= "<div id='$this->mapName' style='width: {$this->width}px; height: {$this->height}px; background-color: #cccccc;'></div>
6060 <script type='$wgJsMimeType'> /*<![CDATA[*/
6161 addOnloadHook(
62 - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[])
 62+ initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[], $this->height)
6363 );
6464 /*]]>*/ </script>";
6565 }
Index: trunk/extensions/Maps/Maps.php
@@ -23,7 +23,7 @@
2424 die( 'Not an entry point.' );
2525 }
2626
27 -define('Maps_VERSION', '0.4 a9');
 27+define('Maps_VERSION', '0.4 a10');
2828
2929 $egMapsScriptPath = $wgScriptPath . '/extensions/Maps';
3030 $egMapsIP = $IP . '/extensions/Maps';
Index: trunk/extensions/Maps/Maps_Mapper.php
@@ -1,6 +1,6 @@
22 <?php
33
4 -/**
 4+/**
55 * A class that holds static helper functions for common functionality that is not map-spesific.
66 *
77 * @file Maps_Mapper.php
Index: trunk/extensions/Maps/Maps_MapFeature.php
@@ -17,19 +17,16 @@
1818
1919 /**
2020 * Set the map service specific element name and the javascript function handling the displaying of an address
21 - *
2221 */
2322 protected abstract function setMapSettings();
2423
2524 /**
2625 * Map service specific map count and loading of dependencies
27 - *
2826 */
2927 protected abstract function doMapServiceLoad();
3028
3129 /**
3230 * Adds the HTML specific to the mapping service to the output
33 - *
3431 */
3532 protected abstract function addSpecificMapHTML();
3633
@@ -51,7 +48,6 @@
5249
5350 /**
5451 * Sets the map properties as class fields.
55 - *
5652 */
5753 protected function manageMapProperties($mapProperties, $className) {
5854 global $egMapsServices;
@@ -67,7 +63,6 @@
6864 }
6965 }
7066
71 - // TODO: refactor down - not every mapping feature/service should support a controls parameter
7267 MapsMapper::enforceArrayValues($this->controls);
7368
7469 MapsUtils::makeMapSizeValid($this->width, $this->height);
@@ -75,7 +70,6 @@
7671
7772 /**
7873 * Sets the $mapName field, using the $elementNamePrefix and $elementNr.
79 - *
8074 */
8175 protected function setMapName() {
8276 $this->mapName = $this->elementNamePrefix.'_'.$this->elementNr;
Index: trunk/extensions/Maps/MapUtilityFunctions.js
@@ -9,26 +9,11 @@
1010 * @author Jeroen De Dauw
1111 */
1212
13 -/*
14 -function addLoadEvent(func) {
15 - var oldonload = window.onload;
16 - if (typeof oldonload == 'function') {
17 - window.onload = function() {
18 - oldonload();
19 - func();
20 - };
21 - }
22 - else {
23 - window.onload = func;
24 - }
25 -}
26 -*/
27 -
2813 function convertLatToDMS (val) {
29 - return Math.abs(val) + "° " + ( val < 0 ? "S" : "N" );
 14+ return Math.abs(val) + "� " + ( val < 0 ? "S" : "N" );
3015 }
3116
3217 function convertLngToDMS (val) {
33 - return Math.abs(val) + "° " + ( val < 0 ? "W" : "E" );
 18+ return Math.abs(val) + "� " + ( val < 0 ? "W" : "E" );
3419
3520 }
\ No newline at end of file
Index: trunk/extensions/Maps/Maps_Settings.php
@@ -64,11 +64,11 @@
6565 include_once $egMapsIP . '/GoogleMaps/Maps_GoogleMaps.php'; // Google Maps
6666 include_once $egMapsIP . '/OpenLayers/Maps_OpenLayers.php'; // OpenLayers
6767 include_once $egMapsIP . '/YahooMaps/Maps_YahooMaps.php'; // Yahoo! Maps
68 -//include_once $egMapsIP . '/OpenStreetMaps/Maps_OSM.php'; // OpenLayers optimized for OSM
 68+include_once $egMapsIP . '/OpenStreetMaps/Maps_OSM.php'; // OpenLayers optimized for OSM
6969
7070 # Array of String. Array containing all the mapping services that will be made available to the user.
7171 # Currently Maps provides the following services: googlemaps, yahoomaps, openlayers
72 -$egMapsAvailableServices = array('googlemaps', 'yahoomaps', 'openlayers'/*, 'osm'*/);
 72+$egMapsAvailableServices = array('googlemaps', 'yahoomaps', 'openlayers', 'osm');
7373
7474 # String. The default mapping service, which will be used when no default service is prsent in the
7575 # $egMapsDefaultServices array for a certain feature. A service that supports all features is recommended.
@@ -203,9 +203,8 @@
204204 $egMapsOpenLayersZoom = 13;
205205
206206 # Array of String. The default controls for Open Layers. This value will only be used when the user does not provide one.
207 -# Available values: layerswitcher, mouseposition, panzoom, panzoombar, scaleline, navigation, keyboarddefaults, overviewmap, permalink
208 -# Note: panzoom and panzoombar can NOT be used together
209 -$egMapsOLControls = array('layerswitcher', 'mouseposition', 'panzoombar', 'scaleline', 'navigation');
 207+# Available values: layerswitcher, mouseposition, autopanzoom, panzoom, panzoombar, scaleline, navigation, keyboarddefaults, overviewmap, permalink
 208+$egMapsOLControls = array('layerswitcher', 'mouseposition', 'autopanzoom', 'scaleline', 'navigation');
210209
211210 # Array of String. The default layers for Open Layers. This value will only be used when the user does not provide one.
212211 # Available values: google, bing, yahoo, openlayers, nasa
@@ -220,3 +219,7 @@
221220
222221 # Integer. The default zoom of a map. This value will only be used when the user does not provide one.
223222 $egMapsOSMZoom = 13;
 223+
 224+# Array of String. The default controls for OSM maps. This value will only be used when the user does not provide one.
 225+# Available values: layerswitcher, mouseposition, autopanzoom, panzoom, panzoombar, scaleline, navigation, keyboarddefaults, overviewmap, permalink
 226+$egMapsOSMControls = array('layerswitcher', 'mouseposition', 'autopanzoom', 'scaleline', 'navigation');
\ No newline at end of file
Index: trunk/extensions/Maps/Maps_Utils.php
@@ -126,7 +126,7 @@
127127 */
128128 public static function makeMapSizeValid(&$width, &$height) {
129129 global $egMapsSizeRestrictions;
130 -
 130+
131131 if ($width < $egMapsSizeRestrictions['width'][0]) {
132132 $width = $egMapsSizeRestrictions['width'][0];
133133 }
@@ -134,11 +134,11 @@
135135 $width = $egMapsSizeRestrictions['width'][1];
136136 }
137137
138 - if ($width < $egMapsSizeRestrictions['height'][0]) {
139 - $width = $egMapsSizeRestrictions['height'][0];
 138+ if ($height < $egMapsSizeRestrictions['height'][0]) {
 139+ $height = $egMapsSizeRestrictions['height'][0];
140140 }
141 - else if($width > $egMapsSizeRestrictions['height'][1]) {
142 - $width = $egMapsSizeRestrictions['height'][1];
 141+ else if($height > $egMapsSizeRestrictions['height'][1]) {
 142+ $height = $egMapsSizeRestrictions['height'][1];
143143 }
144144 }
145145
Index: trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispMap.php
@@ -50,19 +50,22 @@
5151 public function addSpecificMapHTML() {
5252 global $wgJsMimeType;
5353
 54+ $controlItems = MapsOSMUtils::createControlsString($this->controls);
 55+
5456 $this->output .= <<<EOT
5557 <script type='$wgJsMimeType'>slippymaps['$this->mapName'] = new slippymap_map('$this->mapName', {
5658 mode: 'osm-wm',
5759 layer: 'osm-like',
58 - locale: 'en',
 60+ locale: 'en',
5961 lat: $this->centre_lat,
6062 lon: $this->centre_lon,
6163 zoom: $this->zoom,
6264 width: $this->width,
6365 height: $this->height,
64 - marker: 0
 66+ markers: [],
 67+ controls: [$controlItems]
6568 });</script>
66 -
 69+
6770 <!-- map div -->
6871 <div id='$this->mapName' class='map' style='width:{$this->width}px; height:{$this->height}px;'>
6972 <script type='$wgJsMimeType'>slippymaps['$this->mapName'].init();</script>
Index: trunk/extensions/Maps/OpenStreetMaps/OSMFunctions.js
@@ -52,7 +52,30 @@
5353 slippymaps[keyName].init();
5454 }
5555 }
 56+
 57+/**
 58+ * Gets a valid control name (with excat lower and upper case letters),
 59+ * or returns false when the control is not allowed.
 60+ */
 61+function getValidControlName(control) {
 62+ var OLControls = ['ArgParser', 'Attribution', 'Button', 'DragFeature', 'DragPan',
 63+ 'DrawFeature', 'EditingToolbar', 'GetFeature', 'KeyboardDefaults', 'LayerSwitcher',
 64+ 'Measure', 'ModifyFeature', 'MouseDefaults', 'MousePosition', 'MouseToolbar',
 65+ 'Navigation', 'NavigationHistory', 'NavToolbar', 'OverviewMap', 'Pan',
 66+ 'Panel', 'PanPanel', 'PanZoom', 'PanZoomBar', 'Permalink',
 67+ 'Scale', 'ScaleLine', 'SelectFeature', 'Snapping', 'Split',
 68+ 'WMSGetFeatureInfo', 'ZoomBox', 'ZoomIn', 'ZoomOut', 'ZoomPanel',
 69+ 'ZoomToMaxExtent'];
5670
 71+ for (i in OLControls) {
 72+ if (control == OLControls[i].toLowerCase()) {
 73+ return OLControls[i];
 74+ }
 75+ }
 76+
 77+ return false;
 78+}
 79+
5780 function slippymap_map(mapId, mapParams) {
5881 var self = this;
5982 this.mapId = mapId;
@@ -75,17 +98,29 @@
7699 ]);
77100 */
78101
 102+ /*
79103 this.mapOptions = { controls: [ new OpenLayers.Control.Navigation(),
80104 new OpenLayers.Control.ArgParser(),
81105 new OpenLayers.Control.Attribution(),
82 - /* buttonsPanel */ ]
 106+ // buttonsPanel
 107+ ]
83108 };
 109+ */
84110
85 - /* Add the zoom bar control, except if the map is only little */
86 - if (this.height > 320)
87 - this.mapOptions.controls.push(new OpenLayers.Control.PanZoomBar());
88 - else if (this.height > 140)
89 - this.mapOptions.controls.push(new OpenLayers.Control.PanZoom());
 111+ // Add the controls
 112+ this.mapOptions = {controls: []};
 113+
 114+ for (i in this.controls) {
 115+ if (this.controls[i].toLowerCase() == 'autopanzoom') {
 116+ if (this.height > 140) this.controls[i] = this.height > 320 ? 'panzoombar' : 'panzoom';
 117+ }
 118+
 119+ control = getValidControlName(this.controls[i]);
 120+
 121+ if (control) {
 122+ eval(' this.mapOptions.controls.push( new OpenLayers.Control.' + control + '() ); ');
 123+ }
 124+ }
90125 }
91126
92127 slippymap_map.prototype.init = function() {
@@ -96,12 +131,36 @@
97132
98133 this.map = this.osm_create(this.mapId, this.lon, this.lat, this.zoom);
99134
100 - if (this.marker) {
101 - var markers = new OpenLayers.Layer.Markers( "Markers" );
102 - this.map.addLayer(markers);
103 - var icon = OpenLayers.Marker.defaultIcon();
104 - markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(this.lon, this.lat).transform(new OpenLayers.Projection('EPSG:4326'), this.map.getProjectionObject()), icon));
 135+ var centerIsSet = this.lon != null && this.lat != null;
 136+
 137+ var bounds = null;
 138+
 139+ if (this.markers.length > 0) {
 140+ var markerLayer = new OpenLayers.Layer.Markers('Markers');
 141+ markerLayer.id= 'markerLayer';
 142+ this.map.addLayer(markerLayer);
 143+
 144+ if (this.markers.length > 1 && (!centerIsSet || this.zoom == null)) {
 145+ bounds = new OpenLayers.Bounds();
 146+ }
 147+
 148+ for (i in this.markers) {
 149+ this.markers[i].lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
 150+ if (bounds != null) bounds.extend(this.markers[i].lonlat); // Extend the bounds when no center is set
 151+ markerLayer.addMarker(getOSMMarker(markerLayer, this.markers[i], this.map.getProjectionObject())); // Create and add the marker
 152+ }
 153+
105154 }
 155+
 156+ if (bounds != null) map.zoomToExtent(bounds); // If a bounds object has been created, use it to set the zoom and center
 157+
 158+ if (centerIsSet) { // When the center is provided, set it
 159+ var centre = new OpenLayers.LonLat(this.lon, this.lat);
 160+ centre.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
 161+ this.map.setCenter(centre);
 162+ }
 163+
 164+ if (this.zoom != null) this.map.zoomTo(this.zoom); // When the zoom is provided, set it
106165 }
107166
108167 slippymap_map.prototype.osm_create = function(mapId, lon, lat, zoom) {
@@ -109,7 +168,7 @@
110169 var map = new OpenLayers.Map(mapId, this.mapOptions /* all provided for by OSM.js */);
111170
112171 if (this.layer == 'osm-like') {
113 - osmLayer = new OpenLayers.Layer.OSM("meh", 'http://cassini.toolserver.org/tiles/osm-like/' + this.locale + '/${z}/${x}/${y}.png');
 172+ osmLayer = new OpenLayers.Layer.OSM("OpenStreetMaps", 'http://cassini.toolserver.org/tiles/osm-like/' + this.locale + '/${z}/${x}/${y}.png');
114173 }
115174
116175 map.addLayers([osmLayer]);
@@ -132,5 +191,47 @@
133192 );
134193 }
135194
 195+function getOSMMarkerData(lon, lat, title, label, icon) {
 196+ lonLat = new OpenLayers.LonLat(lon, lat);
 197+ return {
 198+ lonlat: lonLat,
 199+ title: title,
 200+ label: label,
 201+ icon: icon
 202+ };
 203+}
136204
 205+function getOSMMarker(markerLayer, markerData, projectionObject) {
 206+ var marker;
 207+
 208+ if (markerData.icon != '') {
 209+ marker = new OpenLayers.Marker(markerData.lonlat, new OpenLayers.Icon(markerData.icon));
 210+ } else {
 211+ marker = new OpenLayers.Marker(markerData.lonlat);
 212+ }
 213+
 214+ if (markerData.title.length + markerData.label.length > 0 ) {
 215+
 216+ // This is the handler for the mousedown event on the marker, and displays the popup
 217+ marker.events.register('mousedown', marker,
 218+ function(evt) {
 219+ var popup = new OpenLayers.Feature(markerLayer, markerData.lonlat).createPopup(true);
 220+
 221+ if (markerData.title.length > 0 && markerData.label.length > 0) { // Add the title and label to the popup text
 222+ popup.setContentHTML('<b>' + markerData.title + "</b><hr />" + markerData.label);
 223+ }
 224+ else {
 225+ popup.setContentHTML(markerData.title + markerData.label);
 226+ }
 227+
 228+ popup.setOpacity(0.85);
 229+ markerLayer.map.addPopup(popup);
 230+ OpenLayers.Event.stop(evt); // Stop the event
 231+ }
 232+ );
 233+
 234+ }
 235+
 236+ return marker;
 237+}
137238
Index: trunk/extensions/Maps/OpenStreetMaps/Maps_OSMUtils.php
@@ -80,13 +80,11 @@
8181 public static function getDefaultParams() {
8282 return array
8383 (
84 - 'layers' => array(),
85 - 'baselayer' => ''
8684 );
8785 }
8886
8987 /**
90 - * If this is the first open layers map on the page, load the API, styles and extra JS functions
 88+ * If this is the first OSM map on the page, load the OpenLayers API, OSM styles and extra JS functions
9189 *
9290 * @param string $output
9391 */
@@ -102,6 +100,17 @@
103101 <script type='$wgJsMimeType' src='$egMapsScriptPath/OpenStreetMaps/OSMFunctions.js?$wgStyleVersion'></script>
104102 <script type='$wgJsMimeType'>slippymaps = Array();</script>\n";
105103 }
 104+ }
 105+
 106+ /**
 107+ * Build up a csv string with the controls, to be outputted as a JS array
 108+ *
 109+ * @param array $controls
 110+ * @return csv string
 111+ */
 112+ public static function createControlsString(array $controls) {
 113+ global $egMapsOSMControls;
 114+ return MapsMapper::createJSItemsString($controls, $egMapsOSMControls);
106115 }
107116
108117 }
Index: trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispPoint.php
@@ -64,17 +64,34 @@
6565 $label = str_replace("'", "\'", $label);
6666
6767 $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : '';
68 - $markerItems[] = "getOLMarkerData($lon, $lat, '$title', '$label', '$icon')";
 68+ $markerItems[] = "getOSMMarkerData($lon, $lat, '$title', '$label', '$icon')";
6969 }
7070
7171 $markersString = implode(',', $markerItems);
7272
73 - $this->output .= "<div id='$this->mapName' style='width: {$this->width}px; height: {$this->height}px; background-color: #cccccc;'></div>
74 - <script type='$wgJsMimeType'> /*<![CDATA[*/
75 - addOnloadHook(
76 - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString])
77 - );
78 - /*]]>*/ </script>";
 73+ $controlItems = MapsOSMUtils::createControlsString($this->controls);
 74+
 75+ $this->output .= <<<EOT
 76+ <script type='$wgJsMimeType'>slippymaps['$this->mapName'] = new slippymap_map('$this->mapName', {
 77+ mode: 'osm-wm',
 78+ layer: 'osm-like',
 79+ locale: 'en',
 80+ lat: $this->centre_lat,
 81+ lon: $this->centre_lon,
 82+ zoom: $this->zoom,
 83+ width: $this->width,
 84+ height: $this->height,
 85+ markers: [$markersString],
 86+ controls: [$controlItems]
 87+
 88+ });</script>
 89+
 90+ <!-- map div -->
 91+ <div id='$this->mapName' class='map' style='width:{$this->width}px; height:{$this->height}px;'>
 92+ <script type='$wgJsMimeType'>slippymaps['$this->mapName'].init();</script>
 93+ <!-- /map div -->
 94+ </div>
 95+EOT;
7996 }
8097
8198 }
\ No newline at end of file

Status & tagging log