Skip to content

Commit 3c64ab6

Browse files
authored
Merge pull request #53 from EasyAbp/ui-fix-2
Refactor toolbar badge to always re-render via server widget
2 parents 9205910 + 5272751 commit 3c64ab6

4 files changed

Lines changed: 47 additions & 19 deletions

File tree

common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<LangVersion>latest</LangVersion>
4-
<Version>1.7.0-preview.2</Version>
4+
<Version>1.7.0-preview.3</Version>
55
<NoWarn>$(NoWarn);CS1591;CS0436</NoWarn>
66
<AbpProjectType>module</AbpProjectType>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

src/EasyAbp.ProcessManagement.Web/Components/NotificationsOffcanvasWidget/Default.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
}
4242
});
4343
refreshBaseUiElements();
44-
refreshToolbarWidget();
44+
currentBadgeCount = res.totalCount;
45+
refreshToolbarWidget(res.totalCount);
4546
}).catch(function () {
4647
// Silently ignore network errors for background notification fetch
4748
});
@@ -106,7 +107,7 @@
106107
});
107108
newAlertNode.addEventListener('closed.bs.alert', function () {
108109
refreshBaseUiElements();
109-
updateToolbarBadgeCount(Math.max(0, getToolbarBadgeCount() - 1));
110+
refreshToolbarWidget(Math.max(0, getToolbarBadgeCount() - 1));
110111
});
111112
}
112113

@@ -146,7 +147,7 @@
146147
}
147148

148149
// Update the toolbar badge count directly to avoid stale server cache
149-
updateToolbarBadgeCount(getToolbarBadgeCount() + 1);
150+
refreshToolbarWidget(getToolbarBadgeCount() + 1);
150151
});
151152

152153
connection.onreconnected(function () {
@@ -185,21 +186,29 @@
185186
}
186187
}
187188

188-
function updateToolbarBadgeCount(count) {
189-
$('.notifications-toolbar-item').each(function () {
190-
var $icon = $(this).find('i');
191-
$(this).text(' ' + count).prepend($icon);
192-
});
189+
// Track the current badge count client-side
190+
var currentBadgeCount = null;
191+
192+
function readBadgeCountFromDom($context) {
193+
var $el = ($context || $(document)).find('.notifications-toolbar-item[data-notification-count]').first();
194+
if (!$el.length) return null;
195+
var val = parseInt($el.attr('data-notification-count'));
196+
return isNaN(val) ? null : val;
193197
}
194198

195199
function getToolbarBadgeCount() {
196-
var $first = $('.notifications-toolbar-item').first();
197-
if (!$first.length) return 0;
198-
var text = $first.text().trim();
199-
return parseInt(text) || 0;
200+
if (currentBadgeCount === null) {
201+
currentBadgeCount = readBadgeCountFromDom() || 0;
202+
}
203+
return currentBadgeCount;
200204
}
201205

202-
function refreshToolbarWidget() {
206+
function refreshToolbarWidget(count) {
207+
if (typeof count === 'number') {
208+
count = Math.min(99, Math.max(0, count));
209+
currentBadgeCount = count;
210+
}
211+
203212
for (const randomId of (window.toolbarNotificationsWidgetAreaRandomIds || [])) {
204213
var $wrapper = $('#ToolbarNotificationsWidgetArea-' + randomId);
205214
var $widgets = $wrapper.find('.abp-widget-wrapper');
@@ -215,13 +224,21 @@
215224
var refreshUrl = $firstWidget.attr('data-refresh-url');
216225
if (!refreshUrl) continue;
217226

227+
var url = typeof count === 'number'
228+
? refreshUrl + (refreshUrl.indexOf('?') >= 0 ? '&' : '?') + 'count=' + count
229+
: refreshUrl;
230+
218231
$.ajax({
219-
url: refreshUrl,
232+
url: url,
220233
type: 'GET',
221234
dataType: 'html',
222235
global: false
223236
}).then(function (result) {
224237
var $result = $(result);
238+
var serverCount = readBadgeCountFromDom($result);
239+
if (serverCount !== null) {
240+
currentBadgeCount = serverCount;
241+
}
225242
for (const rid of (window.toolbarNotificationsWidgetAreaRandomIds || [])) {
226243
var $w = $('#ToolbarNotificationsWidgetArea-' + rid);
227244
var $ww = $w.find('.abp-widget-wrapper');
@@ -288,9 +305,9 @@
288305
removeAlert(alert)
289306
});
290307
if (dismiss === 'DismissAll') {
291-
updateToolbarBadgeCount(0);
308+
refreshToolbarWidget(0);
292309
} else {
293-
updateToolbarBadgeCount(Math.max(0, getToolbarBadgeCount() - dismissedCount));
310+
refreshToolbarWidget(Math.max(0, getToolbarBadgeCount() - dismissedCount));
294311
}
295312
});
296313
}

src/EasyAbp.ProcessManagement.Web/Components/NotificationsToolbarItemWidget/Default.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@model EasyAbp.ProcessManagement.Web.Components.NotificationsToolbarItemWidget.NotificationsToolbarItemWidgetViewModel
55

66
<div class="dropdown">
7-
<a class="nav-link notifications-toolbar-item" data-bs-toggle="offcanvas" href="#notificationsOffcanvas" role="button" aria-controls="notificationsOffcanvas">
7+
<a class="nav-link notifications-toolbar-item" data-notification-count="@Model.UnreadCount" data-bs-toggle="offcanvas" href="#notificationsOffcanvas" role="button" aria-controls="notificationsOffcanvas">
88
<i class="fas fa-bell"></i> @Model.UnreadCount
99
</a>
1010
</div>

src/EasyAbp.ProcessManagement.Web/Components/NotificationsToolbarItemWidget/NotificationsToolbarItemWidgetViewComponent.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
22
using EasyAbp.ProcessManagement.Web.Caches;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Mvc;
45
using Volo.Abp.AspNetCore.Mvc;
56
using Volo.Abp.AspNetCore.Mvc.UI.Widgets;
@@ -21,7 +22,17 @@ public NotificationsToolbarItemWidgetViewComponent(NotificationCountCache notifi
2122

2223
public virtual async Task<IViewComponentResult> InvokeAsync()
2324
{
24-
var notificationCount = await _notificationCountCache.GetOrAddAsync();
25+
int notificationCount;
26+
27+
if (HttpContext.Request.Query.TryGetValue("count", out var countValue) &&
28+
int.TryParse(countValue, out var parsedCount))
29+
{
30+
notificationCount = parsedCount;
31+
}
32+
else
33+
{
34+
notificationCount = await _notificationCountCache.GetOrAddAsync();
35+
}
2536

2637
return View("~/Components/NotificationsToolbarItemWidget/Default.cshtml",
2738
new NotificationsToolbarItemWidgetViewModel(notificationCount));

0 commit comments

Comments
 (0)