accountant/frontend/static/js/scheduler.js
2013-12-03 20:47:59 +01:00

198 lines
5.8 KiB
JavaScript

/*
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.
Foobar 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/>.
*/
var SchedulerController = function($scope, $http, $rootScope, $filter) {
// Operations store and selection
$scope.operations = [];
$scope.selectedOperation = null;
$scope.account = null;
// Placeholder for saved value to cancel entry edition
$scope.savedOperation = null;
$scope.loadOperations = function(account) {
// Clean up selected entry.
$scope.selectedOperation = null;
$scope.savedOperation = null;
if(account) {
$scope.account = account;
$http.get("/api/scheduled_operations/" + account.id).success($scope.loadOperations_success);
} else {
$scope.loadOperations_success(null);
}
};
$scope.loadOperations_success = function(data) {
var operations = [{
id: null,
start_date: null,
stop_date: null,
day: null,
frequency: null,
label: null,
account_id: null,
category: null,
state: "edit"
}];
if(data) {
operations = operations.concat(angular.fromJson(data));
}
$scope.operations = operations;
$scope.$emit("operationsLoadedEvent", {operations: operations});
};
$scope.iconSaveClass = function(operation) {
if($scope.isNew(operation)) {
return "icon-plus";
} else if ($scope.isEditing(operation)) {
return "icon-ok";
}
};
$scope.iconCancelClass = function(operation) {
if($scope.isNew(operation)) {
return "icon-remove";
} else if ($scope.isEditing(operation)) {
return "icon-ban-circle";
}
};
$scope.isNew = function(operation) {
return !operation.id;
};
// Returns true if the entry is in editing state.
$scope.isEditing = function(operation) {
return operation.state === 'edit';
};
$scope.isDisplaying = function(operation) {
return operation.id && (!operation.state || operation.state === 'display');
};
$scope.saveOperation = function(operation) {
if(!operation.account_id) {
operation.account_id = $scope.account.id;
}
// prepare the Ajax xall to save the sceduled operation.
var type;
var url = "/api/scheduled_operations";
if(!$scope.isNew(operation)) {
url += "/" + operation.id;
}
$http.put(url, angular.toJson(operation)).success(function(data) {
$.pnotify({
type: "success",
title: "Save",
text: data
});
$scope.$emit("operationSavedEvent", operation);
});
};
$scope.editOperation = function(operation) {
// Cancel previous editing.
if($scope.selectedOperation) {
$scope.cancelEditOperation($scope.selectedItem);
}
// Save current entry values.
$scope.savedOperation = angular.copy(operation);
$scope.selectedOperation = operation;
// Enter edit state.
operation.state='edit';
};
$scope.cancelEditOperation = function(operation) {
if ($scope.isNew(operation)) {
operation.id = null;
operation.start_date = null;
operation.stop_date = null;
operation.day = null;
operation.frequency = null;
operation.label = null;
operation.value = null;
operation.category = null;
operation.account_id = $scope.account.id;
} else if ($scope.isEditing(operation)) {
if($scope.savedOperation) {
operation.id = $scope.savedOperation.id;
operation.start_date = $scope.savedOperation.start_date;
operation.stop_date = $scope.savedOperation.stop_date;
operation.day = $scope.savedOperation.day;
operation.frequency = $scope.savedOperation.frequency;
operation.label = $scope.savedOperation.label;
operation.value = $scope.savedOperation.value;
operation.category = $scope.savedOperation.category;
operation.account_id = $scope.savedOperation.account_id;
}
$scope.savedOperation = null;
$scope.selectedOperation = null;
operation.state = 'display';
}
};
$scope.removeOperation = function(operation, modalScope) {
// Cancel current editing.
if (!$scope.isNew(operation)) {
$http.delete("/api/scheduled_operations/" + operation.id).success(function (result) {
$.pnotify({
type: "success",
title: "Delete",
text: result
});
// Send the "entry removed" event.
$scope.$emit("operationRemovedEvent", operation);
$scope.closeModal(modalScope);
}).error(function (data) {
$.pnotify({
type: "error",
title: "Delete",
text: data
});
$scope.closeModal(modalScope);
});
}
};
$scope.closeModal = function(modalScope) {
// Close the modal dialog
if(modalScope && modalScope.dismiss) {
modalScope.dismiss();
}
};
$rootScope.$on("accountsLoadedEvent", function(event, args){
$scope.loadOperations(args.account);
});
}