]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/handlebar_helpers.js
better setting of initial zoom on reverse search
[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="//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="//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.wikipedia) return '';
33
34         var parts = aPlace.wikipedia.split(':', 2);
35
36         var sTitle = Handlebars.escapeExpression(aPlace.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="//www.openstreetmap.org/relation/12345">relation 12345</a
46     detailsLink: function(aFeature, sTitle) {
47         if (!aFeature) return '';
48         if (!aFeature.place_id) return '';
49
50         sTitle = 'details >';
51         var sTitle = Handlebars.escapeExpression(sTitle);
52
53         return new Handlebars.SafeString(
54             '<a href="details.html?place_id=' + aFeature.place_id + '">' + (sTitle ? sTitle : aFeature.place_id ) + '</a>'
55         );
56     },
57     coverageType: function(aPlace) {
58         return (aPlace.isarea === 't' ? 'Polygon' : 'Point');
59     },
60     // fDistance is in meters
61     formatDistance: function(fDistanceMeters) {
62         if (fDistanceMeters < 1) return '0';
63
64         var formatted = (fDistanceMeters >= 1000) ?
65             Math.round(fDistanceMeters/1000, 1) + ' km' :
66             Math.round(fDistanceMeters, 0) + ' m';
67
68         return new Handlebars.SafeString(
69             '<abbr class="distance" title="' + fDistanceMeters + '">~' + formatted + '</abbr>'
70         );
71     },
72     // mark partial tokens (those starting with a space) with a star for readability
73     formatKeywordToken: function(sToken) {
74         return (sToken[0] == ' ' ? '*' : '') + Handlebars.escapeExpression(sToken);
75     },
76     // Any over 15 are invalid data in OSM anyway
77     formatAdminLevel: function(iLevel) {
78         return (iLevel < 15 ? iLevel : '');
79     },
80     formatMapIcon: function(sIcon) {
81         if (!sIcon) return;
82         
83         var url = sIcon.match(/png$/) ? Nominatim_Config.Images_Base_Url + '/' + sIcon : Nominatim_Config.Images_Base_Url + 'nominatim/images/mapicons/' + sIcon + '.n.32.png';
84
85         return new Handlebars.SafeString(
86             '<img class="mapicon" src="' + url + '" alt="' + sIcon + '"/>'
87         );
88     },
89     formatLabel: function(aPlace) {
90         if (aPlace.label) return aPlace.label;
91
92         function capitalize(s)
93         {
94             return s && s[0].toUpperCase() + s.slice(1);
95         }
96
97         if (aPlace.type && aPlace.type === 'yes') {
98             return capitalize(aPlace.class.replace(/_/g, ' '));
99         } else {
100             return capitalize(aPlace.type.replace(/_/g, ' '));
101         }
102     },
103     zoomLevels: function(iSelectedZoom) {
104         var aZoomLevels = [
105             /*  0 */ 'Continent / Sea',
106             /*  1 */ '',
107             /*  2 */ '',
108             /*  3 */ 'Country',
109             /*  4 */ '',
110             /*  5 */ 'State',
111             /*  6 */ 'Region',
112             /*  7 */ '',
113             /*  8 */ 'County',
114             /*  9 */ '',
115             /* 10 */ 'City',
116             /* 11 */ '',
117             /* 12 */ 'Town / Village',
118             /* 13 */ '',
119             /* 14 */ 'Suburb',
120             /* 15 */ '',
121             /* 16 */ 'Street',
122             /* 17 */ '',
123             /* 18 */ 'Building',
124             /* 19 */ '',
125             /* 20 */ '',
126             /* 21 */ ''
127         ];
128
129         var select = $('<select>');
130         var option = jQuery('<option>', { value: '', text: '--'});
131         if (typeof(iSelectedZoom) === 'undefined') option.attr('selected', 'selected');
132         option.appendTo(select);
133
134         jQuery.each(aZoomLevels, function(i, title) {
135             var option = jQuery('<option>', { value: i, text: i + ' ' + title });
136             if (i == iSelectedZoom) option.attr('selected', 'selected');
137             option.appendTo(select);
138         });
139         return new Handlebars.SafeString(select.html());
140     }
141 });