2022-04-29 19:35:04 +02:00
|
|
|
/**
|
|
|
|
* Static holiday cache
|
|
|
|
* access through holidays(year)
|
|
|
|
*/
|
2022-06-08 00:39:04 +02:00
|
|
|
import {egw} from "../../jsapi/egw_global";
|
|
|
|
|
2022-04-29 19:35:04 +02:00
|
|
|
let _holiday_cache = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of holidays for the given year
|
|
|
|
*
|
|
|
|
* Returns either a list of holidays indexed by date, in Ymd format:
|
2022-04-29 22:26:54 +02:00
|
|
|
* {20001225: [{day: 14, month: 2, occurence: 2021, name: "Valentinstag"}]}
|
2022-04-29 19:35:04 +02:00
|
|
|
* or a promise that resolves with the list.
|
|
|
|
*
|
|
|
|
* No need to cache the results, we do it here.
|
|
|
|
*
|
|
|
|
* @param year
|
2022-04-29 22:26:54 +02:00
|
|
|
* @returns Promise<{[key: string]: Array<object>}>|{[key: string]: Array<object>}
|
2022-04-29 19:35:04 +02:00
|
|
|
*/
|
2022-04-29 22:26:54 +02:00
|
|
|
export function holidays(year) : Promise<{ [key : string] : Array<object> }> | { [key : string] : Array<object> }
|
2022-04-29 19:35:04 +02:00
|
|
|
{
|
|
|
|
// No country selected causes error, so skip if it's missing
|
2022-06-08 00:39:04 +02:00
|
|
|
if(!egw || !egw.preference('country', 'common'))
|
2022-04-29 19:35:04 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|