Skip to content

Commit

Permalink
Add Show/Update/Delete for network attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
gmjosack committed Jan 6, 2015
1 parent 45fc8fc commit 0f66fae
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 10 deletions.
63 changes: 57 additions & 6 deletions nsot/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@
$scope.user = results[0].data.data.user;
$scope.site = results[1].data.data.site;
var permissions = $scope.user.permissions[$routeParams.siteId] || {};
var permissions = permissions.permissions || [];
permissions = permissions.permissions || [];
$scope.admin = _.any(permissions, function(value){
return _.contains(["admin", "networks"], value);
return _.contains(["admin"], value);
});

$scope.loading = false;
Expand Down Expand Up @@ -246,14 +246,20 @@
]).then(function(results){
$scope.user = results[0].data.data.user;
$scope.attributes = results[1].data.data.network_attributes;
console.log($scope.attribute);
var permissions = $scope.user.permissions[$routeParams.siteId] || {};
permissions = permissions.permissions || [];
$scope.admin = _.any(permissions, function(value){
return _.contains(["admin", "network_attrs"], value);
});
$scope.loading = false;
}, function(data){
console.log(data);
if (data.status === 404) {
$location.path("/");
$location.replace();
}
});

$scope.createAttribute = function(attr){
console.log(attr);
$http.post("/api/sites/" + siteId +
"/network_attributes", attr).success(function(data){
var attr = data.data.network_attribute;
Expand All @@ -271,6 +277,52 @@
function($scope, $http, $route, $location, $q, $routeParams) {

$scope.loading = true;
$scope.user = {};
$scope.attribute = {};
$scope.admin = false;
$scope.updateError = null;
$scope.deleteError = null;
var siteId = $scope.siteId = $routeParams.siteId;
var attributeId = $scope.attributeId = $routeParams.networkAttributeId;


$q.all([
$http.get("/api/users/0"),
$http.get("/api/sites/" + siteId + "/network_attributes/" + attributeId)
]).then(function(results){
$scope.user = results[0].data.data.user;
$scope.attribute = results[1].data.data.network_attribute;
var permissions = $scope.user.permissions[$routeParams.siteId] || {};
permissions = permissions.permissions || [];
$scope.admin = _.any(permissions, function(value){
return _.contains(["admin", "network_attrs"], value);
});

$scope.loading = false;
}, function(data){
if (data.status === 404) {
$location.path("/");
$location.replace();
}
});

$scope.updateAttribute = function(attr){
$http.put("/api/sites/" + siteId +
"/network_attributes/" + attributeId, attr).success(function(data){
$route.reload();
}).error(function(data){
$scope.updateError = data.error;
});
};

$scope.deleteAttribute = function(attr){
$http.delete("/api/sites/" + siteId +
"/network_attributes/" + attributeId).success(function(data){
$location.path("/sites/" + siteId + "/network_attributes");
}).error(function(data){
$scope.deleteError = data.error;
});
};

}]);

Expand Down Expand Up @@ -317,7 +369,6 @@
$scope.change = results[0].data.data.change;
$scope.loading = false;
}, function(data){
console.log(data);
if (data.status === 404) {
$location.path("/");
$location.replace();
Expand Down
74 changes: 74 additions & 0 deletions nsot/static/templates/network-attribute.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
<loading-panel ng-if="loading"></loading-panel>
<div ng-if="!loading">
<heading-bar heading="NetworkAttributes" subheading="[[attribute.name]]">
<button ng-if="admin"
class="btn btn-info"
data-toggle="modal"
data-target="#updateAttrModal"
>Update Attribute</button>
<button ng-if="admin"
class="btn btn-danger"
data-toggle="modal"
data-target="#deleteAttrModal"
>Delete Attribute</button>
</heading-bar>

<nsot-modal title="Update Attribute" modal-id="updateAttrModal">
<div class="modal-body">
<div ng-if="updateError" class="alert alert-danger">
[[updateError.code]] - [[updateError.message]]
</div>
<form novalidate name="attrForm" class="nsot-form">
<label>Name</label> [[attribute.name]]
<div class="form-group">
<textarea style="resize: vertical;"
class="form-control"
rows="5"
name="description"
placeholder="Description"
ng-model="attribute.description"
>
</textarea>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="required"
ng-model="attribute.required"> Required
</label>
</div>

</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="submit" ng-click="updateAttribute(attribute)"
class="btn btn-primary" ng-disabled="attrForm.$invalid"
>
Update
</button>
</div>
</nsot-modal>


<nsot-modal title="Delete Attribute" modal-id="deleteAttrModal">
<div class="modal-body">
<div ng-if="deleteError" class="alert alert-danger">
[[deleteError.code]] - [[deleteError.message]]
</div>
Are you sure you want to delete this network attribute?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="submit"
ng-click="deleteAttribute(attribute)"
class="btn btn-primary"
>
Delete
</button>
</div>
</nsot-modal>



</div>
8 changes: 4 additions & 4 deletions nsot/static/templates/network-attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class="form-control"
name="name"
placeholder="Name (required)"
ng-model="attr.name"
ng-model="attribute.name"
ng-minlength="1"
required
>
Expand All @@ -33,14 +33,14 @@
rows="5"
name="description"
placeholder="Description"
ng-model="attr.description"
ng-model="attribute.description"
>
</textarea>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="required"
ng-model="attr.required"> Required
ng-model="attribute.required"> Required
</label>
</div>
</form>
Expand All @@ -49,7 +49,7 @@
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="submit" ng-click="createAttribute(attr)"
<button type="submit" ng-click="createAttribute(attribute)"
class="btn btn-primary" ng-disabled="attrForm.$invalid"
>
Create
Expand Down

0 comments on commit 0f66fae

Please sign in to comment.