// 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 ngMessages = require('angular-messages'), ngUiNotification = require('angular-ui-notification'), ngBootbox = require('ngbootbox'), ngStrap = require('angular-strap'), ngXEditable = require('angular-xeditable'); // Note: ngBootbox seems to have no module.exports. ngBootbox = 'ngBootbox'; // Note: angular-xeditable seems to have no module.exports. ngXEditable = 'xeditable'; var schedulerModule = angular.module('accountant.scheduler', [ ngMessages, ngUiNotification, ngBootbox, ngXEditable, 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, $ngBootbox, Notification, ScheduledOperation) { 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 }); // Insert new operation at the beginning of the array. vm.operations.splice(0, 0, operation); }; /* * Load operations. */ vm.load = function() { vm.operations = ScheduledOperation.query({ // eslint-disable-next-line camelcase account_id: $routeParams.accountId }); }; /* * Save operation. */ vm.save = function($data, $index) { var operation; if ($data.$save) { operation = $data; } else { operation = vm.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. */ vm.cancelEdit = function(operation, rowform, $index) { if (operation.id) { rowform.$cancel(); } else { vm.operations.splice($index, 1); } }; /* * Delete operation. */ vm.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. vm.operations.splice($index, 1); }); } } ); }; // Load operations on controller initialization. vm.load(); }) ; module.exports = schedulerModule;