cff8a7ff8d3630a1b99beb5050639427800f869e
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.js
1 "use strict";
2
3 function init_wrmap(i, jq_map) {
4         jq_map = $(jq_map);
5         var jq_map_script = jq_map.children();
6         OpenLayers.ImgPath = jq_map.attr('data-img-path'); // e.g. "/mediawiki/extensions/wrmap/openlayers/img/"
7         var json_string = jq_map_script.text();
8         jq_map_script.detach();
9
10         
11         // extract, tranform and split features to layers
12         var EPSG4326 = new OpenLayers.Projection("EPSG:4326"); // lon/lat 
13         var EPSG3857 = new OpenLayers.Projection("EPSG:3857"); // google
14         var format_json = new OpenLayers.Format.JSON();
15         var json_js = format_json.read(json_string);
16         var format_geojson = new OpenLayers.Format.GeoJSON();
17         var features_all = format_geojson.read(json_js);
18         var features_path = new Array();
19         var features_point = new Array();
20         for (var i = 0; i != features_all.length; ++i) {
21                 var feature = features_all[i];
22                 feature.geometry.transform(EPSG4326, EPSG3857);
23                 if (feature.geometry instanceof OpenLayers.Geometry.Point) features_point.push(feature);
24                 else features_path.push(feature);
25         }
26
27
28         // background layer
29         // ----------------
30         var layer_map = new OpenLayers.Layer.Google("Google Physical", {
31                 type: google.maps.MapTypeId.TERRAIN
32         });
33
34         // // Alternative: OSM map
35         // var layer_map = new OpenLayers.Layer.OSM();
36         
37         // // Alternative: Microsoft Bing Maps
38         // var layer_map = new OpenLayers.Layer.Bing({
39         //     type: "Road",
40         //     key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
41
42         
43         // path layer
44         // ----------
45         var layer_path = new OpenLayers.Layer.Vector("Path", {
46                 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
47                                 strokeColor: '${getStrokeColor}',
48                                 strokeWidth: '${getStrokeWidth}'
49                         }, {
50                                 context: {
51                                         getStrokeColor: function(feature) {
52                                                 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
53                                                 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
54                                                 if (feature.attributes.type == 'gehweg') return '#e98401';
55                                                 if (feature.attributes.type == 'alternative') return '#7f7fff';
56                                                 if (feature.attributes.type == 'lift') return '#000000';
57                                                 if (feature.attributes.type == 'anfahrt') return '#e1e100';
58                                                 return '#ee9900';
59                                         },
60                                         getStrokeWidth: function(feature) {
61                                                 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
62                                                 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
63                                                 return 6;
64                                         }
65                                 }
66                         }))
67         });
68
69         
70         // point layer
71         // -----------
72         var filter_point_sledrun = new OpenLayers.Filter.Comparison({
73                 type: OpenLayers.Filter.Comparison.EQUAL_TO,
74                 property: 'type',
75                 value: 'sledrun'
76         });
77
78         var layer_point = new OpenLayers.Layer.Vector("Point", {
79                 styleMap: new OpenLayers.StyleMap({
80                         'default': new OpenLayers.Style({
81                                         graphicZIndex: 12,
82                                         backgroundGraphicZIndex: 11,
83                                 }, {
84                                         context: {
85                                                 // the following context functions should only be available in the rule that uses them,
86                                                 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
87                                                 getCondition: function(feature) {
88                                                         if (feature.condition === undefined) return 0;
89                                                         return feature.condition;
90                                                 },
91                                                 getSymbol: function(feature) {
92                                                         var name = feature.attributes.type;
93                                                         return name;
94                                                 }
95                                         },
96                                         rules: [
97                                                 new OpenLayers.Rule({
98                                                         filter: filter_point_sledrun,
99                                                         symbolizer: {
100                                                                 externalGraphic: '/vorlagen/bahnzustand${getCondition}_0.png',
101                                                                 graphicWidth: 17,
102                                                                 graphicHeight: 17,
103                                                                 graphicXOffset: -8,
104                                                                 graphicYOffset: -8,
105                                                                 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
106                                                                 backgroundWidth: 23,
107                                                                 backgroundHeight: 23,
108                                                                 backgroundXOffset: -8,
109                                                                 backgroundYOffset: -8,
110                                                         }
111                                                 }),
112                                                 new OpenLayers.Rule({
113                                                         elseFilter: true,
114                                                         symbolizer: {
115                                                                 externalGraphic: '/vorlagen/gmap_${getSymbol}.png',
116                                                                 graphicWidth: 20,
117                                                                 graphicHeight: 34,
118                                                                 graphicXOffset: -10,
119                                                                 graphicYOffset: -33,
120                                                         }
121                                                 })
122                                         ]
123                                 }),
124                         highlight: new OpenLayers.Style({
125                                         label: "${getTitle}",
126                                         labelOutlineColor: "white",
127                                         fontWeight: "bold"
128                                 }, {
129                                         context: {
130                                                 getTitle: function(feature) {
131                                                         var title = '';
132                                                         if (feature.attributes.type != 'point') {
133                                                                 title = feature.attributes.type; // TODO: First letter uppercase
134                                                                 if (feature.attributes.name !== undefined) title += ': ';
135                                                         }
136                                                         if (feature.attributes.name !== undefined) title += feature.attributes.name;
137                                                         return title;
138                                                 }
139                                         },
140                                         rules: [
141                                                 new OpenLayers.Rule({
142                                                         filter: filter_point_sledrun,
143                                                         symbolizer: {
144                                                                 label: "${name}",
145                                                                 labelYOffset: 14
146                                                         }
147                                                 }),
148                                                 new OpenLayers.Rule({
149                                                         elseFilter: true,
150                                                         symbolizer: {
151                                                                 labelYOffset: 40
152                                                         }
153                                                 })
154                                         ]
155                                 }) 
156                 }),
157                 rendererOptions: {yOrdering: true}
158         });
159
160
161         // map itself
162         // ----------
163         var lon = json_js.properties.lon;
164         var lat = json_js.properties.lat;
165         var zoom = json_js.properties.zoom;
166         var map = new OpenLayers.Map(jq_map.context, {
167                 projection: EPSG3857,
168                 displayProjection: EPSG4326,
169                 units: "m",
170                 theme: null,
171                 layers: [layer_map, layer_path, layer_point],
172                 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
173                 zoom: zoom
174         });
175         
176
177         // add features
178         // if this would be done before the layer is added to the map, the features are not added
179         layer_path.addFeatures(features_path); 
180         layer_point.addFeatures(features_point); 
181
182         // disable mouse wheel zoom
183         var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
184         navigation_control.disableZoomWheel();
185
186         // layer switcher
187         // map.addControl(new OpenLayers.Control.LayerSwitcher());
188
189         // print sledrun name when mouse moves over it
190         map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
191                 hover: true,
192                 highlightOnly: true,
193                 autoActivate: true,
194                 renderIntent: "highlight"
195         }));
196
197         // show popup when user clicks on a sledrun icon
198         map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
199                 autoActivate: true,
200                 toggle: true,
201                 onSelect: function(feature) {
202                         var popup_text = '';
203                         if (feature.attributes.type == 'sledrun') {
204                                 popup_text += "<h2>" + feature.attributes.name + '</h2>\n' +
205                                 '<ul>\n' +
206                                 '<li><a href="' + feature.attributes.wiki + '">Details zur Rodelbahn</a></li>\n' + 
207                                 '<li>Rodelbahnzustand<br/>';
208                                 if (feature.attributes.condition !== undefined) {
209                                         var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelm&auml;&szlig;ig', 4: 'Schlecht', 5: 'Geht nicht'};
210                                         var year_month_day = feature.attributes.date_report.split('-');
211                                         popup_text += '<a href="' + feature.attributes.wiki + '#Eintr.C3.A4ge">' + condition_text[feature.attributes.condition] + '</a> ' + 
212                                         '<small>' + year_month_day[2] + '.' + year_month_day[1] + '.</small> ' +
213                                         '<em><a href="' + feature.attributes.wiki + '#Eintragen">Neu</a></em>';
214                                 } else {
215                                         popup_text += '<em><a href="' + feature.attributes.wiki + '#Eintragen">Bitte eintragen</a></em>';
216                                 }
217                                 popup_text += '</li>\n</ul>\n';
218                         } else if (feature.attributes.wiki !== undefined) {
219                                 if (feature.attributes.name != undefined) popup_text += '<h2>' + feature.attributes.name + '</h2>\n';
220                                 popup_text += '<p><a href="' + feature.attributes.wiki + '">Details</a></p>\n';
221                         } else return;
222
223                         // Open popup
224                         var selectFeatureControl = this;
225                         var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + feature.attributes.wiki, 
226                         feature.geometry.getBounds().getCenterLonLat(),
227                         null,
228                         popup_text,
229                         null, true, function(event) {
230                                 // onPopupClose
231                                 selectFeatureControl.unselectAll();
232                         });
233                         feature.popup = popup;
234                         map.addPopup(popup);
235                 },
236                 onUnselect: function(feature) {
237                         if (feature.popup === null) return;
238
239                         // Close popup
240                         map.removePopup(feature.popup);
241                         feature.popup.destroy();
242                         feature.popup = null;
243                 }
244         }));
245 }
246
247
248 function init_wrmaps() {
249         var jq_maps = $('.wrmap'); // all wrmap <div> elements
250         jq_maps.each(init_wrmap);
251 }
252
253
254 $(document).ready(init_wrmaps);
255