123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. _ _ _
  3. | |__ ___ ___ | |_ ___| |_ _ __ __ ___ __
  4. | '_ \ / _ \ / _ \| __/ __| __| '__/ _` \ \/ /
  5. | |_) | (_) | (_) | |_\__ | |_| | | (_| |> <
  6. |_.__/ \___/ \___/ \__|___/\__|_| \__,_/_/\_\.js
  7. */
  8. requirejs.config({
  9. baseUrl: '/static/oscar/js',
  10. paths: {
  11. app: '../app'
  12. }
  13. });
  14. if(searx.autocompleter) {
  15. searx.searchResults = new Bloodhound({
  16. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  17. queryTokenizer: Bloodhound.tokenizers.whitespace,
  18. remote: '/autocompleter?q=%QUERY'
  19. });
  20. searx.searchResults.initialize();
  21. }
  22. $(document).ready(function(){
  23. $('.btn-toggle .btn').click(function() {
  24. var btnClass = 'btn-' + $(this).data('btn-class');
  25. var btnLabelDefault = $(this).data('btn-label-default');
  26. var btnLabelToggled = $(this).data('btn-label-toggled');
  27. if(btnLabelToggled != '') {
  28. if($(this).hasClass('btn-default')) {
  29. var html = $(this).html().replace(btnLabelDefault, btnLabelToggled);
  30. } else {
  31. var html = $(this).html().replace(btnLabelToggled, btnLabelDefault);
  32. }
  33. $(this).html(html);
  34. }
  35. $(this).toggleClass(btnClass);
  36. $(this).toggleClass('btn-default');
  37. });
  38. $('.btn-collapse').click(function() {
  39. var btnTextCollapsed = $(this).data('btn-text-collapsed');
  40. var btnTextNotCollapsed = $(this).data('btn-text-not-collapsed');
  41. if(btnTextCollapsed != '' && btnTextNotCollapsed != '') {
  42. if($(this).hasClass('collapsed')) {
  43. var html = $(this).html().replace(btnTextCollapsed, btnTextNotCollapsed);
  44. } else {
  45. var html = $(this).html().replace(btnTextNotCollapsed, btnTextCollapsed);
  46. }
  47. $(this).html(html);
  48. }
  49. });
  50. $(".select-all-on-click").click(function () {
  51. $(this).select();
  52. });
  53. if(searx.autocompleter) {
  54. $('#q').typeahead(null, {
  55. name: 'search-results',
  56. displayKey: function(result) {
  57. return result;
  58. },
  59. source: searx.searchResults.ttAdapter()
  60. });
  61. }
  62. $(".searx_init_map").on( "click", function( event ) {
  63. var leaflet_target = $(this).data('leaflet-target');
  64. var map_lon = $(this).data('map-lon');
  65. var map_lat = $(this).data('map-lat');
  66. var map_zoom = $(this).data('map-zoom');
  67. var map_boundingbox = $(this).data('map-boundingbox');
  68. var map_geojson = $(this).data('map-geojson');
  69. require(['leaflet-0.7.3.min'], function(leaflet) {
  70. if(map_boundingbox) {
  71. var southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]),
  72. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]),
  73. map_bounds = L.latLngBounds(southWest, northEast);
  74. }
  75. // TODO hack
  76. // change default imagePath
  77. L.Icon.Default.imagePath = "/static/oscar/img/map";
  78. // init map
  79. var map = L.map(leaflet_target);
  80. // create the tile layer with correct attribution
  81. var osmUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  82. var osmAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  83. var osm = new L.TileLayer(osmUrl, {minZoom: 1, maxZoom: 19, attribution: osmAttrib});
  84. // init map view
  85. if(map_bounds) {
  86. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  87. setTimeout(function () {
  88. map.fitBounds(map_bounds);
  89. }, 0);
  90. } else if (map_lon && map_lat) {
  91. if(map_zoom)
  92. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  93. else
  94. map.setView(new L.LatLng(map_lat, map_lon),8);
  95. }
  96. map.addLayer(osm);
  97. if(map_geojson)
  98. L.geoJson(map_geojson).addTo(map);
  99. //if(map_bounds)
  100. // L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);
  101. });
  102. // this event occour only once per element
  103. $( this ).off( event );
  104. });
  105. });