From 0e01cfc4fb8bdaa318632f502a04abec86d5ba7e Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 27 Mar 2015 13:37:39 -0700 Subject: [PATCH] GUAC-1138: Remove wrapper-specific filter code. Make FilterPattern, etc. generic. --- .../webapp/app/list/directives/guacFilter.js | 15 ++++-- .../webapp/app/list/types/FilterPattern.js | 46 +++++++++++++------ .../controllers/manageSessionsController.js | 11 +++++ .../app/manage/templates/manageSessions.html | 3 +- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/guacamole/src/main/webapp/app/list/directives/guacFilter.js b/guacamole/src/main/webapp/app/list/directives/guacFilter.js index e1b498f81..cbca76122 100644 --- a/guacamole/src/main/webapp/app/list/directives/guacFilter.js +++ b/guacamole/src/main/webapp/app/list/directives/guacFilter.js @@ -48,12 +48,21 @@ angular.module('list').directive('guacFilter', [function guacFilter() { placeholder : '&', /** - * An array objects to filter. A subset of this array will be + * An array of objects to filter. A subset of this array will be * exposed as filteredItems. * * @type Array */ - items : '&' + items : '&', + + /** + * An array of expressions to filter against for each object in the + * items array. These expressions must be Angular expressions + * which resolve to properties on the objects in the items array. + * + * @type String[] + */ + properties : '&' }, @@ -68,7 +77,7 @@ angular.module('list').directive('guacFilter', [function guacFilter() { * * @type FilterPattern */ - var filterPattern = new FilterPattern(); + var filterPattern = new FilterPattern($scope.properties()); /** * The filter search string to use to restrict the displayed items. diff --git a/guacamole/src/main/webapp/app/list/types/FilterPattern.js b/guacamole/src/main/webapp/app/list/types/FilterPattern.js index 722083eef..82fdfc27e 100644 --- a/guacamole/src/main/webapp/app/list/types/FilterPattern.js +++ b/guacamole/src/main/webapp/app/list/types/FilterPattern.js @@ -23,8 +23,8 @@ /** * A service for defining the FilterPattern class. */ -angular.module('list').factory('FilterPattern', [ - function defineFilterPattern() { +angular.module('list').factory('FilterPattern', ['$parse', + function defineFilterPattern($parse) { /** * Object which handles compilation of filtering predicates as used by @@ -32,8 +32,10 @@ angular.module('list').factory('FilterPattern', [ * specified search string. * * @constructor + * @param {String[]} expressions + * The Angular expressions whose values are to be filtered. */ - var FilterPattern = function FilterPattern() { + var FilterPattern = function FilterPattern(expressions) { /** * Reference to this instance. @@ -53,6 +55,20 @@ angular.module('list').factory('FilterPattern', [ return true; }; + /** + * Array of getters corresponding to the Angular expressions provided + * to the constructor of this class. The functions returns are those + * produced by the $parse service. + * + * @type Function[] + */ + var getters = []; + + // Parse all expressions + angular.forEach(expressions, function parseExpression(expression) { + getters.push($parse(expression)); + }); + /** * The current filtering predicate. * @@ -79,22 +95,24 @@ angular.module('list').factory('FilterPattern', [ // Convert to lower case for case insensitive matching pattern = pattern.toLowerCase(); - // TODONT: Return predicate specific to a type of object this class should know nothing about - filterPattern.predicate = function oddlySpecificPredicate(wrapper) { + // Return predicate which matches against the value of any getter in the getters array + filterPattern.predicate = function matchAny(object) { - // Check to see if the search string matches the connection name - if (wrapper.name.toLowerCase().indexOf(pattern) !== -1) - return true; + // For each defined getter + for (var i=0; i < getters.length; i++) { - // Check to see if the search string matches the username - if (wrapper.activeConnection.username.toLowerCase().indexOf(pattern) !== -1) - return true; + // Retrieve value of current getter + var value = getters[i](object); - // Check to see if the search string matches the remote host - if (wrapper.activeConnection.remoteHost.toLowerCase().indexOf(pattern) !== -1) - return true; + // If the value matches the pattern, the whole object matches + if (String(value).toLowerCase().indexOf(pattern) !== -1) + return true; + } + + // No matches found return false; + }; }; diff --git a/guacamole/src/main/webapp/app/manage/controllers/manageSessionsController.js b/guacamole/src/main/webapp/app/manage/controllers/manageSessionsController.js index ccd264072..ac56335d8 100644 --- a/guacamole/src/main/webapp/app/manage/controllers/manageSessionsController.js +++ b/guacamole/src/main/webapp/app/manage/controllers/manageSessionsController.js @@ -67,6 +67,17 @@ angular.module('manage').controller('manageSessionsController', ['$scope', '$inj 'name' ]); + /** + * Array of all wrapper properties that are filterable. + * + * @type String[] + */ + $scope.filteredWrapperProperties = [ + 'activeConnection.username', + 'activeConnection.remoteHost', + 'name' + ]; + /** * All active connections, if known, or null if active connections have not * yet been loaded. diff --git a/guacamole/src/main/webapp/app/manage/templates/manageSessions.html b/guacamole/src/main/webapp/app/manage/templates/manageSessions.html index 84d9fe6d2..41a49f784 100644 --- a/guacamole/src/main/webapp/app/manage/templates/manageSessions.html +++ b/guacamole/src/main/webapp/app/manage/templates/manageSessions.html @@ -38,7 +38,8 @@ THE SOFTWARE. + placeholder="'MANAGE_SESSION.FIELD_PLACEHOLDER_FILTER' | translate" + properties="filteredWrapperProperties">