Merge pull request #126 from glyptodon/generic-filter

GUAC-1138: Remove wrapper-specific filter code.
This commit is contained in:
James Muehlner
2015-03-27 13:57:19 -07:00
4 changed files with 57 additions and 18 deletions

View File

@@ -48,12 +48,21 @@ angular.module('list').directive('guacFilter', [function guacFilter() {
placeholder : '&', 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. * exposed as filteredItems.
* *
* @type Array * @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 * @type FilterPattern
*/ */
var filterPattern = new FilterPattern(); var filterPattern = new FilterPattern($scope.properties());
/** /**
* The filter search string to use to restrict the displayed items. * The filter search string to use to restrict the displayed items.

View File

@@ -23,8 +23,8 @@
/** /**
* A service for defining the FilterPattern class. * A service for defining the FilterPattern class.
*/ */
angular.module('list').factory('FilterPattern', [ angular.module('list').factory('FilterPattern', ['$parse',
function defineFilterPattern() { function defineFilterPattern($parse) {
/** /**
* Object which handles compilation of filtering predicates as used by * Object which handles compilation of filtering predicates as used by
@@ -32,8 +32,10 @@ angular.module('list').factory('FilterPattern', [
* specified search string. * specified search string.
* *
* @constructor * @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. * Reference to this instance.
@@ -53,6 +55,20 @@ angular.module('list').factory('FilterPattern', [
return true; 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. * The current filtering predicate.
* *
@@ -79,22 +95,24 @@ angular.module('list').factory('FilterPattern', [
// Convert to lower case for case insensitive matching // Convert to lower case for case insensitive matching
pattern = pattern.toLowerCase(); pattern = pattern.toLowerCase();
// TODONT: Return predicate specific to a type of object this class should know nothing about // Return predicate which matches against the value of any getter in the getters array
filterPattern.predicate = function oddlySpecificPredicate(wrapper) { filterPattern.predicate = function matchAny(object) {
// Check to see if the search string matches the connection name // For each defined getter
if (wrapper.name.toLowerCase().indexOf(pattern) !== -1) for (var i=0; i < getters.length; i++) {
return true;
// Check to see if the search string matches the username // Retrieve value of current getter
if (wrapper.activeConnection.username.toLowerCase().indexOf(pattern) !== -1) var value = getters[i](object);
return true;
// Check to see if the search string matches the remote host // If the value matches the pattern, the whole object matches
if (wrapper.activeConnection.remoteHost.toLowerCase().indexOf(pattern) !== -1) if (String(value).toLowerCase().indexOf(pattern) !== -1)
return true; return true;
}
// No matches found
return false; return false;
}; };
}; };

View File

@@ -67,6 +67,17 @@ angular.module('manage').controller('manageSessionsController', ['$scope', '$inj
'name' '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 * All active connections, if known, or null if active connections have not
* yet been loaded. * yet been loaded.

View File

@@ -38,7 +38,8 @@ THE SOFTWARE.
<!-- Session filter --> <!-- Session filter -->
<guac-filter filtered-items="filteredWrappers" items="wrappers" <guac-filter filtered-items="filteredWrappers" items="wrappers"
placeholder="'MANAGE_SESSION.FIELD_PLACEHOLDER_FILTER' | translate"></guac-filter> placeholder="'MANAGE_SESSION.FIELD_PLACEHOLDER_FILTER' | translate"
properties="filteredWrapperProperties"></guac-filter>
<!-- List of current user sessions --> <!-- List of current user sessions -->
<table class="sorted session-list"> <table class="sorted session-list">