]> ToastFreeware Gitweb - philipp/winterrodeln/mediawiki_extensions/wrmap.git/blob - wrmap.js
5188f82f411b16df09479aa0b6b85be4ba56a812
[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         /*
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 ol.format.GeoJSON();
51         var features_all = format_geojson.readFeatures(json_js, {dataProjection: EPSG4326, featureProjection: EPSG3857});
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
93         // path layer
94         // ----------
95
96         function get_feature_title(feature) {
97                 var title = feature.get('type');
98                 title = title.charAt(0).toUpperCase() + title.slice(1); // first letter uppercase
99                 if (feature.get('name')) title += ': ' + feature.get('name');
100                 return title;
101         }
102
103         // Returns 0 to 5 for features that represent sledruns as their condition
104         var get_sledrun_condition = function(feature) {
105                 var condition = feature.get('condition');
106                 if (condition === undefined) return 0;
107                 return condition;
108         }
109
110         function style_point_function(feature, resolution) {
111                 if (feature.get('type') == 'sledrun') {
112                         var condition = get_sledrun_condition(feature);
113                         var src = img_path + '/marker_c_sledrun_' + condition + 'nn.png';
114                         return new ol.style.Style({
115                                 image: new ol.style.Icon({
116                                         src: src,
117                                         imgSize: [17, 17],
118                                         anchor: [0.5, 0.5]
119                                 }),
120                         });
121                 } else {
122                         var src = img_path + '/marker_p_' + feature.get('type') + '.png';
123                         return new ol.style.Style({
124                                 image: new ol.style.Icon({
125                                         src: src,
126                                         imgSize: [20, 34],
127                                         anchor: [0.5, 1.0]
128                                 }),
129                         });
130                 }
131         }
132
133         function style_point_function_selected(feature, resolution) {
134                 var style = style_point_function(feature, resolution);
135                 style.setText(new ol.style.Text({
136                         text: get_feature_title(feature),
137                         font: 'icon',
138                         offsetY: 14,
139                         stroke: new ol.style.Stroke({
140                                 color: '#ddd',
141                                 width: 2,
142                         }),
143                 }));
144                 return style;
145         }
146
147         function style_path_function(feature, resolution) {
148                 var line_color = {
149                         'rodelbahn': '#014e9a',
150                         'gehweg': '#e98401',
151                         'alternative': '#7f7fff',
152                         'lift': '#000000',
153                         'anfahrt': '#e1e100'
154                 };
155                 var color = feature.get('strokeColor') || line_color[feature.get('type')] || '#e7525b';
156                 var width = (feature.get('type') in ['lift', 'anfahrt']) ? 3 : 6;
157                 return new ol.style.Style({
158                         stroke: new ol.style.Stroke({
159                                 color: color,
160                                 width: width
161                         })
162                 });
163         }
164
165         function style_function(feature, resolution) {
166                 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function(feature, resolution);
167                 return style_path_function(feature, resolution);
168         };
169
170         function style_function_selected(feature, resolution) {
171                 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function_selected(feature, resolution);
172                 return style_path_function(feature, resolution);
173         };
174
175
176         // popup overlay
177         // -------------
178         var popup_container = document.getElementById('popup');
179         var popup_content = document.getElementById('popup-content');
180         var popup_closer = document.getElementById('popup-closer');
181         var popup_overlay = new ol.Overlay({element: popup_container, autoPan: true, autoPanAnimation: {duration: 250}});
182         popup_closer.onclick = function() {popup_overlay.setPosition(undefined); popup_closer.blur(); return false;};
183
184         /*
185         var layer_path = new OpenLayers.Layer.Vector("Path", {
186                 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
187                                 strokeColor: '${getStrokeColor}',
188                                 strokeWidth: '${getStrokeWidth}'
189                         }, {
190                                 context: {
191                                         getStrokeColor: function(feature) {
192                                                 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
193                                                 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
194                                                 if (feature.attributes.type == 'gehweg') return '#e98401';
195                                                 if (feature.attributes.type == 'alternative') return '#7f7fff';
196                                                 if (feature.attributes.type == 'lift') return '#000000';
197                                                 if (feature.attributes.type == 'anfahrt') return '#e1e100';
198                                                 return '#e7525b';
199                                         },
200                                         getStrokeWidth: function(feature) {
201                                                 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
202                                                 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
203                                                 return 6;
204                                         }
205                                 }
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 map = new OpenLayers.Map(jq_map[0], {
337                 projection: EPSG3857,
338                 displayProjection: EPSG4326,
339                 units: "m",
340                 theme: null,
341                 layers: [layer_map, layer_path, layer_point],
342                 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
343                 zoom: zoom
344         });
345         */
346
347         var layer_sledrun_source = new ol.source.Vector({features: features_all});
348         var layer_sledrun = new ol.layer.Vector({
349                 source: layer_sledrun_source,
350                 style: style_function
351         });
352
353         var map = new ol.Map({
354                 target: jq_map[0],
355                 layers: [
356                         layer_map,
357                         layer_sledrun
358                 ],
359                 overlays: [popup_overlay],
360                 view: new ol.View({
361                         center: ol.proj.fromLonLat([lon, lat]),
362                         zoom: zoom
363                 }),
364                 controls: ol.control.defaults({
365                         attributionOptions: {
366                                 collapsible: false
367                         }
368                 }),
369                 interactions: ol.interaction.defaults({
370                         mouseWheelZoom: false
371                 })
372         });
373
374
375         var select_hover = new ol.interaction.Select({
376                 condition: ol.events.condition.pointerMove,
377                 style: style_function_selected,
378         });
379         map.addInteraction(select_hover);
380
381         var select_click = new ol.interaction.Select({
382                 condition: ol.events.condition.click,
383         });
384         map.addInteraction(select_click);
385         select_click.on('select', function(event) {
386                 popup_content.innerHTML = '<p>You clicked</p>';
387                 popup_overlay.setPosition(event.coordinate);
388         });
389
390         /*
391         // add features
392         // if this would be done before the layer is added to the map, the features are not added
393         layer_path.addFeatures(features_path); 
394         layer_point.addFeatures(features_point); 
395
396         // disable mouse wheel zoom
397         var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
398         navigation_control.disableZoomWheel();
399
400         // layer switcher
401         // map.addControl(new OpenLayers.Control.LayerSwitcher());
402
403         // print sledrun name when mouse moves over it
404         map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
405                 hover: true,
406                 highlightOnly: true,
407                 autoActivate: true,
408                 renderIntent: "highlight"
409         }));
410
411         // show popup when user clicks on a sledrun icon
412         map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
413                 autoActivate: true,
414                 toggle: true,
415                 onSelect: function(feature) {
416                         var attr = feature.attributes;
417                         var popup_div = createElement('div');
418
419                         // name
420                         if (attr.name !== undefined && (attr.wiki !== undefined || attr.thumb_url !== undefined)) {
421                                 var h2 = appendElement(popup_div, 'h2');
422                                 if (attr.wiki === undefined) h2.text(attr.name);
423                                 else appendElement(h2, 'a', {href: attr.wiki}).text(attr.name);
424                         }
425
426                         // sledrun information
427                         if (attr.type == 'sledrun') {
428                                 var p = appendElement(popup_div, 'p').text('Rodelbahnzustand').append(createElement('br'));
429                                 if (attr.condition !== undefined) {
430                                         var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
431                                         var year_month_day = attr.date_report.split('-');
432                                         p.append(createElement('a', {href: attr.wiki + '#Eintr.C3.A4ge'}).text(condition_text[attr.condition]), ' ');
433                                         p.append(createElement('small').text(year_month_day[2] + '.' + year_month_day[1] + '.'), ' ');
434                                         p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Neu')));
435                                 } else {
436                                         p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Bitte eintragen')));
437                                 }
438                         }
439
440                         // wiki link
441                         if (attr.wiki !== undefined) {
442                                 var a = appendElement(appendElement(popup_div, 'p'), 'a', {href: attr.wiki});
443                                 var detail_text = 'Details';
444                                 if (attr.type == 'sledrun') detail_text += ' zur Rodelbahn';
445                                 if (attr.type == 'gasthaus') detail_text += ' zum Gasthaus';
446                                 if (attr.thumb_url === undefined) a.text(detail_text);
447                                 else a.append(createElement('img', {src: attr.thumb_url, alt: detail_text, title: detail_text}));
448                         }
449
450                         // no popup if we don't have anything to say
451                         if (popup_div.children().length == 0) return;
452
453                         // Open popup
454                         var selectFeatureControl = this;
455                         var popup = new OpenLayers.Popup.WrInfo('sledruninfopopup_' + attr.wiki,
456                         feature.geometry.getBounds().getCenterLonLat(),
457                         null,
458                         popup_div.html(),
459                         null, true, function(event) {
460                                 // onPopupClose
461                                 selectFeatureControl.unselectAll();
462                         });
463                         feature.popup = popup;
464                         map.addPopup(popup);
465                 },
466                 onUnselect: function(feature) {
467                         if (feature.popup === null) return;
468
469                         // Close popup
470                         map.removePopup(feature.popup);
471                         feature.popup.destroy();
472                         feature.popup = null;
473                 }
474         }));
475 */
476 }
477
478 function init_wrmaps() {
479         var jq_maps = $('.wrmap'); // all wrmap <div> elements
480         jq_maps.each(init_wrmap);
481 }
482
483
484 $(document).ready(init_wrmaps);
485