Commit a0cbce65 by Bence Dányi

firewall_gui: generic entity/listcontrollers

parent 7514d52f
...@@ -33,22 +33,72 @@ $.ajaxSetup({ ...@@ -33,22 +33,72 @@ $.ajaxSetup({
* and the `/static/partials/rule-list.html` template will be used. * and the `/static/partials/rule-list.html` template will be used.
* @type {Array} * @type {Array}
*/ */
var listControllers = ['rule', 'host', 'vlan', 'vlangroup', 'hostgroup', 'firewall', 'domain', 'record', 'blacklist']; var controllers = {
var entityControllers = ['rule', 'host', 'vlan', 'vlangroup', 'hostgroup']; rule: function($scope) {
$('#targetName').typeahead({
source: function(query, process) {
$.ajax({
url: '/firewall/autocomplete/' + data.target.type + '/',
type: 'post',
data: 'name=' + query,
success: function autocompleteSuccess(data) {
process(data.map(function(obj) {
return obj.name;
}));
}
});
},
matcher: function() {
return true;
}
});
},
host: function($scope) {
$scope.addHostGroup = function(group) {
for (var i in $scope.entity.groups) {
var group_ = $scope.entity.groups[i];
if (group_.name == group && group_.__destroyed) {
group_.__destroyed = false;
return;
} else if (group_.name == group) {
return;
}
}
console.log('2foooo');
$scope.entity.groups.push({
name: group,
__created: true,
})
}
$scope.removeHostGroup = function(group) {
console.log(group);
for (var i in $scope.entity.groups) {
var group_ = $scope.entity.groups[i];
if (group_.name == group.name) {
group_.__destroyed = true;
return;
}
}
}
},
vlan: function() {},
vlangroup: function() {},
hostgroup: function() {},
firewall: function() {},
domain: function() {},
record: function() {},
blacklist: function() {},
}
var module = angular.module('firewall', []).config( var module = angular.module('firewall', []).config(
['$routeProvider', function($routeProvider) { ['$routeProvider', function($routeProvider) {
for (var i in listControllers) { for (var controller in controllers) {
var c = listControllers[i]; var init = controllers[controller];
$routeProvider.when('/' + c + 's/', { $routeProvider.when('/' + controller + 's/', {
templateUrl: '/static/partials/' + c + '-list.html', templateUrl: '/static/partials/' + controller + '-list.html',
controller: ListController('/firewall/' + c + 's/') controller: ListController('/firewall/' + controller + 's/')
}); }).when('/' + controller + 's/:id/', {
} templateUrl: '/static/partials/' + controller + '-edit.html',
for (var i in entityControllers) { controller: EntityController('/firewall/' + controller + 's/', init)
var c = entityControllers[i];
$routeProvider.when('/' + c + 's/:id/', {
templateUrl: '/static/partials/' + c + '-edit.html',
controller: EntityController('/firewall/' + c + 's/')
}); });
} }
$routeProvider.otherwise({ $routeProvider.otherwise({
...@@ -113,48 +163,18 @@ function ListController(url) { ...@@ -113,48 +163,18 @@ function ListController(url) {
} }
} }
function EntityController(url) { function EntityController(url, init) {
return function($scope, $http, $routeParams) { return function($scope, $http, $routeParams) {
init($scope);
var id = $routeParams.id; var id = $routeParams.id;
$scope.destroyed = function(item) {
return !item.__destroyed;
}
$http.get(url + id + '/').success(function success(data) { $http.get(url + id + '/').success(function success(data) {
console.log(data);
$scope.entity = data; $scope.entity = data;
$('#targetName').typeahead({
source: function(query, process) {
$.ajax({
url: '/firewall/autocomplete/' + data.target.type + '/',
type: 'post',
data: 'name=' + query,
success: function autocompleteSuccess(data) {
process(data.map(function(obj) {
return obj.name;
}));
}
});
},
matcher: function() {
return true;
}
});
$('#foreignNetwork').typeahead({
source: function(query, process) {
$.ajax({
url: '/firewall/autocomplete/vlangroup/',
type: 'post',
data: 'name=' + query,
success: function autocompleteSuccess(data) {
process(data.map(function(obj) {
return obj.name;
}));
}
});
},
matcher: function() {
return true;
}
});
['vlan', 'vlangroup', 'host', 'hostgroup', 'firewall'].forEach(function(t) { ['vlan', 'vlangroup', 'host', 'hostgroup', 'firewall'].forEach(function(t) {
$('#' + t).typeahead({ $('.' + t).typeahead({
source: function(query, process) { source: function(query, process) {
$.ajax({ $.ajax({
url: '/firewall/autocomplete/' + t + '/', url: '/firewall/autocomplete/' + t + '/',
......
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