]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/base.js
fix remaining eslint warnings
[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=1'
65
66   var param_names = Object.keys(params);
67   for (var i = 0; i < param_names.length; i += 1) {
68     var val = param_names[keys[i]];
69     if (typeof (val) === 'undefined' || val === '' || val === null) {
70       delete param_names[keys[i]];
71     }
72   }
73
74   var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
75                   + $.param(params);
76   if (endpoint_name !== 'status') {
77     $('#api-request-link').attr('href', api_url);
78   }
79   $.get(api_url, function (data) {
80     callback(data);
81   });
82 }
83
84 function update_data_date() {
85   fetch_from_api('status', { format: 'json' }, function (data) {
86     $('#data-date').text(data.data_updated);
87   });
88 }
89
90 function render_template(el, template_name, page_context) {
91   var template_source = $('#' + template_name).text();
92   var template = Handlebars.compile(template_source);
93   var html = template(page_context);
94   el.html(html);
95 }
96
97 function update_html_title(title) {
98   var prefix = '';
99   if (title && title.length > 1) {
100     prefix = title + ' | ';
101   }
102   $('head title').text(prefix + 'OpenStreetMap Nominatim');
103 }
104
105 function show_error(html) {
106   $('#error-overlay').html(html).show();
107 }
108
109 function hide_error() {
110   $('#error-overlay').empty().hide();
111 }
112
113
114 $(document).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
115   // console.log(thrownError);
116   // console.log(ajaxSettings);
117   var url = ajaxSettings.url;
118   show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');
119 });
120
121
122 jQuery(document).ready(function () {
123   hide_error();
124 });