2022-05-26 16:17:33 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UI utility.
|
|
|
|
*
|
|
|
|
* This module provides commonly used UI functionality.
|
|
|
|
*/
|
|
|
|
window.App.Utils.UI = (function () {
|
|
|
|
/**
|
2023-02-02 10:54:24 +03:00
|
|
|
* Get the Flatpickr compatible date format.
|
2022-05-26 16:17:33 +03:00
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function getDateFormat() {
|
|
|
|
switch (vars('date_format')) {
|
|
|
|
case 'DMY':
|
2023-02-02 10:54:24 +03:00
|
|
|
return 'd/m/Y';
|
2022-05-26 16:17:33 +03:00
|
|
|
case 'MDY':
|
2023-02-02 10:54:24 +03:00
|
|
|
return 'm/d/Y';
|
2022-05-26 16:17:33 +03:00
|
|
|
case 'YMD':
|
2023-02-02 10:54:24 +03:00
|
|
|
return 'Y/m/d';
|
2022-05-26 16:17:33 +03:00
|
|
|
default:
|
|
|
|
throw new Error('Invalid date format value.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-02 10:54:24 +03:00
|
|
|
* Get the Flatpickr compatible date format.
|
2022-05-26 16:17:33 +03:00
|
|
|
*
|
2023-02-02 10:54:24 +03:00
|
|
|
* @return {String}
|
2022-05-26 16:17:33 +03:00
|
|
|
*/
|
2023-02-02 10:54:24 +03:00
|
|
|
function getTimeFormat() {
|
|
|
|
switch (vars('time_format')) {
|
|
|
|
case 'regular':
|
|
|
|
return 'h:i K';
|
|
|
|
case 'military':
|
|
|
|
return 'H:i';
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid date format value.');
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 16:17:33 +03:00
|
|
|
|
2023-02-02 10:54:24 +03:00
|
|
|
/**
|
|
|
|
* Get the localization object.
|
|
|
|
*
|
|
|
|
* @return Object
|
|
|
|
*/
|
|
|
|
function getFlatpickrLocale() {
|
2022-05-26 16:17:33 +03:00
|
|
|
const firstWeekDay = vars('first_weekday');
|
|
|
|
|
|
|
|
const firstWeekDayNumber = App.Utils.Date.getWeekdayId(firstWeekDay);
|
|
|
|
|
2023-02-02 10:54:24 +03:00
|
|
|
return {
|
|
|
|
weekdays: {
|
|
|
|
shorthand: [
|
|
|
|
lang('sunday_short'),
|
|
|
|
lang('monday_short'),
|
|
|
|
lang('tuesday_short'),
|
|
|
|
lang('wednesday_short'),
|
|
|
|
lang('thursday_short'),
|
|
|
|
lang('friday_short'),
|
2023-12-22 13:35:41 +03:00
|
|
|
lang('saturday_short'),
|
2023-02-02 10:54:24 +03:00
|
|
|
],
|
|
|
|
longhand: [
|
|
|
|
lang('sunday'),
|
|
|
|
lang('monday'),
|
|
|
|
lang('tuesday'),
|
|
|
|
lang('wednesday'),
|
|
|
|
lang('thursday'),
|
|
|
|
lang('friday'),
|
2023-12-22 13:35:41 +03:00
|
|
|
lang('saturday'),
|
2023-02-02 10:54:24 +03:00
|
|
|
],
|
|
|
|
},
|
|
|
|
months: {
|
|
|
|
shorthand: [
|
|
|
|
lang('january_short'),
|
|
|
|
lang('february_short'),
|
|
|
|
lang('march_short'),
|
|
|
|
lang('april_short'),
|
|
|
|
lang('may_short'),
|
|
|
|
lang('june_short'),
|
|
|
|
lang('july_short'),
|
|
|
|
lang('august_short'),
|
|
|
|
lang('september_short'),
|
|
|
|
lang('october_short'),
|
|
|
|
lang('november_short'),
|
|
|
|
lang('december_short'),
|
|
|
|
],
|
|
|
|
longhand: [
|
|
|
|
lang('january'),
|
|
|
|
lang('february'),
|
|
|
|
lang('march'),
|
|
|
|
lang('april'),
|
|
|
|
lang('may'),
|
|
|
|
lang('june'),
|
|
|
|
lang('july'),
|
|
|
|
lang('august'),
|
|
|
|
lang('september'),
|
|
|
|
lang('october'),
|
|
|
|
lang('november'),
|
2023-12-22 13:35:41 +03:00
|
|
|
lang('december'),
|
2023-02-02 10:54:24 +03:00
|
|
|
],
|
|
|
|
},
|
|
|
|
daysInMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
|
|
|
|
firstDayOfWeek: firstWeekDayNumber,
|
|
|
|
ordinal: function (nth) {
|
|
|
|
const s = nth % 100;
|
|
|
|
|
2023-12-22 13:35:41 +03:00
|
|
|
if (s > 3 && s < 21) return 'th';
|
2023-02-02 10:54:24 +03:00
|
|
|
switch (s % 10) {
|
|
|
|
case 1:
|
|
|
|
return 'st';
|
|
|
|
case 2:
|
|
|
|
return 'nd';
|
|
|
|
case 3:
|
|
|
|
return 'rd';
|
|
|
|
default:
|
|
|
|
return 'th';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
rangeSeparator: ` ${lang('to')} `,
|
|
|
|
weekAbbreviation: lang('week_short'),
|
|
|
|
scrollTitle: lang('scroll_to_increment'),
|
|
|
|
toggleTitle: lang('click_to_toggle'),
|
2024-10-21 04:16:57 +03:00
|
|
|
amPM: ['am', 'pm'],
|
2023-02-02 10:54:24 +03:00
|
|
|
yearAriaLabel: lang('year'),
|
|
|
|
monthAriaLabel: lang('month'),
|
|
|
|
hourAriaLabel: lang('hour'),
|
|
|
|
minuteAriaLabel: lang('minute'),
|
|
|
|
time_24hr: false,
|
2023-12-22 13:35:41 +03:00
|
|
|
};
|
2023-02-02 10:54:24 +03:00
|
|
|
}
|
2022-05-26 16:17:33 +03:00
|
|
|
|
2023-02-02 10:54:24 +03:00
|
|
|
/**
|
|
|
|
* Initialize the date time picker component.
|
|
|
|
*
|
|
|
|
* This method is based on jQuery UI.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
* @param {Object} [params]
|
|
|
|
*/
|
2024-01-06 12:46:57 +03:00
|
|
|
function initializeDateTimePicker($target, params = {}) {
|
2023-02-02 10:54:24 +03:00
|
|
|
$target.flatpickr({
|
|
|
|
enableTime: true,
|
|
|
|
allowInput: true,
|
|
|
|
static: true,
|
|
|
|
dateFormat: `${getDateFormat()} ${getTimeFormat()}`,
|
|
|
|
time_24hr: vars('time_format') === 'military',
|
|
|
|
locale: getFlatpickrLocale(),
|
2023-12-22 13:35:41 +03:00
|
|
|
...params,
|
2023-02-02 10:54:24 +03:00
|
|
|
});
|
2022-05-26 16:17:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the date picker component.
|
|
|
|
*
|
|
|
|
* This method is based on jQuery UI.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
* @param {Object} [params]
|
|
|
|
*/
|
2024-01-06 12:46:57 +03:00
|
|
|
function initializeDatePicker($target, params = {}) {
|
2023-02-02 10:54:24 +03:00
|
|
|
$target.flatpickr({
|
|
|
|
allowInput: true,
|
|
|
|
dateFormat: getDateFormat(),
|
|
|
|
locale: getFlatpickrLocale(),
|
2023-02-20 10:09:03 +03:00
|
|
|
static: true,
|
2023-12-22 13:35:41 +03:00
|
|
|
...params,
|
2022-05-26 16:17:33 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the time picker component.
|
|
|
|
*
|
|
|
|
* This method is based on jQuery UI.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
* @param {Object} [params]
|
|
|
|
*/
|
2024-01-06 12:46:57 +03:00
|
|
|
function initializeTimePicker($target, params = {}) {
|
2023-02-02 10:54:24 +03:00
|
|
|
$target.flatpickr({
|
|
|
|
noCalendar: true,
|
|
|
|
enableTime: true,
|
|
|
|
allowInput: true,
|
|
|
|
dateFormat: getTimeFormat(),
|
|
|
|
time_24hr: vars('time_format') === 'military',
|
|
|
|
locale: getFlatpickrLocale(),
|
2023-02-20 10:09:03 +03:00
|
|
|
static: true,
|
2023-12-22 13:35:41 +03:00
|
|
|
...params,
|
2022-05-26 16:17:33 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the select dropdown component.
|
|
|
|
*
|
|
|
|
* This method is based on Select2.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
* @param {Object} [params]
|
|
|
|
*/
|
|
|
|
function initializeDropdown($target, params = {}) {
|
|
|
|
$target.select2(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the text editor component.
|
|
|
|
*
|
|
|
|
* This method is based on Trumbowyg.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
* @param {Object} [params]
|
|
|
|
*/
|
|
|
|
function initializeTextEditor($target, params = {}) {
|
|
|
|
$target.trumbowyg(params);
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:04:27 +03:00
|
|
|
/**
|
|
|
|
* Get Date, Date-Time or Time picker value.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
*
|
|
|
|
* @return {Date}
|
|
|
|
*/
|
2024-01-06 12:46:57 +03:00
|
|
|
function getDateTimePickerValue($target) {
|
2023-11-17 10:04:27 +03:00
|
|
|
if (!$target?.length) {
|
2023-12-22 13:35:41 +03:00
|
|
|
throw new Error('Empty $target argument provided.');
|
2023-11-17 10:04:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $target[0]._flatpickr.selectedDates[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Date, Date-Time or Time picker value.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $target
|
|
|
|
* @param {Date} value
|
|
|
|
*/
|
2024-01-06 12:46:57 +03:00
|
|
|
function setDateTimePickerValue($target, value) {
|
2023-11-17 10:04:27 +03:00
|
|
|
if (!$target?.length) {
|
2023-12-22 13:35:41 +03:00
|
|
|
throw new Error('Empty $target argument provided.');
|
2023-11-17 10:04:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $target[0]._flatpickr.setDate(value);
|
|
|
|
}
|
|
|
|
|
2022-05-26 16:17:33 +03:00
|
|
|
return {
|
2024-01-06 12:46:57 +03:00
|
|
|
initializeDateTimePicker,
|
|
|
|
initializeDatePicker,
|
|
|
|
initializeTimePicker,
|
2022-05-26 16:17:33 +03:00
|
|
|
initializeDropdown,
|
|
|
|
initializeTextEditor,
|
2024-01-06 12:46:57 +03:00
|
|
|
getDateTimePickerValue,
|
|
|
|
setDateTimePickerValue,
|
2022-05-26 16:17:33 +03:00
|
|
|
};
|
|
|
|
})();
|