From d4faff7a6cb4d8e160356844d7bba17553fb1caf Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Fri, 7 Jul 2017 22:52:27 +0200 Subject: [PATCH] Separate operations module into different files. --- src/operations/index.js | 167 ++----------------------- src/operations/operation.config.js | 4 + src/operations/operation.controller.js | 146 +++++++++++++++++++++ src/operations/operation.factory.js | 7 ++ 4 files changed, 164 insertions(+), 160 deletions(-) create mode 100644 src/operations/operation.config.js create mode 100644 src/operations/operation.controller.js create mode 100644 src/operations/operation.factory.js diff --git a/src/operations/index.js b/src/operations/index.js index 04d1145..a09e478 100644 --- a/src/operations/index.js +++ b/src/operations/index.js @@ -34,6 +34,10 @@ var ngResource = require('angular-resource'), ngUiNotification = require('angular-ui-notification'), ngStrap = require('angular-strap'); +var OperationFactory = require('./operation.factory'); +var OperationConfig = require('./operation.config'); +var OperationController = require('./operation.controller'); + module.exports = angular.module('accountant.operations', [ ngResource, ngMessages, @@ -44,167 +48,10 @@ module.exports = angular.module('accountant.operations', [ categoryChartModule ]) - .config(function($resourceProvider) { - // Keep trailing slashes to avoid redirect by flask.. - $resourceProvider.defaults.stripTrailingSlashes = false; - }) + .config(OperationConfig) - .factory('Operation', function($resource) { - return $resource( - '/api/operation/:id', { - id: '@id' - } - ); - }) + .factory('Operation', OperationFactory) - /* - * Controller for the operations. - */ - .controller('OperationController', function($routeParams, $modal, - Notification, Operation, Account) { - - var vm = this; - - /* - * Add an empty operation. - */ - vm.add = function() { - var operation = new Operation({ - // eslint-disable-next-line camelcase - account_id: $routeParams.accountId - }); - - return vm.modify(operation); - }; - - /* - * Load operations. - */ - vm.load = function(minDate, maxDate) { - vm.minDate = minDate; - vm.maxDate = maxDate; - - return Operation.query({ - // eslint-disable-next-line camelcase - account_id: $routeParams.accountId, - begin: minDate ? moment(minDate).format('YYYY-MM-DD') : null, - end: maxDate ? moment(maxDate).format('YYYY-MM-DD') : null - }); - }; - - /* - * Toggle pointed indicator for an operation. - */ - vm.togglePointed = function(operation, rowform) { - operation.pointed = !operation.pointed; - - vm.save(operation); - }; - - /* - * Toggle cancel indicator for an operation. - */ - vm.toggleCanceled = function(operation) { - operation.canceled = !operation.canceled; - - vm.save(operation); - }; - - /* - * Save an operation and return a promise. - */ - vm.save = function(operation) { - operation.confirmed = true; - - return operation.$save().then(function(operation) { - Notification.success('Operation #' + operation.id + ' saved.'); - - vm.operations = vm.load(); - - return operation; - }, function(result){ - Notification.error( - 'Error while saving operation: ' + result.message - ); - }); - }; - - /* - * Delete an operation and return a promise. - */ - vm.confirmDelete = function(operation) { - var title = "Delete operation #" + operation.id; - - $modal({ - templateUrl: operationDeleteTmpl, - 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 - } - }); - }; - - vm.delete = function(operation) { - var id = operation.id; - - 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 - ); - }); - }; - - /* - * 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: operationFormTmpl, - 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 - } - }); - }; - - vm.onUpdate = function(minDate, maxDate) { - vm.operations = vm.load(minDate, maxDate); - }; - - vm.account = Account.get({id: $routeParams.accountId}); - }) + .controller('OperationController', OperationController) .name; diff --git a/src/operations/operation.config.js b/src/operations/operation.config.js new file mode 100644 index 0000000..0617c65 --- /dev/null +++ b/src/operations/operation.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/operations/operation.controller.js b/src/operations/operation.controller.js new file mode 100644 index 0000000..ed7f595 --- /dev/null +++ b/src/operations/operation.controller.js @@ -0,0 +1,146 @@ +module.exports = function($routeParams, $modal, Notification, Operation, + Account) { + + var vm = this; + + /* + * Add an empty operation. + */ + vm.add = function() { + var operation = new Operation({ + // eslint-disable-next-line camelcase + account_id: $routeParams.accountId + }); + + return vm.modify(operation); + }; + + /* + * Load operations. + */ + vm.load = function(minDate, maxDate) { + vm.minDate = minDate; + vm.maxDate = maxDate; + + return Operation.query({ + // eslint-disable-next-line camelcase + account_id: $routeParams.accountId, + begin: minDate ? moment(minDate).format('YYYY-MM-DD') : null, + end: maxDate ? moment(maxDate).format('YYYY-MM-DD') : null + }); + }; + + /* + * Toggle pointed indicator for an operation. + */ + vm.togglePointed = function(operation, rowform) { + operation.pointed = !operation.pointed; + + vm.save(operation); + }; + + /* + * Toggle cancel indicator for an operation. + */ + vm.toggleCanceled = function(operation) { + operation.canceled = !operation.canceled; + + vm.save(operation); + }; + + /* + * Save an operation and return a promise. + */ + vm.save = function(operation) { + operation.confirmed = true; + + return operation.$save().then(function(operation) { + Notification.success('Operation #' + operation.id + ' saved.'); + + vm.operations = vm.load(); + + return operation; + }, function(result){ + Notification.error( + 'Error while saving operation: ' + result.message + ); + }); + }; + + /* + * Delete an operation and return a promise. + */ + vm.confirmDelete = function(operation) { + var title = "Delete operation #" + operation.id; + + $modal({ + templateUrl: operationDeleteTmpl, + 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 + } + }); + }; + + vm.delete = function(operation) { + var id = operation.id; + + 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 + ); + }); + }; + + /* + * 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: operationFormTmpl, + 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 + } + }); + }; + + vm.onUpdate = function(minDate, maxDate) { + vm.operations = vm.load(minDate, maxDate); + }; + + vm.account = Account.get({id: $routeParams.accountId}); +}; diff --git a/src/operations/operation.factory.js b/src/operations/operation.factory.js new file mode 100644 index 0000000..6e79a00 --- /dev/null +++ b/src/operations/operation.factory.js @@ -0,0 +1,7 @@ +module.exports = function($resource) { + return $resource( + '/api/operation/:id', { + id: '@id' + } + ); +};