From: Alexander Ebert Date: Sun, 29 Jun 2014 10:17:13 +0000 (+0200) Subject: Using session keep alive to update notification count X-Git-Tag: 2.1.0_Alpha_1~648 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=7f737b83006540fd5f56fe89262939b02264e045;p=GitHub%2FWoltLab%2FWCF.git Using session keep alive to update notification count --- diff --git a/wcfsetup/install/files/js/WCF.User.js b/wcfsetup/install/files/js/WCF.User.js index d855778447..440210dd87 100644 --- a/wcfsetup/install/files/js/WCF.User.js +++ b/wcfsetup/install/files/js/WCF.User.js @@ -1277,6 +1277,8 @@ WCF.Notification.UserPanel = WCF.UserPanel.extend({ if (this._container.data('count')) { this._favico.badge(this._container.data('count')); } + + WCF.System.PushNotification.addCallback('userNotificationCount', $.proxy(this.updateUserNotificationCount, this)); }, /** @@ -1344,9 +1346,6 @@ WCF.Notification.UserPanel = WCF.UserPanel.extend({ switch (data.actionName) { case 'markAllAsConfirmed': $('.jsNotificationItem').remove(); - - // remove notification count - this._favico.badge(0); // fall through case 'getOutstandingNotifications': if (!data.returnValues || !data.returnValues.template) { @@ -1367,6 +1366,35 @@ WCF.Notification.UserPanel = WCF.UserPanel.extend({ }); break; } + }, + + /** + * @see WCF.UserPanel._updateBadge() + */ + _updateBadge: function(count) { + this._super(count); + + this._favico.badge(count); + }, + + /** + * Updates user notification count. + * + * @param integer count + */ + updateUserNotificationCount: function(count) { + // close dropdown + WCF.Dropdown.close('userNotifications'); + + // revert dropdown to initial state + var $dropdownMenu = WCF.Dropdown.getDropdownMenu('userNotifications'); + var $item = $dropdownMenu.find('.dropdownDivider:eq(0)'); + $item.prevAll().remove(); + $('
  • ' + WCF.Language.get('wcf.global.loading') + '
  • ').insertBefore($item); + this._didLoad = false; + + // update badge + this._updateBadge(count); } }); diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index 34afa84ab6..2593ac09ad 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -1245,8 +1245,8 @@ WCF.Dropdown = { return; } - this._dropdowns[containerID].removeClass('dropdownMenu'); - this._menus[containerID].removeClass('dropdownMenu'); + this._dropdowns[containerID].removeClass('dropdownOpen'); + this._menus[containerID].removeClass('dropdownOpen'); }, /** @@ -7603,12 +7603,55 @@ WCF.System.KeepAlive = Class.extend({ }, failure: function() { pe.stop(); }, showLoadingOverlay: false, + success: function(data) { + WCF.System.PushNotification.executeCallbacks(data); + }, suppressErrors: true }); }, (seconds * 1000)); } }); +/** + * System-wide handler for push notifications. + */ +WCF.System.PushNotification = { + /** + * list of callbacks groupped by type + * @var object + */ + _callbacks: { }, + + /** + * Adds a callback for a specific notification type. + * + * @param string type + * @param object callback + */ + addCallback: function(type, callback) { + if (this._callbacks[type] === undefined) { + this._callbacks[type] = [ ]; + } + + this._callbacks[type].push(callback); + }, + + /** + * Executes all registered callbacks by type. + * + * @param object data + */ + executeCallbacks: function(data) { + for (var $type in data.returnValues) { + if (this._callbacks[$type] !== undefined) { + for (var $i = 0; $i < this._callbacks[$type].length; $i++) { + this._callbacks[$type][$i](data.returnValues[$type]); + } + } + } + } +} + /** * Worker support for frontend based upon DatabaseObjectActions. * @@ -10103,12 +10146,7 @@ WCF.UserPanel = Class.extend({ $('' + data.returnValues.template).prependTo($dropdownMenu); // update badge - var $badge = this._container.find('.badge'); - if (!$badge.length) { - $badge = $('').appendTo(this._container.children('.dropdownToggle')); - $badge.before(' '); - } - $badge.html(data.returnValues.totalCount); + this._updateBadge(data.returnValues.totalCount); this._after($dropdownMenu); } @@ -10116,6 +10154,25 @@ WCF.UserPanel = Class.extend({ $('
  • ' + WCF.Language.get(this._noItems) + '
  • ').prependTo($dropdownMenu); // remove badge + this._updateBadge(0); + } + }, + + /** + * Updates badge count. + * + * @param integer count + */ + _updateBadge: function(count) { + if (count) { + var $badge = this._container.find('.badge'); + if (!$badge.length) { + $badge = $('').appendTo(this._container.children('.dropdownToggle')); + $badge.before(' '); + } + $badge.html(count); + } + else { this._container.find('.badge').remove(); } }, diff --git a/wcfsetup/install/files/lib/data/session/SessionAction.class.php b/wcfsetup/install/files/lib/data/session/SessionAction.class.php index e848995b97..f4b41daddd 100644 --- a/wcfsetup/install/files/lib/data/session/SessionAction.class.php +++ b/wcfsetup/install/files/lib/data/session/SessionAction.class.php @@ -1,7 +1,9 @@ + */ + public $keepAliveData = array(); + /** * Validates the 'keepAlive' action. */ @@ -33,7 +41,10 @@ class SessionAction extends AbstractDatabaseObjectAction { } /** - * Updates session's last activity time to prevent it from expiring. + * Updates session's last activity time to prevent it from expiring. In addition this method + * will return updated counters for notifications and 3rd party components. + * + * @return array */ public function keepAlive() { // ignore sessions created by this request @@ -41,6 +52,17 @@ class SessionAction extends AbstractDatabaseObjectAction { return; } + // update last activity time SessionHandler::getInstance()->keepAlive(); + + // update notification counts + $this->keepAliveData = array( + 'userNotificationCount' => UserNotificationHandler::getInstance()->getNotificationCount(true) + ); + + // notify 3rd party components + EventHandler::getInstance()->fireAction($this, 'keepAlive'); + + return $this->keepAliveData; } }