egroupware/api/js/etemplate/Et2Date/Holidays.ts

41 lines
1.0 KiB
TypeScript
Raw Normal View History

/**
* Static holiday cache
* access through holidays(year)
*/
let _holiday_cache = {};
/**
* Get a list of holidays for the given year
*
* Returns either a list of holidays indexed by date, in Ymd format:
* {20001225: {day: 14, month: 2, occurence: 2021, name: "Valentinstag"}}
* or a promise that resolves with the list.
*
* No need to cache the results, we do it here.
*
* @param year
* @returns Promise | Object
*/
export function holidays(year) : Promise<Object> | {}
{
// No country selected causes error, so skip if it's missing
if(!egw.preference('country', 'common'))
{
return {};
}
if(typeof _holiday_cache[year] === 'undefined')
{
// Fetch with json instead of jsonq because there may be more than
// one widget listening for the response by the time it gets back,
// and we can't do that when it's queued.
_holiday_cache[year] = window.fetch(
egw.link('/calendar/holidays.php', {year: year})
).then((response) =>
{
return _holiday_cache[year] = response.json();
});
}
return _holiday_cache[year];
}