Filtering for nodes

This commit is contained in:
baldo 2016-06-07 12:39:52 +02:00
parent 954b7a3920
commit 122eadc0de
5 changed files with 69 additions and 10 deletions

View file

@ -1,6 +1,20 @@
'use strict';
angular.module('ffffngAdmin').config(function(NgAdminConfigurationProvider, Constraints) {
angular.module('ffffngAdmin').config(function(NgAdminConfigurationProvider, RestangularProvider, Constraints) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) {
if (operation === 'getList') {
if (params._filters) {
// flatten filter query params
for (var filter in params._filters) {
params[filter] = params._filters[filter];
}
delete params._filters;
}
}
return { params: params };
});
function formatMoment(unix) {
return unix ? moment.unix(unix).fromNow() : 'N/A';
}
@ -41,9 +55,11 @@ angular.module('ffffngAdmin').config(function(NgAdminConfigurationProvider, Cons
.batchActions([])
.exportFields([])
.fields([
nga.field('hostname').cssClasses(nodeClasses),
nga.field('nickname').cssClasses(nodeClasses),
nga.field('email').cssClasses(nodeClasses),
nga.field('token').cssClasses(nodeClasses),
nga.field('mac').cssClasses(nodeClasses),
nga.field('hostname').cssClasses(nodeClasses),
nga.field('key').label('VPN').cssClasses(nodeClasses).template(function (node) {
return node.values.key
? '<i class="fa fa-lock vpn-key-set" aria-hidden="true" title="VPN key set"></i>'
@ -67,6 +83,15 @@ angular.module('ffffngAdmin').config(function(NgAdminConfigurationProvider, Cons
}
})
])
.filters([
nga.field('q')
.label('')
.pinned(true)
.template(
'<div class="input-group">' +
'<input type="text" ng-model="value" placeholder="Search" class="form-control"></input>' +
'<span class="input-group-addon"><i class="fa fa-search"></i></span></div>'),
])
.listActions([
'edit',
'delete'

View file

@ -100,16 +100,21 @@ angular.module('ffffng').factory('NodeResource', function (
return Resources.error(res, err);
}
// TODO: Filter
return NodeService.getAllNodes(function (err, nodes, total) {
return NodeService.getAllNodes(function (err, nodes) {
if (err) {
return Resources.error(res, err);
}
var sortedNodes = Resources.sort(
var filteredNodes = Resources.filter(
nodes,
['token', 'mac', 'hostname', 'key', 'coords', 'monitoringState'],
['hostname', 'nickname', 'email', 'token', 'mac', 'key'],
restParams
);
var total = filteredNodes.length;
var sortedNodes = Resources.sort(
filteredNodes,
['hostname', 'nickname', 'email', 'token', 'mac', 'key', 'coords', 'monitoringState'],
restParams
);
var pageNodes = Resources.getPageEntities(sortedNodes, restParams);

View file

@ -372,7 +372,6 @@ angular.module('ffffng')
getAllNodes: function (callback) {
var files = findNodeFiles({});
var total = files.length;
async.mapLimit(
files,
@ -384,7 +383,7 @@ angular.module('ffffng')
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
}
return callback(null, nodes, total);
return callback(null, nodes);
}
);
},

View file

@ -36,8 +36,33 @@ angular.module('ffffng').factory('Resources', function (_, Constraints, Validato
callback(null, restParams);
},
filter: function (entities, allowedFilterFields, restParams) {
var query = restParams.q;
if (!query) {
return entities;
}
query = _.toLower(query.trim());
return _.filter(entities, function (entity) {
return _.some(allowedFilterFields, function (field) {
var value = entity[field];
if (!_.isString(value) || _.isEmpty(value)) {
return false;
}
value = _.toLower(value);
if (field === 'mac') {
return _.includes(value.replace(/:/g, ''), query.replace(/:/g, ''));
}
return _.includes(value, query);
})
});
},
sort: function (entities, allowedSortFields, restParams) {
var sortField = _.indexOf(allowedSortFields, restParams._sortField) >= 0 ? restParams._sortField : undefined;
var sortField = _.includes(allowedSortFields, restParams._sortField) ? restParams._sortField : undefined;
if (!sortField) {
return entities;
}

View file

@ -72,6 +72,11 @@ angular.module('ffffng').constant('Constraints', {
type: 'string',
regex: /^[a-zA-Z0-9_]{1,32}$/,
optional: true
},
q: {
type: 'string',
regex: /^[äöüß a-z0-9!#$%&@:.'*+/=?^_`{|}~-]{1,64}$/i,
optional: true
}
}
}