X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/3d5062bc458e674828760ba0fe07100e3bf69e9b..a60fd032341039346f6804700b36e451d0e47e04:/src/handlebar_helpers.js?ds=sidebyside diff --git a/src/handlebar_helpers.js b/src/handlebar_helpers.js index 7b69d63..4d1889f 100644 --- a/src/handlebar_helpers.js +++ b/src/handlebar_helpers.js @@ -1,24 +1,128 @@ -function formatOSMType(sType, bExcludeExternal) -{ - if (sType == 'N') return 'node'; - if (sType == 'W') return 'way'; - if (sType == 'R') return 'relation'; +'use strict'; + +function formatOSMType(sType, bExcludeExternal) { + if (sType === 'N') return 'node'; + if (sType === 'W') return 'way'; + if (sType === 'R') return 'relation'; if (!bExcludeExternal) return ''; - if (sType == 'T') return 'way'; - if (sType == 'I') return 'way'; + if (sType === 'T') return 'way'; + if (sType === 'I') return 'way'; return ''; } +function getIcon(aPlace) { + // equivalent to PHP Nominatim::ClassTypes::getIcon + // covers 83 of 214 available icon filenames, e.g. transport_roundabout_anticlockwise + // transport_rental_bicycle or place_of_worship_christian would need more data from + // the place. + var aIcons = { + 'boundary:administrative': 'poi_boundary_administrative', + 'place:city': 'poi_place_city', + 'place:town': 'poi_place_town', + 'place:village': 'poi_place_village', + 'place:hamlet': 'poi_place_village', + 'place:suburb': 'poi_place_village', + 'place:locality': 'poi_place_village', + 'place:airport': 'transport_airport2', + 'aeroway:aerodrome': 'transport_airport2', + 'railway:station': 'transport_train_station2', + 'amenity:place_of_worship': 'place_of_worship_unknown3', + 'amenity:pub': 'food_pub', + 'amenity:bar': 'food_bar', + 'amenity:university': 'education_university', + 'tourism:museum': 'tourist_museum', + 'amenity:arts_centre': 'tourist_art_gallery2', + 'tourism:zoo': 'tourist_zoo', + 'tourism:theme_park': 'poi_point_of_interest', + 'tourism:attraction': 'poi_point_of_interest', + 'leisure:golf_course': 'sport_golf', + 'historic:castle': 'tourist_castle', + 'amenity:hospital': 'health_hospital', + 'amenity:school': 'education_school', + 'amenity:theatre': 'tourist_theatre', + 'amenity:library': 'amenity_library', + 'amenity:fire_station': 'amenity_firestation3', + 'amenity:police': 'amenity_police2', + 'amenity:bank': 'money_bank2', + 'amenity:post_office': 'amenity_post_office', + 'tourism:hotel': 'accommodation_hotel2', + 'amenity:cinema': 'tourist_cinema', + 'tourism:artwork': 'tourist_art_gallery2', + 'historic:archaeological_site': 'tourist_archaeological2', + 'amenity:doctors': 'health_doctors', + 'leisure:sports_centre': 'sport_leisure_centre', + 'leisure:swimming_pool': 'sport_swimming_outdoor', + 'shop:supermarket': 'shopping_supermarket', + 'shop:convenience': 'shopping_convenience', + 'amenity:restaurant': 'food_restaurant', + 'amenity:fast_food': 'food_fastfood', + 'amenity:cafe': 'food_cafe', + 'tourism:guest_house': 'accommodation_bed_and_breakfast', + 'amenity:pharmacy': 'health_pharmacy_dispensing', + 'amenity:fuel': 'transport_fuel', + 'natural:peak': 'poi_peak', + 'natural:wood': 'landuse_coniferous_and_deciduous', + 'shop:bicycle': 'shopping_bicycle', + 'shop:clothes': 'shopping_clothes', + 'shop:hairdresser': 'shopping_hairdresser', + 'shop:doityourself': 'shopping_diy', + 'shop:estate_agent': 'shopping_estateagent2', + 'shop:car': 'shopping_car', + 'shop:garden_centre': 'shopping_garden_centre', + 'shop:car_repair': 'shopping_car_repair', + 'shop:bakery': 'shopping_bakery', + 'shop:butcher': 'shopping_butcher', + 'shop:apparel': 'shopping_clothes', + 'shop:laundry': 'shopping_laundrette', + 'shop:beverages': 'shopping_alcohol', + 'shop:alcohol': 'shopping_alcohol', + 'shop:optician': 'health_opticians', + 'shop:chemist': 'health_pharmacy', + 'shop:gallery': 'tourist_art_gallery2', + 'shop:jewelry': 'shopping_jewelry', + 'tourism:information': 'amenity_information', + 'historic:ruins': 'tourist_ruin', + 'amenity:college': 'education_school', + 'historic:monument': 'tourist_monument', + 'historic:memorial': 'tourist_monument', + 'historic:mine': 'poi_mine', + 'tourism:caravan_site': 'accommodation_caravan_park', + 'amenity:bus_station': 'transport_bus_station', + 'amenity:atm': 'money_atm2', + 'tourism:viewpoint': 'tourist_view_point', + 'tourism:guesthouse': 'accommodation_bed_and_breakfast', + 'railway:tram': 'transport_tram_stop', + 'amenity:courthouse': 'amenity_court', + 'amenity:recycling': 'amenity_recycling', + 'amenity:dentist': 'health_dentist', + 'natural:beach': 'tourist_beach', + 'railway:tram_stop': 'transport_tram_stop', + 'amenity:prison': 'amenity_prison', + 'highway:bus_stop': 'transport_bus_stop2' + }; + + var sCategoryPlace = aPlace.category + ':' + aPlace.type; + + return aIcons[sCategoryPlace]; +} + + Handlebars.registerHelper({ - isaddresses_unused: function(aAddressLine) { - return ((aAddressLine.isaddress && aAddressLine.isaddress == 'f') ? 'notused' : ''); + formatOSMType: function (sType, bExcludeExternal) { + return formatOSMType(sType, bExcludeExternal); + }, + shortOSMType: function (sType) { + if (sType === 'node') return 'N'; + if (sType === 'way') return 'W'; + if (sType === 'relation') return 'R'; + return ''; }, // { osm_type: 'R', osm_id: 12345 } // relation 12345 https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */ - wikipediaLink: function(aPlace) { - if (! aPlace.calculated_wikipedia) return ''; + wikipediaLink: function (aPlace) { + if (!aPlace.calculated_wikipedia) return ''; var parts = aPlace.calculated_wikipedia.split(':', 2); - var sTitle = Handlebars.escapeExpression(aPlace.calculated_wikipedia), - sLanguage = Handlebars.escapeExpression(parts[0]), - sArticle = Handlebars.escapeExpression(parts[1]); + var sTitle = Handlebars.escapeExpression(aPlace.calculated_wikipedia); + var sLanguage = Handlebars.escapeExpression(parts[0]); + var sArticle = Handlebars.escapeExpression(parts[1]); return new Handlebars.SafeString( '' + sTitle + '' @@ -43,130 +147,124 @@ Handlebars.registerHelper({ }, // { osm_type: 'R', osm_id: 12345 } // details - detailsLink: function(aFeature, sTitle) { + detailsLink: function (aFeature, sTitle) { if (!aFeature) return ''; if (!aFeature.place_id) return ''; - sTitle = Handlebars.escapeExpression(sTitle || 'details >'); + var sTitleEscaped = Handlebars.escapeExpression(sTitle || 'details >'); return new Handlebars.SafeString( - '' + sTitle + '' + '' + sTitleEscaped + '' ); }, - detailsPermaLink: function(aFeature, sTitle) { + detailsPermaLink: function (aFeature, sTitle) { if (!aFeature) return ''; var sOSMType = formatOSMType(aFeature.osm_type, false); if (!sOSMType) return ''; - sTitle = Handlebars.escapeExpression(sTitle || sOSMType + ' ' + aFeature.osm_id); + var sTitleEscaped = Handlebars.escapeExpression(sTitle || sOSMType + ' ' + aFeature.osm_id); + + var sURL = 'details.html?osmtype=' + aFeature.osm_type + '&osmid=' + aFeature.osm_id; + if (aFeature.category) { + sURL = sURL + '&class=' + aFeature.category; + } return new Handlebars.SafeString( - '' + sTitle + '' + '' + sTitleEscaped + '' ); }, - coverageType: function(aPlace) { + formatPlaceType: function (aPlace) { + var sOut = aPlace.class + ':' + aPlace.type; + if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) { + sOut = sOut + ' (' + aPlace.place_type + ')'; + } + return new Handlebars.SafeString(sOut); + }, + coverageType: function (aPlace) { return (aPlace.isarea ? 'Polygon' : 'Point'); }, // fDistance is in meters - formatDistance: function(fDistanceMeters) { + formatDistance: function (fDistanceMeters) { if (fDistanceMeters < 1) return '0'; - var formatted = (fDistanceMeters >= 1000) ? - Math.round(fDistanceMeters/1000, 1) + ' km' : - Math.round(fDistanceMeters, 0) + ' m'; + var formatted = (fDistanceMeters >= 1000) + ? Math.round(fDistanceMeters / 1000, 1) + ' km' + : Math.round(fDistanceMeters, 0) + ' m'; return new Handlebars.SafeString( '~' + formatted + '' ); }, // mark partial tokens (those starting with a space) with a star for readability - formatKeywordToken: function(sToken) { - return (sToken[0] == ' ' ? '*' : '') + Handlebars.escapeExpression(sToken); + formatKeywordToken: function (sToken) { + return (sToken[0] === ' ' ? '*' : '') + Handlebars.escapeExpression(sToken); }, // Any over 15 are invalid data in OSM anyway - formatAdminLevel: function(iLevel) { + formatAdminLevel: function (iLevel) { return (iLevel < 15 ? iLevel : ''); }, - formatMapIcon: function(sIcon) { - if (!sIcon) return; - - var url = sIcon; - if (!url.match(/^http/)) url = get_config_value('Images_Base_Url') + url; + formatMapIcon: function (aPlace) { + var sIcon = getIcon(aPlace); + + if (!sIcon) return ''; + + var title = 'icon for ' + aPlace.category + ' ' + aPlace.type; + var url = get_config_value('Images_Base_Url') + sIcon + '.p.20.png'; return new Handlebars.SafeString( - '' + sIcon + '' + '' + title + '' ); }, - formatLabel: function(aPlace) { + formatLabel: function (aPlace) { if (aPlace.label) return aPlace.label; - function capitalize(s) - { + function capitalize(s) { return s && s[0].toUpperCase() + s.slice(1); } - if (aPlace.type && aPlace.type === 'yes') { + if (aPlace.type && aPlace.type === 'yes' && aPlace.class) { return capitalize(aPlace.class.replace(/_/g, ' ')); - } else { + } + if (aPlace.type) { return capitalize(aPlace.type.replace(/_/g, ' ')); } + return ''; }, - formatSearchRank: function(iRank) { + formatSearchRank: function (iRank) { // same as - // https://github.com/openstreetmap/Nominatim/blob/master/sql/functions.sql + // https://github.com/osm-search/Nominatim/blob/master/sql/functions.sql // get_searchrank_label() - if (iRank < 2) { - return 'continent'; - } else if (iRank < 4) { - return 'sea'; - } else if (iRank < 8) { - return 'country'; - } else if (iRank < 12) { - return 'state'; - } else if (iRank < 16) { - return 'county'; - } else if (iRank == 16) { - return 'city'; - } else if (iRank == 17) { - return 'town / island'; - } else if (iRank == 18) { - return 'village / hamlet'; - } else if (iRank == 20) { - return 'suburb'; - } else if (iRank == 21) { - return 'postcode area'; - } else if (iRank == 22) { - return 'croft / farm / locality / islet'; - } else if (iRank == 23) { - return 'postcode area'; - } else if (iRank == 25) { - return 'postcode point'; - } else if (iRank == 26) { - return 'street / major landmark'; - } else if (iRank == 27) { - return 'minory street / path'; - } else if (iRank == 28) { - return 'house / building'; - } else { - return 'other: ' + iRank; - } + if (iRank < 2) return 'continent'; + if (iRank < 4) return 'sea'; + if (iRank < 8) return 'country'; + if (iRank < 12) return 'state'; + if (iRank < 16) return 'county'; + if (iRank === 16) return 'city'; + if (iRank === 17) return 'town / island'; + if (iRank === 18) return 'village / hamlet'; + if (iRank === 20) return 'suburb'; + if (iRank === 21) return 'postcode area'; + if (iRank === 22) return 'croft / farm / locality / islet'; + if (iRank === 23) return 'postcode area'; + if (iRank === 25) return 'postcode point'; + if (iRank === 26) return 'street / major landmark'; + if (iRank === 27) return 'minory street / path'; + if (iRank === 28) return 'house / building'; + return 'other: ' + iRank; }, - tooManyHierarchyLinesWarning: function(aPlace) { - if (!aPlace.hierarchy) return; + tooManyHierarchyLinesWarning: function (aPlace) { + if (!aPlace.hierarchy) return ''; - var c = 0; - for (var type in aPlace.hierarchy) { - c = c + type.length+1; - } - if (c < 500) return; + var c = Object.keys(aPlace.hierarchy); + if (c < 500) return ''; return new Handlebars.SafeString( '

There are more child objects which are not shown.

' ); }, - zoomLevels: function(iSelectedZoom) { + zoomLevels: function (iSelectedZoom) { var aZoomLevels = [ /* 0 */ 'Continent / Sea', /* 1 */ '', @@ -193,13 +291,15 @@ Handlebars.registerHelper({ ]; var select = $('