]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/handlebar_helpers.js
handlebar 4.1 => 4.5
[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 = sIcon;
95         if (!url.match(/^http/)) url = get_config_value('Images_Base_Url') + url;
96
97         return new Handlebars.SafeString(
98             '<img class="mapicon" src="' + url + '" alt="' + sIcon + '"/>'
99         );
100     },
101     formatLabel: function(aPlace) {
102         if (aPlace.label) return aPlace.label;
103
104         function capitalize(s)
105         {
106             return s && s[0].toUpperCase() + s.slice(1);
107         }
108
109         if (aPlace.type && aPlace.type === 'yes') {
110             return capitalize(aPlace.class.replace(/_/g, ' '));
111         } else {
112             return capitalize(aPlace.type.replace(/_/g, ' '));
113         }
114     },
115     formatSearchRank: function(iRank) {
116         // same as
117         // https://github.com/openstreetmap/Nominatim/blob/master/sql/functions.sql
118         // get_searchrank_label()
119
120         if (iRank < 2) {
121             return 'continent';
122         } else if (iRank < 4) {
123             return 'sea';
124         } else if (iRank < 8) {
125             return 'country';
126         } else if (iRank < 12) {
127             return 'state';
128         } else if (iRank < 16) { 
129             return 'county';
130         } else if (iRank == 16) {
131             return 'city';
132         } else if (iRank == 17) {
133             return 'town / island';
134         } else if (iRank == 18) {
135             return 'village / hamlet';
136         } else if (iRank == 20) {
137             return 'suburb';
138         } else if (iRank == 21) {
139             return 'postcode area';
140         } else if (iRank == 22) {
141             return 'croft / farm / locality / islet';
142         } else if (iRank == 23) {
143             return 'postcode area';
144         } else if (iRank == 25) {
145             return 'postcode point';
146         } else if (iRank == 26) {
147             return 'street / major landmark';
148         } else if (iRank == 27) {
149             return 'minory street / path';
150         } else if (iRank == 28) {
151             return 'house / building';
152         } else {
153             return 'other: ' + iRank;
154         }
155     },
156     tooManyHierarchyLinesWarning: function(aPlace) {
157         if (!aPlace.hierarchy) return;
158
159         var c = 0;
160         for (var type in aPlace.hierarchy) {
161             c = c + type.length+1;
162         }
163         if (c < 500) return;
164
165         return new Handlebars.SafeString(
166             '<p>There are more child objects which are not shown.</p>'
167         );
168     },
169     zoomLevels: function(iSelectedZoom) {
170         var aZoomLevels = [
171             /*  0 */ 'Continent / Sea',
172             /*  1 */ '',
173             /*  2 */ '',
174             /*  3 */ 'Country',
175             /*  4 */ '',
176             /*  5 */ 'State',
177             /*  6 */ 'Region',
178             /*  7 */ '',
179             /*  8 */ 'County',
180             /*  9 */ '',
181             /* 10 */ 'City',
182             /* 11 */ '',
183             /* 12 */ 'Town / Village',
184             /* 13 */ '',
185             /* 14 */ 'Suburb',
186             /* 15 */ '',
187             /* 16 */ 'Street',
188             /* 17 */ '',
189             /* 18 */ 'Building',
190             /* 19 */ '',
191             /* 20 */ '',
192             /* 21 */ ''
193         ];
194
195         var select = $('<select>');
196         var option = jQuery('<option>', { value: '', text: '--'});
197         if (typeof(iSelectedZoom) === 'undefined') option.attr('selected', 'selected');
198         option.appendTo(select);
199
200         jQuery.each(aZoomLevels, function(i, title) {
201             var option = jQuery('<option>', { value: i, text: i + ' ' + title });
202             if (i == iSelectedZoom) option.attr('selected', 'selected');
203             option.appendTo(select);
204         });
205         return new Handlebars.SafeString(select.html());
206     }
207 });