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