Merge pull request #115 from glyptodon/notification-service

GUAC-1120: Move notifications into own service.
This commit is contained in:
James Muehlner
2015-03-12 18:17:50 -07:00
13 changed files with 219 additions and 145 deletions

View File

@@ -23,4 +23,13 @@
/**
* The module for code used to connect to a connection or balancing group.
*/
angular.module('client', ['auth', 'element', 'history', 'osk', 'rest', 'textInput', 'touch']);
angular.module('client', [
'auth',
'element',
'history',
'notification',
'osk',
'rest',
'textInput',
'touch'
]);

View File

@@ -33,6 +33,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
// Required services
var $location = $injector.get('$location');
var guacClientManager = $injector.get('guacClientManager');
var guacNotification = $injector.get('guacNotification');
/**
* The minimum number of pixels a drag gesture must move to result in the
@@ -142,7 +143,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
name : "CLIENT.ACTION_RECONNECT",
callback : function reconnectCallback() {
$scope.client = guacClientManager.replaceManagedClient(uniqueId, $routeParams.params);
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -407,7 +408,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
$scope.$watch('client.clientState.connectionState', function clientStateChanged(connectionState) {
// Hide any existing status
$scope.showStatus(false);
guacNotification.showStatus(false);
// Do not display status if status not known
if (!connectionState)
@@ -419,7 +420,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
// Connecting
if (connectionState === ManagedClientState.ConnectionState.CONNECTING
|| connectionState === ManagedClientState.ConnectionState.WAITING) {
$scope.showStatus({
guacNotification.showStatus({
title: "CLIENT.DIALOG_HEADER_CONNECTING",
text: "CLIENT.TEXT_CLIENT_STATUS_" + connectionState.toUpperCase()
});
@@ -435,7 +436,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
var countdown = (status in CLIENT_AUTO_RECONNECT) ? RECONNECT_COUNTDOWN : null;
// Show error status
$scope.showStatus({
guacNotification.showStatus({
className: "error",
title: "CLIENT.DIALOG_HEADER_CONNECTION_ERROR",
text: "CLIENT.ERROR_CLIENT_" + errorName,
@@ -455,7 +456,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
var countdown = (status in TUNNEL_AUTO_RECONNECT) ? RECONNECT_COUNTDOWN : null;
// Show error status
$scope.showStatus({
guacNotification.showStatus({
className: "error",
title: "CLIENT.DIALOG_HEADER_CONNECTION_ERROR",
text: "CLIENT.ERROR_TUNNEL_" + errorName,
@@ -467,7 +468,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
// Disconnected
else if (connectionState === ManagedClientState.ConnectionState.DISCONNECTED) {
$scope.showStatus({
guacNotification.showStatus({
title: "CLIENT.DIALOG_HEADER_DISCONNECTED",
text: "CLIENT.TEXT_CLIENT_STATUS_" + connectionState.toUpperCase(),
actions: [ NAVIGATE_HOME_ACTION, RECONNECT_ACTION ]
@@ -476,7 +477,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams
// Hide status for all other states
else
$scope.showStatus(false);
guacNotification.showStatus(false);
});

View File

@@ -27,23 +27,14 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
function indexController($scope, $injector) {
// Required services
var $document = $injector.get("$document");
var $window = $injector.get("$window");
var $document = $injector.get('$document');
var $window = $injector.get('$window');
var guacNotification = $injector.get('guacNotification');
/**
* The current status notification, or false if no status is currently
* shown.
*
* @type Notification|Boolean
* The notification service.
*/
$scope.status = false;
/**
* All currently-visible notifications.
*
* @type Notification[]
*/
$scope.notifications = [];
$scope.guacNotification = guacNotification;
/**
* Basic page-level information.
@@ -66,91 +57,6 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
};
/**
* 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
* notification is currently shown, no further statuses will be shown
* until the current status is hidden.
*
* @param {Notification|Boolean|Object} status
* The status notification to show.
*
* @example
*
* // To show a status message with actions
* $scope.showStatus({
* 'title' : 'Disconnected',
* 'text' : 'You have been disconnected!',
* 'actions' : {
* 'name' : 'reconnect',
* 'callback' : function () {
* // Reconnection code goes here
* }
* }
* });
*
* // To hide the status message
* $scope.showStatus(false);
*/
$scope.showStatus = function showStatus(status) {
if (!$scope.status || !status)
$scope.status = status;
};
/**
* Adds a notification to the the list of notifications shown.
*
* @param {Notification|Object} notification The notification to add.
*
* @returns {Number}
* A unique ID for the notification that's just been added.
*
* @example
*
* var id = $scope.addNotification({
* 'title' : 'Download',
* 'text' : 'You have a file ready for download!',
* 'actions' : {
* 'name' : 'download',
* 'callback' : function () {
* // download the file and remove the notification here
* }
* }
* });
*/
$scope.addNotification = function addNotification(notification) {
var id = ++notificationUniqueID;
$scope.notifications.push({
notification : notification,
id : id
});
return id;
};
/**
* Remove a notification by unique ID.
*
* @param {type} id The unique ID of the notification to remove. This ID is
* retrieved from the initial call to addNotification.
*/
$scope.removeNotification = function removeNotification(id) {
for(var i = 0; i < $scope.notifications.length; i++) {
if($scope.notifications[i].id === id) {
$scope.notifications.splice(i, 1);
return;
}
}
};
// Create event listeners at the global level
var keyboard = new Guacamole.Keyboard($document[0]);
@@ -197,9 +103,6 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
// Set body CSS class
$scope.page.bodyClassName = current.$$route.bodyClassName || '';
// Hide any status dialog
$scope.showStatus(false);
});
}]);

View File

@@ -23,5 +23,15 @@
/**
* The module for the root of the application.
*/
angular.module('index', ['ngRoute', 'ngTouch', 'pascalprecht.translate',
'auth', 'home', 'manage', 'login', 'client', 'notification', 'rest']);
angular.module('index', [
'auth',
'client',
'home',
'login',
'manage',
'ngRoute',
'ngTouch',
'notification',
'pascalprecht.translate',
'rest'
]);

View File

@@ -36,6 +36,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
var $location = $injector.get('$location');
var $routeParams = $injector.get('$routeParams');
var authenticationService = $injector.get('authenticationService');
var guacNotification = $injector.get('guacNotification');
var connectionService = $injector.get('connectionService');
var connectionGroupService = $injector.get('connectionGroupService');
var permissionService = $injector.get('permissionService');
@@ -50,7 +51,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
name : "MANAGE_CONNECTION.ACTION_ACKNOWLEDGE",
// Handle action
callback : function acknowledgeCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -317,7 +318,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
// Notify of any errors
.error(function connectionSaveFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_CONNECTION.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -337,7 +338,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
// Handle action
callback : function deleteCallback() {
deleteConnectionImmediately();
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -349,7 +350,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
name : "MANAGE_CONNECTION.ACTION_CANCEL",
// Handle action
callback : function cancelCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -367,7 +368,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
// Notify of any errors
.error(function connectionDeletionFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_CONNECTION.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -384,7 +385,7 @@ angular.module('manage').controller('manageConnectionController', ['$scope', '$i
$scope.deleteConnection = function deleteConnection() {
// Confirm deletion request
$scope.showStatus({
guacNotification.showStatus({
'title' : 'MANAGE_CONNECTION.DIALOG_HEADER_CONFIRM_DELETE',
'text' : 'MANAGE_CONNECTION.TEXT_CONFIRM_DELETE',
'actions' : [ DELETE_ACTION, CANCEL_ACTION]

View File

@@ -35,6 +35,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
var $routeParams = $injector.get('$routeParams');
var authenticationService = $injector.get('authenticationService');
var connectionGroupService = $injector.get('connectionGroupService');
var guacNotification = $injector.get('guacNotification');
var permissionService = $injector.get('permissionService');
/**
@@ -45,7 +46,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
name : "MANAGE_CONNECTION_GROUP.ACTION_ACKNOWLEDGE",
// Handle action
callback : function acknowledgeCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -187,7 +188,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
// Notify of any errors
.error(function connectionGroupSaveFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_CONNECTION_GROUP.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -207,7 +208,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
// Handle action
callback : function deleteCallback() {
deleteConnectionGroupImmediately();
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -219,7 +220,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
name : "MANAGE_CONNECTION_GROUP.ACTION_CANCEL",
// Handle action
callback : function cancelCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -237,7 +238,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
// Notify of any errors
.error(function connectionGroupDeletionFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_CONNECTION_GROUP.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -254,7 +255,7 @@ angular.module('manage').controller('manageConnectionGroupController', ['$scope'
$scope.deleteConnectionGroup = function deleteConnectionGroup() {
// Confirm deletion request
$scope.showStatus({
guacNotification.showStatus({
'title' : 'MANAGE_CONNECTION_GROUP.DIALOG_HEADER_CONFIRM_DELETE',
'text' : 'MANAGE_CONNECTION_GROUP.TEXT_CONFIRM_DELETE',
'actions' : [ DELETE_ACTION, CANCEL_ACTION]

View File

@@ -35,6 +35,7 @@ angular.module('manage').controller('manageController', ['$scope', '$injector',
var $location = $injector.get('$location');
var authenticationService = $injector.get('authenticationService');
var connectionGroupService = $injector.get('connectionGroupService');
var guacNotification = $injector.get('guacNotification');
var permissionService = $injector.get('permissionService');
var userService = $injector.get('userService');
@@ -49,7 +50,7 @@ angular.module('manage').controller('manageController', ['$scope', '$injector',
name : "MANAGE.ACTION_ACKNOWLEDGE",
// Handle action
callback : function acknowledgeCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -232,7 +233,7 @@ angular.module('manage').controller('manageController', ['$scope', '$injector',
// Notify of any errors
.error(function userCreationFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE.DIALOG_HEADER_ERROR',
'text' : error.message,

View File

@@ -36,6 +36,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
var $routeParams = $injector.get('$routeParams');
var authenticationService = $injector.get('authenticationService');
var connectionGroupService = $injector.get('connectionGroupService');
var guacNotification = $injector.get('guacNotification');
var userService = $injector.get('userService');
var permissionService = $injector.get('permissionService');
@@ -47,7 +48,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
name : "MANAGE_USER.ACTION_ACKNOWLEDGE",
// Handle action
callback : function acknowledgeCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -409,7 +410,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
// Verify passwords match
if ($scope.passwordMatch !== $scope.user.password) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_USER.DIALOG_HEADER_ERROR',
'text' : 'MANAGE_USER.ERROR_PASSWORD_MISMATCH',
@@ -430,7 +431,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
// Notify of any errors
.error(function userPermissionsPatchFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_USER.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -442,7 +443,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
// Notify of any errors
.error(function userSaveFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_USER.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -462,7 +463,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
// Handle action
callback : function deleteCallback() {
deleteUserImmediately();
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -474,7 +475,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
name : "MANAGE_USER.ACTION_CANCEL",
// Handle action
callback : function cancelCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -492,7 +493,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
// Notify of any errors
.error(function userDeletionFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
'className' : 'error',
'title' : 'MANAGE_USER.DIALOG_HEADER_ERROR',
'text' : error.message,
@@ -509,7 +510,7 @@ angular.module('manage').controller('manageUserController', ['$scope', '$injecto
$scope.deleteUser = function deleteUser() {
// Confirm deletion request
$scope.showStatus({
guacNotification.showStatus({
'title' : 'MANAGE_USER.DIALOG_HEADER_CONFIRM_DELETE',
'text' : 'MANAGE_USER.TEXT_CONFIRM_DELETE',
'actions' : [ DELETE_ACTION, CANCEL_ACTION]

View File

@@ -23,5 +23,11 @@
/**
* The module for the administration functionality.
*/
angular.module('manage', ['groupList', 'locale', 'pager', 'rest', 'userMenu']);
angular.module('manage', [
'groupList',
'locale',
'notification',
'pager',
'rest',
'userMenu'
]);

View File

@@ -0,0 +1,140 @@
/*
* Copyright (C) 2015 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Service for displaying notifications and modal status dialogs.
*/
angular.module('notification').factory('guacNotification', ['$rootScope',
function guacNotification($rootScope) {
var service = {};
/**
* The current status notification, or false if no status is currently
* shown.
*
* @type Notification|Boolean
*/
service.status = false;
/**
* 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
* notification is currently shown, no further statuses will be shown
* until the current status is hidden.
*
* @param {Notification|Boolean|Object} status
* The status notification to show.
*
* @example
*
* // To show a status message with actions
* guacNotification.showStatus({
* 'title' : 'Disconnected',
* 'text' : 'You have been disconnected!',
* 'actions' : {
* 'name' : 'reconnect',
* 'callback' : function () {
* // Reconnection code goes here
* }
* }
* });
*
* // To hide the status message
* guacNotification.showStatus(false);
*/
service.showStatus = function showStatus(status) {
if (!service.status || !status)
service.status = status;
};
/**
* Adds a notification to the the list of notifications shown.
*
* @param {Notification|Object} notification
* The notification to add.
*
* @returns {Number}
* A unique ID for the notification that's just been added.
*
* @example
*
* var id = guacNotification.addNotification({
* 'title' : 'Download',
* 'text' : 'You have a file ready for download!',
* 'actions' : {
* 'name' : 'download',
* 'callback' : function () {
* // download the file and remove the notification here
* }
* }
* });
*/
service.addNotification = function addNotification(notification) {
var id = ++notificationUniqueID;
service.notifications.push({
notification : notification,
id : id
});
return id;
};
/**
* Remove a notification by unique ID.
*
* @param {Number} id
* The unique ID of the notification to remove. This ID is retrieved
* from the initial call to addNotification.
*/
service.removeNotification = function removeNotification(id) {
for (var i = 0; i < service.notifications.length; i++) {
if (service.notifications[i].id === id) {
service.notifications.splice(i, 1);
return;
}
}
};
// Hide status upon navigation
$rootScope.$on('$routeChangeSuccess', function() {
service.showStatus(false);
});
return service;
}]);

View File

@@ -52,6 +52,7 @@ angular.module('userMenu').directive('guacUserMenu', [function guacUserMenu() {
var $document = $injector.get("$document");
var $location = $injector.get("$location");
var authenticationService = $injector.get("authenticationService");
var guacNotification = $injector.get('guacNotification');
var userService = $injector.get("userService");
/**
@@ -62,7 +63,7 @@ angular.module('userMenu').directive('guacUserMenu', [function guacUserMenu() {
name : "USER_MENU.ACTION_ACKNOWLEDGE",
// Handle action
callback : function acknowledgeCallback() {
$scope.showStatus(false);
guacNotification.showStatus(false);
}
};
@@ -234,7 +235,7 @@ angular.module('userMenu').directive('guacUserMenu', [function guacUserMenu() {
// Verify passwords match
if ($scope.newPasswordMatch !== $scope.newPassword) {
$scope.showStatus({
guacNotification.showStatus({
className : 'error',
title : 'USER_MENU.DIALOG_HEADER_ERROR',
text : 'USER_MENU.ERROR_PASSWORD_MISMATCH',
@@ -245,7 +246,7 @@ angular.module('userMenu').directive('guacUserMenu', [function guacUserMenu() {
// Verify that the new password is not blank
if (!$scope.newPassword) {
$scope.showStatus({
guacNotification.showStatus({
className : 'error',
title : 'USER_MENU.DIALOG_HEADER_ERROR',
text : 'USER_MENU.ERROR_PASSWORD_BLANK',
@@ -262,7 +263,7 @@ angular.module('userMenu').directive('guacUserMenu', [function guacUserMenu() {
$scope.closePasswordUpdate();
// Indicate that the password has been changed
$scope.showStatus({
guacNotification.showStatus({
text : 'USER_MENU.PASSWORD_CHANGED',
actions : [ ACKNOWLEDGE_ACTION ]
});
@@ -270,7 +271,7 @@ angular.module('userMenu').directive('guacUserMenu', [function guacUserMenu() {
// Notify of any errors
.error(function passwordUpdateFailed(error) {
$scope.showStatus({
guacNotification.showStatus({
className : 'error',
title : 'USER_MENU.DIALOG_HEADER_ERROR',
'text' : error.message,

View File

@@ -24,4 +24,4 @@
* Module for displaying a user-oriented menu, containing the current username
* and options for navigation, changing the user's password, logging out, etc.
*/
angular.module('userMenu', []);
angular.module('userMenu', ['notification']);

View File

@@ -35,9 +35,9 @@ THE SOFTWARE.
<body ng-class="page.bodyClassName">
<!-- Global status/error dialog -->
<div ng-class="{shown: status}" class="status-outer">
<div ng-class="{shown: guacNotification.status}" class="status-outer">
<div class="status-middle">
<guac-notification notification="status"></guac-notification>
<guac-notification notification="guacNotification.status"></guac-notification>
</div>
</div>
@@ -46,7 +46,7 @@ THE SOFTWARE.
<!-- Notification area -->
<div id="notificationArea">
<div ng-repeat="wrapper in notifications">
<div ng-repeat="wrapper in guacNotification.notifications">
<guac-notification notification="wrapper.notification"></guac-notification>
</div>
</div>