Improve entry loading on extremes selection.

This commit is contained in:
Alexis Lahouze 2015-06-17 00:29:59 +02:00
parent eea61a3a9b
commit 120e22c1fe

View File

@ -31,11 +31,18 @@ accountantApp.controller(
$scope.begin = moment.utc().startOf('month');
$scope.end = moment.utc().endOf('month');
$scope.selectRange = function(e) {
$scope.begin = moment.utc(e.min);
$scope.end = moment.utc(e.max);
$scope.setExtremes = function(e) {
begin = moment.utc(e.min);
end = moment.utc(e.max);
$scope.loadEntries($scope.account);
$scope.selectRange(begin, end);
};
$scope.selectRange = function(begin, end) {
$scope.begin = begin;
$scope.end = end;
$scope.$emit("rangeSelectedEvent", {begin: begin, end: end});
};
// Configure pie chart for categories.
@ -153,7 +160,7 @@ accountantApp.controller(
},
minRange: 3600 * 1000 * 24 * 14, // 2 weeks
events: {
afterSetExtremes: $scope.selectRange
afterSetExtremes: $scope.setExtremes
}
},
yAxis: {
@ -203,11 +210,15 @@ accountantApp.controller(
// Note: expenses and revenues must be in the same order than in series[0].
config.series[1].data = revenues.concat(expenses);
$scope.categoriesChartConfig.loaded = true;
$scope.loadSolds();
});
};
$scope.loadSolds = function() {
$scope.soldChartConfig.loaded = true;
$http.get("/api/solds", {
params: {
account: $scope.account.id,
@ -224,16 +235,28 @@ accountantApp.controller(
]);
});
$scope.loadEntries($scope.account);
$scope.loadEntries();
});
};
$scope.getAccountStatus = function(account, month) {
// Note: Month is 0 indexed.
$scope.begin = moment({year: month.year, month: month.month - 1, day: 1});
$scope.end = $scope.begin.clone().endOf('month');
$rootScope.$on("accountSelectedEvent", function(event, args) {
$scope.account = args.account;
$http.get("/api/accounts/" + account.id, {
$scope.getAccountStatus();
});
$rootScope.$on("rangeSelectedEvent", function(event, args) {
$scope.getAccountStatus();
});
$rootScope.$on("entriesLoadedEvent", function(event, args) {
//$scope.loadCategories();
});
$scope.getAccountStatus = function() {
$scope.categoriesChartConfig.loaded = true;
$http.get("/api/accounts/" + $scope.account.id, {
params: {
begin: $scope.begin.format('YYYY-MM-DD'),
end: $scope.end.format('YYYY-MM-DD')
@ -265,58 +288,49 @@ accountantApp.controller(
};
// Function to load entries from server for a specific account and month.
$scope.loadEntries = function(account, month) {
if(account) {
$scope.account = account;
}
$scope.loadEntries = function() {
// Clean up selected entry.
$scope.selectedItem = null;
$scope.savedItem = null;
if(account && !$scope.soldChartConfig.loading) {
$scope.soldChartConfig.loading = true;
if(!$scope.entriesLoading) {
$scope.entriesLoading = true;
$http.get("/api/entries", {
params: {
account: account.id,
account: $scope.account.id,
begin: $scope.begin.format('YYYY-MM-DD'),
end: $scope.end.format('YYYY-MM-DD')
}
}).success($scope.loadEntries_success);
} else {
$scope.loadEntries_success(null);
}).success(function(data) {
var entries = [{
id: null,
pointed: false,
operation_date: null,
label: null,
value: null,
sold: null,
pointedsold: null,
category: null,
account_id: null,
state: 'new',
canceled: false,
scheduled_operation_id: null
}];
if(data) {
entries = entries.concat(angular.fromJson(data));
}
$scope.entries = entries;
$scope.entriesLoading = false;
$scope.$emit("entriesLoadedEvent", {entries: entries});
});
}
};
// Load entries success callback
$scope.loadEntries_success = function(data) {
var entries = [{
id: null,
pointed: false,
operation_date: null,
label: null,
value: null,
sold: null,
pointedsold: null,
category: null,
account_id: null,
state: 'new',
canceled: false,
scheduled_operation_id: null
}];
if(data) {
entries = entries.concat(angular.fromJson(data));
}
$scope.entries = entries;
$scope.soldChartConfig.loading = false;
$scope.$emit("entriesLoadedEvent", {entries: entries});
};
// Returns the CSS class for a pointed entry.
$scope.pointedEntryClass = function(entry) {
if(entry.pointed) {
@ -523,12 +537,4 @@ accountantApp.controller(
modalScope.dismiss();
}
};
$rootScope.$on("monthsLoadedEvent", function(event, args){
$scope.loadEntries(args.account, args.month);
});
$rootScope.$on("monthsLoadedEvent", function(event, args){
$scope.getAccountStatus(args.account, args.month);
});
});