angular.module('app') .controller('HomeController', ['$scope', '$state', 'DataShareBusiness', 'WeeksBusiness', function($scope, $state, DataShareBusiness, WeeksBusiness) { $scope.DataShareBusiness = DataShareBusiness; $scope.dateFormat = "dd MMM yyyy"; $scope.courses = null; $scope.days = [{day:"Monday", date: null}, {day:"Tuesday", date: null}, {day:"Wednesday", date: null}, {day:"Thursday", date: null}, {day:"Friday", date: null}, {day:"Saturday", date: null}, {day:"Sunday", date: null}]; $scope.getSheetCount = function() { var sheetCount = function(group) { var c = (group.Groups == null || group.Groups.length == 0) ? 1 : 0; if (group.Groups != null) { group.Groups.forEach(function (group) { c += sheetCount(group); }); } return c; }; return sheetCount(DataShareBusiness.getCurrentGroupOrRoot()) }; $scope.getMonday = function(d) { d = new Date(d); var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday d = new Date(d.setDate(diff)); d.setHours(0, 0, 0, 0); return d; }; $scope.setDates = function(monday) { $scope.days[0].date = new Date(monday); for (var i = 1; i < $scope.days.length; ++i) { $scope.days[i].date = new Date(monday.setDate(monday.getDate() + 1)); } }; $scope.getCourseListByDay = function(date) { if ($scope.courses == null || date == null) { return null; } return $scope.courses.DayList.find(function(day) { var d = new Date(day.DateTime); return d.getTime() == date.getTime(); }); }; $scope.getCoursesByDate = function(date) { var courses = $scope.getCourseListByDay(date); if (courses == null) { return []; } return ["42", "24"]; }; $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, function() { var group = DataShareBusiness.getCurrentGroup(); if (group != null && $scope.getSheetCount() < 5) { var promiseLoadCourses = { id: "promiseLoadCourses", groups: ["coursesView"] }; WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses) .then(function(data) { $scope.courses = data; $scope.setDates($scope.getMonday(new Date())); }); } else { $scope.courses = null; } }); }]);