Skip to content

Commit

Permalink
Extract functions out of the ready function
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Feb 16, 2024
1 parent 8ea75c0 commit 243b697
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions assets/js/translation-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,61 +57,61 @@
);
}
);
}
);

function validateEventDates() {
const startDateTimeInput = $( '#event-start' );
const endDateTimeInput = $( '#event-end' );
if ( ! startDateTimeInput.length || ! endDateTimeInput.length ) {
return;
}
function validateEventDates() {
const startDateTimeInput = $( '#event-start' );
const endDateTimeInput = $( '#event-end' );
if ( ! startDateTimeInput.length || ! endDateTimeInput.length ) {
return;
}

startDateTimeInput.add( endDateTimeInput ).on(
'input',
function () {
endDateTimeInput.prop( 'min', startDateTimeInput.val() );
if (endDateTimeInput.val() < startDateTimeInput.val()) {
endDateTimeInput.val( startDateTimeInput.val() );
}
}
);
startDateTimeInput.add( endDateTimeInput ).on(
'input',
function () {
endDateTimeInput.prop( 'min', startDateTimeInput.val() );
if (endDateTimeInput.val() < startDateTimeInput.val()) {
endDateTimeInput.val( startDateTimeInput.val() );
}
}
);
}

function selectUserTimezone() {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// phpcs:disable WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore
// phpcs:disable WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
document.querySelector( `#event-timezone option[value="${timezone}"]` ).selected = true
// phpcs:enable
}
function selectUserTimezone() {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// phpcs:disable WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore
// phpcs:disable WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
document.querySelector( `#event-timezone option[value="${timezone}"]` ).selected = true
// phpcs:enable
}

function convertToUserLocalTime() {
const timeElements = document.querySelectorAll( 'time.event-utc-time' );
if ( timeElements.length === 0 ) {
return;
}
timeElements.forEach(
function ( timeEl ) {
const eventDateObj = new Date( timeEl.getAttribute( 'datetime' ) );
const userTimezoneOffset = new Date().getTimezoneOffset();
const userTimezoneOffsetMs = userTimezoneOffset * 60 * 1000;
const userLocalDateTime = new Date( eventDateObj.getTime() - userTimezoneOffsetMs );
function convertToUserLocalTime() {
const timeElements = document.querySelectorAll( 'time.event-utc-time' );
if ( timeElements.length === 0 ) {
return;
}
timeElements.forEach(
function ( timeEl ) {
const eventDateObj = new Date( timeEl.getAttribute( 'datetime' ) );
const userTimezoneOffset = new Date().getTimezoneOffset();
const userTimezoneOffsetMs = userTimezoneOffset * 60 * 1000;
const userLocalDateTime = new Date( eventDateObj.getTime() - userTimezoneOffsetMs );

const options = {
weekday: 'short',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: true,
timeZoneName: 'short'
};
const options = {
weekday: 'short',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: true,
timeZoneName: 'short'
};

timeEl.textContent = userLocalDateTime.toLocaleString( 'en-US', options );
}
);
timeEl.textContent = userLocalDateTime.toLocaleString( 'en-US', options );
}
}
);
);
}
}( jQuery, $gp )
);

0 comments on commit 243b697

Please sign in to comment.