From 740594a4b73952ad3f5fa52dd1bb939c73dcc7c2 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sun, 2 Nov 2014 13:00:28 +0100 Subject: [enh] oscar_template: initial osm-map support for map results * TODO: remove leaflet.min.css if not required --- searx/static/oscar/js/scripts.js | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'searx/static/oscar/js/scripts.js') diff --git a/searx/static/oscar/js/scripts.js b/searx/static/oscar/js/scripts.js index 6c3a10a74..37285c9c9 100644 --- a/searx/static/oscar/js/scripts.js +++ b/searx/static/oscar/js/scripts.js @@ -7,6 +7,13 @@ */ +requirejs.config({ +baseUrl: '/static/oscar/js', +paths: { +app: '../app' +} +}); + if(searx.autocompleter) { searx.searchResults = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), @@ -61,4 +68,56 @@ $(document).ready(function(){ source: searx.searchResults.ttAdapter() }); } + + $(".searx_init_map").on( "click", function( event ) { + var leaflet_target = $(this).data('leaflet-target'); + var map_lon = $(this).data('map-lon'); + var map_lat = $(this).data('map-lat'); + var map_zoom = $(this).data('map-zoom'); + var map_boundingbox = $(this).data('map-boundingbox'); + var map_geojson = $(this).data('map-geojson'); + + require(['leaflet-0.7.3.min'], function(leaflet) { + if(map_boundingbox) { + var southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]), + northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]), + map_bounds = L.latLngBounds(southWest, northEast); + } + + // TODO hack + // change default imagePath + L.Icon.Default.imagePath = "/static/oscar/img/map"; + + // init map + var map = L.map(leaflet_target); + + // create the tile layer with correct attribution + var osmUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; + var osmAttrib='Map data © OpenStreetMap contributors'; + var osm = new L.TileLayer(osmUrl, {minZoom: 1, maxZoom: 19, attribution: osmAttrib}); + + // init map view + if(map_bounds) { + // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021 + setTimeout(function () { + map.fitBounds(map_bounds); + }, 0); + } else if (map_lon && map_lat) { + if(map_zoom) + map.setView(new L.LatLng(map_lat, map_lon),map_zoom); + else + map.setView(new L.LatLng(map_lat, map_lon),8); + } + + map.addLayer(osm); + + if(map_geojson) + L.geoJson(map_geojson).addTo(map); + //if(map_bounds) + // L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map); + }); + + // this event occour only once per element + $( this ).off( event ); + }); }); -- cgit v1.2.3