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