2022-01-26 17:06:44 +03:00
|
|
|
/**
|
2021-01-23 10:07:18 +03:00
|
|
|
* Calculate the Timeago
|
2022-01-26 17:06:44 +03:00
|
|
|
*
|
|
|
|
* Requirement: <https://github.com/iamkun/dayjs>
|
2019-09-30 15:38:41 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
$(function() {
|
2022-01-26 17:06:44 +03:00
|
|
|
const attrTimestamp = LocaleHelper.attrTimestamp();
|
|
|
|
const attrCapitalize = 'data-capitalize';
|
|
|
|
const $timeago = $(".timeago");
|
2020-07-23 10:33:42 +03:00
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
let timeagoTasks = $timeago.length;
|
|
|
|
let intervalId = void 0;
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
dayjs.locale(LocaleHelper.locale());
|
|
|
|
dayjs.extend(window.dayjs_plugin_relativeTime);
|
|
|
|
dayjs.extend(window.dayjs_plugin_localizedFormat);
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
function relativetime($elem) {
|
|
|
|
const now = dayjs();
|
|
|
|
const past = dayjs.unix(LocaleHelper.getTimestamp($elem));
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
let diffMonth = now.diff(past, 'month', true);
|
|
|
|
if (diffMonth > 10) { // year ago range: 11 months to 17months
|
|
|
|
$elem.removeAttr(attrTimestamp);
|
|
|
|
return past.format('ll'); // see: https://day.js.org/docs/en/display/format#list-of-localized-formats
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
let diffMinute = now.diff(past, 'minute', true);
|
|
|
|
if (diffMinute > 44) { // an hour ago range: 45 to 89 minutes
|
|
|
|
$elem.removeAttr(attrTimestamp);
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
return past.fromNow();
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateTimeago() {
|
2022-01-26 17:06:44 +03:00
|
|
|
$timeago.each(function() {
|
|
|
|
if (typeof $(this).attr(attrTimestamp) === 'undefined') {
|
|
|
|
timeagoTasks -= 1;
|
2021-12-04 01:56:33 +03:00
|
|
|
return;
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
2021-12-04 01:56:33 +03:00
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
let relativeTime = relativetime($(this));
|
|
|
|
const capitalize = $(this).attr(attrCapitalize);
|
|
|
|
if (typeof capitalize !== 'undefined' && capitalize === 'true') {
|
|
|
|
relativeTime = relativeTime.replace(/^\w/, (c) => c.toUpperCase());
|
2021-12-04 01:56:33 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
if ($(this).text() !== relativeTime) {
|
|
|
|
$(this).text(relativeTime);
|
|
|
|
}
|
2019-09-30 15:38:41 +03:00
|
|
|
});
|
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
if (timeagoTasks === 0 && typeof intervalId !== "undefined") {
|
2020-08-19 07:26:45 +03:00
|
|
|
clearInterval(intervalId); /* stop interval */
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
2022-01-26 17:06:44 +03:00
|
|
|
|
|
|
|
return timeagoTasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupTooltips() {
|
|
|
|
$timeago.each(function() {
|
|
|
|
const tooltip = $(this).attr('data-toggle');
|
|
|
|
if (typeof tooltip === 'undefined' || tooltip !== 'tooltip') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const df = $(this).attr('data-tooltip-df');
|
|
|
|
const ts = LocaleHelper.getTimestamp($(this));
|
|
|
|
const dateStr = dayjs.unix(ts).format(df);
|
|
|
|
$(this).attr('data-original-title', dateStr);
|
|
|
|
$(this).removeAttr('data-tooltip-df');
|
|
|
|
});
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
if (timeagoTasks === 0) {
|
2019-09-30 15:38:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:06:44 +03:00
|
|
|
setupTooltips();
|
|
|
|
|
|
|
|
if (updateTimeago()) { /* run immediately */
|
|
|
|
intervalId = setInterval(updateTimeago, 60 * 1000); /* run every minute */
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
});
|