2017-05-23 18:56:10 +00:00
|
|
|
// @@OLD_SERVICE_CONTROLLER: this service should be rewritten to use services.
|
|
|
|
// See app/components/templates/templatesController.js as a reference.
|
2017-01-25 21:12:04 +00:00
|
|
|
angular.module('node', [])
|
2017-09-21 14:00:53 +00:00
|
|
|
.controller('NodeController', ['$scope', '$state', '$transition$', 'LabelHelper', 'Node', 'NodeHelper', 'Task', 'Pagination', 'Notifications',
|
|
|
|
function ($scope, $state, $transition$, LabelHelper, Node, NodeHelper, Task, Pagination, Notifications) {
|
2017-01-25 21:12:04 +00:00
|
|
|
|
2017-01-25 21:35:05 +00:00
|
|
|
$scope.state = {};
|
|
|
|
$scope.state.pagination_count = Pagination.getPaginationCount('node_tasks');
|
2017-01-25 21:12:04 +00:00
|
|
|
$scope.loading = true;
|
|
|
|
$scope.tasks = [];
|
|
|
|
$scope.sortType = 'Status';
|
|
|
|
$scope.sortReverse = false;
|
|
|
|
|
|
|
|
var originalNode = {};
|
|
|
|
var editedKeys = [];
|
|
|
|
|
|
|
|
$scope.order = function(sortType) {
|
|
|
|
$scope.sortReverse = ($scope.sortType === sortType) ? !$scope.sortReverse : false;
|
|
|
|
$scope.sortType = sortType;
|
|
|
|
};
|
|
|
|
|
2017-01-25 21:35:05 +00:00
|
|
|
$scope.changePaginationCount = function() {
|
|
|
|
Pagination.setPaginationCount('node_tasks', $scope.state.pagination_count);
|
|
|
|
};
|
|
|
|
|
2017-01-25 21:12:04 +00:00
|
|
|
$scope.updateNodeAttribute = function updateNodeAttribute(node, key) {
|
|
|
|
editedKeys.push(key);
|
|
|
|
};
|
|
|
|
$scope.addLabel = function addLabel(node) {
|
|
|
|
node.Labels.push({ key: '', value: '', originalValue: '', originalKey: '' });
|
|
|
|
$scope.updateNodeAttribute(node, 'Labels');
|
|
|
|
};
|
|
|
|
$scope.removeLabel = function removeLabel(node, index) {
|
|
|
|
var removedElement = node.Labels.splice(index, 1);
|
|
|
|
if (removedElement !== null) {
|
|
|
|
$scope.updateNodeAttribute(node, 'Labels');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.updateLabel = function updateLabel(node, label) {
|
|
|
|
if (label.value !== label.originalValue || label.key !== label.originalKey) {
|
|
|
|
$scope.updateNodeAttribute(node, 'Labels');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.hasChanges = function(node, elements) {
|
|
|
|
if (!elements) {
|
|
|
|
elements = Object.keys(originalNode);
|
|
|
|
}
|
|
|
|
var hasChanges = false;
|
|
|
|
elements.forEach(function(key) {
|
|
|
|
hasChanges = hasChanges || ((editedKeys.indexOf(key) >= 0) && node[key] !== originalNode[key]);
|
|
|
|
});
|
|
|
|
return hasChanges;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.cancelChanges = function(node) {
|
|
|
|
editedKeys.forEach(function(key) {
|
|
|
|
node[key] = originalNode[key];
|
|
|
|
});
|
|
|
|
editedKeys = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.updateNode = function updateNode(node) {
|
|
|
|
var config = NodeHelper.nodeToConfig(node.Model);
|
|
|
|
config.Name = node.Name;
|
|
|
|
config.Availability = node.Availability;
|
|
|
|
config.Role = node.Role;
|
|
|
|
config.Labels = LabelHelper.fromKeyValueToLabelHash(node.Labels);
|
|
|
|
|
|
|
|
Node.update({ id: node.Id, version: node.Version }, config, function (data) {
|
|
|
|
$('#loadServicesSpinner').hide();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.success('Node successfully updated', 'Node updated');
|
2017-01-25 21:12:04 +00:00
|
|
|
$state.go('node', {id: node.Id}, {reload: true});
|
|
|
|
}, function (e) {
|
|
|
|
$('#loadServicesSpinner').hide();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Failed to update node');
|
2017-01-25 21:12:04 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function loadNodeAndTasks() {
|
|
|
|
$scope.loading = true;
|
|
|
|
if ($scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE') {
|
2017-09-21 14:00:53 +00:00
|
|
|
Node.get({ id: $transition$.params().id}, function(d) {
|
2017-01-25 21:12:04 +00:00
|
|
|
if (d.message) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to inspect the node');
|
2017-01-25 21:12:04 +00:00
|
|
|
} else {
|
|
|
|
var node = new NodeViewModel(d);
|
|
|
|
originalNode = angular.copy(node);
|
|
|
|
$scope.node = node;
|
|
|
|
getTasks(d);
|
|
|
|
}
|
|
|
|
$scope.loading = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$scope.loading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTasks(node) {
|
|
|
|
if (node) {
|
|
|
|
Task.query({filters: {node: [node.ID]}}, function (tasks) {
|
|
|
|
$scope.tasks = tasks.map(function (task) {
|
2017-05-23 18:56:10 +00:00
|
|
|
return new TaskViewModel(task);
|
2017-01-25 21:12:04 +00:00
|
|
|
});
|
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to retrieve tasks associated to the node');
|
2017-01-25 21:12:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadNodeAndTasks();
|
|
|
|
|
|
|
|
}]);
|