|
41 | 41 | } |
42 | 42 | }); |
43 | 43 | refreshBaseUiElements(); |
44 | | - refreshToolbarWidget(); |
| 44 | + currentBadgeCount = res.totalCount; |
| 45 | + refreshToolbarWidget(res.totalCount); |
45 | 46 | }).catch(function () { |
46 | 47 | // Silently ignore network errors for background notification fetch |
47 | 48 | }); |
|
106 | 107 | }); |
107 | 108 | newAlertNode.addEventListener('closed.bs.alert', function () { |
108 | 109 | refreshBaseUiElements(); |
109 | | - updateToolbarBadgeCount(Math.max(0, getToolbarBadgeCount() - 1)); |
| 110 | + refreshToolbarWidget(Math.max(0, getToolbarBadgeCount() - 1)); |
110 | 111 | }); |
111 | 112 | } |
112 | 113 |
|
|
146 | 147 | } |
147 | 148 |
|
148 | 149 | // Update the toolbar badge count directly to avoid stale server cache |
149 | | - updateToolbarBadgeCount(getToolbarBadgeCount() + 1); |
| 150 | + refreshToolbarWidget(getToolbarBadgeCount() + 1); |
150 | 151 | }); |
151 | 152 |
|
152 | 153 | connection.onreconnected(function () { |
|
185 | 186 | } |
186 | 187 | } |
187 | 188 |
|
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; |
193 | 197 | } |
194 | 198 |
|
195 | 199 | 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; |
200 | 204 | } |
201 | 205 |
|
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 | + |
203 | 212 | for (const randomId of (window.toolbarNotificationsWidgetAreaRandomIds || [])) { |
204 | 213 | var $wrapper = $('#ToolbarNotificationsWidgetArea-' + randomId); |
205 | 214 | var $widgets = $wrapper.find('.abp-widget-wrapper'); |
|
215 | 224 | var refreshUrl = $firstWidget.attr('data-refresh-url'); |
216 | 225 | if (!refreshUrl) continue; |
217 | 226 |
|
| 227 | + var url = typeof count === 'number' |
| 228 | + ? refreshUrl + (refreshUrl.indexOf('?') >= 0 ? '&' : '?') + 'count=' + count |
| 229 | + : refreshUrl; |
| 230 | + |
218 | 231 | $.ajax({ |
219 | | - url: refreshUrl, |
| 232 | + url: url, |
220 | 233 | type: 'GET', |
221 | 234 | dataType: 'html', |
222 | 235 | global: false |
223 | 236 | }).then(function (result) { |
224 | 237 | var $result = $(result); |
| 238 | + var serverCount = readBadgeCountFromDom($result); |
| 239 | + if (serverCount !== null) { |
| 240 | + currentBadgeCount = serverCount; |
| 241 | + } |
225 | 242 | for (const rid of (window.toolbarNotificationsWidgetAreaRandomIds || [])) { |
226 | 243 | var $w = $('#ToolbarNotificationsWidgetArea-' + rid); |
227 | 244 | var $ww = $w.find('.abp-widget-wrapper'); |
|
288 | 305 | removeAlert(alert) |
289 | 306 | }); |
290 | 307 | if (dismiss === 'DismissAll') { |
291 | | - updateToolbarBadgeCount(0); |
| 308 | + refreshToolbarWidget(0); |
292 | 309 | } else { |
293 | | - updateToolbarBadgeCount(Math.max(0, getToolbarBadgeCount() - dismissedCount)); |
| 310 | + refreshToolbarWidget(Math.max(0, getToolbarBadgeCount() - dismissedCount)); |
294 | 311 | } |
295 | 312 | }); |
296 | 313 | } |
|
0 commit comments