/* This file is part of Accountant. Accountant is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Accountant is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Accountant. If not, see . */ // vim: set tw=80 ts=2 sw=2 sts=2: 'use strict'; angular.module('accountant.scheduler', [ 'ngRoute', 'ngBootbox', 'ui-notification', 'mgcrea.ngStrap' ]) .config(['$resourceProvider', function($resourceProvider) { // Keep trailing slashes to avoid redirect by flask.. $resourceProvider.defaults.stripTrailingSlashes = false; }]) .factory('ScheduledOperation', ['$resource', function($resource) { return $resource( '/api/scheduled_operation/:id', { id: '@id' } ); }]) .controller( 'SchedulerController', [ '$scope', '$rootScope', '$routeParams', '$ngBootbox', 'Notification', 'ScheduledOperation', function($scope, $rootScope, $routeParams, $ngBootbox, Notification, ScheduledOperation) { // Operation store. $scope.operations = []; /* * Add a new operation at the beginning of th array. */ $scope.add = function() { var operation = new ScheduledOperation({ account_id: $routeParams.accountId }); // Insert new operation at the beginning of the array. $scope.operations.splice(0, 0, operation); }; /* * Load operations. */ $scope.load = function() { $scope.operations = ScheduledOperation.query({ account_id: $routeParams.accountId }); }; /* * Save operation. */ $scope.save = function($data, $index) { var operation; if($data.$save) { operation = $data; } else { operation = $scope.operations[$index]; operation = angular.merge(operation, $data); } return operation.$save().then(function(data) { Notification.success('Operation #' + data.id + ' saved.'); }); }; /* * Cancel operation edition. Delete if new. */ $scope.cancelEdit = function(operation, rowform, $index) { if(!operation.id) { $scope.operations.splice($index, 1); } else { rowform.$cancel(); } }; /* * Delete operation. */ $scope.delete = function(operation, $index) { var id = operation.id; $ngBootbox.confirm( 'Voulez-vous supprimer l\'operation planifiée \\\'' + operation.label + '\\\' ?', function(result) { if(result) { operation.$delete().then(function() { Notification.success('Operation #' + id + ' deleted.'); // Remove account from array. $scope.operations.splice($index, 1); }); } } ); }; // Load operations on controller initialization. $scope.load(); }]);