]> git.openstreetmap.org Git - nominatim-ui.git/commitdiff
split javascript into several files
authorMarc Tobias Metten <mtmail@gmx.net>
Wed, 7 Mar 2018 23:45:29 +0000 (00:45 +0100)
committerMarc Tobias Metten <mtmail@gmx.net>
Wed, 7 Mar 2018 23:45:29 +0000 (00:45 +0100)
build.sh
src/assets/js/base.js [new file with mode: 0644]
src/assets/js/detailpage.js [new file with mode: 0644]
src/assets/js/searchpage.js [moved from src/assets/js/nominatim-ui.js with 67% similarity]

index 964a501d5a9d3b1ebe5af5a237bdb31e036fcbfd..a066b37a32abc9669bfbb1532ae6fa949fceb554 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -1,6 +1,9 @@
 #!/bin/bash
 
-rsync --quiet --recursive src/assets dist/
+rsync --quiet --recursive src/assets/css dist/css/
+rsync --quiet --recursive src/assets/images/ dist/images/
+cat src/assets/js/base.js src/assets/js/detailpage.js src/assets/js/searchpage.js > dist/assets/js/nominatim-ui.js
+
 rsync --quiet --recursive src/vendor/js/* dist/assets/js/
 rsync --quiet --recursive src/vendor/css/* dist/assets/css/
 # rsync --quiet --recursive src/vendor/images/* dist/assets/images/
diff --git a/src/assets/js/base.js b/src/assets/js/base.js
new file mode 100644 (file)
index 0000000..d72fc79
--- /dev/null
@@ -0,0 +1,90 @@
+var map;
+var last_click_latlng;
+
+
+/*********************************************************
+* HELPERS
+*********************************************************/
+
+function get_config_value(str, default_val) {
+    return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] :  default_val);
+}
+
+function parse_and_normalize_geojson_string(raw_string){
+    // normalize places the geometry into a featurecollection, similar to
+    // https://github.com/mapbox/geojson-normalize
+    var parsed_geojson = {
+        type: "FeatureCollection",
+        features: [
+            {
+                type: "Feature",
+                geometry: JSON.parse(raw_string),
+                properties: {}
+            }
+        ]
+    };
+    return parsed_geojson;
+}
+
+function map_link_to_osm(){
+    return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
+}
+
+function map_viewbox_as_string() {
+    // since .toBBoxString() doesn't round numbers
+    return [
+        map.getBounds().getSouthWest().lng.toFixed(5), // left
+        map.getBounds().getNorthEast().lat.toFixed(5), // top
+        map.getBounds().getNorthEast().lng.toFixed(5), // right
+        map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
+    ].join(',');
+}
+
+
+/*********************************************************
+* PAGE HELPERS
+*********************************************************/
+
+function fetch_from_api(endpoint_name, params, callback) {
+    var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + $.param(params);
+    if (endpoint_name !== 'status') {
+        $('#api-request-link').attr('href', api_url);
+    }
+    $.get(api_url, function(data){
+        callback(data);
+    });
+}
+
+function update_data_date() {
+    fetch_from_api('status', {format: 'json'}, function(data){
+        $('#data-date').text(data.data_last_updated.formatted);
+    });
+}
+
+function render_template(el, template_name, page_context) {
+    var template_source = $('#' + template_name).text();
+    var template = Handlebars.compile(template_source);
+    var html    = template(page_context);
+    el.html(html);
+}
+
+function show_error(html) {
+    $('#error-overlay').html(html).show();   
+}
+
+function hide_error() {
+    $('#error-overlay').empty().hide();    
+}
+
+
+$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
+    // console.log(thrownError);
+    // console.log(ajaxSettings);
+    show_error('Error fetching results from <a href="' + ajaxSettings.url + '">' + ajaxSettings.url + '</a>');
+});
+
+
+jQuery(document).ready(function(){
+    hide_error();
+});
+
diff --git a/src/assets/js/detailpage.js b/src/assets/js/detailpage.js
new file mode 100644 (file)
index 0000000..aa3d771
--- /dev/null
@@ -0,0 +1,73 @@
+/*********************************************************
+* DETAILS PAGE
+*********************************************************/
+
+
+
+function init_map_on_detail_page(lat, lon, geojson) {
+    map = new L.map('map', {
+        // center: [nominatim_map_init.lat, nominatim_map_init.lon],
+        // zoom:   nominatim_map_init.zoom,
+        attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
+        scrollWheelZoom:    true, // !L.Browser.touch,
+        touchZoom:          false,
+    });
+
+    L.tileLayer(get_config_value('Map_Tile_URL'), {
+        // moved to footer
+        attribution: (get_config_value('Map_Tile_Attribution') || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
+    }).addTo(map);
+
+    var layerGroup = new L.layerGroup().addTo(map);
+
+    var circle = L.circleMarker([lat,lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
+    map.addLayer(circle);
+
+    if (geojson) {
+        var geojson_layer = L.geoJson(
+            // http://leafletjs.com/reference-1.0.3.html#path-option
+            parse_and_normalize_geojson_string(geojson),
+            {
+                style: function(feature) {
+                    return { interactive: false, color: 'blue' }; 
+                }
+            }
+        );
+        map.addLayer(geojson_layer);
+        map.fitBounds(geojson_layer.getBounds());
+    } else {
+        map.setView([lat,lon],10);
+    }
+
+    var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {minZoom: 0, maxZoom: 13, attribution: (get_config_value('Map_Tile_Attribution') || null )});
+    var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
+}
+
+
+
+jQuery(document).ready(function(){
+
+    if ( !$('#details-page').length ){ return; }
+
+    var search_params = new URLSearchParams(location.search);
+    // var place_id = search_params.get('place_id');
+
+    var api_request_params = {
+        place_id: search_params.get('place_id'),
+        place_id: search_params.get('osmtype'),
+        place_id: search_params.get('osmid'),
+        group_parents: 1,
+        format: 'json'
+    };
+
+    fetch_from_api('details', api_request_params, function(aFeature){
+
+        var context = { aPlace: aFeature };
+
+        render_template($('main'), 'detailspage-template', context);
+
+        update_data_date();
+
+        init_map_on_detail_page(aFeature.lat, aFeature.lon, aFeature.asgeojson);
+    });
+});
\ No newline at end of file
similarity index 67%
rename from src/assets/js/nominatim-ui.js
rename to src/assets/js/searchpage.js
index 5f8eeeff212fcbf0c40ba40673b5f5b62ac9a68e..391da96da3df9552fe9e14adb678e8e531d8c9f8 100755 (executable)
@@ -1,87 +1,3 @@
-var map;
-var last_click_latlng;
-
-
-/*********************************************************
-* HELPERS
-*********************************************************/
-
-function get_config_value(str, default_val) {
-    return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] :  default_val);
-}
-
-function parse_and_normalize_geojson_string(raw_string){
-    // normalize places the geometry into a featurecollection, similar to
-    // https://github.com/mapbox/geojson-normalize
-    var parsed_geojson = {
-        type: "FeatureCollection",
-        features: [
-            {
-                type: "Feature",
-                geometry: JSON.parse(raw_string),
-                properties: {}
-            }
-        ]
-    };
-    return parsed_geojson;
-}
-
-function map_link_to_osm(){
-    return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
-}
-
-function map_viewbox_as_string() {
-    // since .toBBoxString() doesn't round numbers
-    return [
-        map.getBounds().getSouthWest().lng.toFixed(5), // left
-        map.getBounds().getNorthEast().lat.toFixed(5), // top
-        map.getBounds().getNorthEast().lng.toFixed(5), // right
-        map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
-    ].join(',');
-}
-
-
-/*********************************************************
-* PAGE HELPERS
-*********************************************************/
-
-function fetch_from_api(endpoint_name, params, callback) {
-    var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + $.param(params);
-    if (endpoint_name !== 'status') {
-        $('#api-request-link').attr('href', api_url);
-    }
-    $.get(api_url, function(data){
-        callback(data);
-    });
-}
-
-function update_data_date() {
-    fetch_from_api('status', {format: 'json'}, function(data){
-        $('#data-date').text(data.data_last_updated.formatted);
-    });
-}
-
-function render_template(el, template_name, page_context) {
-    var template_source = $('#' + template_name).text();
-    var template = Handlebars.compile(template_source);
-    var html    = template(page_context);
-    el.html(html);
-}
-
-function show_error(html) {
-    $('#error-overlay').html(html).show();   
-}
-
-function hide_error() {
-    $('#error-overlay').empty().hide();    
-}
-
-
-$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
-    // console.log(thrownError);
-    // console.log(ajaxSettings);
-    show_error('Error fetching results from <a href="' + ajaxSettings.url + '">' + ajaxSettings.url + '</a>');
-});
 
 /*********************************************************
 * FORWARD/REVERSE SEARCH PAGE
@@ -320,11 +236,6 @@ function init_map_on_search_page(is_reverse_search, nominatim_results, request_l
 
 
 
-jQuery(document).ready(function(){
-    hide_error();
-});
-
-
 
 
 jQuery(document).ready(function(){
@@ -406,75 +317,5 @@ jQuery(document).ready(function(){
 });
 
 
-/*********************************************************
-* DETAILS PAGE
-*********************************************************/
-
-
-
-function init_map_on_detail_page(lat, lon, geojson) {
-    map = new L.map('map', {
-        // center: [nominatim_map_init.lat, nominatim_map_init.lon],
-        // zoom:   nominatim_map_init.zoom,
-        attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
-        scrollWheelZoom:    true, // !L.Browser.touch,
-        touchZoom:          false,
-    });
-
-    L.tileLayer(get_config_value('Map_Tile_URL'), {
-        // moved to footer
-        attribution: (get_config_value('Map_Tile_Attribution') || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
-    }).addTo(map);
-
-    var layerGroup = new L.layerGroup().addTo(map);
-
-    var circle = L.circleMarker([lat,lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
-    map.addLayer(circle);
 
-    if (geojson) {
-        var geojson_layer = L.geoJson(
-            // http://leafletjs.com/reference-1.0.3.html#path-option
-            parse_and_normalize_geojson_string(geojson),
-            {
-                style: function(feature) {
-                    return { interactive: false, color: 'blue' }; 
-                }
-            }
-        );
-        map.addLayer(geojson_layer);
-        map.fitBounds(geojson_layer.getBounds());
-    } else {
-        map.setView([lat,lon],10);
-    }
-
-    var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {minZoom: 0, maxZoom: 13, attribution: (get_config_value('Map_Tile_Attribution') || null )});
-    var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
-}
-
-jQuery(document).ready(function(){
-
-    if ( !$('#details-page').length ){ return; }
-
-    var search_params = new URLSearchParams(location.search);
-    // var place_id = search_params.get('place_id');
-
-    var api_request_params = {
-        place_id: search_params.get('place_id'),
-        place_id: search_params.get('osmtype'),
-        place_id: search_params.get('osmid'),
-        group_parents: 1,
-        format: 'json'
-    };
-
-    fetch_from_api('details', api_request_params, function(aFeature){
-
-        var context = { aPlace: aFeature };
-
-        render_template($('main'), 'detailspage-template', context);
-
-        update_data_date();
-
-        init_map_on_detail_page(aFeature.lat, aFeature.lon, aFeature.asgeojson);
-    });
-});