Skip to content

Commit

Permalink
more improvements to tz
Browse files Browse the repository at this point in the history
  • Loading branch information
laander committed Sep 26, 2018
1 parent d3e878c commit a5f1c46
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 76 deletions.
163 changes: 103 additions & 60 deletions dist/booking.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/booking.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/booking.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/single.htm
Expand Up @@ -18,6 +18,9 @@
resources: [
'42b956f0-be11-4af3-9221-19e899fcd1fb'
],
availability: {
length: '1 hour'
}
});
</script>

Expand Down
1 change: 0 additions & 1 deletion src/defaults.js
Expand Up @@ -36,7 +36,6 @@ var primary = {
},
allDaySlot: false,
scrollTime: '08:00:00',
timezone: 'local',
nowIndicator: true
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/init.js
Expand Up @@ -141,6 +141,9 @@ function Initialize() {
// Setup Timekit SDK config
configureSdk();

// Start by guessing customer timezone
render.guessCustomerTimezone();

// Initialize FullCalendar
render.initializeCalendar();

Expand Down
55 changes: 48 additions & 7 deletions src/render.js
Expand Up @@ -25,6 +25,9 @@ function InitRender(deps) {
var loadingTarget;
var errorTarget;

// State
var customerTimezone;

// Make sure DOM element is ready and clean it
var prepareDOM = function(suppliedConfig) {

Expand All @@ -44,7 +47,9 @@ function InitRender(deps) {
// Fetch availabile time through Timekit SDK
var timekitFetchAvailability = function() {

var args = {};
var args = {
output_timezone: customerTimezone
};

if (getConfig().project_id) args.project_id = getConfig().project_id
if (getConfig().resources) args.resources = getConfig().resources
Expand Down Expand Up @@ -216,7 +221,6 @@ function InitRender(deps) {

// Calculate and display timezone helper
var renderTimezoneHelper = function() {
var tzGuess = moment.tz.guess();
var tzList = moment.tz.names();
var timezoneIcon = require('!svg-inline!./assets/timezone-icon.svg');
var arrowDownIcon = require('!svg-inline!./assets/arrow-down-icon.svg');
Expand All @@ -225,14 +229,33 @@ function InitRender(deps) {
var timezoneHelperTarget = $(template.render({
timezoneIcon: timezoneIcon,
arrowDownIcon: arrowDownIcon,
localTimezone: tzGuess,
listTimezones: tzList,
timekitLogo: timekitLogo
}));
rootTarget.addClass('has-timezonehelper');
rootTarget.append(timezoneHelperTarget);

// Set initial customer timezone
var pickerSelect = $('.bookingjs-footer-tz-picker-select');
pickerSelect.val(customerTimezone);

pickerSelect.change(function() {
setCustomerTimezone(pickerSelect.val());
})
};

// Guess the timezone and set global variable
var guessCustomerTimezone = function () {
var tzGuess = moment.tz.guess();
customerTimezone = tzGuess;
}

// Set timezone and trigger event
var setCustomerTimezone = function (newTz) {
customerTimezone = newTz;
$(document).trigger('customer-timezone-changed');
}

// Setup and render FullCalendar
var initializeCalendar = function() {

Expand All @@ -256,6 +279,12 @@ function InitRender(deps) {

calendarTarget.fullCalendar(args);

$(document).on('customer-timezone-changed', function () {
if (!calendarTarget) return
getAvailability();
calendarTarget.fullCalendar('option', 'now', moment().tz(customerTimezone).format());
})

utils.doCallback('fullCalendarInitialized');

};
Expand Down Expand Up @@ -454,8 +483,8 @@ function InitRender(deps) {
var allocatedResource = eventData.resources ? eventData.resources[0].name : false;

bookingPageTarget = $(template.render({
chosenDate: moment(eventData.start).format(dateFormat),
chosenTime: moment(eventData.start).format(timeFormat) + ' - ' + moment(eventData.end).format(timeFormat),
chosenDate: formatTimestamp(eventData.start, dateFormat),
chosenTime: formatTimestamp(eventData.start, timeFormat) + ' - ' + formatTimestamp(eventData.end, timeFormat),
allocatedResourcePrefix: getConfig().ui.localization.allocated_resource_prefix,
allocatedResource: allocatedResource,
closeIcon: require('!svg-inline!./assets/close-icon.svg'),
Expand Down Expand Up @@ -497,6 +526,12 @@ function InitRender(deps) {
renderPoweredByMessage(bookingPageTarget);
}

$(document).on('customer-timezone-changed', function () {
if (!bookingPageTarget) return
$('.bookingjs-bookpage-date').text(formatTimestamp(eventData.start, dateFormat));
$('.bookingjs-bookpage-time').text(formatTimestamp(eventData.start, timeFormat) + ' - ' + formatTimestamp(eventData.end, timeFormat));
});

$(document).on('keyup', function(e) {
// escape key maps to keycode `27`
if (e.keyCode === 27) { hideBookingPage(); }
Expand All @@ -510,6 +545,11 @@ function InitRender(deps) {

};

// Output timestamp into given format in customers timezone
var formatTimestamp = function (start, format) {
return moment(start).tz(customerTimezone).format(format);
}

// Remove the booking page DOM node
var hideBookingPage = function() {

Expand Down Expand Up @@ -598,7 +638,7 @@ function InitRender(deps) {
customer: {
name: formData.name,
email: formData.email,
timezone: moment.tz.guess()
timezone: customerTimezone
},
participants: [formData.email]
};
Expand Down Expand Up @@ -710,7 +750,8 @@ function InitRender(deps) {
timekitCreateBooking: timekitCreateBooking,
fullCalendar: fullCalendar,
destroyFullCalendar: destroyFullCalendar,
renderTimezoneHelper: renderTimezoneHelper
renderTimezoneHelper: renderTimezoneHelper,
guessCustomerTimezone: guessCustomerTimezone
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/styles/main.scss
Expand Up @@ -46,14 +46,15 @@
// Timezone Helper

&-footer {
position: relative;
color: $textLighterColor;
text-align: left;
padding: 0px 25px;
background-color: $bgColor;
border-top: 1px solid $borderColor;
min-height: 30px;
line-height: 30px;
z-index: 20;
z-index: 40;
border-radius: 0 0 4px 4px;

&-tz {
Expand Down

0 comments on commit a5f1c46

Please sign in to comment.