http://www.mediawiki.org/wiki/Special:Code/MediaWiki/74052

Revision: 74052
Author:   jeroendedauw
Date:     2010-10-01 03:59:02 +0000 (Fri, 01 Oct 2010)

Log Message:
-----------
Changes for 0.7 - stole some stuff from wikibits.js to make maps works 
correctly with the resource loader. This is a hack to make the extension work 
for now. I know it's not ideal, and will properly integrate with the RL at some 
future release.

Modified Paths:
--------------
    trunk/extensions/Maps/Maps.php
    trunk/extensions/Maps/includes/Maps_Mapper.php
    trunk/extensions/Maps/includes/Maps_MappingService.php
    trunk/extensions/Maps/includes/features/Maps_BaseMap.php
    trunk/extensions/Maps/includes/features/Maps_BasePointMap.php
    
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispMap.php
    
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispPoint.php
    
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispMap.php
    
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispPoint.php
    
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispMap.php
    
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispPoint.php
    trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispMap.php
    
trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispPoint.php

Added Paths:
-----------
    trunk/extensions/Maps/includes/mapsonload.js

Modified: trunk/extensions/Maps/Maps.php
===================================================================
--- trunk/extensions/Maps/Maps.php      2010-10-01 02:29:15 UTC (rev 74051)
+++ trunk/extensions/Maps/Maps.php      2010-10-01 03:59:02 UTC (rev 74052)
@@ -74,6 +74,8 @@
        $wgHooks['AdminLinks'][] = 'efMapsAddToAdminLinks';
        
        $wgHooks['UnitTestsList'][] = 'efMapsUnitTests';
+       
+       $wgHooks['SkinAfterBottomScripts'][] = 'efMapsAddOnloadFunction';
 }
 
 /**
@@ -86,8 +88,10 @@
 function efMapsSetup() {
        global $wgExtensionCredits, $wgLang, $wgAutoloadClasses;
        global $egMapsDefaultService, $egMapsAvailableServices;
-       global $egMapsDir, $egMapsUseMinJs;
+       global $egMapsDir, $egMapsUseMinJs, $egMapsEvilJS;
 
+       $egMapsEvilJS = '';
+       
        // Autoload the "includes/" classes and interfaces.
        $incDir = dirname( __FILE__ ) . '/includes/';
        $wgAutoloadClasses['MapsMapper']                                = 
$incDir . 'Maps_Mapper.php';
@@ -165,10 +169,6 @@
                'description' => wfMsgExt( 'maps_desc', 'parsemag', 
$servicesList ),
        );
 
-       // TODO
-       //Validator::addOutputFormat( 'mapdimension', array( 'MapsMapper', 
'setMapDimension' ) );
-       //Validator::addOutputFormat( 'coordinateset', array( 'MapsMapper', 
'formatLocation' ) );
-
        return true;
 }
 
@@ -203,4 +203,17 @@
        $testDir = dirname( __FILE__ ) . '/test/';
        //$files[] = $testDir . 'MapsCoordinateParserTest.php';
        return true;
+}
+
+/**
+ * Adds the map JS to the bottom of the page. This is a hack to get
+ * around the lack of inline script support in the MW 1.17 resource loader.
+ * 
+ * @since 0.7
+ */
+function efMapsAddOnloadFunction( $skin, &$text ) {
+       if ( method_exists( 'ParserOutput', 'addModules' ) ) {
+               $text .= Html::inlineScript( 'if (window.runMapsOnloadHook) 
runMapsOnloadHook();' );
+       }
+       return true;
 }
\ No newline at end of file

Modified: trunk/extensions/Maps/includes/Maps_Mapper.php
===================================================================
--- trunk/extensions/Maps/includes/Maps_Mapper.php      2010-10-01 02:29:15 UTC 
(rev 74051)
+++ trunk/extensions/Maps/includes/Maps_Mapper.php      2010-10-01 03:59:02 UTC 
(rev 74052)
@@ -112,26 +112,45 @@
        }
        
        /**
-        * Adds a string of JavaScript to the page after wrapping it in a 
script tag.
-        * This function takes care of incompatible changes between MW 1.16 and 
1.17.
+        * Adds a string of JavaScript to the end of the page in a script 
container.
+        * This is a hack to get around the lack of inline script support in 
the 
+        * MW 1.17 resource loader.
         * 
         * @since 0.7
         * 
-        * @param Parser $parser
+        * @param Parser $parser 
         * @param string $script
         */
        public static function addInlineScript( Parser $parser, $script ) {
-               $script = Html::inlineScript( $script );
+               $onloadFunction = method_exists( 'ParserOutput', 'addModules' ) 
? 'addMapsOnloadHook' : 'addOnloadHook';
                
+               $parser->getOutput()->addHeadItem( Html::inlineScript( 
+                       "$onloadFunction( function() { $script } );"
+               ) ); 
+       }
+       
+       /**
+        * Includes the mapsonload.js script.
+        * 
+        * This is a hack to get around the lack of inline script support in 
the 
+        * MW 1.17 resource loader.
+        * 
+        * @since 0.7
+        * 
+        * @param Parser $parser
+        */
+       public static function addBCJS( Parser $parser ) {
                if ( method_exists( 'ParserOutput', 'addModules' ) ) {
-                       // 1.17 and later
-                       // TODO
-                       throw new Exception( "MediaWiki doesn't like inline 
JS!" );
-               }
-               else {
-                       // 1.16 and earlier
-                       $parser->getOutput()->addHeadItem( $script );
-               }
+                       static $addOnloadJs = false;
+                       
+                       if ( !$addOnloadJs ) {
+                               $addOnloadJs = true;
+                               global $egMapsScriptPath, $egMapsStyleVersion;
+                               $parser->getOutput()->addHeadItem(
+                                       Html::linkedScript( 
"$egMapsScriptPath/includes/mapsonload.js?$egMapsStyleVersion" )
+                               );
+                       }
+               }               
        }
        
        /**

Modified: trunk/extensions/Maps/includes/Maps_MappingService.php
===================================================================
--- trunk/extensions/Maps/includes/Maps_MappingService.php      2010-10-01 
02:29:15 UTC (rev 74051)
+++ trunk/extensions/Maps/includes/Maps_MappingService.php      2010-10-01 
03:59:02 UTC (rev 74052)
@@ -107,7 +107,7 @@
        public final function addDependencies( &$parserOrOut ) {
                $dependencies = $this->getDependencyHtml();
                
-               // Only aff a head item when there are dependencies.
+               // Only add a head item when there are dependencies.
                if ( $dependencies ) {
                        if ( $parserOrOut instanceof Parser ) {
                                $parserOrOut->getOutput()->addHeadItem( 
$dependencies );

Modified: trunk/extensions/Maps/includes/features/Maps_BaseMap.php
===================================================================
--- trunk/extensions/Maps/includes/features/Maps_BaseMap.php    2010-10-01 
02:29:15 UTC (rev 74051)
+++ trunk/extensions/Maps/includes/features/Maps_BaseMap.php    2010-10-01 
03:59:02 UTC (rev 74052)
@@ -84,6 +84,8 @@
         * @return html
         */
        public final function getMapHtml( array $params, Parser $parser ) {
+               MapsMapper::addBCJS( $parser );
+               
                $this->setMapProperties( $params );
                
                $this->setCentre();

Modified: trunk/extensions/Maps/includes/features/Maps_BasePointMap.php
===================================================================
--- trunk/extensions/Maps/includes/features/Maps_BasePointMap.php       
2010-10-01 02:29:15 UTC (rev 74051)
+++ trunk/extensions/Maps/includes/features/Maps_BasePointMap.php       
2010-10-01 03:59:02 UTC (rev 74052)
@@ -82,6 +82,8 @@
         * @return html
         */
        public final function getMapHtml( array $params, Parser $parser ) {
+               MapsMapper::addBCJS( $parser );
+               
                $this->setMapProperties( $params );
                
                $this->setMarkerData();

Added: trunk/extensions/Maps/includes/mapsonload.js
===================================================================
--- trunk/extensions/Maps/includes/mapsonload.js                                
(rev 0)
+++ trunk/extensions/Maps/includes/mapsonload.js        2010-10-01 03:59:02 UTC 
(rev 74052)
@@ -0,0 +1,60 @@
+/**
+ * Some JS stolen from wikibits.js to allow Maps to work in MW 1.17 with the 
resource loader.
+ * This approach might not be optimal, but prevents the need for a rewrite of 
the actual output for now.
+ * 
+ * @since 0.7
+ * 
+ * @file maponload.js
+ * @ingroup Maps
+ * 
+ * @author Jeroen De Dauw
+ */
+
+// add any onload functions in this hook (please don't hard-code any events in 
the xhtml source)
+var doneMapsOnloadHook;
+
+if (!window.mapsOnloadFuncts) {
+       var mapsOnloadFuncts = [];
+}
+
+function addMapsOnloadHook(hookFunct) {
+       // Allows add-on scripts to add onload functions
+       if(!doneMapsOnloadHook) {
+               mapsOnloadFuncts[mapsOnloadFuncts.length] = hookFunct;
+       } else {
+               hookFunct();  // bug in MSIE script loading
+       }
+}
+
+addMapOnloadHandler(window, "load",
+       function () {
+               // don't run anything below this for non-dom browsers
+               if (doneMapsOnloadHook || !(document.getElementById && 
document.getElementsByTagName)) {
+                       return;
+               }
+       
+               // set this before running any hooks, since any errors below
+               // might cause the function to terminate prematurely
+               doneMapsOnloadHook = true;
+       
+               // Run any added-on functions
+               for (var i = 0; i < mapsOnloadFuncts.length; i++) {
+                       mapsOnloadFuncts[i]();
+               }
+       }
+);
+
+/**
+ * Add an event handler to an element
+ *
+ * @param Element element Element to add handler to
+ * @param String attach Event to attach to
+ * @param callable handler Event handler callback
+ */
+function addMapOnloadHandler( element, attach, handler ) {
+       if( window.addEventListener ) {
+               element.addEventListener( attach, handler, false );
+       } else if( window.attachEvent ) {
+               element.attachEvent( 'on' + attach, handler );
+       }
+}


Property changes on: trunk/extensions/Maps/includes/mapsonload.js
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: 
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispMap.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispMap.php   
    2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispMap.php   
    2010-10-01 03:59:02 UTC (rev 74052)
@@ -30,9 +30,7 @@
                        wfMsg( 'maps-loading-map' )
                );
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initializeGoogleMap("$mapName", 
                        {
                        lat: $this->centreLat,
@@ -45,8 +43,6 @@
                        kml: [$this->kml]
                        },
                []);
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispPoint.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispPoint.php 
    2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/GoogleMaps/Maps_GoogleMapsDispPoint.php 
    2010-10-01 03:59:02 UTC (rev 74052)
@@ -32,9 +32,7 @@
                        wfMsg( 'maps-loading-map' )
                );
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initializeGoogleMap("$mapName", 
                        {
                        lat: $this->centreLat,
@@ -48,8 +46,6 @@
                        },
                        $this->markerJs
                );
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispMap.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispMap.php 
    2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispMap.php 
    2010-10-01 03:59:02 UTC (rev 74052)
@@ -37,9 +37,7 @@
                        null
                );
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initGMap3(
                        "$mapName",
                        {
@@ -51,8 +49,6 @@
                        },
                        []
                );
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispPoint.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispPoint.php
   2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/GoogleMaps3/Maps_GoogleMaps3DispPoint.php
   2010-10-01 03:59:02 UTC (rev 74052)
@@ -26,9 +26,7 @@
                        null
                );
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initGMap3(
                        "$mapName",
                        {
@@ -40,8 +38,6 @@
                        },
                        $this->markerJs
                );
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispMap.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispMap.php   
    2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispMap.php   
    2010-10-01 03:59:02 UTC (rev 74052)
@@ -32,8 +32,6 @@
                $langCode = $wgLang->getCode();
                
                MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
                initOpenLayer(
                        "$mapName",
                        $this->centreLon,
@@ -44,8 +42,6 @@
                        [],
                        "$langCode"
                );
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispPoint.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispPoint.php 
    2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/OpenLayers/Maps_OpenLayersDispPoint.php 
    2010-10-01 03:59:02 UTC (rev 74052)
@@ -31,9 +31,7 @@
                
                $langCode = $wgLang->getCode();
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initOpenLayer(
                        "$mapName",
                        $this->centreLon,
@@ -44,8 +42,6 @@
                        $this->markerJs,
                        "$langCode"
                );
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispMap.php
===================================================================
--- trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispMap.php 
2010-10-01 02:29:15 UTC (rev 74051)
+++ trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispMap.php 
2010-10-01 03:59:02 UTC (rev 74052)
@@ -30,9 +30,7 @@
                        wfMsg( 'maps-loading-map' )
                );
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initializeYahooMap(
                        "$mapName",
                        $this->centreLat,
@@ -44,8 +42,6 @@
                        $this->autozoom,
                        []
                );
-       }
-);
 EOT
                );
        }

Modified: 
trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispPoint.php
===================================================================
--- 
trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispPoint.php   
    2010-10-01 02:29:15 UTC (rev 74051)
+++ 
trunk/extensions/Maps/includes/services/YahooMaps/Maps_YahooMapsDispPoint.php   
    2010-10-01 03:59:02 UTC (rev 74052)
@@ -25,9 +25,7 @@
                        wfMsg( 'maps-loading-map' )
                );
                
-               MapsMapper::addInlineScript( $parser, <<<EOT
-addOnloadHook(
-       function() {
+               MapsMapper::addInlineScript( <<<EOT
                initializeYahooMap(
                        "$mapName",
                        $this->centreLat,
@@ -39,8 +37,6 @@
                        $this->autozoom,
                        $this->markerJs
                );
-       }
-);
 EOT
                );
        }



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to