From 33796f7bd5def85a5753cc3b4430769379b60475 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 11 Jul 2018 07:47:27 -0700 Subject: set timezone automatically based on location --- views/event.php | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/views/event.php b/views/event.php index aab5ef7..3c26f73 100644 --- a/views/event.php +++ b/views/event.php @@ -13,6 +13,14 @@ +
+ + + +
+ + +
@@ -31,14 +39,6 @@
-
- - - -
- - -
@@ -67,6 +67,9 @@ var map = null; + var d = new Date(); + var tzOffset = tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1); + var selectedPlace; if(map) { var gservice = new google.maps.places.AutocompleteService(); @@ -75,9 +78,17 @@ } $(function(){ - var d = new Date(); - $("#start_date .timezone").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1)); - /* $("#end_date .timezone").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1)); */ + // Start the event timezone offset in the browser's timezone + $("#start_date .timezone").attr("placeholder", tzOffset); + $("#end_date .timezone").attr("placeholder", tzOffset); + + // As soon as a time is entered, move the placeholder offset to the value + $("#start_date .time").on("keydown", function(){ + $("#start_date .timezone").val($("#start_date .timezone").attr("placeholder")); + }); + $("#end_date .time").on("keydown", function(){ + $("#end_date .timezone").val($("#end_date .timezone").attr("placeholder")); + }); if(map) { $("#event_location").typeahead({ @@ -105,12 +116,13 @@ gplaces.getDetails({ placeId: suggestion.place_id, - fields: ["geometry", "name", "address_component", "url"] + fields: ["geometry", "name", "address_component", "url", "utc_offset"] }, function(result, status) { if(status != google.maps.places.PlacesServiceStatus.OK) { alert('Cannot find address'); return; } + console.log(result); map.setCenter(result.geometry.location); @@ -168,6 +180,18 @@ selectedPlace['properties']['country-name'] = [country]; } + if(result.utc_offset) { + tzOffset = tz_seconds_to_offset(result.utc_offset * 60); + $("#start_date .timezone").attr("placeholder", tzOffset); + $("#end_date .timezone").attr("placeholder", tzOffset); + if($("#start_date .timezone").val()) { + $("#start_date .timezone").val($("#start_date .timezone").attr("placeholder")); + } + if($("#end_date .timezone").val()) { + $("#end_date .timezone").val($("#end_date .timezone").attr("placeholder")); + } + } + $("#map").removeClass("hidden"); $("#location_preview").text(''); }); -- cgit v1.2.3