]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/base.js
/reverse.html should be reverse.html to app works in subdirectory
[nominatim-ui.git] / src / assets / js / base.js
1 'use strict';
2
3 var map;
4 var last_click_latlng;
5
6 // *********************************************************
7 // DEFAULTS
8 // *********************************************************
9
10 var Nominatim_Config_Defaults = {
11   Nominatim_API_Endpoint: 'http://localhost/nominatim/',
12   Images_Base_Url: '/mapicons/',
13   Search_AreaPolygons: 1,
14   Reverse_Default_Search_Zoom: 18,
15   Map_Default_Lat: 20.0,
16   Map_Default_Lon: 0.0,
17   Map_Default_Zoom: 2,
18   Map_Tile_URL: 'https://{s}.tile.osm.org/{z}/{x}/{y}.png',
19   Map_Tile_Attribution: '<a href="https://osm.org/copyright">OpenStreetMap contributors</a>'
20 };
21
22 // *********************************************************
23 // HELPERS
24 // *********************************************************
25
26
27 function get_config_value(str, default_val) {
28   var value = ((typeof Nominatim_Config !== 'undefined')
29                && (typeof Nominatim_Config[str] !== 'undefined'))
30     ? Nominatim_Config[str]
31     : Nominatim_Config_Defaults[str];
32   return (typeof value !== 'undefined' ? value : default_val);
33 }
34
35 function parse_and_normalize_geojson_string(part) {
36   // normalize places the geometry into a featurecollection, similar to
37   // https://github.com/mapbox/geojson-normalize
38   var parsed_geojson = {
39     type: 'FeatureCollection',
40     features: [
41       {
42         type: 'Feature',
43         geometry: part,
44         properties: {}
45       }
46     ]
47   };
48   return parsed_geojson;
49 }
50
51 function map_link_to_osm() {
52   var zoom = map.getZoom();
53   var lat = map.getCenter().lat;
54   var lng = map.getCenter().lng;
55   return 'https://openstreetmap.org/#map=' + zoom + '/' + lat + '/' + lng;
56 }
57
58 function map_viewbox_as_string() {
59   var bounds = map.getBounds();
60   var west = bounds.getWest();
61   var east = bounds.getEast();
62
63   if ((east - west) >= 360) { // covers more than whole planet
64     west = map.getCenter().lng - 179.999;
65     east = map.getCenter().lng + 179.999;
66   }
67   east = L.latLng(77, east).wrap().lng;
68   west = L.latLng(77, west).wrap().lng;
69
70   return [
71     west.toFixed(5), // left
72     bounds.getNorth().toFixed(5), // top
73     east.toFixed(5), // right
74     bounds.getSouth().toFixed(5) // bottom
75   ].join(',');
76 }
77
78
79 // *********************************************************
80 // PAGE HELPERS
81 // *********************************************************
82
83 function generate_full_api_url(endpoint_name, params) {
84   //
85   // `&a=&b=&c=1` => '&c=1'
86   var param_names = Object.keys(params);
87   for (var i = 0; i < param_names.length; i += 1) {
88     var val = params[param_names[i]];
89     if (typeof (val) === 'undefined' || val === '' || val === null) {
90       delete params[param_names[i]];
91     }
92   }
93
94   var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
95                   + $.param(params);
96   return api_url;
97 }
98
99 function update_last_updated(endpoint_name, params) {
100   if (endpoint_name === 'status') return;
101
102   var api_url = generate_full_api_url(endpoint_name, params);
103   $('#last-updated').show();
104
105   $('#api-request a').attr('href', api_url);
106   $('#api-request').show();
107
108   if (endpoint_name === 'search' || endpoint_name === 'reverse') {
109     $('#api-request-debug a').attr('href', api_url + '&debug=1');
110     $('#api-request-debug').show();
111   } else {
112     $('#api-request-debug').hide();
113   }
114 }
115
116 function fetch_from_api(endpoint_name, params, callback) {
117   var api_url = generate_full_api_url(endpoint_name, params);
118   $.get(api_url, function (data) {
119     if (endpoint_name !== 'status') {
120       update_last_updated(endpoint_name, params);
121     }
122     callback(data);
123   });
124 }
125
126 function update_data_date() {
127   fetch_from_api('status', { format: 'json' }, function (data) {
128     $('#last-updated').show();
129     $('#data-date').text(data.data_updated);
130   });
131 }
132
133 function render_template(el, template_name, page_context) {
134   var template_source = $('#' + template_name).text();
135   var template = Handlebars.compile(template_source);
136   var html = template(page_context);
137   el.html(html);
138 }
139
140 function update_html_title(title) {
141   var prefix = '';
142   if (title && title.length > 1) {
143     prefix = title + ' | ';
144   }
145   $('head title').text(prefix + 'OpenStreetMap Nominatim');
146 }
147
148 function show_error(html) {
149   $('#error-overlay').html(html).show();
150 }
151
152 function hide_error() {
153   $('#error-overlay').empty().hide();
154 }
155
156
157 jQuery(document).ready(function () {
158   hide_error();
159
160   $('#last-updated').hide();
161
162   $(document).ajaxStart(function () {
163     $('#loading').fadeIn('fast');
164   }).ajaxComplete(function () {
165     $('#loading').fadeOut('fast');
166   }).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
167     // console.log(thrownError);
168     // console.log(ajaxSettings);
169     var url = ajaxSettings.url;
170     show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');
171   });
172 });