]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/base.js
javascript: indent 2 space, not 4 space
[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   var bounds = map.getBounds();
35   var west = bounds.getWest();
36   var east = bounds.getEast();
37
38   if ((east - west) >= 360) { // covers more than whole planet
39     west = map.getCenter().lng-179.999;
40     east = map.getCenter().lng+179.999;
41   }
42   east = L.latLng(77, east).wrap().lng;
43   west = L.latLng(77, west).wrap().lng;
44
45   return [
46     west.toFixed(5), // left
47     bounds.getNorth().toFixed(5), // top
48     east.toFixed(5), // right
49     bounds.getSouth().toFixed(5) // bottom
50   ].join(',');
51 }
52
53
54 /*********************************************************
55 * PAGE HELPERS
56 *********************************************************/
57
58 function fetch_from_api(endpoint_name, params, callback) {
59
60   // `&a=&b=&c=1` => '&c='
61   for(var k in params) {
62     if (typeof(params[k]) === 'undefined' || params[k] === '' || params[k] === null ) delete params[k];
63   }
64
65   var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + $.param(params);
66   if (endpoint_name !== 'status') {
67     $('#api-request-link').attr('href', api_url);
68   }
69   $.get(api_url, function(data){
70     callback(data);
71   });
72 }
73
74 function update_data_date() {
75   fetch_from_api('status', {format: 'json'}, function(data){
76     $('#data-date').text(data.data_updated);
77   });
78 }
79
80 function render_template(el, template_name, page_context) {
81   var template_source = $('#' + template_name).text();
82   var template = Handlebars.compile(template_source);
83   var html    = template(page_context);
84   el.html(html);
85 }
86
87 function show_error(html) {
88   $('#error-overlay').html(html).show();   
89 }
90
91 function hide_error() {
92   $('#error-overlay').empty().hide();    
93 }
94
95
96 $(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
97   // console.log(thrownError);
98   // console.log(ajaxSettings);
99   show_error('Error fetching results from <a href="' + ajaxSettings.url + '">' + ajaxSettings.url + '</a>');
100 });
101
102
103 jQuery(document).ready(function(){
104   hide_error();
105 });
106