2022-01-26 17:06:44 +03:00
|
|
|
/**
|
|
|
|
* Update month/day to locale datetime
|
|
|
|
*
|
|
|
|
* Requirement: <https://github.com/iamkun/dayjs>
|
|
|
|
*/
|
|
|
|
|
2022-04-25 19:39:58 +03:00
|
|
|
/* A tool for locale datetime */
|
|
|
|
const LocaleHelper = (function () {
|
2022-10-25 14:26:44 +03:00
|
|
|
const locale = $('html').attr('lang').substr(0, 2);
|
|
|
|
const attrTimestamp = 'data-ts';
|
|
|
|
const attrDateFormat = 'data-df';
|
2022-04-25 19:39:58 +03:00
|
|
|
|
2022-10-25 14:26:44 +03:00
|
|
|
return {
|
|
|
|
locale: () => locale,
|
|
|
|
attrTimestamp: () => attrTimestamp,
|
|
|
|
attrDateFormat: () => attrDateFormat,
|
|
|
|
getTimestamp: ($elem) => Number($elem.attr(attrTimestamp)), // unix timestamp
|
|
|
|
getDateFormat: ($elem) => $elem.attr(attrDateFormat)
|
|
|
|
};
|
2022-04-25 19:39:58 +03:00
|
|
|
|
|
|
|
}());
|
|
|
|
|
2022-10-25 14:26:44 +03:00
|
|
|
$(function () {
|
|
|
|
dayjs.locale(LocaleHelper.locale());
|
|
|
|
dayjs.extend(window.dayjs_plugin_localizedFormat);
|
2022-01-26 17:06:44 +03:00
|
|
|
|
2022-10-25 14:26:44 +03:00
|
|
|
$(`[${LocaleHelper.attrTimestamp()}]`).each(function () {
|
|
|
|
const date = dayjs.unix(LocaleHelper.getTimestamp($(this)));
|
|
|
|
const text = date.format(LocaleHelper.getDateFormat($(this)));
|
|
|
|
$(this).text(text);
|
|
|
|
$(this).removeAttr(LocaleHelper.attrTimestamp());
|
|
|
|
$(this).removeAttr(LocaleHelper.attrDateFormat());
|
2022-04-25 19:39:58 +03:00
|
|
|
|
2022-10-25 14:26:44 +03:00
|
|
|
// setup tooltips
|
|
|
|
const tooltip = $(this).attr('data-toggle');
|
|
|
|
if (typeof tooltip === 'undefined' || tooltip !== 'tooltip') {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-25 19:39:58 +03:00
|
|
|
|
2022-10-25 14:26:44 +03:00
|
|
|
const tooltipText = date.format('llll'); // see: https://day.js.org/docs/en/display/format#list-of-localized-formats
|
|
|
|
$(this).attr('data-original-title', tooltipText);
|
|
|
|
});
|
2022-01-26 17:06:44 +03:00
|
|
|
});
|