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