From 0507872493573e85823f8569b4d839badf9ca1f2 Mon Sep 17 00:00:00 2001 From: ralf Date: Wed, 9 Mar 2022 22:01:01 +0200 Subject: [PATCH] using window.fetch() and promises to load holidays --- calendar/js/app.ts | 18 +++++++------- calendar/js/et2_widget_daycol.ts | 6 ++--- calendar/js/et2_widget_planner.ts | 39 ++++++++++++++---------------- calendar/js/et2_widget_view.ts | 40 +++++++------------------------ 4 files changed, 39 insertions(+), 64 deletions(-) diff --git a/calendar/js/app.ts b/calendar/js/app.ts index da1a99c997..91a053faed 100644 --- a/calendar/js/app.ts +++ b/calendar/js/app.ts @@ -4157,7 +4157,7 @@ export class CalendarApp extends EgwApp * are set up here. * */ - _setup_sidebox_filters() + async _setup_sidebox_filters() { // Further date customizations var date_widget = 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, onChangeMonthYear: function(year, month, inst) { @@ -4221,19 +4222,18 @@ export class CalendarApp extends EgwApp // Mark holidays beforeShowDay: function (date) { - var holidays = et2_calendar_view.get_holidays({day_class_holiday: function() {}}, date.getFullYear()); - var day_holidays = holidays[''+date.getFullYear() + - sprintf("%02d",date.getMonth()+1) + - sprintf("%02d",date.getDate())]; + var day_holidays = holidays['' + date.getFullYear() + + sprintf("%02d", date.getMonth() + 1) + + sprintf("%02d", date.getDate())]; var css_class = ''; 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') { - css_class +='calendar_calBirthday '; + css_class += 'calendar_calBirthday '; } else { diff --git a/calendar/js/et2_widget_daycol.ts b/calendar/js/et2_widget_daycol.ts index fa0204c472..c837f85df6 100644 --- a/calendar/js/et2_widget_daycol.ts +++ b/calendar/js/et2_widget_daycol.ts @@ -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 */ - day_class_holiday( ) + async day_class_holiday( ) { this.title // Remove all special day classes @@ -472,7 +472,7 @@ export class et2_calendar_daycol extends et2_valueWidget implements et2_IDetache ); // 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 = []; let holiday_pref = (egw.preference('birthdays_as_events', 'calendar') || []); if(typeof holiday_pref === 'string') @@ -1237,4 +1237,4 @@ export class et2_calendar_daycol extends et2_valueWidget implements et2_IDetache this._out_of_view(); } } -et2_register_widget(et2_calendar_daycol, ["calendar-daycol"]); +et2_register_widget(et2_calendar_daycol, ["calendar-daycol"]); \ No newline at end of file diff --git a/calendar/js/et2_widget_planner.ts b/calendar/js/et2_widget_planner.ts index 2bb2669fe4..938f400436 100644 --- a/calendar/js/et2_widget_planner.ts +++ b/calendar/js/et2_widget_planner.ts @@ -388,7 +388,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta return this.egw().lang('User'); }, // Column headers - headers: function() + headers: async function() { var start = new Date(this.options.start_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) { - var days = this._header_days(start, day_count); + var days = await this._header_days(start, day_count); this.headers.append(days); this.grid.append(days); } @@ -691,7 +691,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta category: { title: function() { return this.egw().lang('Category');}, - headers: function() { + headers: async function() { var start = new Date(this.options.start_date); var end = new Date(this.options.end_date); 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) { - var days = this._header_days(start, day_count); + var days = await this._header_days(start, day_count); this.headers.append(days); this.grid.append(days); } @@ -937,7 +937,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta * @private * */ - _drawGrid() + async _drawGrid() { this.div.css('height', this.options.height); @@ -961,7 +961,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta // Headers this.headers.empty(); this.headerTitle.text(grouper.title.apply(this)); - grouper.headers.apply(this); + await grouper.headers.apply(this); this.grid.find('*').contents().filter(function(){ return this.nodeType === 3; }).remove(); @@ -1229,7 +1229,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta * @param {number} days * @returns {string} HTML snippet */ - _header_days(start, days) + async _header_days(start, days) { var day_width = 100 / days; var content = '
'; @@ -1244,7 +1244,7 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta tempDate.setMinutes(tempDate.getMinutes()-tempDate.getTimezoneOffset()); var title = ''; 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) { @@ -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 */ - day_class_holiday( date,holiday_list, days?) + async day_class_holiday(date, holiday_list, days?) { - if(!date) return ''; - var day_class = ''; - // 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 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()); - if(holidays && holidays[date_key]) + if (holidays && holidays[date_key]) { - holidays = holidays[date_key]; - for(var i = 0; i < holidays.length; i++) + const dates = holidays[date_key]; + for(var i = 0; i < dates.length; i++) { - if (typeof holidays[i]['birthyear'] !== 'undefined') + if (typeof dates[i]['birthyear'] !== 'undefined') { day_class += ' calendar_calBirthday '; 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 '; } - holiday_list.push(holidays[i]['name']); + holiday_list.push(dates[i]['name']); } else { day_class += 'calendar_calHoliday '; - holiday_list.push(holidays[i]['name']); + holiday_list.push(dates[i]['name']); } } } - holidays = holiday_list.join(','); var today = new Date(); if(date_key === ''+today.getFullYear()+ sprintf("%02d",today.getMonth()+1)+ @@ -2628,4 +2625,4 @@ export class et2_calendar_planner extends et2_calendar_view implements et2_IDeta this.rows.css('overflow-y', 'auto'); } } -et2_register_widget(et2_calendar_planner, ["calendar-planner"]); +et2_register_widget(et2_calendar_planner, ["calendar-planner"]); \ No newline at end of file diff --git a/calendar/js/et2_widget_view.ts b/calendar/js/et2_widget_view.ts index 2242f5c4bc..1c9915b352 100644 --- a/calendar/js/et2_widget_view.ts +++ b/calendar/js/et2_widget_view.ts @@ -694,9 +694,9 @@ export class et2_calendar_view extends et2_valueWidget * * @param {et2_calendar_timegrid} widget * @param {string|numeric} year - * @returns {Array} + * @returns Promise<{[key: string]: Array}>|{[key: string]: Array} */ - static get_holidays(widget,year) + static get_holidays(year) : Promise<{[key: string]: Array}>|{[key: string]: Array} { // Loaded in an iframe or something 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 if(!view || !egw.preference('country','common')) return {}; - var cache = view.holiday_cache[year]; - if (typeof cache == 'undefined') + if (typeof view.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. - view.holiday_cache[year] = jQuery.getJSON( + view.holiday_cache[year] = window.fetch( egw.link('/calendar/holidays.php', {year: year}) - ); - } - 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; + ).then((response) => { + return view.holiday_cache[year] = response.json(); + }); } + return view.holiday_cache[year]; } -} +} \ No newline at end of file