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