]> git.openstreetmap.org Git - nominatim-ui.git/blob - dist/handlebar_helpers.js
use new place_type from details JSON endpoint
[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 Handlebars.registerHelper({
17   shortOSMType: function (sType) {
18     if (sType === 'node') return 'N';
19     if (sType === 'way') return 'W';
20     if (sType === 'relation') return 'R';
21     return '';
22   },
23   isaddresses_unused: function (aAddressLine) {
24     return ((aAddressLine.isaddress && aAddressLine.isaddress === 'f') ? 'notused' : '');
25   },
26   // { osm_type: 'R', osm_id: 12345 }
27   // <a href="https://www.openstreetmap.org/relation/12345">relation 12345</a
28   osmLink: function (aPlace) {
29     if (!aPlace.osm_type) return '';
30     var sOSMType = formatOSMType(aPlace.osm_type, false);
31     if (!sOSMType) return '';
32
33     return new Handlebars.SafeString(
34       '<a href="https://www.openstreetmap.org/' + sOSMType + '/' + aPlace.osm_id + '">' + sOSMType + ' ' + aPlace.osm_id + '</a>'
35     );
36   },
37   /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */
38   wikipediaLink: function (aPlace) {
39     if (!aPlace.calculated_wikipedia) return '';
40
41     var parts = aPlace.calculated_wikipedia.split(':', 2);
42
43     var sTitle = Handlebars.escapeExpression(aPlace.calculated_wikipedia);
44     var sLanguage = Handlebars.escapeExpression(parts[0]);
45     var sArticle = Handlebars.escapeExpression(parts[1]);
46
47     return new Handlebars.SafeString(
48       '<a href="https://' + sLanguage + '.wikipedia.org/wiki/' + sArticle + '" target="_blank">' + sTitle + '</a>'
49     );
50   },
51   // { osm_type: 'R', osm_id: 12345 }
52   // <a href="details.html?place_id=12345">details</a>
53   detailsLink: function (aFeature, sTitle) {
54     if (!aFeature) return '';
55     if (!aFeature.place_id) return '';
56
57     var sTitleEscaped = Handlebars.escapeExpression(sTitle || 'details >');
58
59     return new Handlebars.SafeString(
60       '<a href="details.html?place_id=' + aFeature.place_id + '">' + sTitleEscaped + '</a>'
61     );
62   },
63   detailsPermaLink: function (aFeature, sTitle) {
64     if (!aFeature) return '';
65
66     var sOSMType = formatOSMType(aFeature.osm_type, false);
67     if (!sOSMType) return '';
68
69     var sTitleEscaped = Handlebars.escapeExpression(sTitle || sOSMType + ' ' + aFeature.osm_id);
70
71     var sURL = 'details.html?osmtype=' + aFeature.osm_type + '&osmid=' + aFeature.osm_id;
72     if (aFeature.category) {
73       sURL = sURL + '&class=' + aFeature.category;
74     }
75
76     return new Handlebars.SafeString(
77       '<a href="' + sURL + '">' + sTitleEscaped + '</a>'
78     );
79   },
80   formatPlaceType: function (aPlace) {
81     var sOut = aPlace.class + ':' + aPlace.type;
82     if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) {
83       sOut = sOut + ' (' + aPlace.place_type + ')';
84     }
85     return new Handlebars.SafeString(sOut);
86   },
87   coverageType: function (aPlace) {
88     return (aPlace.isarea ? 'Polygon' : 'Point');
89   },
90   // fDistance is in meters
91   formatDistance: function (fDistanceMeters) {
92     if (fDistanceMeters < 1) return '0';
93
94     var formatted = (fDistanceMeters >= 1000)
95       ? Math.round(fDistanceMeters / 1000, 1) + ' km'
96       : Math.round(fDistanceMeters, 0) + ' m';
97
98     return new Handlebars.SafeString(
99       '<abbr class="distance" title="' + fDistanceMeters + '">~' + formatted + '</abbr>'
100     );
101   },
102   // mark partial tokens (those starting with a space) with a star for readability
103   formatKeywordToken: function (sToken) {
104     return (sToken[0] === ' ' ? '*' : '') + Handlebars.escapeExpression(sToken);
105   },
106   // Any over 15 are invalid data in OSM anyway
107   formatAdminLevel: function (iLevel) {
108     return (iLevel < 15 ? iLevel : '');
109   },
110   formatMapIcon: function (sIcon) {
111     if (!sIcon) return '';
112
113     var url = sIcon;
114     if (!url.match(/^http/)) url = get_config_value('Images_Base_Url') + url;
115
116     return new Handlebars.SafeString(
117       '<img class="mapicon" src="' + url + '" alt="' + sIcon + '"/>'
118     );
119   },
120   formatLabel: function (aPlace) {
121     if (aPlace.label) return aPlace.label;
122
123     function capitalize(s) {
124       return s && s[0].toUpperCase() + s.slice(1);
125     }
126
127     if (aPlace.type && aPlace.type === 'yes' && aPlace.class) {
128       return capitalize(aPlace.class.replace(/_/g, ' '));
129     }
130     if (aPlace.type) {
131       return capitalize(aPlace.type.replace(/_/g, ' '));
132     }
133     return '';
134   },
135   formatSearchRank: function (iRank) {
136     // same as
137     // https://github.com/openstreetmap/Nominatim/blob/master/sql/functions.sql
138     // get_searchrank_label()
139
140     if (iRank < 2) return 'continent';
141     if (iRank < 4) return 'sea';
142     if (iRank < 8) return 'country';
143     if (iRank < 12) return 'state';
144     if (iRank < 16) return 'county';
145     if (iRank === 16) return 'city';
146     if (iRank === 17) return 'town / island';
147     if (iRank === 18) return 'village / hamlet';
148     if (iRank === 20) return 'suburb';
149     if (iRank === 21) return 'postcode area';
150     if (iRank === 22) return 'croft / farm / locality / islet';
151     if (iRank === 23) return 'postcode area';
152     if (iRank === 25) return 'postcode point';
153     if (iRank === 26) return 'street / major landmark';
154     if (iRank === 27) return 'minory street / path';
155     if (iRank === 28) return 'house / building';
156     return 'other: ' + iRank;
157   },
158   tooManyHierarchyLinesWarning: function (aPlace) {
159     if (!aPlace.hierarchy) return '';
160
161     var c = Object.keys(aPlace.hierarchy);
162     if (c < 500) return '';
163
164     return new Handlebars.SafeString(
165       '<p>There are more child objects which are not shown.</p>'
166     );
167   },
168   zoomLevels: function (iSelectedZoom) {
169     var aZoomLevels = [
170       /*  0 */ 'Continent / Sea',
171       /*  1 */ '',
172       /*  2 */ '',
173       /*  3 */ 'Country',
174       /*  4 */ '',
175       /*  5 */ 'State',
176       /*  6 */ 'Region',
177       /*  7 */ '',
178       /*  8 */ 'County',
179       /*  9 */ '',
180       /* 10 */ 'City',
181       /* 11 */ '',
182       /* 12 */ 'Town / Village',
183       /* 13 */ '',
184       /* 14 */ 'Suburb',
185       /* 15 */ '',
186       /* 16 */ 'Street',
187       /* 17 */ '',
188       /* 18 */ 'Building',
189       /* 19 */ '',
190       /* 20 */ '',
191       /* 21 */ ''
192     ];
193
194     var select = $('<select>');
195     var option = jQuery('<option>', { value: '', text: '--' });
196     if (typeof (iSelectedZoom) === 'undefined') {
197       option.attr('selected', 'selected');
198     }
199     option.appendTo(select);
200
201     jQuery.each(aZoomLevels, function (i, title) {
202       option = jQuery('<option>', { value: i, text: i + ' ' + title });
203       if (i === iSelectedZoom) option.attr('selected', 'selected');
204       option.appendTo(select);
205     });
206     return new Handlebars.SafeString(select.html());
207   }
208 });