2023-03-13 15:20:59 +03:00
|
|
|
/**
|
|
|
|
* Update month/day to locale datetime
|
|
|
|
*
|
|
|
|
* Requirement: <https://github.com/iamkun/dayjs>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* A tool for locale datetime */
|
|
|
|
class LocaleHelper {
|
|
|
|
static get attrTimestamp() {
|
|
|
|
return 'data-ts';
|
|
|
|
}
|
|
|
|
|
|
|
|
static get attrDateFormat() {
|
|
|
|
return 'data-df';
|
|
|
|
}
|
|
|
|
|
|
|
|
static get locale() {
|
2024-04-17 01:10:01 +03:00
|
|
|
return document.documentElement.getAttribute('lang').substring(0, 2);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static getTimestamp(elem) {
|
2024-04-17 01:10:01 +03:00
|
|
|
return Number(elem.getAttribute(this.attrTimestamp)); // unix timestamp
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDateFormat(elem) {
|
2024-04-17 01:10:01 +03:00
|
|
|
return elem.getAttribute(this.attrDateFormat);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initLocaleDatetime() {
|
|
|
|
dayjs.locale(LocaleHelper.locale);
|
|
|
|
dayjs.extend(window.dayjs_plugin_localizedFormat);
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
document
|
|
|
|
.querySelectorAll(`[${LocaleHelper.attrTimestamp}]`)
|
|
|
|
.forEach((elem) => {
|
|
|
|
const date = dayjs.unix(LocaleHelper.getTimestamp(elem));
|
|
|
|
const text = date.format(LocaleHelper.getDateFormat(elem));
|
|
|
|
elem.textContent = text;
|
|
|
|
elem.removeAttribute(LocaleHelper.attrTimestamp);
|
|
|
|
elem.removeAttribute(LocaleHelper.attrDateFormat);
|
|
|
|
|
|
|
|
// setup tooltips
|
|
|
|
if (
|
|
|
|
elem.hasAttribute('data-bs-toggle') &&
|
|
|
|
elem.getAttribute('data-bs-toggle') === 'tooltip'
|
|
|
|
) {
|
|
|
|
// see: https://day.js.org/docs/en/display/format#list-of-localized-formats
|
|
|
|
const tooltipText = date.format('llll');
|
|
|
|
elem.setAttribute('data-bs-title', tooltipText);
|
|
|
|
}
|
|
|
|
});
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|