]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/handlebar_helpers.js
fix remaining eslint warnings
[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 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   coverageType: function (aPlace) {
81     return (aPlace.isarea ? 'Polygon' : 'Point');
82   },
83   // fDistance is in meters
84   formatDistance: function (fDistanceMeters) {
85     if (fDistanceMeters < 1) return '0';
86
87     var formatted = (fDistanceMeters >= 1000)
88       ? Math.round(fDistanceMeters / 1000, 1) + ' km'
89       : Math.round(fDistanceMeters, 0) + ' m';
90
91     return new Handlebars.SafeString(
92       '<abbr class="distance" title="' + fDistanceMeters + '">~' + formatted + '</abbr>'
93     );
94   },
95   // mark partial tokens (those starting with a space) with a star for readability
96   formatKeywordToken: function (sToken) {
97     return (sToken[0] === ' ' ? '*' : '') + Handlebars.escapeExpression(sToken);
98   },
99   // Any over 15 are invalid data in OSM anyway
100   formatAdminLevel: function (iLevel) {
101     return (iLevel < 15 ? iLevel : '');
102   },
103   formatMapIcon: function (sIcon) {
104     if (!sIcon) return '';
105
106     var url = sIcon;
107     if (!url.match(/^http/)) url = get_config_value('Images_Base_Url') + url;
108
109     return new Handlebars.SafeString(
110       '<img class="mapicon" src="' + url + '" alt="' + sIcon + '"/>'
111     );
112   },
113   formatLabel: function (aPlace) {
114     if (aPlace.label) return aPlace.label;
115
116     function capitalize(s) {
117       return s && s[0].toUpperCase() + s.slice(1);
118     }
119
120     if (aPlace.type && aPlace.type === 'yes' && aPlace.class) {
121       return capitalize(aPlace.class.replace(/_/g, ' '));
122     }
123     if (aPlace.type) {
124       return capitalize(aPlace.type.replace(/_/g, ' '));
125     }
126     return '';
127   },
128   formatSearchRank: function (iRank) {
129     // same as
130     // https://github.com/openstreetmap/Nominatim/blob/master/sql/functions.sql
131     // get_searchrank_label()
132
133     if (iRank < 2) return 'continent';
134     if (iRank < 4) return 'sea';
135     if (iRank < 8) return 'country';
136     if (iRank < 12) return 'state';
137     if (iRank < 16) return 'county';
138     if (iRank === 16) return 'city';
139     if (iRank === 17) return 'town / island';
140     if (iRank === 18) return 'village / hamlet';
141     if (iRank === 20) return 'suburb';
142     if (iRank === 21) return 'postcode area';
143     if (iRank === 22) return 'croft / farm / locality / islet';
144     if (iRank === 23) return 'postcode area';
145     if (iRank === 25) return 'postcode point';
146     if (iRank === 26) return 'street / major landmark';
147     if (iRank === 27) return 'minory street / path';
148     if (iRank === 28) return 'house / building';
149     return 'other: ' + iRank;
150   },
151   tooManyHierarchyLinesWarning: function (aPlace) {
152     if (!aPlace.hierarchy) return '';
153
154     var c = Object.keys(aPlace.hierarchy);
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') {
190       option.attr('selected', 'selected');
191     }
192     option.appendTo(select);
193
194     jQuery.each(aZoomLevels, function (i, title) {
195       option = jQuery('<option>', { value: i, text: i + ' ' + title });
196       if (i === iSelectedZoom) option.attr('selected', 'selected');
197       option.appendTo(select);
198     });
199     return new Handlebars.SafeString(select.html());
200   }
201 });