webpack-bootstrap-ui-kit/src/js_old/_components/_ui.search.js

183 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2020-12-24 23:42:33 +01:00
2022-05-03 20:50:57 +02:00
import $ from 'jquery'
2020-12-24 23:42:33 +01:00
2019-10-20 01:40:40 +02:00
$(() => {
2022-05-03 20:50:57 +02:00
const $searchLat = $('[name="search-lat"]')
const $searchLng = $('[name="search-lng"]')
const $nearbyLat = $('#nearby-lat')
const $nearbyLng = $('#nearby-lng')
const $radius = $('#distance-radius')
const $category = $('#distance-category')
const $newLocation = $('[name="newlocation"]')
const $setnewlocation = $('#setnewlocation')
const $newlocationholder = $('.set-newlocation-holder')
2019-10-20 01:40:40 +02:00
const updatePosition = (lat, lng) => {
2022-05-03 20:50:57 +02:00
$searchLat.val(lat)
$searchLng.val(lng)
$nearbyLat.val(lat)
$nearbyLng.val(lng)
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$searchLat.change()
$nearbyLat.change()
}
2019-10-20 01:40:40 +02:00
const getGeoPosition = () => {
2022-05-03 20:50:57 +02:00
const newLocation = $newLocation.val()
2019-10-20 01:40:40 +02:00
if (!newLocation.length) {
2022-05-03 20:50:57 +02:00
return
2019-10-20 01:40:40 +02:00
}
2022-05-03 20:50:57 +02:00
$('.search-location .current-val').text(newLocation)
const geoUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${newLocation}&key=AIzaSyC00L0023LPBhzj12uTCL-4EwJ_6zgwcTU&sensor=true`
2019-10-20 01:40:40 +02:00
2020-12-24 23:42:33 +01:00
$.getJSON(geoUrl).done((data) => {
2022-05-03 20:50:57 +02:00
if (data.status === 'OK') {
2020-12-24 23:42:33 +01:00
updatePosition(
data.results[0].geometry.location.lat,
2021-08-18 20:51:15 +02:00
data.results[0].geometry.location.lng
2022-05-03 20:50:57 +02:00
)
// getCategories();
2020-12-24 23:42:33 +01:00
}
2022-05-03 20:50:57 +02:00
})
}
2019-10-20 01:40:40 +02:00
const getCurrentPosition = () => {
2022-05-03 20:50:57 +02:00
$('.search-location .current-val').text('Current Location')
2019-10-20 01:40:40 +02:00
2020-12-24 23:42:33 +01:00
navigator.geolocation.getCurrentPosition(
(position) => {
2022-05-03 20:50:57 +02:00
updatePosition(position.coords.latitude, position.coords.longitude)
// hideDistancesThatDontMatter();
2020-12-24 23:42:33 +01:00
},
() => {
2022-05-03 20:50:57 +02:00
$('.search-location .current-val').text('Unable to get your location')
updatePosition('', '')
2021-08-18 20:51:15 +02:00
}
2022-05-03 20:50:57 +02:00
)
}
2019-10-20 01:40:40 +02:00
if ($newLocation.length && $newLocation.val().length) {
2022-05-03 20:50:57 +02:00
getGeoPosition()
2019-10-20 01:40:40 +02:00
} else {
2022-05-03 20:50:57 +02:00
getCurrentPosition()
2019-10-20 01:40:40 +02:00
}
2022-05-03 20:50:57 +02:00
$('#Form_SearchForm').on('keyup keypress', (e) => {
const keyCode = e.keyCode || e.which
2019-10-20 01:40:40 +02:00
if (keyCode === 13) {
2022-05-03 20:50:57 +02:00
e.preventDefault()
return false
2019-10-20 01:40:40 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$('.get-current-location').on('click', (e) => {
e.preventDefault()
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
getCurrentPosition()
$newlocationholder.toggle()
$newLocation.val('')
})
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$setnewlocation.on('click', (e) => {
e.preventDefault()
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$newlocationholder.toggle()
})
2019-10-20 01:40:40 +02:00
$newLocation.blur(() => {
2022-05-03 20:50:57 +02:00
getGeoPosition()
})
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$('.new-search').on('click', (e) => {
e.preventDefault()
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$('.section-search-secondary').animate(
2020-12-24 23:42:33 +01:00
{
2022-05-03 20:50:57 +02:00
'max-height': 300,
2020-12-24 23:42:33 +01:00
},
2022-05-03 20:50:57 +02:00
'slow'
)
})
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
/* $radius.on('change', () => {
2019-10-20 01:40:40 +02:00
getCategories();
});
function getCategories() {
$.getJSON(`/api/categoriesnearby/${$searchLat.val()}/${$searchLng.val()}/${$radius.val()}`)
.done((data) => {
$category.empty(); // remove old options
$category.append($('<option></option>').attr('value', 'all').text('All Categories'));
$.each(data.categories, (i, cat) => {
$category.append($('<option></option>').attr('value', cat.id).text(cat.name));
});
});
}
function hideDistancesThatDontMatter() {
$.getJSON(`/api/nearestevent/${ $searchLat.val()}/${$searchLat.val()}`)
.done((data) => {
const nearest = parseFloat(data);
$radius.find('option').each(function() {
if (this.value !== 'all' && parseFloat(this.value) < nearest) {
$(this).hide();
}
});
});
2022-05-03 20:50:57 +02:00
} */
const $map = $('#Map')
if (typeof google !== 'undefined' && $map.length) {
const $directions = $('#DirectionsPanel')
const $fromAddress = $('#FromAddress')
const $getDirections = $('#GetDirections')
const $directionContainer = $('#DirectionContainer')
const directionsDisplay = new google.maps.DirectionsRenderer()
const directionsService = new google.maps.DirectionsService()
const currentPosition = {
lat: $map.data('lat'),
lng: $map.data('lng'),
}
const map = new google.maps.Map($map[0], {
zoom: 15,
mapTypeControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
})
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
directionsDisplay.setMap(map)
directionsDisplay.setPanel($directions[0])
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
map.setCenter(currentPosition)
2019-10-20 01:40:40 +02:00
new google.maps.Marker({
map,
2020-12-24 23:42:33 +01:00
position: currentPosition,
2022-05-03 20:50:57 +02:00
})
2019-10-20 01:40:40 +02:00
$getDirections.click((e) => {
2022-05-03 20:50:57 +02:00
e.preventDefault()
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
const fromLocation = $fromAddress.val()
2019-10-20 01:40:40 +02:00
if (fromLocation.length) {
2020-12-24 23:42:33 +01:00
directionsService.route(
{
origin: fromLocation,
destination: currentPosition,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
},
(response, status) => {
if (status === google.maps.DirectionsStatus.OK) {
2022-05-03 20:50:57 +02:00
directionsDisplay.setDirections(response)
2020-12-24 23:42:33 +01:00
}
2021-08-18 20:51:15 +02:00
}
2022-05-03 20:50:57 +02:00
)
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
$directionContainer.slideDown()
2019-10-20 01:40:40 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-10-20 01:40:40 +02:00
}
2022-05-03 20:50:57 +02:00
})