2bd5ad7610f8beb88428bacfca61c1aa9ff1d5f3
[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         // define constants
19         var EPSG4326 = ol.proj.get("EPSG:4326"); // lon/lat
20         var EPSG3857 = ol.proj.get("EPSG:3857"); // google
21
22         // tool functions
23         function createElement(tagName, attributes) {
24                 var element = $(document.createElement(tagName));
25                 if (attributes === undefined) return element;
26                 for (var attribute in attributes) {
27                         element.attr(attribute, attributes[attribute]);
28                 }
29                 return element;
30         }
31
32         function appendElement(parentElement, tagName, attributes) {
33                 if (attributes === undefined) attributes = {};
34                 var element = createElement(tagName, attributes);
35                 parentElement.append(element);
36                 return element;
37         }
38
39
40         // extract geojson from map element and clear map element's content
41         jq_map = $(jq_map);
42         var ext_path = jq_map.attr('data-ext-path'); // e.g. '/mediawiki/extensions/wrmap'
43         var img_path = ext_path + '/img';
44         var json_string = jq_map.children().last().text();
45         jq_map.empty(); // once parsed, remove geojson string from the map element.
46         var json_js = JSON.parse(json_string);
47         var format_geojson = new ol.format.GeoJSON();
48         var features_all = format_geojson.readFeatures(json_js, {dataProjection: EPSG4326, featureProjection: EPSG3857});
49
50
51         // background layer
52         // ----------------
53
54         // OSM map
55         var layer_map = new ol.layer.Tile({
56                 source: new ol.source.OSM()
57         });
58
59
60         /*
61         // Microsoft Bing Maps
62         // var layer_map = new OpenLayers.Layer.Bing({
63         //      type: "Road",
64         //      key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA",
65         //      tileOptions: {crossOriginKeyword: null}});
66
67         // // Alternative: Base map
68         // // see: http://www.basemap.at
69         
70         // // Alternative: OpenTopoMap
71         // // see: https://opentopomap.org/about
72
73         // // Alternative: Dummy base layer
74         // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
75         //     isBaseLayer: true});
76         */
77
78
79         // path layer
80         // ----------
81
82         function get_feature_title(feature) {
83                 var title = feature.get('type');
84                 if (title == 'sledrun') return feature.get('name');
85                 title = title.charAt(0).toUpperCase() + title.slice(1); // first letter uppercase
86                 if (feature.get('name')) title += ': ' + feature.get('name');
87                 return title;
88         }
89
90         // Returns 0 to 5 for features that represent sledruns as their condition
91         var get_sledrun_condition = function(feature) {
92                 var condition = feature.get('condition');
93                 if (condition === undefined) return 0;
94                 return condition;
95         }
96
97         function style_point_function(feature, resolution) {
98                 if (feature.get('type') == 'sledrun') {
99                         var condition = get_sledrun_condition(feature);
100                         var src = img_path + '/marker_c_sledrun_' + condition + 'nn.png';
101                         return new ol.style.Style({
102                                 image: new ol.style.Icon({
103                                         src: src,
104                                         imgSize: [17, 17],
105                                         anchor: [0.5, 0.5]
106                                 }),
107                         });
108                 } else {
109                         var src = img_path + '/marker_p_' + feature.get('type') + '.png';
110                         return new ol.style.Style({
111                                 image: new ol.style.Icon({
112                                         src: src,
113                                         imgSize: [20, 34],
114                                         anchor: [0.5, 1.0]
115                                 }),
116                         });
117                 }
118         }
119
120         function style_point_function_selected(feature, resolution) {
121                 var style = style_point_function(feature, resolution);
122                 style.setText(new ol.style.Text({
123                         text: get_feature_title(feature),
124                         font: 'icon',
125                         offsetY: 14,
126                         stroke: new ol.style.Stroke({
127                                 color: '#ddd',
128                                 width: 2,
129                         }),
130                 }));
131                 return style;
132         }
133
134         function style_path_function(feature, resolution) {
135                 var line_color = {
136                         'rodelbahn': '#014e9a',
137                         'gehweg': '#e98401',
138                         'alternative': '#7f7fff',
139                         'lift': '#000000',
140                         'anfahrt': '#e1e100'
141                 };
142                 var color = feature.get('strokeColor') || line_color[feature.get('type')] || '#e7525b';
143                 var width = (feature.get('type') in ['lift', 'anfahrt']) ? 3 : 6;
144                 return new ol.style.Style({
145                         stroke: new ol.style.Stroke({
146                                 color: color,
147                                 width: width
148                         })
149                 });
150         }
151
152         function style_function(feature, resolution) {
153                 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function(feature, resolution);
154                 return style_path_function(feature, resolution);
155         };
156
157         function style_function_selected(feature, resolution) {
158                 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function_selected(feature, resolution);
159                 return style_path_function(feature, resolution);
160         };
161
162
163         // popup overlay
164         // -------------
165         var popup_container = document.getElementById('popup');
166         var popup_content = document.getElementById('popup-content');
167         var popup_closer = document.getElementById('popup-closer');
168         var popup_overlay = new ol.Overlay({element: popup_container, autoPan: true, autoPanAnimation: {duration: 250}});
169         popup_closer.onclick = function() {popup_overlay.setPosition(undefined); popup_closer.blur(); return false;};
170
171         function create_popup_dom(feature) {
172                 var popup_div = createElement('div');
173
174                 // name
175                 if (feature.get('name') !== undefined && (feature.get('wiki') !== undefined || feature.get('thumb_url') !== undefined)) {
176                         var h2 = appendElement(popup_div, 'h2');
177                         if (feature.get('wiki') === undefined) h2.text(feature.get('name'));
178                         else appendElement(h2, 'a', {href: feature.get('wiki')}).text(feature.get('name'));
179                 }
180
181                 // sledrun information
182                 if (feature.get('type') == 'sledrun') {
183                         var p = appendElement(popup_div, 'p').text('Rodelbahnzustand').append(createElement('br'));
184                         if (feature.get('condition') !== undefined) {
185                                 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
186                                 var year_month_day = feature.get('date_report').split('-');
187                                 p.append(createElement('a', {href: feature.get('wiki') + '#Eintr.C3.A4ge'}).text(condition_text[feature.get('condition')]), ' ');
188                                 p.append(createElement('small').text(year_month_day[2] + '.' + year_month_day[1] + '.'), ' ');
189                                 p.append(createElement('em').append(createElement('a', {href: feature.get('wiki') + '#Eintragen'}).text('Neu')));
190                         } else {
191                                 p.append(createElement('em').append(createElement('a', {href: feature.get('wiki') + '#Eintragen'}).text('Bitte eintragen')));
192                         }
193                 }
194
195                 // wiki link
196                 if (feature.get('wiki') !== undefined) {
197                         var a = appendElement(appendElement(popup_div, 'p'), 'a', {href: feature.get('wiki')});
198                         var detail_text = 'Details';
199                         if (feature.get('type') == 'sledrun') detail_text += ' zur Rodelbahn';
200                         if (feature.get('type') == 'gasthaus') detail_text += ' zum Gasthaus';
201                         if (feature.get('thumb_url') === undefined) a.text(detail_text);
202                         else a.append(createElement('img', {src: feature.get('thumb_url'), alt: detail_text, title: detail_text}));
203                 }
204
205                 return popup_div;
206         }
207
208
209         /*
210         // point layer
211         // -----------
212         var filter_point_sledrun = new OpenLayers.Filter.Comparison({
213                 type: OpenLayers.Filter.Comparison.EQUAL_TO,
214                 property: 'type',
215                 value: 'sledrun'
216         });
217
218
219         var layer_point = new OpenLayers.Layer.Vector("Point", {
220                 styleMap: new OpenLayers.StyleMap({
221                         'default': new OpenLayers.Style({
222                                         graphicZIndex: 12,
223                                         backgroundGraphicZIndex: 11,
224                                 }, {
225                                         context: {
226                                                 // the following context functions should only be available in the rule that uses them,
227                                                 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
228                                                 getCondition: get_sledrun_condition,
229                                                 getSymbol: function(feature) {
230                                                         var name = feature.attributes.type;
231                                                         return name;
232                                                 }
233                                         },
234                                         rules: [
235                                                 new OpenLayers.Rule({
236                                                         filter: filter_point_sledrun,
237                                                         symbolizer: {
238                                                                 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nn.png',
239                                                                 graphicWidth: 17,
240                                                                 graphicHeight: 17,
241                                                                 graphicXOffset: -8,
242                                                                 graphicYOffset: -8,
243                                                                 backgroundGraphic: img_path + '/marker_c_shadow.png',
244                                                                 backgroundWidth: 23,
245                                                                 backgroundHeight: 23,
246                                                                 backgroundXOffset: -8,
247                                                                 backgroundYOffset: -8,
248                                                         }
249                                                 }),
250                                                 new OpenLayers.Rule({
251                                                         elseFilter: true,
252                                                         symbolizer: {
253                                                                 externalGraphic: img_path + '/marker_p_${getSymbol}.png',
254                                                                 graphicWidth: 20,
255                                                                 graphicHeight: 34,
256                                                                 graphicXOffset: -10,
257                                                                 graphicYOffset: -33,
258                                                         }
259                                                 })
260                                         ]
261                                 }),
262                         highlight: new OpenLayers.Style({
263                                         label: "${getTitle}",
264                                         labelOutlineColor: "white",
265                                         fontWeight: "bold"
266                                 }, {
267                                         context: {
268                                                 getCondition: get_sledrun_condition,
269                                                 getTitle: function(feature) {
270                                                         var title = '';
271                                                         if (feature.attributes.type != 'point') {
272                                                                 title = feature.attributes.type;
273                                                                 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
274                                                                 if (feature.attributes.name !== undefined) title += ': ';
275                                                         }
276                                                         if (feature.attributes.name !== undefined) title += feature.attributes.name;
277                                                         return title;
278                                                 }
279                                         },
280                                         rules: [
281                                                 new OpenLayers.Rule({
282                                                         filter: filter_point_sledrun,
283                                                         symbolizer: {
284                                                                 label: "${name}",
285                                                                 labelYOffset: 14,
286                                                                 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png'
287                                                         }
288                                                 }),
289                                                 new OpenLayers.Rule({
290                                                         elseFilter: true,
291                                                         symbolizer: {
292                                                                 labelYOffset: 40
293                                                         }
294                                                 })
295                                         ]
296                                 }),
297                         select: new OpenLayers.Style({
298                                         fillOpacity: 1.
299                                 }, {
300                                         context: {
301                                                 getCondition: get_sledrun_condition,
302                                         },
303                                         rules: [
304                                                 new OpenLayers.Rule({
305                                                         filter: filter_point_sledrun,
306                                                         symbolizer: {
307                                                                 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png',
308                                                                 backgroundGraphic: false,
309                                                                 graphicXOffset: -6,
310                                                                 graphicYOffset: -6
311                                                         }
312                                                 }),
313                                                 new OpenLayers.Rule({
314                                                         elseFilter: true
315                                                 })
316                                         ]
317                                 })
318                 }),
319                 rendererOptions: {yOrdering: true}
320         });
321
322 */
323         // map itself
324         // ----------
325         var lon = json_js.properties.lon;
326         var lat = json_js.properties.lat;
327         var zoom = json_js.properties.zoom;
328         var width = json_js.properties.width;
329         var height = json_js.properties.height;
330         if (zoom === undefined) zoom = 10; // default zoom
331         if (width === undefined) width = '100%'; // default width
332         if (height === undefined) height = 450;  // default: 450 pixel
333         jq_map.width(width);
334         jq_map.height(height);
335
336         var layer_sledrun_source = new ol.source.Vector({features: features_all});
337         var layer_sledrun = new ol.layer.Vector({
338                 source: layer_sledrun_source,
339                 style: style_function
340         });
341
342         var map = new ol.Map({
343                 target: jq_map[0],
344                 layers: [
345                         layer_map,
346                         layer_sledrun
347                 ],
348                 overlays: [popup_overlay],
349                 view: new ol.View({
350                         center: ol.proj.fromLonLat([lon, lat]),
351                         zoom: zoom
352                 }),
353                 controls: ol.control.defaults({
354                         attributionOptions: {
355                                 collapsible: false
356                         }
357                 }),
358                 interactions: ol.interaction.defaults({
359                         mouseWheelZoom: false
360                 })
361         });
362
363
364         var select_hover = new ol.interaction.Select({
365                 condition: ol.events.condition.pointerMove,
366                 style: style_function_selected,
367         });
368         map.addInteraction(select_hover);
369
370         var select_click = new ol.interaction.Select({
371                 condition: ol.events.condition.click,
372         });
373         map.addInteraction(select_click);
374         select_click.on('select', function(event) {
375                 if (event.selected.length > 0) {
376                         let feature = event.selected[0];
377                         let coordinates = feature.getGeometry().getCoordinates();
378                         let popup_dom = create_popup_dom(feature);
379                         $(popup_content).empty().append(popup_dom);
380                         popup_overlay.setPosition(coordinates);
381                 }
382         });
383 }
384
385 function init_wrmaps() {
386         var jq_maps = $('.wrmap'); // all wrmap <div> elements
387         jq_maps.each(init_wrmap);
388 }
389
390
391 $(document).ready(init_wrmaps);
392