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