diff --git a/client/scripts/database.js b/client/scripts/database.js index 0cef309..5d4b45a 100644 --- a/client/scripts/database.js +++ b/client/scripts/database.js @@ -508,6 +508,9 @@ function sync() { $('#i-sync').removeClass('fa-spin'); }, 500); + if (typeof onAfterSync === 'function') { + onAfterSync(); + } runPageScript(); } }, 100); diff --git a/client/scripts/datetime.js b/client/scripts/datetime.js index ac5e354..ca2789d 100644 --- a/client/scripts/datetime.js +++ b/client/scripts/datetime.js @@ -27,13 +27,13 @@ function parseDate(string) { } else { return null; } - + if (isNaN(year) || isNaN(month) || isNaN(day)) return null; if (year.toString().length == 2) year = (year < 70 ? 2000 : 1900) + year; if ((year < 1970) || (year > 3000)) return null; if ((month < 1) || (month > 12)) return null; if ((day < 1) || (day > 31)) return null; - + var date = new Date(Date.UTC(year, month - 1, day)); return date; } @@ -51,37 +51,37 @@ function formatDate(format, date = null) { date = new Date(date.valueOf()); date.setMinutes(date.getMinutes() + date.getTimezoneOffset()); } - + format = format.replace("M", "%1%"); format = format.replace("F", "%2%"); format = format.replace("D", "%3%"); format = format.replace("l", "%4%"); - + var tmp = date.getFullYear().toString(); var tmp2 = tmp.substr(2); format = format.replace("Y", tmp); format = format.replace('y', tmp2); - + tmp = (date.getMonth() + 1).toString(); tmp2 = (tmp.length > 1 ? tmp : ('0' + tmp)); format = format.replace('n', tmp); format = format.replace('m', tmp2); - + tmp = date.getDate().toString(); tmp2 = (tmp.length > 1 ? tmp : ('0' + tmp)); format = format.replace('j', tmp); format = format.replace('d', tmp2); - + tmp = date.getDay(); tmp2 = (tmp == 0 ? 7 : tmp); format = format.replace('w', tmp); format = format.replace('N', tmp2); - + format = format.replace('%1%', strings.months_short[date.getMonth()]); format = format.replace('%2%', strings.months_long[date.getMonth()]); - + format = format.replace('%3%', strings.weekdays_short[date.getDay()]); format = format.replace('%4%', strings.weekdays_long[date.getDay()]); - + return format; } diff --git a/client/scripts/regatten.js.php b/client/scripts/regatten.js.php index 0fdd28c..d0b5f44 100644 --- a/client/scripts/regatten.js.php +++ b/client/scripts/regatten.js.php @@ -402,6 +402,22 @@ function updatePushBadge() { }); } +async function updateNewsBadge() { + var newsRead = await dbSettingsGet('news_read_' + BOATCLASS); + if (newsRead === null) dbSettingsSet('news_read_' + BOATCLASS, newsRead = new Date()); + var news = await dbGetData('news'); + var now = new Date(); + var sum = 0; + for (var n in news) { + var newsEntry = news[n]; + newsEntry.date = new Date(Date.parse(newsEntry.date)); + if (newsEntry.date > now) continue; + if (newsEntry.date < newsRead) continue; + sum ++; + } + updateBadge('more/news', sum); +} + var initRegatten = function() { showLoader(); @@ -436,6 +452,12 @@ var onServiceWorkerLoaded = function() { var onDatabaseLoaded = function() { onServiceWorkerLoaded(); initPushSettings(); + + updateNewsBadge(); +} + +var onAfterSync = function() { + updateNewsBadge(); } // Add console opener to preloader diff --git a/server/scripts/news.js b/server/scripts/news.js index 914f340..6b55da4 100644 --- a/server/scripts/news.js +++ b/server/scripts/news.js @@ -1,25 +1,24 @@ async function onNewsClicked(id) { var newsEntry = await dbGetData('news', id); if (newsEntry == null) return; - + $('#menu-news').css('height', '80%'); $('#menu-news').css('width', '90%'); $('#menu-news').find('.menu-title').find('p').text(newsEntry.title); $('#menu-news').find('.content').addClass('pb-3'); $('#menu-news').find('.content').html(newsEntry.html); - + $('#menu-news').showMenu(); } function addCard(newsEntry) { - console.log(newsEntry); var content = '
' + formatDate('d.m.Y', newsEntry.date) + '
'; content += '' + newsEntry.description.replace('\n', '
') + '