From 00c1c93b807a9f8ce1e2762d83652418f9bb6aa6 Mon Sep 17 00:00:00 2001 From: Timon Ostertun Date: Sun, 27 Sep 2020 23:17:56 +0200 Subject: [PATCH] Added news --- client/scripts/database.js | 44 ++++++++++++++++++++++++++++++++++++-- client/styles/regatten.css | 7 ++++++ server/content/news.php | 27 ++++++++++++----------- server/scripts/news.js | 44 ++++++++++++++++++++++++++++++++++++++ service-worker.js.php | 4 ++++ 5 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 server/scripts/news.js diff --git a/client/scripts/database.js b/client/scripts/database.js index c0a50d9..7496e0a 100644 --- a/client/scripts/database.js +++ b/client/scripts/database.js @@ -1,4 +1,4 @@ -const DB_VERSION = 5; +const DB_VERSION = 6; const USER_ID = localStorage.getItem('auth_user'); const USER_NAME = localStorage.getItem('auth_username'); @@ -491,7 +491,7 @@ function sync() { localTimes[entry['table']] = entry['time']; }); - syncInProgress = 10; + syncInProgress = 11; var syncOkay = true; console.log("Sync Start"); $('#i-sync').addClass('fa-spin'); @@ -824,6 +824,38 @@ function sync() { syncInProgress -= 3; } + // NEWS + if (localTimes['news'] < serverTimes['news']) { + getJSON(QUERY_URL + 'get_news?changed-after=' + localTimes['news'], function (code, data) { + if (code == 200) { + var os = db.transaction('news', 'readwrite').objectStore('news'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'news', time: serverTimes['news'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + // USERS if (localTimes['users'] < serverTimes['users']) { getJSON(QUERY_URL + 'get_users?changed-after=' + localTimes['users'], function (code, data) { @@ -986,6 +1018,13 @@ function initDatabase() { var osPushes = db.createObjectStore('settings', { keyPath: 'key' }); } + if ((oldVersion < 6) && (newVersion >= 6)) { + console.log('to version 6'); + var osNews = db.createObjectStore('news', { keyPath: 'id' }); + var osUpdateTimes = upgradeTransaction.objectStore('update_times'); + osUpdateTimes.add({ table: 'news', time: 0 }); + } + var osUpdateTimes = upgradeTransaction.objectStore('update_times'); osUpdateTimes.put({ table: 'last_sync', time: 0 }); } @@ -1009,6 +1048,7 @@ function resetDb(silent = true) { osUpdateTimes.put({ table: 'trim_boats', time: 0 }); osUpdateTimes.put({ table: 'trim_users', time: 0 }); osUpdateTimes.put({ table: 'trim_trims', time: 0 }); + osUpdateTimes.put({ table: 'news', time: 0 }); osUpdateTimes.put({ table: 'users', time: 0 }); console.log('DB update times reset'); if (!silent) diff --git a/client/styles/regatten.css b/client/styles/regatten.css index efea685..4f22174 100644 --- a/client/styles/regatten.css +++ b/client/styles/regatten.css @@ -173,6 +173,13 @@ text-align: right; } +/*** BLOCKQUOTE ***/ +blockquote { + border-left: 0.5em solid rgba(0, 0, 0, 0.125); + font-style: italic; + padding-left: 1em; +} + /*** BLINKING ICONS ***/ @keyframes fa-blink { 0% { opacity: 1; } diff --git a/server/content/news.php b/server/content/news.php index 85a23df..41f9efa 100644 --- a/server/content/news.php +++ b/server/content/news.php @@ -1,19 +1,22 @@ load('error', ['404', 'Seite existiert noch nicht']); - $content .= '

'; - $content .= 'Die gesuchte Seite ist leider noch nicht verfügbar.
'; - $content .= 'Wir arbeiten daran, sie schnellstmöglich zur Verfügung zu stellen.
'; - $content .= 'Wie wäre es mit der Homepage?'; - $content .= '

'; - $content .= $tpl->load('button', ['Zur Startseite', LINK_PRE . 'index', 'css-class' => 'mb-3']); - $content .= $tpl->load('button', ['Kontakt', LINK_PRE . 'contact']); + // Title + $content = "

Neuigkeiten

"; + $content .= '

Aktuelles der letzten zwölf Monate

'; - $sp['output'] = $tpl->load('card', [$content, 'css-class' => 'text-center pt-3']); + $sp['output'] .= $tpl->load('card', [$content]); + + // Menu + $sp['menus'] .= $tpl->load('menu/modal', ['html-id' => 'menu-news', 'title' => 'Details']); + + $cardTemplate = $tpl->load('card', ['%CONTENT%', 'html-id' => '%ID%', 'css-class' => 'card-news']); + $cardTemplate = str_replace("\n", '', $cardTemplate); + $cardTemplate = str_replace("\r", '', $cardTemplate); + $sp['scripts'] .= ""; + $sp['scripts'] .= $scripts->load('news'); ?> \ No newline at end of file diff --git a/server/scripts/news.js b/server/scripts/news.js new file mode 100644 index 0000000..914f340 --- /dev/null +++ b/server/scripts/news.js @@ -0,0 +1,44 @@ +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 = '

' + newsEntry.title + '

'; + content += '

' + formatDate('d.m.Y', newsEntry.date) + '

'; + content += '

' + newsEntry.description.replace('\n', '
') + '

'; + if (newsEntry.html != '') { + content += 'Mehr lesen'; + } + + $('.page-content').append(cardTemplate.replace('%ID%', 'card-news-' + newsEntry.id).replace('%CONTENT%', content)); +} + +var siteScript = async function() { + $('.card-news').remove(); + var news = await dbGetData('news'); + news.sort(function (a,b) { + return b.date.localeCompare(a.date); + }); + var today = getToday(); + var lastYear = new Date(today); + lastYear.setFullYear(lastYear.getFullYear() - 1); + console.log(today, lastYear); + for (var n in news) { + var newsEntry = news[n]; + newsEntry.date = parseDate(newsEntry.date.substring(0, 10)); + if (newsEntry.date > today) continue; + if (newsEntry.date < lastYear) break; + addCard(newsEntry); + } + hideLoader(); +} \ No newline at end of file diff --git a/service-worker.js.php b/service-worker.js.php index 2caaedf..35b049b 100644 --- a/service-worker.js.php +++ b/service-worker.js.php @@ -226,6 +226,10 @@ self.addEventListener('push', async function(event) { options.image = image; } + // Force refresh on next app open + var os = db.transaction('update_times', 'readwrite').objectStore('update_times'); + os.put({ table: 'last_sync', time: 0 }); + console.log('Showing notification'); self.registration.showNotification(data.title, options); break;