]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/base.js
48f8feb4839b23fe7b9a8f657b0e8f7f591e6b33
[nominatim-ui.git] / src / assets / js / base.js
1 var map;
2 var last_click_latlng;
3
4
5 /*********************************************************
6 * HELPERS
7 *********************************************************/
8
9 function get_config_value(str, default_val) {
10     return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] :  default_val);
11 }
12
13 function parse_and_normalize_geojson_string(part){
14     // normalize places the geometry into a featurecollection, similar to
15     // https://github.com/mapbox/geojson-normalize
16     var parsed_geojson = {
17         type: "FeatureCollection",
18         features: [
19             {
20                 type: "Feature",
21                 geometry: part,
22                 properties: {}
23             }
24         ]
25     };
26     return parsed_geojson;
27 }
28
29 function map_link_to_osm(){
30     return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
31 }
32
33 function map_viewbox_as_string() {
34     // since .toBBoxString() doesn't round numbers
35     return [
36         map.getBounds().getSouthWest().lng.toFixed(5), // left
37         map.getBounds().getNorthEast().lat.toFixed(5), // top
38         map.getBounds().getNorthEast().lng.toFixed(5), // right
39         map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
40     ].join(',');
41 }
42
43
44 /*********************************************************
45 * PAGE HELPERS
46 *********************************************************/
47
48 function fetch_from_api(endpoint_name, params, callback) {
49
50     // `&a=&b=&c=1` => '&c='
51     for(var k in params) {
52         if (typeof(params[k]) === 'undefined' || params[k] === '' || params[k] === null ) delete params[k];
53     }
54
55     var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + $.param(params);
56     if (endpoint_name !== 'status') {
57         $('#api-request-link').attr('href', api_url);
58     }
59     $.get(api_url, function(data){
60         callback(data);
61     });
62 }
63
64 function update_data_date() {
65     fetch_from_api('status', {format: 'json'}, function(data){
66         $('#data-date').text(data.data_updated);
67     });
68 }
69
70 function render_template(el, template_name, page_context) {
71     var template_source = $('#' + template_name).text();
72     var template = Handlebars.compile(template_source);
73     var html    = template(page_context);
74     el.html(html);
75 }
76
77 function show_error(html) {
78     $('#error-overlay').html(html).show();   
79 }
80
81 function hide_error() {
82     $('#error-overlay').empty().hide();    
83 }
84
85
86 $(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
87     // console.log(thrownError);
88     // console.log(ajaxSettings);
89     show_error('Error fetching results from <a href="' + ajaxSettings.url + '">' + ajaxSettings.url + '</a>');
90 });
91
92
93 jQuery(document).ready(function(){
94     hide_error();
95 });
96