]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/base.js
90a358857d8246458d7c29364a1ec11afac94444
[nominatim-ui.git] / src / assets / js / base.js
1 'use strict';
2
3 var map;
4 var last_click_latlng;
5
6
7 // *********************************************************
8 // HELPERS
9 // *********************************************************
10
11 function get_config_value(str, default_val) {
12   return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] : default_val);
13 }
14
15 function parse_and_normalize_geojson_string(part) {
16   // normalize places the geometry into a featurecollection, similar to
17   // https://github.com/mapbox/geojson-normalize
18   var parsed_geojson = {
19     type: 'FeatureCollection',
20     features: [
21       {
22         type: 'Feature',
23         geometry: part,
24         properties: {}
25       }
26     ]
27   };
28   return parsed_geojson;
29 }
30
31 function map_link_to_osm() {
32   var zoom = map.getZoom();
33   var lat = map.getCenter().lat;
34   var lng = map.getCenter().lng;
35   return 'https://openstreetmap.org/#map=' + zoom + '/' + lat + '/' + lng;
36 }
37
38 function map_viewbox_as_string() {
39   var bounds = map.getBounds();
40   var west = bounds.getWest();
41   var east = bounds.getEast();
42
43   if ((east - west) >= 360) { // covers more than whole planet
44     west = map.getCenter().lng - 179.999;
45     east = map.getCenter().lng + 179.999;
46   }
47   east = L.latLng(77, east).wrap().lng;
48   west = L.latLng(77, west).wrap().lng;
49
50   return [
51     west.toFixed(5), // left
52     bounds.getNorth().toFixed(5), // top
53     east.toFixed(5), // right
54     bounds.getSouth().toFixed(5) // bottom
55   ].join(',');
56 }
57
58
59 // *********************************************************
60 // PAGE HELPERS
61 // *********************************************************
62
63 function fetch_from_api(endpoint_name, params, callback) {
64   // `&a=&b=&c=1` => '&c='
65   for (var k in params) {
66     if (typeof (params[k]) === 'undefined' || params[k] === '' || params[k] === null) {
67       delete params[k];
68     }
69   }
70
71   var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
72                   + $.param(params);
73   if (endpoint_name !== 'status') {
74     $('#api-request-link').attr('href', api_url);
75   }
76   $.get(api_url, function (data) {
77     callback(data);
78   });
79 }
80
81 function update_data_date() {
82   fetch_from_api('status', { format: 'json' }, function (data) {
83     $('#data-date').text(data.data_updated);
84   });
85 }
86
87 function render_template(el, template_name, page_context) {
88   var template_source = $('#' + template_name).text();
89   var template = Handlebars.compile(template_source);
90   var html = template(page_context);
91   el.html(html);
92 }
93
94 function update_html_title(title) {
95   var prefix = '';
96   if (title && title.length > 1) {
97     prefix = title + ' | ';
98   }
99   $('head title').text(prefix + 'OpenStreetMap Nominatim');
100 }
101
102 function show_error(html) {
103   $('#error-overlay').html(html).show();
104 }
105
106 function hide_error() {
107   $('#error-overlay').empty().hide();
108 }
109
110
111 $(document).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
112   // console.log(thrownError);
113   // console.log(ajaxSettings);
114   var url = ajaxSettings.url;
115   show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');
116 });
117
118
119 jQuery(document).ready(function () {
120   hide_error();
121 });