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) {
var module = angular.module('firewall', []).config( $('#targetName').typeahead({
['$routeProvider', function($routeProvider) { source: function(query, process) {
for (var i in listControllers) { $.ajax({
var c = listControllers[i]; url: '/firewall/autocomplete/' + data.target.type + '/',
$routeProvider.when('/' + c + 's/', { type: 'post',
templateUrl: '/static/partials/' + c + '-list.html', data: 'name=' + query,
controller: ListController('/firewall/' + c + 's/') success: function autocompleteSuccess(data) {
process(data.map(function(obj) {
return obj.name;
}));
}
}); });
},
matcher: function() {
return true;
} }
for (var i in entityControllers) { });
var c = entityControllers[i]; },
$routeProvider.when('/' + c + 's/:id/', { host: function($scope) {
templateUrl: '/static/partials/' + c + '-edit.html', $scope.addHostGroup = function(group) {
controller: EntityController('/firewall/' + c + 's/') 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(
['$routeProvider', function($routeProvider) {
for (var controller in controllers) {
var init = controllers[controller];
$routeProvider.when('/' + controller + 's/', {
templateUrl: '/static/partials/' + controller + '-list.html',
controller: ListController('/firewall/' + controller + 's/')
}).when('/' + controller + 's/:id/', {
templateUrl: '/static/partials/' + controller + '-edit.html',
controller: EntityController('/firewall/' + controller + 's/', init)
}); });
} }
$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