angular.module('images', []) .controller('ImagesController', ['$scope', 'Image', 'ViewSpinner', 'Messages', function ($scope, Image, ViewSpinner, Messages) { $scope.state = {}; $scope.sortType = 'Created'; $scope.sortReverse = true; $scope.state.toggle = false; $scope.state.selectedItemCount = 0; $scope.order = function(sortType) { $scope.sortReverse = ($scope.sortType === sortType) ? !$scope.sortReverse : false; $scope.sortType = sortType; }; $scope.toggleSelectAll = function () { angular.forEach($scope.state.filteredImages, function (i) { i.Checked = $scope.state.toggle; }); if ($scope.state.toggle) { $scope.state.selectedItemCount = $scope.state.filteredImages.length; } else { $scope.state.selectedItemCount = 0; } }; $scope.selectItem = function (item) { if (item.Checked) { $scope.state.selectedItemCount++; } else { $scope.state.selectedItemCount--; } }; $scope.removeAction = function () { ViewSpinner.spin(); var counter = 0; var complete = function () { counter = counter - 1; if (counter === 0) { ViewSpinner.stop(); } }; angular.forEach($scope.images, function (i) { if (i.Checked) { counter = counter + 1; Image.remove({id: i.Id}, function (d) { angular.forEach(d, function (resource) { Messages.send("Image deleted", resource.Deleted); }); var index = $scope.images.indexOf(i); $scope.images.splice(index, 1); complete(); }, function (e) { Messages.error("Failure", e.data); complete(); }); } }); }; function fetchImages() { ViewSpinner.spin(); Image.query({}, function (d) { $scope.images = d.map(function (item) { return new ImageViewModel(item); }); ViewSpinner.stop(); }, function (e) { Messages.error("Failure", e.data); ViewSpinner.stop(); }); } fetchImages(); }]);