// vim: set tw=80 ts=4 sw=4 sts=4: /* 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 . */ /* jshint node: true */ 'use strict'; var angular = require('angular'); var scheduleFormTmpl = require('./schedule.form.tmpl.html'), scheduleDeleteTmpl = require('./schedule.delete.tmpl.html'); var ngMessages = require('angular-messages'), ngUiBootstrap = require('angular-ui-bootstrap'), ngUiNotification = require('angular-ui-notification'), ngStrap = require('angular-strap'); module.exports = angular.module('accountant.scheduler', [ ngMessages, ngUiBootstrap, ngUiNotification, ngStrap ]) .config(function($resourceProvider) { // Keep trailing slashes to avoid redirect by flask.. $resourceProvider.defaults.stripTrailingSlashes = false; }) .factory('ScheduledOperation', function($resource) { return $resource( '/api/scheduled_operation/:id', { id: '@id' } ); }) .controller('SchedulerController', function($rootScope, $routeParams, Notification, ScheduledOperation, $log, $uibModal, $q) { 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 ); return $q.reject(result); }); }; /* * Delete operation. */ vm.delete = function(operation) { var id = operation.id; $uibModal.open({ component: 'scheduleDeleteModalComponent', resolve: { operation: function() { return operation; } } }).result.then(function(operation) { return operation.$delete().then(function() { Notification.success('Operation #' + id + ' deleted.'); vm.operations = vm.load(); return operation; }, function(result) { Notification.error( 'An error occurred while trying to delete operation #' + id + ':
' + result ); return $q.reject(result); }); }, function() { return false; }); }; /* * Open the popup to modify the operation, save it on confirm. * @returns a promise. */ vm.modify = function(operation) { return $uibModal.open({ component: 'scheduleModifyModalComponent', resolve: { operation: function() { return operation; } } }).result.then(function(operation) { return vm.save(operation); }, function() { return false; }); }; // Load operations on controller initialization. vm.operations = vm.load(); }) .component('scheduleModifyModalComponent', { templateUrl: scheduleFormTmpl, bindings: { resolve: '<', close: '&', dismiss: '&' }, controller: function() { var vm = this; vm.$onInit = function() { vm.operation = vm.resolve.operation; }; vm.ok = function() { vm.close({ $value: vm.operation }); }; vm.cancel = function() { vm.dismiss({ $value: 'cancel' }); }; vm.title = function() { // FIXME Alexis Lahouze 2017-06-15 i18n if (vm.operation.id) { return "Scheduled operation #" + vm.operation.id; } else { return "Scheduled operation"; } }; } }) .component('scheduleDeleteModalComponent', { templateUrl: scheduleDeleteTmpl, bindings: { resolve: '<', close: '&', dismiss: '&' }, controller: function() { var vm = this; vm.$onInit = function() { vm.operation = vm.resolve.operation; }; vm.ok = function() { vm.close({ $value: vm.operation }); }; vm.cancel = function() { vm.dismiss({ $value: 'cancel' }); }; vm.title = function() { // FIXME Alexis Lahouze 2017-06-15 i18n if (vm.operation.id) { return "Scheduled operation #" + vm.operation.id; } else { return "Scheduled operation"; } }; } }) .name;