accountant-ui/src/scheduler/index.js

142 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-10-09 20:17:11 +02:00
// vim: set tw=80 ts=4 sw=4 sts=4:
2016-04-12 10:55:08 +02:00
/*
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 <http://www.gnu.org/licenses/>.
*/
2017-06-10 18:02:19 +02:00
/* jshint node: true */
2016-04-12 10:55:08 +02:00
'use strict';
2017-06-10 18:02:19 +02:00
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
2016-04-12 10:55:08 +02:00
])
2016-10-14 08:21:43 +02:00
.config(function($resourceProvider) {
2016-04-12 10:55:08 +02:00
// Keep trailing slashes to avoid redirect by flask..
$resourceProvider.defaults.stripTrailingSlashes = false;
2016-10-14 08:21:43 +02:00
})
2016-04-12 10:55:08 +02:00
2016-10-14 08:21:43 +02:00
.factory('ScheduledOperation', function($resource) {
2016-04-12 10:55:08 +02:00
return $resource(
'/api/scheduled_operation/:id', {
id: '@id'
}
);
2016-10-14 08:21:43 +02:00
})
.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);
}
2016-10-09 20:17:11 +02:00
2016-10-14 08:21:43 +02:00
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);
});
2016-10-09 20:17:11 +02:00
}
2016-10-14 08:21:43 +02:00
}
);
};
// Load operations on controller initialization.
vm.load();
})
2016-10-09 20:17:11 +02:00
2016-10-14 08:21:43 +02:00
;
2017-06-10 18:02:19 +02:00
module.exports = schedulerModule;