using window.fetch() and promises to load holidays

This commit is contained in:
ralf 2022-03-09 22:01:01 +02:00
parent 944c0ad68e
commit 0507872493
4 changed files with 39 additions and 64 deletions

View File

@ -4157,7 +4157,7 @@ export class CalendarApp extends EgwApp
* are set up here. * are set up here.
* *
*/ */
_setup_sidebox_filters() async _setup_sidebox_filters()
{ {
// Further date customizations // Further date customizations
var date_widget = <et2_date>this.sidebox_et2.getWidgetById('date'); var date_widget = <et2_date>this.sidebox_et2.getWidgetById('date');
@ -4197,7 +4197,8 @@ export class CalendarApp extends EgwApp
} }
}; };
var datepicker = date_widget.input_date.datepicker("option", { const holidays = await et2_calendar_view.get_holidays((new Date).getFullYear());
const datepicker = date_widget.input_date.datepicker("option", {
showButtonPanel: false, showButtonPanel: false,
onChangeMonthYear: function(year, month, inst) onChangeMonthYear: function(year, month, inst)
{ {
@ -4221,19 +4222,18 @@ export class CalendarApp extends EgwApp
// Mark holidays // Mark holidays
beforeShowDay: function (date) beforeShowDay: function (date)
{ {
var holidays = et2_calendar_view.get_holidays({day_class_holiday: function() {}}, date.getFullYear()); var day_holidays = holidays['' + date.getFullYear() +
var day_holidays = holidays[''+date.getFullYear() + sprintf("%02d", date.getMonth() + 1) +
sprintf("%02d",date.getMonth()+1) + sprintf("%02d", date.getDate())];
sprintf("%02d",date.getDate())];
var css_class = ''; var css_class = '';
var tooltip = ''; var tooltip = '';
if(typeof day_holidays !== 'undefined' && day_holidays.length) if (typeof day_holidays !== 'undefined' && day_holidays.length)
{ {
for(var i = 0; i < day_holidays.length; i++) for (var i = 0; i < day_holidays.length; i++)
{ {
if (typeof day_holidays[i]['birthyear'] !== 'undefined') if (typeof day_holidays[i]['birthyear'] !== 'undefined')
{ {
css_class +='calendar_calBirthday '; css_class += 'calendar_calBirthday ';
} }
else else
{ {

View File

@ -453,7 +453,7 @@ export class et2_calendar_daycol extends et2_valueWidget implements et2_IDetache
/** /**
* Applies class for today, and any holidays for current day * Applies class for today, and any holidays for current day
*/ */
day_class_holiday( ) async day_class_holiday( )
{ {
this.title this.title
// Remove all special day classes // Remove all special day classes
@ -472,7 +472,7 @@ export class et2_calendar_daycol extends et2_valueWidget implements et2_IDetache
); );
// Holidays and birthdays // Holidays and birthdays
let holidays = et2_calendar_view.get_holidays(this, this.options.date.substring(0, 4)); let holidays = await et2_calendar_view.get_holidays(this.options.date.substring(0, 4));
const holiday_list = []; const holiday_list = [];
let holiday_pref = (egw.preference('birthdays_as_events', 'calendar') || []); let holiday_pref = (egw.preference('birthdays_as_events', 'calendar') || []);
if(typeof holiday_pref === 'string') if(typeof holiday_pref === 'string')

View File

@ -388,7 +388,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
return this.egw().lang('User'); return this.egw().lang('User');
}, },
// Column headers // Column headers
headers: function() headers: async function()
{ {
var start = new Date(this.options.start_date); var start = new Date(this.options.start_date);
var end = new Date(this.options.end_date); var end = new Date(this.options.end_date);
@ -407,7 +407,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
} }
if(day_count < 60) if(day_count < 60)
{ {
var days = this._header_days(start, day_count); var days = await this._header_days(start, day_count);
this.headers.append(days); this.headers.append(days);
this.grid.append(days); this.grid.append(days);
} }
@ -691,7 +691,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
category: category:
{ {
title: function() { return this.egw().lang('Category');}, title: function() { return this.egw().lang('Category');},
headers: function() { headers: async function() {
var start = new Date(this.options.start_date); var start = new Date(this.options.start_date);
var end = new Date(this.options.end_date); var end = new Date(this.options.end_date);
var start_date = new Date(start.getUTCFullYear(), start.getUTCMonth(),start.getUTCDate()); var start_date = new Date(start.getUTCFullYear(), start.getUTCMonth(),start.getUTCDate());
@ -710,7 +710,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
} }
if(day_count < 60) if(day_count < 60)
{ {
var days = this._header_days(start, day_count); var days = await this._header_days(start, day_count);
this.headers.append(days); this.headers.append(days);
this.grid.append(days); this.grid.append(days);
} }
@ -937,7 +937,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
* @private * @private
* *
*/ */
_drawGrid() async _drawGrid()
{ {
this.div.css('height', this.options.height); this.div.css('height', this.options.height);
@ -961,7 +961,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
// Headers // Headers
this.headers.empty(); this.headers.empty();
this.headerTitle.text(grouper.title.apply(this)); this.headerTitle.text(grouper.title.apply(this));
grouper.headers.apply(this); await grouper.headers.apply(this);
this.grid.find('*').contents().filter(function(){ this.grid.find('*').contents().filter(function(){
return this.nodeType === 3; return this.nodeType === 3;
}).remove(); }).remove();
@ -1229,7 +1229,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
* @param {number} days * @param {number} days
* @returns {string} HTML snippet * @returns {string} HTML snippet
*/ */
_header_days(start, days) async _header_days(start, days)
{ {
var day_width = 100 / days; var day_width = 100 / days;
var content = '<div class="calendar_plannerScale'+(days > 3 ? 'Day' : '')+'" data-planner_view="day" >'; var content = '<div class="calendar_plannerScale'+(days > 3 ? 'Day' : '')+'" data-planner_view="day" >';
@ -1244,7 +1244,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
tempDate.setMinutes(tempDate.getMinutes()-tempDate.getTimezoneOffset()); tempDate.setMinutes(tempDate.getMinutes()-tempDate.getTimezoneOffset());
var title = ''; var title = '';
let state = new Date(t.valueOf() - t.getTimezoneOffset() * 60 * 1000); let state = new Date(t.valueOf() - t.getTimezoneOffset() * 60 * 1000);
var day_class = this.day_class_holiday(state,holidays, days); var day_class = await this.day_class_holiday(state,holidays, days);
if (days <= 3) if (days <= 3)
{ {
@ -1322,25 +1322,23 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
* *
* @return {string} CSS Classes for the day. calendar_calBirthday, calendar_calHoliday, calendar_calToday and calendar_weekend as appropriate * @return {string} CSS Classes for the day. calendar_calBirthday, calendar_calHoliday, calendar_calToday and calendar_weekend as appropriate
*/ */
day_class_holiday( date,holiday_list, days?) async day_class_holiday(date, holiday_list, days?)
{ {
if(!date) return ''; if(!date) return '';
var day_class = '';
// Holidays and birthdays // Holidays and birthdays
var holidays = et2_calendar_view.get_holidays(this,date.getUTCFullYear()); const holidays = await et2_calendar_view.get_holidays(date.getUTCFullYear());
var day_class = '';
// Pass a string rather than the date object, to make sure it doesn't get changed // Pass a string rather than the date object, to make sure it doesn't get changed
this.date_helper.set_value(date.toJSON()); this.date_helper.set_value(date.toJSON());
var date_key = ''+this.date_helper.get_year() + sprintf('%02d',this.date_helper.get_month()) + sprintf('%02d',this.date_helper.get_date()); var date_key = ''+this.date_helper.get_year() + sprintf('%02d',this.date_helper.get_month()) + sprintf('%02d',this.date_helper.get_date());
if(holidays && holidays[date_key]) if (holidays && holidays[date_key])
{ {
holidays = holidays[date_key]; const dates = holidays[date_key];
for(var i = 0; i < holidays.length; i++) for(var i = 0; i < dates.length; i++)
{ {
if (typeof holidays[i]['birthyear'] !== 'undefined') if (typeof dates[i]['birthyear'] !== 'undefined')
{ {
day_class += ' calendar_calBirthday '; day_class += ' calendar_calBirthday ';
if(typeof days == 'undefined' || days <= 21) if(typeof days == 'undefined' || days <= 21)
@ -1348,17 +1346,16 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta
day_class += ' calendar_calBirthdayIcon '; day_class += ' calendar_calBirthdayIcon ';
} }
holiday_list.push(holidays[i]['name']); holiday_list.push(dates[i]['name']);
} }
else else
{ {
day_class += 'calendar_calHoliday '; day_class += 'calendar_calHoliday ';
holiday_list.push(holidays[i]['name']); holiday_list.push(dates[i]['name']);
} }
} }
} }
holidays = holiday_list.join(',');
var today = new Date(); var today = new Date();
if(date_key === ''+today.getFullYear()+ if(date_key === ''+today.getFullYear()+
sprintf("%02d",today.getMonth()+1)+ sprintf("%02d",today.getMonth()+1)+

View File

@ -694,9 +694,9 @@ export class et2_calendar_view extends et2_valueWidget
* *
* @param {et2_calendar_timegrid} widget * @param {et2_calendar_timegrid} widget
* @param {string|numeric} year * @param {string|numeric} year
* @returns {Array} * @returns Promise<{[key: string]: Array<object>}>|{[key: string]: Array<object>}
*/ */
static get_holidays(widget,year) static get_holidays(year) : Promise<{[key: string]: Array<object>}>|{[key: string]: Array<object>}
{ {
// Loaded in an iframe or something // Loaded in an iframe or something
var view = egw.window.et2_calendar_view ? egw.window.et2_calendar_view : this; var view = egw.window.et2_calendar_view ? egw.window.et2_calendar_view : this;
@ -704,39 +704,17 @@ export class et2_calendar_view extends et2_valueWidget
// No country selected causes error, so skip if it's missing // No country selected causes error, so skip if it's missing
if(!view || !egw.preference('country','common')) return {}; if(!view || !egw.preference('country','common')) return {};
var cache = view.holiday_cache[year]; if (typeof view.holiday_cache[year] === 'undefined')
if (typeof cache == 'undefined')
{ {
// Fetch with json instead of jsonq because there may be more than // Fetch with json instead of jsonq because there may be more than
// one widget listening for the response by the time it gets back, // one widget listening for the response by the time it gets back,
// and we can't do that when it's queued. // and we can't do that when it's queued.
view.holiday_cache[year] = jQuery.getJSON( view.holiday_cache[year] = window.fetch(
egw.link('/calendar/holidays.php', {year: year}) egw.link('/calendar/holidays.php', {year: year})
); ).then((response) => {
} return view.holiday_cache[year] = response.json();
cache = view.holiday_cache[year]; });
if(typeof cache.done == 'function')
{
// pending, wait for it
cache.done(jQuery.proxy(function(response) {
view.holiday_cache[this.year] = response||undefined;
egw.window.setTimeout(jQuery.proxy(function() {
// Make sure widget hasn't been destroyed while we wait
if(typeof this.widget.free == 'undefined')
{
this.widget.day_class_holiday();
}
},this),1);
},{widget:widget,year:year}))
.fail(jQuery.proxy(function() {
view.holiday_cache[this.year] = undefined;
}, {widget: widget, year: year}));
return {};
}
else
{
return cache;
} }
return view.holiday_cache[year];
} }
} }