]> git.openstreetmap.org Git - nominatim-ui.git/blob - dist/handlebar_helpers.js
add dist/ directory for first time
[nominatim-ui.git] / dist / 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   isaddresses_unused: function (aAddressLine) {
16     return ((aAddressLine.isaddress && aAddressLine.isaddress === 'f') ? 'notused' : '');
17   },
18   // { osm_type: 'R', osm_id: 12345 }
19   // <a href="https://www.openstreetmap.org/relation/12345">relation 12345</a
20   osmLink: function (aPlace) {
21     if (!aPlace.osm_type) return '';
22     var sOSMType = formatOSMType(aPlace.osm_type, false);
23     if (!sOSMType) return '';
24
25     return new Handlebars.SafeString(
26       '<a href="https://www.openstreetmap.org/' + sOSMType + '/' + aPlace.osm_id + '">' + sOSMType + ' ' + aPlace.osm_id + '</a>'
27     );
28   },
29   /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */
30   wikipediaLink: function (aPlace) {
31     if (!aPlace.calculated_wikipedia) return '';
32
33     var parts = aPlace.calculated_wikipedia.split(':', 2);
34
35     var sTitle = Handlebars.escapeExpression(aPlace.calculated_wikipedia);
36     var sLanguage = Handlebars.escapeExpression(parts[0]);
37     var sArticle = Handlebars.escapeExpression(parts[1]);
38
39     return new Handlebars.SafeString(
40       '<a href="https://' + sLanguage + '.wikipedia.org/wiki/' + sArticle + '" target="_blank">' + sTitle + '</a>'
41     );
42   },
43   // { osm_type: 'R', osm_id: 12345 }
44   // <a href="details.html?place_id=12345">details</a>
45   detailsLink: function (aFeature, sTitle) {
46     if (!aFeature) return '';
47     if (!aFeature.place_id) return '';
48
49     var sTitleEscaped = Handlebars.escapeExpression(sTitle || 'details >');
50
51     return new Handlebars.SafeString(
52       '<a href="details.html?place_id=' + aFeature.place_id + '">' + sTitleEscaped + '</a>'
53     );
54   },
55   detailsPermaLink: function (aFeature, sTitle) {
56     if (!aFeature) return '';
57
58     var sOSMType = formatOSMType(aFeature.osm_type, false);
59     if (!sOSMType) return '';
60
61     var sTitleEscaped = Handlebars.escapeExpression(sTitle || sOSMType + ' ' + aFeature.osm_id);
62
63     return new Handlebars.SafeString(
64       '<a href="details.html?osmtype=' + aFeature.osm_type + '&osmid=' + aFeature.osm_id + '&class=' + aFeature.category + '">' + sTitleEscaped + '</a>'
65     );
66   },
67   coverageType: function (aPlace) {
68     return (aPlace.isarea ? 'Polygon' : 'Point');
69   },
70   // fDistance is in meters
71   formatDistance: function (fDistanceMeters) {
72     if (fDistanceMeters < 1) return '0';
73
74     var formatted = (fDistanceMeters >= 1000)
75       ? Math.round(fDistanceMeters / 1000, 1) + ' km'
76       : Math.round(fDistanceMeters, 0) + ' m';
77
78     return new Handlebars.SafeString(
79       '<abbr class="distance" title="' + fDistanceMeters + '">~' + formatted + '</abbr>'
80     );
81   },
82   // mark partial tokens (those starting with a space) with a star for readability
83   formatKeywordToken: function (sToken) {
84     return (sToken[0] === ' ' ? '*' : '') + Handlebars.escapeExpression(sToken);
85   },
86   // Any over 15 are invalid data in OSM anyway
87   formatAdminLevel: function (iLevel) {
88     return (iLevel < 15 ? iLevel : '');
89   },
90   formatMapIcon: function (sIcon) {
91     if (!sIcon) return;
92
93     var url = sIcon;
94     if (!url.match(/^http/)) url = get_config_value('Images_Base_Url') + url;
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       return s && s[0].toUpperCase() + s.slice(1);
105     }
106
107     if (aPlace.type && aPlace.type === 'yes') {
108       return capitalize(aPlace.class.replace(/_/g, ' '));
109     }
110     return capitalize(aPlace.type.replace(/_/g, ' '));
111   },
112   formatSearchRank: function (iRank) {
113     // same as
114     // https://github.com/openstreetmap/Nominatim/blob/master/sql/functions.sql
115     // get_searchrank_label()
116
117     if (iRank < 2) return 'continent';
118     if (iRank < 4) return 'sea';
119     if (iRank < 8) return 'country';
120     if (iRank < 12) return 'state';
121     if (iRank < 16) return 'county';
122     if (iRank === 16) return 'city';
123     if (iRank === 17) return 'town / island';
124     if (iRank === 18) return 'village / hamlet';
125     if (iRank === 20) return 'suburb';
126     if (iRank === 21) return 'postcode area';
127     if (iRank === 22) return 'croft / farm / locality / islet';
128     if (iRank === 23) return 'postcode area';
129     if (iRank === 25) return 'postcode point';
130     if (iRank === 26) return 'street / major landmark';
131     if (iRank === 27) return 'minory street / path';
132     if (iRank === 28) return 'house / building';
133     return 'other: ' + iRank;
134   },
135   tooManyHierarchyLinesWarning: function (aPlace) {
136     if (!aPlace.hierarchy) return;
137
138     var c = 0;
139     for (var type in aPlace.hierarchy) {
140       c = c + type.length + 1;
141     }
142     if (c < 500) return;
143
144     return new Handlebars.SafeString(
145       '<p>There are more child objects which are not shown.</p>'
146     );
147   },
148   zoomLevels: function (iSelectedZoom) {
149     var aZoomLevels = [
150       /*  0 */ 'Continent / Sea',
151       /*  1 */ '',
152       /*  2 */ '',
153       /*  3 */ 'Country',
154       /*  4 */ '',
155       /*  5 */ 'State',
156       /*  6 */ 'Region',
157       /*  7 */ '',
158       /*  8 */ 'County',
159       /*  9 */ '',
160       /* 10 */ 'City',
161       /* 11 */ '',
162       /* 12 */ 'Town / Village',
163       /* 13 */ '',
164       /* 14 */ 'Suburb',
165       /* 15 */ '',
166       /* 16 */ 'Street',
167       /* 17 */ '',
168       /* 18 */ 'Building',
169       /* 19 */ '',
170       /* 20 */ '',
171       /* 21 */ ''
172     ];
173
174     var select = $('<select>');
175     var option = jQuery('<option>', { value: '', text: '--' });
176     if (typeof (iSelectedZoom) === 'undefined') option.attr('selected', 'selected');
177     option.appendTo(select);
178
179     jQuery.each(aZoomLevels, function (i, title) {
180       option = jQuery('<option>', { value: i, text: i + ' ' + title });
181       if (i === iSelectedZoom) option.attr('selected', 'selected');
182       option.appendTo(select);
183     });
184     return new Handlebars.SafeString(select.html());
185   }
186 });