diff --git a/src/scheduler/index.js b/src/scheduler/index.js index a393c76..e0cc799 100644 --- a/src/scheduler/index.js +++ b/src/scheduler/index.js @@ -20,155 +20,24 @@ var angular = require('angular'); -var scheduleFormTmpl = require('./schedule.form.tmpl.html'), - scheduleDeleteTmpl = require('./schedule.delete.tmpl.html'); - var ngMessages = require('angular-messages'), ngUiNotification = require('angular-ui-notification'), ngStrap = require('angular-strap'); +var ScheduleConfig = require('./schedule.config.js'); +var ScheduleController = require('./schedule.controller.js'); +var ScheduleFactory = require('./schedule.factory.js'); + module.exports = angular.module('accountant.scheduler', [ ngMessages, ngUiNotification, ngStrap ]) - .config(function($resourceProvider) { - // Keep trailing slashes to avoid redirect by flask.. - $resourceProvider.defaults.stripTrailingSlashes = false; - }) + .config(ScheduleConfig) - .factory('ScheduledOperation', function($resource) { - return $resource( - '/api/scheduled_operation/:id', { - id: '@id' - } - ); - }) + .factory('ScheduledOperation', ScheduleFactory) - .controller('SchedulerController', 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(); - }) + .controller('SchedulerController', ScheduleController) .name; diff --git a/src/scheduler/schedule.config.js b/src/scheduler/schedule.config.js new file mode 100644 index 0000000..0617c65 --- /dev/null +++ b/src/scheduler/schedule.config.js @@ -0,0 +1,4 @@ +module.exports = function($resourceProvider) { + // Keep trailing slashes to avoid redirect by flask.. + $resourceProvider.defaults.stripTrailingSlashes = false; +}; diff --git a/src/scheduler/schedule.controller.js b/src/scheduler/schedule.controller.js new file mode 100644 index 0000000..7a3663f --- /dev/null +++ b/src/scheduler/schedule.controller.js @@ -0,0 +1,127 @@ +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(); +}; diff --git a/src/scheduler/schedule.factory.js b/src/scheduler/schedule.factory.js new file mode 100644 index 0000000..aa00231 --- /dev/null +++ b/src/scheduler/schedule.factory.js @@ -0,0 +1,7 @@ +module.exports = function($resource) { + return $resource( + '/api/scheduled_operation/:id', { + id: '@id' + } + ); +};