portainer/app/components/containers/containersController.js

119 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-11-12 17:54:49 +00:00
angular.module('containers', [])
.controller('ContainersController', ['$scope', 'Container', 'Settings', 'Messages', 'ViewSpinner',
function ($scope, Container, Settings, Messages, ViewSpinner) {
2016-03-19 22:30:47 +00:00
$scope.sortType = 'Created';
$scope.sortReverse = true;
$scope.toggle = false;
$scope.displayAll = Settings.displayAll;
2014-11-12 17:54:49 +00:00
$scope.order = function (sortType) {
2016-03-19 22:30:47 +00:00
$scope.sortReverse = ($scope.sortType === sortType) ? !$scope.sortReverse : false;
$scope.sortType = sortType;
};
var update = function (data) {
ViewSpinner.spin();
Container.query(data, function (d) {
$scope.containers = d.map(function (item) {
return new ContainerViewModel(item);
});
ViewSpinner.stop();
});
};
var batch = function (items, action, msg) {
ViewSpinner.spin();
var counter = 0;
var complete = function () {
counter = counter - 1;
if (counter === 0) {
ViewSpinner.stop();
update({all: Settings.displayAll ? 1 : 0});
}
};
angular.forEach(items, function (c) {
if (c.Checked) {
if (action === Container.start) {
Container.get({id: c.Id}, function (d) {
c = d;
counter = counter + 1;
action({id: c.Id, HostConfig: c.HostConfig || {}}, function (d) {
Messages.send("Container " + msg, c.Id);
var index = $scope.containers.indexOf(c);
complete();
}, function (e) {
Messages.error("Failure", e.data);
complete();
});
}, function (e) {
if (e.status === 404) {
$('.detail').hide();
Messages.error("Not found", "Container not found.");
} else {
Messages.error("Failure", e.data);
}
complete();
});
}
else {
counter = counter + 1;
action({id: c.Id}, function (d) {
Messages.send("Container " + msg, c.Id);
var index = $scope.containers.indexOf(c);
complete();
}, function (e) {
Messages.error("Failure", e.data);
complete();
});
}
2014-11-12 17:54:49 +00:00
}
});
if (counter === 0) {
ViewSpinner.stop();
}
};
$scope.toggleSelectAll = function () {
angular.forEach($scope.filteredContainers, function (i) {
i.Checked = $scope.toggle;
});
};
2014-11-12 17:54:49 +00:00
$scope.toggleGetAll = function () {
Settings.displayAll = $scope.displayAll;
update({all: Settings.displayAll ? 1 : 0});
};
2014-11-12 17:54:49 +00:00
$scope.startAction = function () {
batch($scope.containers, Container.start, "Started");
};
2014-11-12 17:54:49 +00:00
$scope.stopAction = function () {
batch($scope.containers, Container.stop, "Stopped");
};
2014-11-12 17:54:49 +00:00
$scope.restartAction = function () {
batch($scope.containers, Container.restart, "Restarted");
};
$scope.killAction = function () {
batch($scope.containers, Container.kill, "Killed");
};
2014-11-12 17:54:49 +00:00
$scope.pauseAction = function () {
batch($scope.containers, Container.pause, "Paused");
};
2014-11-12 17:54:49 +00:00
$scope.unpauseAction = function () {
batch($scope.containers, Container.unpause, "Unpaused");
};
2014-11-12 17:54:49 +00:00
$scope.removeAction = function () {
batch($scope.containers, Container.remove, "Removed");
};
2014-11-12 17:54:49 +00:00
update({all: Settings.displayAll ? 1 : 0});
}]);