Commit 565ee8ef by Bence Dányi

firewall_gui: generic list controller for any list

parent 30a6c788
...@@ -2,10 +2,10 @@ var module = angular.module('firewall', []).config( ...@@ -2,10 +2,10 @@ var module = angular.module('firewall', []).config(
['$routeProvider', function($routeProvider) { ['$routeProvider', function($routeProvider) {
$routeProvider.when('/rules/', { $routeProvider.when('/rules/', {
templateUrl: '/static/partials/rule-list.html', templateUrl: '/static/partials/rule-list.html',
controller: RuleListCtrl controller: ListController('/firewall/rules/')
}).when('/hosts/', { }).when('/hosts/', {
templateUrl: '/static/partials/host-list.html', templateUrl: '/static/partials/host-list.html',
controller: HostListCtrl controller: ListController('/firewall/hosts/')
}). }).
otherwise({ otherwise({
redirectTo: '/rules/' redirectTo: '/rules/'
...@@ -30,7 +30,8 @@ function matchAnything(obj, query) { ...@@ -30,7 +30,8 @@ function matchAnything(obj, query) {
return false; return false;
} }
function RuleListCtrl($scope, $http) { function ListController(url) {
return function($scope, $http) {
$scope.page = 1; $scope.page = 1;
var rules = []; var rules = [];
var pageSize = 10; var pageSize = 10;
...@@ -61,49 +62,9 @@ function RuleListCtrl($scope, $http) { ...@@ -61,49 +62,9 @@ function RuleListCtrl($scope, $http) {
$scope.prevPage = function() { $scope.prevPage = function() {
$scope.page = Math.max($scope.page - 1, 1); $scope.page = Math.max($scope.page - 1, 1);
}; };
$http.get('/firewall/rules/').success(function success(data) { $http.get(url).success(function success(data) {
console.log('foo');
rules = data; rules = data;
$scope.pages = range(1, Math.ceil(data.length / pageSize)); $scope.pages = range(1, Math.ceil(data.length / pageSize));
console.log($scope.pages);
}); });
}
function HostListCtrl($scope, $http) {
$scope.page = 1;
var rules = [];
var pageSize = 10;
var itemCount = 0;
$scope.getPage = function() {
var res = [];
if ($scope.query) {
for (var i in rules) {
var rule = rules[i];
if (matchAnything(rule, $scope.query)) {
res.push(rule);
}
}
} else {
res = rules;
} }
$scope.pages = range(1, Math.ceil(res.length / pageSize));
$scope.page = Math.min($scope.page, $scope.pages.length);
return res.slice(($scope.page - 1) * pageSize, $scope.page * pageSize);
};
$scope.setPage = function(page) {
$scope.page = page;
};
$scope.nextPage = function() {
$scope.page = Math.min($scope.page + 1, $scope.pages.length);
};
$scope.prevPage = function() {
$scope.page = Math.max($scope.page - 1, 1);
};
$http.get('/firewall/hosts/').success(function success(data) {
console.log('foo');
rules = data;
$scope.pages = range(1, Math.ceil(data.length / pageSize));
console.log($scope.pages);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment