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 : '&',
/**
* 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.

View File

@@ -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;
};
};

View File

@@ -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.

View File

@@ -38,7 +38,8 @@ THE SOFTWARE.
<!-- Session filter -->
<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 -->
<table class="sorted session-list">