]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/handlebar_helpers.js
3ef3a662f3331c9574c7a3f74fd341ab737effc5
[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     return new Handlebars.SafeString(
70       '<a href="details.html?osmtype=' + aFeature.osm_type + '&osmid=' + aFeature.osm_id + '&class=' + aFeature.category + '">' + sTitleEscaped + '</a>'
71     );
72   },
73   coverageType: function (aPlace) {
74     return (aPlace.isarea ? 'Polygon' : 'Point');
75   },
76   // fDistance is in meters
77   formatDistance: function (fDistanceMeters) {
78     if (fDistanceMeters < 1) return '0';
79
80     var formatted = (fDistanceMeters >= 1000)
81       ? Math.round(fDistanceMeters / 1000, 1) + ' km'
82       : Math.round(fDistanceMeters, 0) + ' m';
83
84     return new Handlebars.SafeString(
85       '<abbr class="distance" title="' + fDistanceMeters + '">~' + formatted + '</abbr>'
86     );
87   },
88   // mark partial tokens (those starting with a space) with a star for readability
89   formatKeywordToken: function (sToken) {
90     return (sToken[0] === ' ' ? '*' : '') + Handlebars.escapeExpression(sToken);
91   },
92   // Any over 15 are invalid data in OSM anyway
93   formatAdminLevel: function (iLevel) {
94     return (iLevel < 15 ? iLevel : '');
95   },
96   formatMapIcon: function (sIcon) {
97     if (!sIcon) return;
98
99     var url = sIcon;
100     if (!url.match(/^http/)) url = get_config_value('Images_Base_Url') + url;
101
102     return new Handlebars.SafeString(
103       '<img class="mapicon" src="' + url + '" alt="' + sIcon + '"/>'
104     );
105   },
106   formatLabel: function (aPlace) {
107     if (aPlace.label) return aPlace.label;
108
109     function capitalize(s) {
110       return s && s[0].toUpperCase() + s.slice(1);
111     }
112
113     if (aPlace.type && aPlace.type === 'yes') {
114       return capitalize(aPlace.class.replace(/_/g, ' '));
115     }
116     return capitalize(aPlace.type.replace(/_/g, ' '));
117   },
118   formatSearchRank: function (iRank) {
119     // same as
120     // https://github.com/openstreetmap/Nominatim/blob/master/sql/functions.sql
121     // get_searchrank_label()
122
123     if (iRank < 2) return 'continent';
124     if (iRank < 4) return 'sea';
125     if (iRank < 8) return 'country';
126     if (iRank < 12) return 'state';
127     if (iRank < 16) return 'county';
128     if (iRank === 16) return 'city';
129     if (iRank === 17) return 'town / island';
130     if (iRank === 18) return 'village / hamlet';
131     if (iRank === 20) return 'suburb';
132     if (iRank === 21) return 'postcode area';
133     if (iRank === 22) return 'croft / farm / locality / islet';
134     if (iRank === 23) return 'postcode area';
135     if (iRank === 25) return 'postcode point';
136     if (iRank === 26) return 'street / major landmark';
137     if (iRank === 27) return 'minory street / path';
138     if (iRank === 28) return 'house / building';
139     return 'other: ' + iRank;
140   },
141   tooManyHierarchyLinesWarning: function (aPlace) {
142     if (!aPlace.hierarchy) return;
143
144     var c = 0;
145     for (var type in aPlace.hierarchy) {
146       c = c + type.length + 1;
147     }
148     if (c < 500) return;
149
150     return new Handlebars.SafeString(
151       '<p>There are more child objects which are not shown.</p>'
152     );
153   },
154   zoomLevels: function (iSelectedZoom) {
155     var aZoomLevels = [
156       /*  0 */ 'Continent / Sea',
157       /*  1 */ '',
158       /*  2 */ '',
159       /*  3 */ 'Country',
160       /*  4 */ '',
161       /*  5 */ 'State',
162       /*  6 */ 'Region',
163       /*  7 */ '',
164       /*  8 */ 'County',
165       /*  9 */ '',
166       /* 10 */ 'City',
167       /* 11 */ '',
168       /* 12 */ 'Town / Village',
169       /* 13 */ '',
170       /* 14 */ 'Suburb',
171       /* 15 */ '',
172       /* 16 */ 'Street',
173       /* 17 */ '',
174       /* 18 */ 'Building',
175       /* 19 */ '',
176       /* 20 */ '',
177       /* 21 */ ''
178     ];
179
180     var select = $('<select>');
181     var option = jQuery('<option>', { value: '', text: '--' });
182     if (typeof (iSelectedZoom) === 'undefined') option.attr('selected', 'selected');
183     option.appendTo(select);
184
185     jQuery.each(aZoomLevels, function (i, title) {
186       option = jQuery('<option>', { value: i, text: i + ' ' + title });
187       if (i === iSelectedZoom) option.attr('selected', 'selected');
188       option.appendTo(select);
189     });
190     return new Handlebars.SafeString(select.html());
191   }
192 });