Skip to content

Commit

Permalink
Bit of cleanup on network updates
Browse files Browse the repository at this point in the history
still has a bug, going to revisit
  • Loading branch information
gmjosack committed Jan 18, 2015
1 parent 7af507f commit 7749819
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
43 changes: 35 additions & 8 deletions nsot/static/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@
$scope.admin = false;
var siteId = $scope.siteId = $routeParams.siteId;

$scope.form_url = "/static/templates/includes/networks-form.html";
$scope.form_attrs = [];
$scope.formMode = "create";
$scope.formUrl = "/static/templates/includes/networks-form.html";
$scope.formAttrs = [];

var params = _.extend(pagerParams(), {
siteId: siteId,
Expand All @@ -157,6 +158,7 @@
$scope.networks = results[1].data;
$scope.admin = $scope.user.isAdmin(siteId, ["admin", "networks"]);


$scope.paginator = new Paginator(results[1]);
$scope.loading = false;
}, function(data){
Expand All @@ -178,16 +180,16 @@


$scope.addAttr = function() {
$scope.form_attrs.push({});
$scope.formAttrs.push({});
};

$scope.removeAttr = function(idx) {
$scope.form_attrs.splice(idx, 1);
$scope.formAttrs.splice(idx, 1);
};

$scope.createNetwork = function() {
var network = $scope.network;
var optional_attrs = _.reduce($scope.form_attrs, function(acc, value, key){
var optional_attrs = _.reduce($scope.formAttrs, function(acc, value, key){
acc[value.name] = value.value;
return acc;
}, {});
Expand Down Expand Up @@ -219,8 +221,9 @@
$scope.deleteError = null;
var siteId = $scope.siteId = $routeParams.siteId;
var networkId = $scope.networkId = $routeParams.networkId;
$scope.form_url = "/static/templates/includes/networks-form.html";
$scope.form_attrs = [];
$scope.formMode = "update";
$scope.formUrl = "/static/templates/includes/networks-form.html";
$scope.formAttrs = [];


$q.all([
Expand All @@ -230,6 +233,7 @@
$scope.user = results[0];
$scope.network = results[1];
$scope.admin = $scope.user.isAdmin(siteId, ["admin", "networks"]);

$scope.loading = false;
}, function(data){
if (data.status === 404) {
Expand All @@ -241,20 +245,43 @@
$("body").on("show.bs.modal", "#updateNetworkModal", function(e){
NetworkAttribute.query({siteId: siteId}, function(response){
$scope.attributes = response.data;
$scope.formAttrs = [];

_.forEach($scope.attributes, function(value, idx){
if (!value.required && $scope.network.attributes[value.name]){
$scope.formAttrs.push({
name: value.name,
value: $scope.network.attributes[value.name]
});
}
});

});
});

$scope.$on('$destroy', function() {
$("body").off("show.bs.modal", "#updateNetworkModal");
});

$scope.addAttr = function() {
$scope.formAttrs.push({});
};

$scope.removeAttr = function(idx) {
var attrName = $scope.formAttrs[idx].name;
delete $scope.network.attributes[attrName];
$scope.formAttrs.splice(idx, 1);
};

$scope.updateNetwork = function(){
var network = $scope.network;
var optional_attrs = _.reduce($scope.form_attrs, function(acc, value, key){
var optional_attrs = _.reduce($scope.formAttrs, function(acc, value, key){
acc[value.name] = value.value;
return acc;
}, {});

_.extend(network.attributes, optional_attrs);

$scope.network.$update({siteId: siteId}, function(data){
$route.reload();
}, function(data){
Expand Down
11 changes: 8 additions & 3 deletions nsot/static/templates/includes/networks-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
class="form-control"
name="cidr"
placeholder="Network (required)"
ng-if="formMode === 'create'"
ng-model="network.cidr"
ng-minlength="1"
required
>
<span ng-if="formMode === 'update'">
<label>cidr</label> [[network.network_address]]/[[network.prefix_length]]
<span>
</div>
<h4 class="form-subheading">Attributes</h4>

Expand Down Expand Up @@ -37,13 +41,14 @@ <h4 class="form-subheading">Attributes</h4>
</div>
</div>

<div class="row" ng-repeat="form_attr in form_attrs">
<div class="row" ng-repeat="formAttr in formAttrs">
[[formAttr]] [[$index]]
<div class="col-sm-5">
<select
name="attribute"
class="form-control"
required
ng-model="form_attr.name">
ng-model="formAttrs[$index].name">
<option value="" disabled selected></option>
<option value="[[attr.name]]"
ng-repeat="(idx, attr) in attributes
Expand All @@ -59,7 +64,7 @@ <h4 class="form-subheading">Attributes</h4>
class="form-control"
name="value"
placeholder="Value"
ng-model="form_attr.value"
ng-model="formAttrs[$index].value"
required
>
</div>
Expand Down
2 changes: 1 addition & 1 deletion nsot/static/templates/network.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[[updateError.code]] - [[updateError.message]]
</div>
<form novalidate name="networkForm" class="nsot-form">
<div ng-include="form_url"></div>
<div ng-include="formUrl"></div>
</form>

</div>
Expand Down
2 changes: 1 addition & 1 deletion nsot/static/templates/networks.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[[error.code]] - [[error.message]]
</div>
<form novalidate name="networkForm" class="nsot-form">
<div ng-include="form_url"></div>
<div ng-include="formUrl"></div>
</form>
</div>
<div class="modal-footer">
Expand Down

0 comments on commit 7749819

Please sign in to comment.