]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/handlebar_helpers.js
detail page: add permalink, equivalent to https://github.com/openstreetmap/Nominatim...
[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.class + '">' + 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     tooManyHierarchyLinesWarning: function(aPlace) {
115         if (!aPlace.hierarchy) return;
116
117         var c = 0;
118         for (var type in aPlace.hierarchy) {
119             c = c + type.length+1;
120         }
121         if (c < 500) return;
122
123         return new Handlebars.SafeString(
124             '<p>There are more child objects which are not shown.</p>'
125         );
126     },
127     zoomLevels: function(iSelectedZoom) {
128         var aZoomLevels = [
129             /*  0 */ 'Continent / Sea',
130             /*  1 */ '',
131             /*  2 */ '',
132             /*  3 */ 'Country',
133             /*  4 */ '',
134             /*  5 */ 'State',
135             /*  6 */ 'Region',
136             /*  7 */ '',
137             /*  8 */ 'County',
138             /*  9 */ '',
139             /* 10 */ 'City',
140             /* 11 */ '',
141             /* 12 */ 'Town / Village',
142             /* 13 */ '',
143             /* 14 */ 'Suburb',
144             /* 15 */ '',
145             /* 16 */ 'Street',
146             /* 17 */ '',
147             /* 18 */ 'Building',
148             /* 19 */ '',
149             /* 20 */ '',
150             /* 21 */ ''
151         ];
152
153         var select = $('<select>');
154         var option = jQuery('<option>', { value: '', text: '--'});
155         if (typeof(iSelectedZoom) === 'undefined') option.attr('selected', 'selected');
156         option.appendTo(select);
157
158         jQuery.each(aZoomLevels, function(i, title) {
159             var option = jQuery('<option>', { value: i, text: i + ' ' + title });
160             if (i == iSelectedZoom) option.attr('selected', 'selected');
161             option.appendTo(select);
162         });
163         return new Handlebars.SafeString(select.html());
164     }
165 });