multiple osm fixes
authorJoey Hess <joey@kitenet.net>
Sun, 18 Mar 2012 21:07:01 +0000 (17:07 -0400)
committerJoey Hess <joey@kitenet.net>
Sun, 18 Mar 2012 21:07:01 +0000 (17:07 -0400)
* fix will_render calls to pass proper relative filenames
* fix urls to kml etc files to not assume wiki's top is at /
* avoid building the javascript to display the map in two different
  ways between the cgi and on-page maps
* refactor duplicate code

IkiWiki/Plugin/osm.pm
underlays/osm/ikiwiki/osm.js

index 41202b4efe332a6729240427aeedc2f9e7993630..661f72bab0ef1a5cc6773b92ae0a37feca4bb4c8 100644 (file)
@@ -154,18 +154,15 @@ sub process_waypoint {
        $icon = urlto($icon, $dest, 1);
        $tag = '' unless $tag;
        if ($page eq $dest) {
-               if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
-                       $config{'osm_format'} = 'KML';
-               }
-               my %formats = map { $_ => 1 } split(/, */, $config{'osm_format'});
+               my %formats = get_formats();
                if ($formats{'GeoJSON'}) {
-                       will_render($page,$config{destdir} . "/$map/pois.json");
+                       will_render($page, "$map/pois.json");
                }
                if ($formats{'CSV'}) {
-                       will_render($page,$config{destdir} . "/$map/pois.txt");
+                       will_render($page, "$map/pois.txt");
                }
                if ($formats{'KML'}) {
-                       will_render($page,$config{destdir} . "/$map/pois.kml");
+                       will_render($page, "$map/pois.kml");
                }
        }
        my $href = IkiWiki::cgiurl(
@@ -292,10 +289,7 @@ sub savestate {
                }
        }
 
-       if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
-               $config{'osm_format'} = 'KML';
-       }
-       my %formats = map { $_ => 1 } split(/, */, $config{'osm_format'});
+       my %formats = get_formats();
        if ($formats{'GeoJSON'}) {
                writejson(\%waypoints, \%linestrings);
        }
@@ -436,7 +430,7 @@ Sample style:
                $writer->endTag();
                $writer->end();
 
-               writefile("pois.kmp", $config{destdir} . "/$map", $output);
+               writefile("pois.kml", $config{destdir} . "/$map", $output);
        }
 }
 
@@ -484,7 +478,7 @@ sub format (@) {
        return $params{content};
 }
 
-sub prefered_format() {
+sub preferred_format() {
        if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
                $config{'osm_format'} = 'KML';
        }
@@ -492,19 +486,21 @@ sub prefered_format() {
        return shift @spl;
 }
 
+sub get_formats() {
+       if (!defined($config{'osm_format'}) || !$config{'osm_format'}) {
+               $config{'osm_format'} = 'KML';
+       }
+       map { $_ => 1 } split(/, */, $config{'osm_format'});
+}
+
 sub include_javascript ($) {
        my $page=shift;
        my $loader;
 
-       eval q{use JSON};
-       error $@ if $@;
        if (exists $pagestate{$page}{'osm'}) {
                foreach my $map (keys %{$pagestate{$page}{'osm'}}) {
                        foreach my $name (keys %{$pagestate{$page}{'osm'}{$map}{'displays'}}) {
-                               my %options = %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}};
-                               $options{'map'} = $map;
-                               $options{'format'} = prefered_format();
-                               $loader .= "mapsetup(\"mapdiv-$name\", " . to_json(\%options) . ");\n";
+                               $loader .= map_setup_code($map, $name, %{$pagestate{$page}{'osm'}{$map}{'displays'}{$name}});
                        }
                }
        }
@@ -536,7 +532,15 @@ sub cgi($) {
        print "<html><body>";
        print "<div id=\"mapdiv-$map\"></div>";
        print embed_map_code();
-       print "<script type=\"text/javascript\" charset=\"utf-8\">mapsetup( 'mapdiv-$map', { 'map': '$map', 'lat': urlParams['lat'], 'lon': urlParams['lon'], 'zoom': urlParams['zoom'], 'fullscreen': 1, 'editable': 1, 'format': '" . prefered_format() . "'});</script>";
+       print "<script type=\"text/javascript\" charset=\"utf-8\">";
+       print map_setup_code($map, $map,
+               lat => "urlParams['lat']",
+               lon => "urlParams['lon']",
+               zoom => "urlParams['zoom']",
+               fullscreen => 1,
+               editable => 1,
+       );
+       print "</script>";
        print "</body></html>";
 
        exit 0;
@@ -549,4 +553,28 @@ sub embed_map_code(;$) {
                '" type="text/javascript" charset="utf-8"></script>'."\n";
 }
 
+sub map_setup_code($;@) {
+       my $map=shift;
+       my $name=shift;
+       my %options=@_;
+
+       eval q{use JSON};
+       error $@ if $@;
+                               
+       $options{'format'} = preferred_format();
+
+       my %formats = get_formats();
+       if ($formats{'GeoJSON'}) {
+               $options{'jsonurl'} = urlto($map."/pois.json");
+       }
+       if ($formats{'CSV'}) {
+               $options{'csvurl'} = urlto($map."/pois.txt");
+       }
+       if ($formats{'KML'}) {
+               $options{'kmlurl'} = urlto($map."/pois.kml");
+       }
+
+       return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";
+}
+
 1;
index 7994c62fc8ff61bb795391aeae3587d9dac91a35..d7e3d53f42e7c832423d21ea6b82b1305ddec22d 100644 (file)
@@ -41,13 +41,13 @@ function mapsetup(divname, options) {
        map.addLayer(new OpenLayers.Layer.OSM());
        if (options.format == 'CSV') {
                pois = new OpenLayers.Layer.Text( "CSV",
-                       { location:"/" + options.map + "/pois.txt",
+                       { location: options.csvurl,
                          projection: map.displayProjection
                        });
        } else if (options.format == 'GeoJSON') {
                pois = new OpenLayers.Layer.Vector("GeoJSON", {
                        protocol: new OpenLayers.Protocol.HTTP({
-                               url: "/" + options.map + "/pois.json",
+                               url: options.jsonurl,
                                format: new OpenLayers.Format.GeoJSON()
                        }),
                        strategies: [new OpenLayers.Strategy.Fixed()]
@@ -55,7 +55,7 @@ function mapsetup(divname, options) {
        } else {
                pois = new OpenLayers.Layer.Vector("KML", {
                        protocol: new OpenLayers.Protocol.HTTP({
-                               url: "/" + options.map + "/pois.kml",
+                               url: options.kmlurl,
                                format: new OpenLayers.Format.KML({
                                        extractStyles: true,
                                        extractAttributes: true