From 69374f081822b2bdce2253cc04a1dcdc9f62552a Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 7 Apr 2015 22:25:33 -0700 Subject: [PATCH] GUAC-1126: Move menu actions into own class. Make rendering of menu actions generic. --- .../app/navigation/directives/guacUserMenu.js | 59 +++++++++----- .../navigation/templates/guacUserMenu.html | 16 +--- .../webapp/app/navigation/types/MenuAction.js | 77 +++++++++++++++++++ 3 files changed, 119 insertions(+), 33 deletions(-) create mode 100644 guacamole/src/main/webapp/app/navigation/types/MenuAction.js diff --git a/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js b/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js index c9c09f7d2..d5fd0a901 100644 --- a/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js +++ b/guacamole/src/main/webapp/app/navigation/directives/guacUserMenu.js @@ -73,15 +73,6 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu() */ var document = $document[0]; - /** - * Whether the current user has sufficient permissions to change - * his/her own password. If permissions have not yet been loaded, - * this will be null. - * - * @type Boolean - */ - $scope.canChangePassword = null; - /** * Whether the password edit dialog should be shown. * @@ -125,18 +116,6 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu() */ $scope.pages = null; - // Retrieve current permissions - permissionService.getPermissions(authenticationService.getCurrentUserID()) - .success(function permissionsRetrieved(permissions) { - - // Check whether the current user can change their own password - $scope.canChangePassword = PermissionSet.hasUserPermission( - permissions, PermissionSet.ObjectPermissionType.UPDATE, - authenticationService.getCurrentUserID() - ); - - }); - // Retrieve the main pages from the user page service userPageService.getMainPages() .then(function retrievedMainPages(pages) { @@ -258,6 +237,44 @@ angular.module('navigation').directive('guacUserMenu', [function guacUserMenu() }); }; + /** + * Action which logs out the current user, redirecting them to back + * to the login screen after logout completes. + */ + var LOGOUT_ACTION = { + name : 'USER_MENU.ACTION_LOGOUT', + className : 'logout', + callback : $scope.logout + }; + + /** + * Action which shows the password update dialog. + */ + var CHANGE_PASSWORD_ACTION = { + name : 'USER_MENU.ACTION_CHANGE_PASSWORD', + className : 'change-password', + callback : $scope.showPasswordUpdate + }; + + /** + * All available actions for the current user. + */ + $scope.actions = [ LOGOUT_ACTION ]; + + // Retrieve current permissions + permissionService.getPermissions(authenticationService.getCurrentUserID()) + .success(function permissionsRetrieved(permissions) { + + // Add action for changing password if permission is granted + if (PermissionSet.hasUserPermission(permissions, + PermissionSet.ObjectPermissionType.UPDATE, + authenticationService.getCurrentUserID())) + $scope.actions.unshift(CHANGE_PASSWORD_ACTION); + + + }); + + // Close menu when use clicks anywhere else document.body.addEventListener('click', function clickOutsideMenu() { $scope.$apply(function closeMenu() { diff --git a/guacamole/src/main/webapp/app/navigation/templates/guacUserMenu.html b/guacamole/src/main/webapp/app/navigation/templates/guacUserMenu.html index 3c00345ae..fa287ac71 100644 --- a/guacamole/src/main/webapp/app/navigation/templates/guacUserMenu.html +++ b/guacamole/src/main/webapp/app/navigation/templates/guacUserMenu.html @@ -36,18 +36,10 @@ - -
  • - - {{'USER_MENU.ACTION_CHANGE_PASSWORD' | translate}} - -
  • - - -
  • - - {{'USER_MENU.ACTION_LOGOUT' | translate}} + +
  • + + {{action.name | translate}}
  • diff --git a/guacamole/src/main/webapp/app/navigation/types/MenuAction.js b/guacamole/src/main/webapp/app/navigation/types/MenuAction.js new file mode 100644 index 000000000..eb8c7b72b --- /dev/null +++ b/guacamole/src/main/webapp/app/navigation/types/MenuAction.js @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2014 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. + */ + +/** + * Provides the MenuAction class definition. + */ +angular.module('navigation').factory('MenuAction', [function defineMenuAction() { + + /** + * Creates a new MenuAction, which pairs an arbitrary callback with + * an action name. The name of this action will ultimately be presented to + * the user when the user when this action's associated menu is open. + * + * @constructor + * @param {String} name + * The name of this action. + * + * @param {Function} callback + * The callback to call when the user elects to perform this action. + * + * @param {String} className + * The CSS class to associate with this action, if any. + */ + var MenuAction = function MenuAction(name, callback, className) { + + /** + * Reference to this MenuAction. + * + * @type MenuAction + */ + var action = this; + + /** + * The CSS class associated with this action. + * + * @type String + */ + this.className = className; + + /** + * The name of this action. + * + * @type String + */ + this.name = name; + + /** + * The callback to call when this action is performed. + * + * @type Function + */ + this.callback = callback; + + }; + + return MenuAction; + +}]);