Files
wekan/client/lib/dateDisplay.js
T
Lauri Ojansivu 46607d4299 Add an admin date format override for everyone
Place the global date format checkbox and selector between Board member
list and Wait Spinner in Visibility settings. While enabled, use the
selected format for members and guests, call the card section Date and
hide its personal format selector. Preserve personal preferences so they
return when the override is disabled.

Apply the same policy to date displays, filter parsing and browser export
requests. Validate supported formats and retain admin-only settings writes.
Add positive, negative and browser regression coverage and document the
setting. Translate the new label without changing existing translations.

Lower-confidence new translations include Acehnese, Aymara, Cherokee,
Inuktitut, Kashmiri, Nahuatl, Veps, Volapuk and Wolaytta; native-speaker
review would improve these labels.

Thanks to Mathia2 and xet7 !

Fixes #6703,
2026-09-17 18:13:20 +03:00

132 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { resolveDateFormat } = require('/models/lib/dateFormatPolicy');
import { ReactiveCache } from '/imports/reactiveCache';
import { formatDateByUserPreference } from '/imports/lib/dateUtils';
import { formatJalaliDate, gregorianToJalali } from '/imports/lib/jalaliDate';
import { TAPi18n } from '/imports/i18n';
const { CALENDAR_SYSTEMS, formatNativeCalendarDate, nativeCalendarParts } = require('/imports/lib/calendarSystems');
const { calendarMonthRange, shiftedDay } = require('/imports/lib/calendarMonth');
export function isDateFormatForced() {
return !!ReactiveCache.getCurrentSetting()?.hideDateFormat;
}
export function dateDisplayPreferences() {
const setting = ReactiveCache.getCurrentSetting();
const user = ReactiveCache.getCurrentUser();
if (user) {
return {
calendarSystem: user.getCalendarSystem ? user.getCalendarSystem() : 'gregorian',
dateFormat: resolveDateFormat(user.getDateFormat ? user.getDateFormat() : null, setting),
};
}
try {
return {
calendarSystem: window.localStorage.getItem('calendarSystem') || 'gregorian',
dateFormat: resolveDateFormat(window.localStorage.getItem('dateFormat'), setting),
};
} catch (error) {
return { calendarSystem: 'gregorian', dateFormat: resolveDateFormat(null, setting) };
}
}
// Only rendered text belongs here. ISO attributes, picker values and persisted
// dates must continue to use Gregorian formatting. A supplied callback keeps
// each existing Gregorian display's locale, precision and layout.
export function formatDateForDisplay(date, includeTime = true, gregorianFormatter) {
if (date === null || date === undefined || date === '') return '';
const value = new Date(date);
if (Number.isNaN(value.getTime())) return '';
const { calendarSystem, dateFormat } = dateDisplayPreferences();
if (calendarSystem === 'jalali') {
try {
return formatJalaliDate(value, dateFormat, includeTime);
} catch (error) {
// Intl's Persian calendar covers dates beyond the tabular converter's
// finite range without displaying a different calendar.
return formatNativeCalendarDate(value, 'persian', dateFormat, includeTime, TAPi18n.getLanguage() || 'en') || '';
}
} else if (calendarSystem !== 'gregorian') {
const system = CALENDAR_SYSTEMS.find(item => item.value === calendarSystem);
const text = system && formatNativeCalendarDate(
value, system.intl, dateFormat, includeTime, TAPi18n.getLanguage() || 'en',
);
if (system) return text || '';
}
return gregorianFormatter && !isDateFormatForced()
? gregorianFormatter(value)
: formatDateByUserPreference(value, dateFormat, includeTime);
}
// FullCalendar 5.11.5 exposes its displayed wall-calendar fields to formatting
// callbacks. Construct from those fields instead of shifting its UTC marker
// through the browser's time zone. Grid placement and navigation stay native.
export function calendarDateDisplayOptions(calendarId) {
const { calendarSystem } = dateDisplayPreferences();
if (calendarSystem === 'gregorian') return {};
const system = CALENDAR_SYSTEMS.find(item => item.value === calendarSystem);
if (!system) return {};
const displayMarker = marker => formatDateForDisplay(
new Date(marker.year, marker.month, marker.day, 12), false,
);
const navigate = direction => {
const calendar = document.getElementById(calendarId)?._wekanCalendar;
if (!calendar) return;
if (!['selectedCalendarMonth', 'selectedCalendarListMonth'].includes(calendar.view.type)) {
calendar[direction < 0 ? 'prev' : 'next']();
return;
}
const range = calendarMonthRange(calendar.getDate(), system.intl);
calendar.gotoDate(direction < 0 ? shiftedDay(range.start, -1) : range.end);
};
return {
// Built-in month views have a fixed Gregorian duration that takes
// precedence over visibleRange. Inherit the duration-free base views.
initialView: 'selectedCalendarMonth',
views: {
selectedCalendarMonth: {
type: 'dayGrid', monthMode: true, fixedWeekCount: false,
visibleRange: date => calendarMonthRange(date, system.intl),
},
selectedCalendarListMonth: {
type: 'list', visibleRange: date => calendarMonthRange(date, system.intl),
},
},
customButtons: {
dayGridMonth: { text: TAPi18n.__('month'), click: () => {
document.getElementById(calendarId)?._wekanCalendar.changeView('selectedCalendarMonth');
} },
listMonth: { text: TAPi18n.__('list'), click: () => {
document.getElementById(calendarId)?._wekanCalendar.changeView('selectedCalendarListMonth');
} },
prev: { text: TAPi18n.__('previous'), click: () => navigate(-1) },
next: { text: TAPi18n.__('next'), click: () => navigate(1) },
},
titleFormat(info) {
const start = displayMarker(info.start);
const end = info.end && displayMarker(info.end);
return end && end !== start ? `${start}${info.defaultSeparator || ' – '}${end}` : start;
},
dayCellContent(info) {
try {
if (calendarSystem !== 'jalali') {
const parts = nativeCalendarParts(info.date, system.intl, TAPi18n.getLanguage() || 'en');
return parts ? parts.day : info.dayNumberText;
}
return String(gregorianToJalali(
info.date.getFullYear(), info.date.getMonth() + 1, info.date.getDate(),
).jd);
} catch (error) {
return info.dayNumberText;
}
},
dayHeaderContent(info) {
// Month-view column headings contain weekdays, with no date to convert.
return ['dayGridMonth', 'selectedCalendarMonth'].includes(info.view.type)
? info.text : formatDateForDisplay(info.date, false);
},
listDayFormat(info) { return displayMarker(info.date); },
// The list header has two date slots. Use one selected-calendar date.
listDaySideFormat() { return ''; },
};
}