Commit e6e4c489 by Bence Dányi

firewall_gui: format code

parent 3ed3adb8
...@@ -5,18 +5,18 @@ ...@@ -5,18 +5,18 @@
*/ */
function getCookie(name) { function getCookie(name) {
var cookieValue = null; var cookieValue = null;
if (document.cookie && document.cookie != '') { if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';'); var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) { for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]); var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) { if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break; break;
} }
}
} }
return cookieValue; }
return cookieValue;
} }
/** /**
* Extract CSRF token for AJAX calls * Extract CSRF token for AJAX calls
...@@ -24,45 +24,45 @@ function getCookie(name) { ...@@ -24,45 +24,45 @@ function getCookie(name) {
var csrftoken = getCookie('csrftoken'); var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) { function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
} }
$.ajaxSetup({ $.ajaxSetup({
crossDomain: false, crossDomain: false,
beforeSend: function(xhr, settings) { beforeSend: function(xhr, settings) {
//attach CSRF token to every AJAX call //attach CSRF token to every AJAX call
if (!csrfSafeMethod(settings.type)) { if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken); xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
} }
}
}); });
function makeAddRemove($scope, name, model) { function makeAddRemove($scope, name, model) {
$scope['add' + name] = function(entity) { $scope['add' + name] = function(entity) {
for (var i in $scope.entity[model]) { for (var i in $scope.entity[model]) {
var item = $scope.entity[model][i]; var item = $scope.entity[model][i];
if (item.name == entity && item.__destroyed) { if (item.name == entity && item.__destroyed) {
item.__destroyed = false; item.__destroyed = false;
return; return;
} else if (item.name == entity) { } else if (item.name == entity) {
return; return;
} }
}
$scope.entity[model].push({
name: entity,
__created: true,
});
} }
$scope['remove' + name] = function(entity) { $scope.entity[model].push({
for (var i in $scope.entity[model]) { name: entity,
var item = $scope.entity[model][i]; __created: true,
if (item.name == entity.name && item.__created) { });
$scope.entity[model].splice(i, 1); }
} else if (item.name == entity.name) { $scope['remove' + name] = function(entity) {
item.__destroyed = true; for (var i in $scope.entity[model]) {
return; var item = $scope.entity[model][i];
} if (item.name == entity.name && item.__created) {
} $scope.entity[model].splice(i, 1);
} else if (item.name == entity.name) {
item.__destroyed = true;
return;
}
} }
}
} }
/** /**
...@@ -73,67 +73,67 @@ function makeAddRemove($scope, name, model) { ...@@ -73,67 +73,67 @@ function makeAddRemove($scope, name, model) {
* @type {Array} * @type {Array}
*/ */
var controllers = { var controllers = {
rule: function($scope) { rule: function($scope) {
$('#targetName').typeahead({ $('#targetName').typeahead({
source: function(query, process) { source: function(query, process) {
$.ajax({ $.ajax({
url: '/firewall/autocomplete/' + $scope.entity.target.type + '/', url: '/firewall/autocomplete/' + $scope.entity.target.type + '/',
type: 'post', type: 'post',
data: 'name=' + query, data: 'name=' + query,
success: function autocompleteSuccess(data) { success: function autocompleteSuccess(data) {
process(data.map(function(obj) { process(data.map(function(obj) {
return obj.name; return obj.name;
})); }));
} }
});
},
matcher: function() {
return true;
},
updater: function(item) {
var self = this;
$scope.$apply(function() {
$scope.entity.target.name = item;
})
return item;
}
}); });
}, },
host: function($scope) { matcher: function() {
makeAddRemove($scope, 'HostGroup', 'groups'); return true;
}, },
vlan: function($scope) { updater: function(item) {
makeAddRemove($scope, 'Vlan', 'vlans'); var self = this;
}, $scope.$apply(function() {
vlangroup: function($scope) { $scope.entity.target.name = item;
makeAddRemove($scope, 'Vlan', 'vlans'); })
}, return item;
hostgroup: function() {}, }
firewall: function() {}, });
domain: function() {}, },
record: function() {}, host: function($scope) {
blacklist: function() {}, makeAddRemove($scope, 'HostGroup', 'groups');
},
vlan: function($scope) {
makeAddRemove($scope, 'Vlan', 'vlans');
},
vlangroup: function($scope) {
makeAddRemove($scope, 'Vlan', 'vlans');
},
hostgroup: function() {},
firewall: function() {},
domain: function() {},
record: function() {},
blacklist: function() {},
} }
/** /**
* Configures AngularJS with the defined controllers * Configures AngularJS with the defined controllers
*/ */
var module = angular.module('firewall', []).config( var module = angular.module('firewall', []).config(
['$routeProvider', ['$routeProvider',
function($routeProvider) { function($routeProvider) {
for (var controller in controllers) { for (var controller in controllers) {
var init = controllers[controller]; var init = controllers[controller];
$routeProvider.when('/' + controller + 's/', { $routeProvider.when('/' + controller + 's/', {
templateUrl: '/static/partials/' + controller + '-list.html', templateUrl: '/static/partials/' + controller + '-list.html',
controller: ListController('/firewall/' + controller + 's/') controller: ListController('/firewall/' + controller + 's/')
}).when('/' + controller + 's/:id/', { }).when('/' + controller + 's/:id/', {
templateUrl: '/static/partials/' + controller + '-edit.html', templateUrl: '/static/partials/' + controller + '-edit.html',
controller: EntityController('/firewall/' + controller + 's/', init) controller: EntityController('/firewall/' + controller + 's/', init)
}); });
}
$routeProvider.otherwise({
redirectTo: '/rules/'
});
} }
$routeProvider.otherwise({
redirectTo: '/rules/'
});
}
]); ]);
/** /**
...@@ -144,10 +144,10 @@ var module = angular.module('firewall', []).config( ...@@ -144,10 +144,10 @@ var module = angular.module('firewall', []).config(
*/ */
function range(a, b) { function range(a, b) {
var res = []; var res = [];
do res.push(a++); do res.push(a++);
while (a < b) while (a < b)
return res; return res;
} }
/** /**
...@@ -158,14 +158,14 @@ function range(a, b) { ...@@ -158,14 +158,14 @@ function range(a, b) {
*/ */
function matchAnything(obj, query) { function matchAnything(obj, query) {
var expr = new RegExp(query, 'i') var expr = new RegExp(query, 'i')
for (var i in obj) { for (var i in obj) {
var prop = obj[i]; var prop = obj[i];
if (typeof prop === 'number' && prop == query) return true; if (typeof prop === 'number' && prop == query) return true;
if (typeof prop === 'string' && prop.match(expr)) return true; if (typeof prop === 'string' && prop.match(expr)) return true;
if (typeof prop === 'object' && matchAnything(prop, query)) return true; if (typeof prop === 'object' && matchAnything(prop, query)) return true;
} }
return false; return false;
} }
/** /**
...@@ -175,74 +175,74 @@ function matchAnything(obj, query) { ...@@ -175,74 +175,74 @@ function matchAnything(obj, query) {
*/ */
function ListController(url) { function ListController(url) {
/**
* ListController for the given REST endpoint
* @param {Object} $scope Current controllers scope
* @param {Object} $http Helper for AJAX calls
*/
return function($scope, $http) {
$scope.page = 1;
var rules = [];
var pageSize = 10;
var itemCount = 0;
/** /**
* ListController for the given REST endpoint * Does filtering&paging
* @param {Object} $scope Current controllers scope * @return {Array} Items to be displayed
* @param {Object} $http Helper for AJAX calls
*/ */
return function($scope, $http) { $scope.getPage = function() {
$scope.page = 1; var res = [];
var rules = []; if ($scope.query) {
var pageSize = 10; for (var i in rules) {
var itemCount = 0; var rule = rules[i];
/** if (matchAnything(rule, $scope.query)) {
* Does filtering&paging res.push(rule);
* @return {Array} Items to be displayed }
*/ }
$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);
};
/**
* Setter for current page
* @param {Number} page Page to navigate to
*/
$scope.setPage = function(page) {
$scope.page = page;
};
/**
* Jumps to the next page (if available)
*/
$scope.nextPage = function() {
$scope.page = Math.min($scope.page + 1, $scope.pages.length);
};
/**
* Jumps to the previous page (if available)
*/
$scope.prevPage = function() {
$scope.page = Math.max($scope.page - 1, 1);
};
$scope.deleteEntity = function(id) { } else {
$.ajax({ res = rules;
url: url.split('/')[2]+'/'+id+'/delete/', }
type: 'post', $scope.pages = range(1, Math.ceil(res.length / pageSize));
success: reloadList $scope.page = Math.min($scope.page, $scope.pages.length);
}); return res.slice(($scope.page - 1) * pageSize, $scope.page * pageSize);
}; };
/**
* Setter for current page
* @param {Number} page Page to navigate to
*/
$scope.setPage = function(page) {
$scope.page = page;
};
/**
* Jumps to the next page (if available)
*/
$scope.nextPage = function() {
$scope.page = Math.min($scope.page + 1, $scope.pages.length);
};
/**
* Jumps to the previous page (if available)
*/
$scope.prevPage = function() {
$scope.page = Math.max($scope.page - 1, 1);
};
function reloadList() { $scope.deleteEntity = function(id) {
$http.get(url).success(function success(data) { $.ajax({
rules = data; url: url.split('/')[2] + '/' + id + '/delete/',
$scope.pages = range(1, Math.ceil(data.length / pageSize)); type: 'post',
}); success: reloadList
} });
};
reloadList(); function reloadList() {
$http.get(url).success(function success(data) {
rules = data;
$scope.pages = range(1, Math.ceil(data.length / pageSize));
});
} }
reloadList();
}
} }
/** /**
...@@ -252,121 +252,122 @@ function ListController(url) { ...@@ -252,121 +252,122 @@ function ListController(url) {
*/ */
function EntityController(url, init) { function EntityController(url, init) {
/**
* Entity Controller for the given model URL
* @param {Object} $scope Current controllers scope
* @param {Object} $http Helper for AJAX calls
* @param {Object} $routeParams Helper for route parameter parsing
*/
return function($scope, $http, $routeParams) {
init($scope);
var id = $routeParams.id;
$scope.errors = {};
/** /**
* Entity Controller for the given model URL * Generic filter for collections
* @param {Object} $scope Current controllers scope *
* @param {Object} $http Helper for AJAX calls * Hides destroyed items
* @param {Object} $routeParams Helper for route parameter parsing * @param {Object} item Current item in collection
* @return {Boolean} Item should be displayed, or not
*/ */
return function($scope, $http, $routeParams) { $scope.destroyed = function(item) {
init($scope); return !item.__destroyed;
var id = $routeParams.id; }
$scope.errors = {}; $scope.hasError = function(name) {
/** return $scope.errors[name] ? 'error' : null;
* Generic filter for collections }
* $scope.getError = function(name) {
* Hides destroyed items return $scope.errors[name] ? $scope.errors[name] : '';
* @param {Object} item Current item in collection }
* @return {Boolean} Item should be displayed, or not $scope.save = function() {
*/ $scope.errors = {};
$scope.destroyed = function(item) { $.ajax({
return !item.__destroyed; url: url + 'save/',
} type: 'post',
$scope.hasError = function(name) { data: JSON.stringify($scope.entity),
return $scope.errors[name] ? 'error' : null; success: function(data) {
console.log(data);
$scope.$apply(function() {
$scope.errors = {};
});
window.location.hash = '/' + url.split('/')[2] + '/' + data + '/';
} }
$scope.getError = function(name) { }).error(function(data) {
return $scope.errors[name] ? $scope.errors[name] : ''; try {
data = JSON.parse(data.responseText);
var newErrors = {};
for (var i in data) {
var id = $('#' + i).length ? i : 'targetName';
newErrors[id] = data[i];
}
$scope.$apply(function() {
$scope.errors = newErrors;
})
} catch (ex) {
} }
$scope.save = function() { })
$scope.errors = {}; }
$.ajax({
url: url + 'save/', function reloadEntity() {
$http.get(url + id + '/').success(function success(data) {
$scope.entity = data;
$('input[type=text], input[type=number], select, textarea, .has-tooltip').tooltip({
placement: 'right'
});
['vlan', 'vlangroup', 'host', 'hostgroup', 'firewall', 'owner', 'domain', 'record'].forEach(function(t) {
$('.' + t).typeahead({
/**
* Typeahead does AJAX queries
* @param {String} query Partial name of the entity
* @param {Function} process Callback function after AJAX returned result
*/
source: function(query, process) {
$.ajax({
url: '/firewall/autocomplete/' + t + '/',
type: 'post', type: 'post',
data: JSON.stringify($scope.entity), data: 'name=' + query,
success: function(data) { success: function autocompleteSuccess(data) {
console.log(data); process(data.map(function(obj) {
$scope.$apply(function() { return obj.name;
$scope.errors = {}; }));
});
window.location.hash = '/' + url.split('/')[2] + '/' + data + '/';
} }
}).error(function(data) { });
},
/**
* Filtering is done on server-side, show all results
* @return {Boolean} Always true, so all result are visible
*/
matcher: function() {
return true;
},
/**
* Typeahead does not trigger proper DOM events, so we have to refresh
* the model manually.
* @param {String} item Selected entity name
* @return {String} Same as `item`, the input value is set to this
*/
updater: function(item) {
var self = this;
console.log(this);
$scope.$apply(function() {
var model = self.$element[0].getAttribute('ng-model').split('.')[1];
console.log(self.$element[0].getAttribute('ng-model'), model);
try { try {
data = JSON.parse(data.responseText); $scope.entity[model].name = item;
var newErrors = {};
for (var i in data) {
var id = $('#' + i).length ? i : 'targetName';
newErrors[id] = data[i];
}
$scope.$apply(function() {
$scope.errors = newErrors;
})
} catch (ex) { } catch (ex) {
try {
$scope[self.$element[0].getAttribute('ng-model')] = item;
} catch (ex) {
}
} }
}) })
} return item;
function reloadEntity() { }
$http.get(url + id + '/').success(function success(data) { });
$scope.entity = data; })
$('input[type=text], input[type=number], select, textarea, .has-tooltip').tooltip({ });
placement: 'right'
});
['vlan', 'vlangroup', 'host', 'hostgroup', 'firewall', 'owner', 'domain', 'record'].forEach(function(t) {
$('.' + t).typeahead({
/**
* Typeahead does AJAX queries
* @param {String} query Partial name of the entity
* @param {Function} process Callback function after AJAX returned result
*/
source: function(query, process) {
$.ajax({
url: '/firewall/autocomplete/' + t + '/',
type: 'post',
data: 'name=' + query,
success: function autocompleteSuccess(data) {
process(data.map(function(obj) {
return obj.name;
}));
}
});
},
/**
* Filtering is done on server-side, show all results
* @return {Boolean} Always true, so all result are visible
*/
matcher: function() {
return true;
},
/**
* Typeahead does not trigger proper DOM events, so we have to refresh
* the model manually.
* @param {String} item Selected entity name
* @return {String} Same as `item`, the input value is set to this
*/
updater: function(item) {
var self = this;
console.log(this);
$scope.$apply(function() {
var model = self.$element[0].getAttribute('ng-model').split('.')[1];
console.log(self.$element[0].getAttribute('ng-model'), model);
try {
$scope.entity[model].name = item;
} catch (ex) {
try {
$scope[self.$element[0].getAttribute('ng-model')] = item;
} catch (ex) {
}
}
})
return item;
}
});
})
});
}
reloadEntity();
} }
reloadEntity();
}
} }
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