81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
|
// Month object
|
||
|
function month() {
|
||
|
this.year = null;
|
||
|
this.month = null;
|
||
|
}
|
||
|
|
||
|
|
||
|
var MonthController = function($scope, $http, $rootScope) {
|
||
|
// Month store and selection
|
||
|
$scope.months = null;
|
||
|
$scope.month = null;
|
||
|
$scope.account = null;
|
||
|
|
||
|
$scope.monthClass = function(month) {
|
||
|
if(month == $scope.month) {
|
||
|
return "active";
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// Function to load months
|
||
|
$scope.loadMonths = function(account) {
|
||
|
if(account) {
|
||
|
$scope.account = account;
|
||
|
}
|
||
|
|
||
|
if($scope.account) {
|
||
|
$http.get("api/accounts/" + $scope.account.id + "/months").success($scope.loadMonths_success);
|
||
|
} else {
|
||
|
$scope.$emit("monthsLoadedEvent", {account: null, month: null});
|
||
|
}
|
||
|
};
|
||
|
|
||
|
$scope.loadMonths_success = function (data) {
|
||
|
// Update months
|
||
|
$scope.months = angular.fromJson(data);
|
||
|
|
||
|
var monthToSelect = null;
|
||
|
|
||
|
var today = new Date();
|
||
|
var currentYear = today.getFullYear().toString();
|
||
|
var currentMonth = (today.getMonth() + 1 < 10 ? "0" : "") + (today.getMonth() + 1);
|
||
|
|
||
|
// Find the new instance of the previously selected month.
|
||
|
angular.forEach($scope.months, function(month) {
|
||
|
// Reset selected month to the new instance corresponding to the old one
|
||
|
if($scope.month) {
|
||
|
if(month == $scope.month) {
|
||
|
monthToSelect = month;
|
||
|
}
|
||
|
} else {
|
||
|
if(month.year === currentYear && month.month === currentMonth) {
|
||
|
monthToSelect = month;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Set selected month to the last one if not yet selected.
|
||
|
if(!monthToSelect && $scope.months.length > 0) {
|
||
|
monthToSelect = $scope.months[$scope.months.length - 1];
|
||
|
}
|
||
|
|
||
|
// Reset to month to select
|
||
|
$scope.month = monthToSelect;
|
||
|
|
||
|
$scope.$emit("monthsLoadedEvent", {account: $scope.account, month: $scope.month});
|
||
|
};
|
||
|
|
||
|
// Callback function to select a new month.
|
||
|
$scope.selectMonth = function(month) {
|
||
|
if(month) {
|
||
|
$scope.month = month;
|
||
|
$scope.$emit("monthsLoadedEvent", {account: $scope.account, month: month});
|
||
|
}
|
||
|
};
|
||
|
|
||
|
$rootScope.$on("accountsLoadedEvent", function(event, args) {
|
||
|
$scope.loadMonths(args.account);
|
||
|
});
|
||
|
};
|
||
|
|