Separate schedule module into different files.

This commit is contained in:
Alexis Lahouze 2017-07-08 12:20:17 +02:00
parent e5857bc68e
commit cae86d3014
4 changed files with 145 additions and 138 deletions

View File

@ -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 + ':<br />' + 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;

View File

@ -0,0 +1,4 @@
module.exports = function($resourceProvider) {
// Keep trailing slashes to avoid redirect by flask..
$resourceProvider.defaults.stripTrailingSlashes = false;
};

View File

@ -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 + ':<br />' + 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();
};

View File

@ -0,0 +1,7 @@
module.exports = function($resource) {
return $resource(
'/api/scheduled_operation/:id', {
id: '@id'
}
);
};