GUAC-1161: Store notification state in session-local storage.

This commit is contained in:
Michael Jumper
2015-04-23 13:57:44 -07:00
parent afd9cd8032
commit 592ce3b8d3
3 changed files with 63 additions and 32 deletions

View File

@@ -23,4 +23,6 @@
/** /**
* The module for code used to display arbitrary notifications. * The module for code used to display arbitrary notifications.
*/ */
angular.module('notification', []); angular.module('notification', [
'storage'
]);

View File

@@ -23,34 +23,49 @@
/** /**
* Service for displaying notifications and modal status dialogs. * Service for displaying notifications and modal status dialogs.
*/ */
angular.module('notification').factory('guacNotification', ['$rootScope', angular.module('notification').factory('guacNotification', ['$injector',
function guacNotification($rootScope) { function guacNotification($injector) {
// Required services
var $rootScope = $injector.get('$rootScope');
var sessionStorageFactory = $injector.get('sessionStorageFactory');
var service = {}; var service = {};
/** /**
* The current status notification, or false if no status is currently * Getter/setter which retrieves or sets the current status notification,
* shown. * which may simply be false if no status is currently shown.
*
* @type Function
*/
var storedStatus = sessionStorageFactory.create(false);
/**
* Getter/setter which retrieves or sets an array of all currently-visible
* notifications.
*
* @type Function
*/
var storedNotifications = sessionStorageFactory.create([]);
/**
* Getter/setter which retrieves or sets the ID of the most recently shown
* notification, or 0 if no notifications have yet been shown.
*
* @type Function
*/
var storedNotificationUniqueID = sessionStorageFactory.create(0);
/**
* Retrieves the current status notification, which may simply be false if
* no status is currently shown.
* *
* @type Notification|Boolean * @type Notification|Boolean
*/ */
service.status = false; service.getStatus = function getStatus() {
return storedStatus();
};
/**
* All currently-visible notifications.
*
* @type Notification[]
*/
service.notifications = [];
/**
* The ID of the most recently shown notification, or 0 if no notifications
* have yet been shown.
*
* @type Number
*/
var notificationUniqueID = 0;
/** /**
* Shows or hides the given notification as a modal status. If a status * Shows or hides the given notification as a modal status. If a status
* notification is currently shown, no further statuses will be shown * notification is currently shown, no further statuses will be shown
@@ -77,10 +92,20 @@ angular.module('notification').factory('guacNotification', ['$rootScope',
* guacNotification.showStatus(false); * guacNotification.showStatus(false);
*/ */
service.showStatus = function showStatus(status) { service.showStatus = function showStatus(status) {
if (!service.status || !status) if (!storedStatus() || !status)
service.status = status; storedStatus(status);
}; };
/**
* Returns an array of all currently-visible notifications.
*
* @returns {Notification[]}
* An array of all currently-visible notifications.
*/
service.getNotifications = function getNotifications() {
return storedNotifications();
};
/** /**
* Adds a notification to the the list of notifications shown. * Adds a notification to the the list of notifications shown.
* *
@@ -104,9 +129,9 @@ angular.module('notification').factory('guacNotification', ['$rootScope',
* }); * });
*/ */
service.addNotification = function addNotification(notification) { service.addNotification = function addNotification(notification) {
var id = ++notificationUniqueID; var id = storedNotificationUniqueID(storedNotificationUniqueID() + 1);
service.notifications.push({ storedNotifications().push({
notification : notification, notification : notification,
id : id id : id
}); });
@@ -122,12 +147,16 @@ angular.module('notification').factory('guacNotification', ['$rootScope',
* from the initial call to addNotification. * from the initial call to addNotification.
*/ */
service.removeNotification = function removeNotification(id) { service.removeNotification = function removeNotification(id) {
for (var i = 0; i < service.notifications.length; i++) {
if (service.notifications[i].id === id) { var notifications = storedNotifications();
service.notifications.splice(i, 1);
for (var i = 0; i < notifications.length; i++) {
if (notifications[i].id === id) {
notifications.splice(i, 1);
return; return;
} }
} }
}; };
// Hide status upon navigation // Hide status upon navigation

View File

@@ -38,9 +38,9 @@ THE SOFTWARE.
<div ng-if="!expectedCredentials"> <div ng-if="!expectedCredentials">
<!-- Global status/error dialog --> <!-- Global status/error dialog -->
<div ng-class="{shown: guacNotification.status}" class="status-outer"> <div ng-class="{shown: guacNotification.getStatus()}" class="status-outer">
<div class="status-middle"> <div class="status-middle">
<guac-notification notification="guacNotification.status"></guac-notification> <guac-notification notification="guacNotification.getStatus()"></guac-notification>
</div> </div>
</div> </div>
@@ -49,7 +49,7 @@ THE SOFTWARE.
<!-- Notification area --> <!-- Notification area -->
<div id="notificationArea"> <div id="notificationArea">
<div ng-repeat="wrapper in guacNotification.notifications"> <div ng-repeat="wrapper in guacNotification.getNotifications()">
<guac-notification notification="wrapper.notification"></guac-notification> <guac-notification notification="wrapper.notification"></guac-notification>
</div> </div>
</div> </div>