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'; '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) { function formatMoment(unix) {
return unix ? moment.unix(unix).fromNow() : 'N/A'; return unix ? moment.unix(unix).fromNow() : 'N/A';
} }
@ -41,9 +55,11 @@ angular.module('ffffngAdmin').config(function(NgAdminConfigurationProvider, Cons
.batchActions([]) .batchActions([])
.exportFields([]) .exportFields([])
.fields([ .fields([
nga.field('hostname').cssClasses(nodeClasses),
nga.field('nickname').cssClasses(nodeClasses),
nga.field('email').cssClasses(nodeClasses),
nga.field('token').cssClasses(nodeClasses), nga.field('token').cssClasses(nodeClasses),
nga.field('mac').cssClasses(nodeClasses), nga.field('mac').cssClasses(nodeClasses),
nga.field('hostname').cssClasses(nodeClasses),
nga.field('key').label('VPN').cssClasses(nodeClasses).template(function (node) { nga.field('key').label('VPN').cssClasses(nodeClasses).template(function (node) {
return node.values.key return node.values.key
? '<i class="fa fa-lock vpn-key-set" aria-hidden="true" title="VPN key set"></i>' ? '<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([ .listActions([
'edit', 'edit',
'delete' 'delete'

View file

@ -100,16 +100,21 @@ angular.module('ffffng').factory('NodeResource', function (
return Resources.error(res, err); return Resources.error(res, err);
} }
// TODO: Filter return NodeService.getAllNodes(function (err, nodes) {
return NodeService.getAllNodes(function (err, nodes, total) {
if (err) { if (err) {
return Resources.error(res, err); return Resources.error(res, err);
} }
var sortedNodes = Resources.sort( var filteredNodes = Resources.filter(
nodes, 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 restParams
); );
var pageNodes = Resources.getPageEntities(sortedNodes, restParams); var pageNodes = Resources.getPageEntities(sortedNodes, restParams);

View file

@ -372,7 +372,6 @@ angular.module('ffffng')
getAllNodes: function (callback) { getAllNodes: function (callback) {
var files = findNodeFiles({}); var files = findNodeFiles({});
var total = files.length;
async.mapLimit( async.mapLimit(
files, files,
@ -384,7 +383,7 @@ angular.module('ffffng')
return callback({data: 'Internal error.', type: ErrorTypes.internalError}); 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); 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) { 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) { if (!sortField) {
return entities; return entities;
} }

View file

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