diff options
author | Joey Hess <joey@kitenet.net> | 2012-03-18 17:07:01 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-03-18 17:07:01 -0400 |
commit | 917cdb7ade640c1e88f26d99acb42cff55a4c6fc (patch) | |
tree | f3dab1533ae3de4f6a1f108bfca8b95ad1384496 | |
parent | 3867f038a79784119e67ad36a191b64d2f574a3b (diff) | |
download | ikiwiki-917cdb7ade640c1e88f26d99acb42cff55a4c6fc.tar ikiwiki-917cdb7ade640c1e88f26d99acb42cff55a4c6fc.tar.gz |
multiple osm fixes
* 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
-rw-r--r-- | IkiWiki/Plugin/osm.pm | 68 | ||||
-rw-r--r-- | underlays/osm/ikiwiki/osm.js | 6 |
2 files changed, 51 insertions, 23 deletions
diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm index 41202b4ef..661f72bab 100644 --- a/IkiWiki/Plugin/osm.pm +++ b/IkiWiki/Plugin/osm.pm @@ -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; diff --git a/underlays/osm/ikiwiki/osm.js b/underlays/osm/ikiwiki/osm.js index 7994c62fc..d7e3d53f4 100644 --- a/underlays/osm/ikiwiki/osm.js +++ b/underlays/osm/ikiwiki/osm.js @@ -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 |