]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/handlebar_helpers.js
detailsURL helper which replaces detailsLink and detailsPermaLink
[nominatim-ui.git] / src / handlebar_helpers.js
1 'use strict';
2
3 function formatOSMType(sType, bExcludeExternal) {
4   if (sType === 'N') return 'node';
5   if (sType === 'W') return 'way';
6   if (sType === 'R') return 'relation';
7
8   if (!bExcludeExternal) return '';
9
10   if (sType === 'T') return 'way';
11   if (sType === 'I') return 'way';
12
13   return '';
14 }
15
16 function formatShortOSMType(sType) {
17   if (sType === 'node') return 'N';
18   if (sType === 'way') return 'W';
19   if (sType === 'relation') return 'R';
20   return '';
21 }
22
23 function getIcon(aPlace) {
24   // equivalent to PHP Nominatim::ClassTypes::getIcon
25   // covers 83 of 214 available icon filenames, e.g. transport_roundabout_anticlockwise
26   // transport_rental_bicycle or place_of_worship_christian would need more data from
27   // the place.
28   var aIcons = {
29     'boundary:administrative': 'poi_boundary_administrative',
30     'place:city': 'poi_place_city',
31     'place:town': 'poi_place_town',
32     'place:village': 'poi_place_village',
33     'place:hamlet': 'poi_place_village',
34     'place:suburb': 'poi_place_village',
35     'place:locality': 'poi_place_village',
36     'place:airport': 'transport_airport2',
37     'aeroway:aerodrome': 'transport_airport2',
38     'railway:station': 'transport_train_station2',
39     'amenity:place_of_worship': 'place_of_worship_unknown3',
40     'amenity:pub': 'food_pub',
41     'amenity:bar': 'food_bar',
42     'amenity:university': 'education_university',
43     'tourism:museum': 'tourist_museum',
44     'amenity:arts_centre': 'tourist_art_gallery2',
45     'tourism:zoo': 'tourist_zoo',
46     'tourism:theme_park': 'poi_point_of_interest',
47     'tourism:attraction': 'poi_point_of_interest',
48     'leisure:golf_course': 'sport_golf',
49     'historic:castle': 'tourist_castle',
50     'amenity:hospital': 'health_hospital',
51     'amenity:school': 'education_school',
52     'amenity:theatre': 'tourist_theatre',
53     'amenity:library': 'amenity_library',
54     'amenity:fire_station': 'amenity_firestation3',
55     'amenity:police': 'amenity_police2',
56     'amenity:bank': 'money_bank2',
57     'amenity:post_office': 'amenity_post_office',
58     'tourism:hotel': 'accommodation_hotel2',
59     'amenity:cinema': 'tourist_cinema',
60     'tourism:artwork': 'tourist_art_gallery2',
61     'historic:archaeological_site': 'tourist_archaeological2',
62     'amenity:doctors': 'health_doctors',
63     'leisure:sports_centre': 'sport_leisure_centre',
64     'leisure:swimming_pool': 'sport_swimming_outdoor',
65     'shop:supermarket': 'shopping_supermarket',
66     'shop:convenience': 'shopping_convenience',
67     'amenity:restaurant': 'food_restaurant',
68     'amenity:fast_food': 'food_fastfood',
69     'amenity:cafe': 'food_cafe',
70     'tourism:guest_house': 'accommodation_bed_and_breakfast',
71     'amenity:pharmacy': 'health_pharmacy_dispensing',
72     'amenity:fuel': 'transport_fuel',
73     'natural:peak': 'poi_peak',
74     'natural:wood': 'landuse_coniferous_and_deciduous',
75     'shop:bicycle': 'shopping_bicycle',
76     'shop:clothes': 'shopping_clothes',
77     'shop:hairdresser': 'shopping_hairdresser',
78     'shop:doityourself': 'shopping_diy',
79     'shop:estate_agent': 'shopping_estateagent2',
80     'shop:car': 'shopping_car',
81     'shop:garden_centre': 'shopping_garden_centre',
82     'shop:car_repair': 'shopping_car_repair',
83     'shop:bakery': 'shopping_bakery',
84     'shop:butcher': 'shopping_butcher',
85     'shop:apparel': 'shopping_clothes',
86     'shop:laundry': 'shopping_laundrette',
87     'shop:beverages': 'shopping_alcohol',
88     'shop:alcohol': 'shopping_alcohol',
89     'shop:optician': 'health_opticians',
90     'shop:chemist': 'health_pharmacy',
91     'shop:gallery': 'tourist_art_gallery2',
92     'shop:jewelry': 'shopping_jewelry',
93     'tourism:information': 'amenity_information',
94     'historic:ruins': 'tourist_ruin',
95     'amenity:college': 'education_school',
96     'historic:monument': 'tourist_monument',
97     'historic:memorial': 'tourist_monument',
98     'historic:mine': 'poi_mine',
99     'tourism:caravan_site': 'accommodation_caravan_park',
100     'amenity:bus_station': 'transport_bus_station',
101     'amenity:atm': 'money_atm2',
102     'tourism:viewpoint': 'tourist_view_point',
103     'tourism:guesthouse': 'accommodation_bed_and_breakfast',
104     'railway:tram': 'transport_tram_stop',
105     'amenity:courthouse': 'amenity_court',
106     'amenity:recycling': 'amenity_recycling',
107     'amenity:dentist': 'health_dentist',
108     'natural:beach': 'tourist_beach',
109     'railway:tram_stop': 'transport_tram_stop',
110     'amenity:prison': 'amenity_prison',
111     'highway:bus_stop': 'transport_bus_stop2'
112   };
113
114   var sCategoryPlace = aPlace.category + ':' + aPlace.type;
115
116   return aIcons[sCategoryPlace];
117 }
118
119
120 Handlebars.registerHelper({
121   formatOSMType: function (sType, bExcludeExternal) {
122     return formatOSMType(sType, bExcludeExternal);
123   },
124   shortOSMType: function (sType) {
125     return formatShortOSMType(sType);
126   },
127   // { osm_type: 'R', osm_id: 12345 }
128   // <a href="https://www.openstreetmap.org/relation/12345">relation 12345</a
129   osmLink: function (aPlace) {
130     if (!aPlace.osm_type) return '';
131     var sOSMType = formatOSMType(aPlace.osm_type, false);
132     if (!sOSMType) return '';
133
134     return new Handlebars.SafeString(
135       '<a href="https://www.openstreetmap.org/' + sOSMType + '/' + aPlace.osm_id + '">' + sOSMType + ' ' + aPlace.osm_id + '</a>'
136     );
137   },
138   /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */
139   wikipediaLink: function (aPlace) {
140     if (!aPlace.calculated_wikipedia) return '';
141
142     var parts = aPlace.calculated_wikipedia.split(':', 2);
143
144     var sTitle = Handlebars.escapeExpression(aPlace.calculated_wikipedia);
145     var sLanguage = Handlebars.escapeExpression(parts[0]);
146     var sArticle = Handlebars.escapeExpression(parts[1]);
147
148     return new Handlebars.SafeString(
149       '<a href="https://' + sLanguage + '.wikipedia.org/wiki/' + sArticle + '" target="_blank">' + sTitle + '</a>'
150     );
151   },
152   // 'details.html?osmtype=R&osmid=2181874&class=boundary'
153   detailsURL: function (aFeature) {
154     if (!aFeature) return '';
155
156     var sOSMType = aFeature.osm_type;
157     if (sOSMType && sOSMType.length !== 1) {
158       sOSMType = formatShortOSMType(aFeature.osm_type, false); // node => N
159     }
160     if (!sOSMType) return '';
161
162     var sURL = 'details.html?osmtype=' + sOSMType + '&osmid=' + aFeature.osm_id;
163     if (aFeature.class) {
164       sURL = sURL + '&class=' + aFeature.class;
165     } else if (aFeature.category) {
166       sURL = sURL + '&class=' + aFeature.category;
167     }
168     return sURL;
169   },
170   formatPlaceType: function (aPlace) {
171     var sOut = aPlace.class + ':' + aPlace.type;
172     if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) {
173       sOut = sOut + ' (' + aPlace.place_type + ')';
174     }
175     return new Handlebars.SafeString(sOut);
176   },
177   coverageType: function (aPlace) {
178     return (aPlace.isarea ? 'Polygon' : 'Point');
179   },
180   formatDistance: function (fDistance, bInMeters) {
181     if (bInMeters) {
182       if (fDistance < 1) return '0';
183       var sFormatted = (fDistance >= 1000)
184         ? Math.round(fDistance / 1000, 1) + ' km'
185         : Math.round(fDistance, 0) + ' m';
186
187       return new Handlebars.SafeString(
188         '<abbr class="distance" title="' + fDistance + ' meters">~' + sFormatted + '</abbr>'
189       );
190     }
191
192     // spheric distance, http://postgis.net/docs/ST_Distance_Spheroid.html
193     if (fDistance === 0) return '0';
194
195     return new Handlebars.SafeString(
196       '<abbr class="distance" title="spheric distance ' + fDistance + '">~'
197         + (Math.round(fDistance * 1000, 4) / 1000)
198         + '</abbr>'
199     );
200   },
201   // mark partial tokens (those starting with a space) with a star for readability
202   formatKeywordToken: function (sToken) {
203     return (sToken[0] === ' ' ? '*' : '') + Handlebars.escapeExpression(sToken);
204   },
205   // Any over 15 are invalid data in OSM anyway
206   formatAdminLevel: function (iLevel) {
207     return (iLevel < 15 ? iLevel : '');
208   },
209   formatMapIcon: function (aPlace) {
210     var sIcon = getIcon(aPlace);
211
212     if (!sIcon) return '';
213
214     var title = 'icon for ' + aPlace.category + ' ' + aPlace.type;
215     var url = get_config_value('Images_Base_Url') + sIcon + '.p.20.png';
216
217     return new Handlebars.SafeString(
218       '<img class="mapicon" src="' + url + '" alt="' + title + '"/>'
219     );
220   },
221   formatLabel: function (aPlace) {
222     if (aPlace.label) return aPlace.label;
223
224     function capitalize(s) {
225       return s && s[0].toUpperCase() + s.slice(1);
226     }
227
228     if (aPlace.type && aPlace.type === 'yes' && aPlace.class) {
229       return capitalize(aPlace.class.replace(/_/g, ' '));
230     }
231     if (aPlace.type) {
232       return capitalize(aPlace.type.replace(/_/g, ' '));
233     }
234     return '';
235   },
236   formatSearchRank: function (iRank) {
237     // same as
238     // https://github.com/osm-search/Nominatim/blob/master/sql/functions.sql
239     // get_searchrank_label()
240
241     if (iRank < 2) return 'continent';
242     if (iRank < 4) return 'sea';
243     if (iRank < 8) return 'country';
244     if (iRank < 12) return 'state';
245     if (iRank < 16) return 'county';
246     if (iRank === 16) return 'city';
247     if (iRank === 17) return 'town / island';
248     if (iRank === 18) return 'village / hamlet';
249     if (iRank === 20) return 'suburb';
250     if (iRank === 21) return 'postcode area';
251     if (iRank === 22) return 'croft / farm / locality / islet';
252     if (iRank === 23) return 'postcode area';
253     if (iRank === 25) return 'postcode point';
254     if (iRank === 26) return 'street / major landmark';
255     if (iRank === 27) return 'minory street / path';
256     if (iRank === 28) return 'house / building';
257     return 'other: ' + iRank;
258   },
259   tooManyHierarchyLinesWarning: function (aPlace) {
260     if (!aPlace.hierarchy) return '';
261
262     var c = Object.keys(aPlace.hierarchy);
263     if (c < 500) return '';
264
265     return new Handlebars.SafeString(
266       '<p>There are more child objects which are not shown.</p>'
267     );
268   },
269   zoomLevels: function (iSelectedZoom) {
270     var aZoomLevels = [
271       /*  0 */ 'Continent / Sea',
272       /*  1 */ '',
273       /*  2 */ '',
274       /*  3 */ 'Country',
275       /*  4 */ '',
276       /*  5 */ 'State',
277       /*  6 */ 'Region',
278       /*  7 */ '',
279       /*  8 */ 'County',
280       /*  9 */ '',
281       /* 10 */ 'City',
282       /* 11 */ '',
283       /* 12 */ 'Town / Village',
284       /* 13 */ '',
285       /* 14 */ 'Suburb',
286       /* 15 */ '',
287       /* 16 */ 'Street',
288       /* 17 */ '',
289       /* 18 */ 'Building',
290       /* 19 */ '',
291       /* 20 */ '',
292       /* 21 */ ''
293     ];
294
295     var select = $('<select>');
296     var option = jQuery('<option>', { value: '', text: '--' });
297     if (typeof (iSelectedZoom) === 'undefined') {
298       option.attr('selected', 'selected');
299     }
300     option.appendTo(select);
301
302     jQuery.each(aZoomLevels, function (i, title) {
303       option = jQuery('<option>', { value: i, text: i + ' ' + title });
304       if (i === iSelectedZoom) option.attr('selected', 'selected');
305       option.appendTo(select);
306     });
307     return new Handlebars.SafeString(select.html());
308   }
309 });