var scheduleFormTmpl = require('./schedule.form.tmpl.html'), scheduleDeleteTmpl = require('./schedule.delete.tmpl.html'); module.exports= function($rootScope, $routeParams, Notification, ScheduledOperation, $log, $modal) { var vm = this; // Operation store. vm.operations = []; /* * Add a new operation at the beginning of th array. */ vm.add = function() { var operation = new ScheduledOperation({ // eslint-disable-next-line camelcase account_id: $routeParams.accountId }); return vm.modify(operation); }; /* * Load operations. */ vm.load = function() { return ScheduledOperation.query({ // eslint-disable-next-line camelcase account_id: $routeParams.accountId }); }; /* * Save operation. */ vm.save = function(operation) { return operation.$save().then(function(operation) { Notification.success('Scheduled operation #' + operation.id + ' saved.'); vm.operations = vm.load(); return operation; }, function(result){ $log.error('Error while saving scheduled operation', operation, result); Notification.error( 'Error while saving scheduled operation: ' + result.message ); }); }; /* * Delete an operation and return a promise. */ vm.confirmDelete = function(operation) { var title = "Delete operation #" + operation.id; $modal({ templateUrl: scheduleDeleteTmpl, controller: function($scope, title, operation, $delete) { $scope.title = title; $scope.operation = operation; $scope.$delete = function() { $scope.$hide(); $delete($scope.operation); }; }, locals: { title: title, operation: operation, $delete: vm.delete } }); }; /* * Delete operation. */ vm.delete = function(operation) { var id = operation.id; return operation.$delete().then(function() { Notification.success('Scheduled operation #' + id + ' deleted.'); vm.operations = vm.load(); return operation; }, function(result) { Notification.error( 'An error occurred while trying to delete scheduled operation #' + id + ':
' + result ); }); }; /* * Open the popup to modify the operation, save it on confirm. * @returns a promise. */ vm.modify = function(operation) { // FIXME Alexis Lahouze 2017-06-15 i18n var title = "Operation"; if (operation.id) { title = title + " #" + operation.id; } $modal({ templateUrl: scheduleFormTmpl, controller: function($scope, title, operation, $save) { $scope.title = title; $scope.operation = operation; $scope.$save = function() { $scope.$hide(); $save($scope.operation); }; }, locals: { title: title, operation: operation, $save: vm.save } }); }; // Load operations on controller initialization. vm.operations = vm.load(); };