From 93259e4e92eaa7b692b5fb882dcca2b44cdc9a4a Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Wed, 24 Jan 2018 22:46:36 +0100 Subject: consolidate notifications caching and loading --- view/js/main.js | 54 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'view/js/main.js') diff --git a/view/js/main.js b/view/js/main.js index 00c536243..89202c94f 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -68,14 +68,11 @@ $(document).ready(function() { $('a.notification-link').click(function(e){ var notifyType = $(this).data('type'); - if (!$(this).data('clicked') && (sessionStorage.getItem(notifyType + '_notifications_cache') !== null)) { - var cached_data = JSON.parse(sessionStorage.getItem(notifyType + '_notifications_cache')); - loadNotificationItems(notifyType, cached_data); - $(this).data('clicked', true); - } - else { + if(! $('#nav-' + notifyType + '-sub').hasClass('show')) { loadNotificationItems(notifyType); } + + $(this).data('clicked', true); }); // Allow folks to stop the ajax page updates with the pause/break key @@ -391,6 +388,15 @@ function notificationsUpdate(cached_data) { }); } + var notifyType = null; + + if($('.notification-content.show').length) + notifyType = $('.notification-content.show').data('type'); + + if(notifyType !== null) { + loadNotificationItems(notifyType); + } + if(timer) clearTimeout(timer); timer = setTimeout(updateInit,updateInterval); } @@ -924,33 +930,35 @@ function justifyPhotosAjax(id) { $('#' + id).justifiedGallery('norewind').on('jg.complete', function(e){ justifiedGalleryActive = false; }); } -function loadNotificationItems(notifyType, cached_data) { +function loadNotificationItems(notifyType) { var pingExCmd = 'ping/' + notifyType + ((localUser != 0) ? '?f=&uid=' + localUser : ''); - if(cached_data !== undefined) { + var clicked = $('[data-type=\'' + notifyType + '\']').data('clicked'); + + if((clicked === undefined) && (sessionStorage.getItem(notifyType + '_notifications_cache') !== null)) { + var cached_data = JSON.parse(sessionStorage.getItem(notifyType + '_notifications_cache')); handleNotificationsItems(notifyType, cached_data); + console.log('updating ' + notifyType + ' notifications from cache...'); } else { - $.get(pingExCmd, function(data) { - if(data.invalid == 1) { - window.location.href=window.location.href; - } - - $("." + notifyType + "-update").html(data.notify.length); + var cached_data = []; + } - sessionStorage.setItem(notifyType + '_notifications_cache', JSON.stringify(data.notify)); + console.log('updating ' + notifyType + ' notifications...'); + $.get(pingExCmd, function(data) { + if(data.invalid == 1) { + window.location.href=window.location.href; + } + if(JSON.stringify(cached_data[0]) === JSON.stringify(data.notify[0])) { + console.log(notifyType + ' notifications cache up to date - update deferred'); + } + else { handleNotificationsItems(notifyType, data.notify); - }); - } - - setTimeout(function() { - if($('#nav-' + notifyType + '-sub').hasClass('show')) { - console.log('updating ' + notifyType + ' notifications...'); - setTimeout(loadNotificationItems, updateInterval, notifyType); + sessionStorage.setItem(notifyType + '_notifications_cache', JSON.stringify(data.notify)); } - }, 1000); + }); } // Since our ajax calls are asynchronous, we will give a few -- cgit v1.2.3 From 019dcefecad12fb99f5b5caddd6a73ed4a99addb Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Thu, 25 Jan 2018 12:56:52 +0100 Subject: reset timer on manual update trigger --- view/js/main.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'view/js/main.js') diff --git a/view/js/main.js b/view/js/main.js index 89202c94f..41ca0bb3f 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -70,6 +70,8 @@ $(document).ready(function() { if(! $('#nav-' + notifyType + '-sub').hasClass('show')) { loadNotificationItems(notifyType); + if(timer) clearTimeout(timer); + timer = setTimeout(updateInit,updateInterval); } $(this).data('clicked', true); @@ -389,10 +391,9 @@ function notificationsUpdate(cached_data) { } var notifyType = null; - - if($('.notification-content.show').length) + if($('.notification-content.show').length) { notifyType = $('.notification-content.show').data('type'); - + } if(notifyType !== null) { loadNotificationItems(notifyType); } @@ -437,7 +438,6 @@ function handleNotifications(data) { } function handleNotificationsItems(notifyType, data) { - var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html()); var notify_menu = $("#nav-" + notifyType + "-menu"); @@ -931,11 +931,9 @@ function justifyPhotosAjax(id) { } function loadNotificationItems(notifyType) { - var pingExCmd = 'ping/' + notifyType + ((localUser != 0) ? '?f=&uid=' + localUser : ''); var clicked = $('[data-type=\'' + notifyType + '\']').data('clicked'); - if((clicked === undefined) && (sessionStorage.getItem(notifyType + '_notifications_cache') !== null)) { var cached_data = JSON.parse(sessionStorage.getItem(notifyType + '_notifications_cache')); handleNotificationsItems(notifyType, cached_data); -- cgit v1.2.3 From 4656856dfbf41e855b1bb8625290c07a5dcf8d38 Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Thu, 25 Jan 2018 17:27:48 +0100 Subject: move template out of #notifications and revert reset timer on manual trigger --- view/js/main.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'view/js/main.js') diff --git a/view/js/main.js b/view/js/main.js index 41ca0bb3f..8eb02156b 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -70,8 +70,6 @@ $(document).ready(function() { if(! $('#nav-' + notifyType + '-sub').hasClass('show')) { loadNotificationItems(notifyType); - if(timer) clearTimeout(timer); - timer = setTimeout(updateInit,updateInterval); } $(this).data('clicked', true); -- cgit v1.2.3