Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
tischsoic committed Mar 19, 2019
2 parents 2f0000c + ad85331 commit b4c5647
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
28 changes: 21 additions & 7 deletions src/bundle/Resources/public/js/scripts/fieldType/ezdatetime.js
Expand Up @@ -4,6 +4,8 @@
const SELECTOR_FLATPICKR_INPUT = '.flatpickr-input';
const SELECTOR_LABEL_WRAPPER = '.ez-field-edit__label-wrapper';
const EVENT_VALUE_CHANGED = 'valueChanged';
const { convertDateToTimezone } = eZ.helpers.timezone;
const userTimezone = eZ.adminUiConfig.timezone;

class EzDateTimeValidator extends eZ.BaseFieldValidator {
/**
Expand Down Expand Up @@ -63,21 +65,22 @@
const datetimeConfig = {
enableTime: true,
time_24hr: true,
formatDate: (date) => new Date(date).toLocaleString(),
};
const updateInputValue = (sourceInput, date) => {
const updateInputValue = (sourceInput, dates) => {
const event = new CustomEvent(EVENT_VALUE_CHANGED);

if (!date.length) {
if (!dates.length) {
sourceInput.value = '';
sourceInput.dispatchEvent(event);

return;
}

date = new Date(date[0]);
sourceInput.value = Math.floor(date.getTime() / 1000);
const selectedDate = dates[0];
const selectedDateWithUserTimezone = convertDateToTimezone(selectedDate, userTimezone, true);
const timestamp = Math.floor(selectedDateWithUserTimezone.valueOf() / 1000);

sourceInput.value = timestamp;
sourceInput.dispatchEvent(event);
};
const clearValue = (sourceInput, flatpickrInstance, event) => {
Expand All @@ -91,13 +94,24 @@
const sourceInput = field.querySelector(SELECTOR_INPUT);
const flatPickrInput = field.querySelector(SELECTOR_FLATPICKR_INPUT);
const btnClear = field.querySelector('.ez-data-source__btn--clear-input');
const defaultDate = sourceInput.value ? new Date(sourceInput.value * 1000) : null;
const secondsEnabled = sourceInput.dataset.seconds === '1';
const formatDate = secondsEnabled ? (date) => date.toLocaleString() : (date) => eZ.helpers.timezone.formatDate(date);
let defaultDate = null;

if (sourceInput.value) {
const defaultDateWithUserTimezone = convertDateToTimezone(sourceInput.value * 1000);
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

defaultDate = new Date(convertDateToTimezone(defaultDateWithUserTimezone, browserTimezone, true));
}

const flatpickrInstance = flatpickr(
flatPickrInput,
Object.assign({}, datetimeConfig, {
onChange: updateInputValue.bind(null, sourceInput),
defaultDate,
enableSeconds: !!parseInt(sourceInput.dataset.seconds, 10),
enableSeconds: secondsEnabled,
formatDate,
})
);

Expand Down
Expand Up @@ -3,8 +3,8 @@
const userPreferedFullDateFormat = eZ.adminUiConfig.dateFormat.full;
const userPreferedShortDateFormat = eZ.adminUiConfig.dateFormat.short;

const convertDateToTimezone = (date, timezone = userPreferedTimezone) => {
return moment(date).tz(timezone);
const convertDateToTimezone = (date, timezone = userPreferedTimezone, forceSameTime = false) => {
return moment(date).tz(timezone, forceSameTime);
};
const formatDate = (date, format = userPreferedFullDateFormat) => {
return moment(date).formatICU(format);
Expand Down
4 changes: 1 addition & 3 deletions src/lib/Behat/PageElement/DateAndTimePopup.php
Expand Up @@ -47,9 +47,7 @@ public function setTime(string $hour, string $minute): void

if (!$isTimeOnly) {
// get current date as it's not possible to set time without setting date
$currentDateScript = sprintf('document.querySelector("%s %s")._flatpickr.formatDate(document.querySelector("%s %s")._flatpickr.selectedDates, "Y-m-d")',
$this->fields['containerSelector'],
$this->fields['flatpickrSelector'],
$currentDateScript = sprintf('document.querySelector("%s %s")._flatpickr.selectedDates[0].toLocaleString()',
$this->fields['containerSelector'],
$this->fields['flatpickrSelector']);
$currentDate = $this->context->getSession()->getDriver()->evaluateScript($currentDateScript);
Expand Down

0 comments on commit b4c5647

Please sign in to comment.