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