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