X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/0224e5348603ae8927c61acca5f41620e9e66dd9..e0d087f4a2b5fbc758de432f8932de296279a82f:/src/assets/js/base.js diff --git a/src/assets/js/base.js b/src/assets/js/base.js index d72fc79..4ff6b23 100644 --- a/src/assets/js/base.js +++ b/src/assets/js/base.js @@ -1,90 +1,147 @@ +'use strict'; + var map; var last_click_latlng; +// ********************************************************* +// DEFAULTS +// ********************************************************* + +var Nominatim_Config_Defaults = { + Nominatim_API_Endpoint: 'http://localhost/nominatim/', + Images_Base_Url: '/mapicons/', + Search_AreaPolygons: 1, + Reverse_Default_Search_Zoom: 18, + Map_Default_Lat: 20.0, + Map_Default_Lon: 0.0, + Map_Default_Zoom: 2, + Map_Tile_URL: 'https://{s}.tile.osm.org/{z}/{x}/{y}.png', + Map_Tile_Attribution: 'OpenStreetMap contributors' +}; + +// ********************************************************* +// HELPERS +// ********************************************************* -/********************************************************* -* HELPERS -*********************************************************/ function get_config_value(str, default_val) { - return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] : default_val); + var value = ((typeof Nominatim_Config !== 'undefined') + && (typeof Nominatim_Config[str] !== 'undefined')) + ? Nominatim_Config[str] + : Nominatim_Config_Defaults[str]; + return (typeof value !== 'undefined' ? value : default_val); } -function parse_and_normalize_geojson_string(raw_string){ - // normalize places the geometry into a featurecollection, similar to - // https://github.com/mapbox/geojson-normalize - var parsed_geojson = { - type: "FeatureCollection", - features: [ - { - type: "Feature", - geometry: JSON.parse(raw_string), - properties: {} - } - ] - }; - return parsed_geojson; +function parse_and_normalize_geojson_string(part) { + // normalize places the geometry into a featurecollection, similar to + // https://github.com/mapbox/geojson-normalize + var parsed_geojson = { + type: 'FeatureCollection', + features: [ + { + type: 'Feature', + geometry: part, + properties: {} + } + ] + }; + return parsed_geojson; } -function map_link_to_osm(){ - return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng; +function map_link_to_osm() { + var zoom = map.getZoom(); + var lat = map.getCenter().lat; + var lng = map.getCenter().lng; + return 'https://openstreetmap.org/#map=' + zoom + '/' + lat + '/' + lng; } function map_viewbox_as_string() { - // since .toBBoxString() doesn't round numbers - return [ - map.getBounds().getSouthWest().lng.toFixed(5), // left - map.getBounds().getNorthEast().lat.toFixed(5), // top - map.getBounds().getNorthEast().lng.toFixed(5), // right - map.getBounds().getSouthWest().lat.toFixed(5) // bottom - ].join(','); + var bounds = map.getBounds(); + var west = bounds.getWest(); + var east = bounds.getEast(); + + if ((east - west) >= 360) { // covers more than whole planet + west = map.getCenter().lng - 179.999; + east = map.getCenter().lng + 179.999; + } + east = L.latLng(77, east).wrap().lng; + west = L.latLng(77, west).wrap().lng; + + return [ + west.toFixed(5), // left + bounds.getNorth().toFixed(5), // top + east.toFixed(5), // right + bounds.getSouth().toFixed(5) // bottom + ].join(','); } -/********************************************************* -* PAGE HELPERS -*********************************************************/ +// ********************************************************* +// PAGE HELPERS +// ********************************************************* function fetch_from_api(endpoint_name, params, callback) { - var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + $.param(params); - if (endpoint_name !== 'status') { - $('#api-request-link').attr('href', api_url); + // + // `&a=&b=&c=1` => '&c=1' + var param_names = Object.keys(params); + for (var i = 0; i < param_names.length; i += 1) { + var val = params[param_names[i]]; + if (typeof (val) === 'undefined' || val === '' || val === null) { + delete params[param_names[i]]; } - $.get(api_url, function(data){ - callback(data); - }); + } + + var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + + $.param(params); + if (endpoint_name !== 'status') { + $('#api-request-link').attr('href', api_url); + } + $.get(api_url, function (data) { + callback(data); + }); } function update_data_date() { - fetch_from_api('status', {format: 'json'}, function(data){ - $('#data-date').text(data.data_last_updated.formatted); - }); + fetch_from_api('status', { format: 'json' }, function (data) { + $('#data-date').text(data.data_updated); + }); } function render_template(el, template_name, page_context) { - var template_source = $('#' + template_name).text(); - var template = Handlebars.compile(template_source); - var html = template(page_context); - el.html(html); + var template_source = $('#' + template_name).text(); + var template = Handlebars.compile(template_source); + var html = template(page_context); + el.html(html); +} + +function update_html_title(title) { + var prefix = ''; + if (title && title.length > 1) { + prefix = title + ' | '; + } + $('head title').text(prefix + 'OpenStreetMap Nominatim'); } function show_error(html) { - $('#error-overlay').html(html).show(); + $('#error-overlay').html(html).show(); } function hide_error() { - $('#error-overlay').empty().hide(); + $('#error-overlay').empty().hide(); } -$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) { +jQuery(document).ready(function () { + hide_error(); + + $(document).ajaxStart(function () { + $('#loading').fadeIn('fast'); + }).ajaxComplete(function () { + $('#loading').fadeOut('fast'); + }).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) { // console.log(thrownError); // console.log(ajaxSettings); - show_error('Error fetching results from ' + ajaxSettings.url + ''); + var url = ajaxSettings.url; + show_error('Error fetching results from ' + url + ''); + }); }); - - -jQuery(document).ready(function(){ - hide_error(); -}); -