angular.module('app') .controller('HomeController', ['$scope', '$state', 'DataShareBusiness', 'WeeksBusiness', '$mdDialog', function($scope, $state, DataShareBusiness, WeeksBusiness, $mdDialog) { $scope.DataShareBusiness = DataShareBusiness; $scope.dateFormat = "dd MMM yyyy"; $scope.hourFormat = "HH:mm"; $scope.courses = null; $scope.weekId = { id: null, date: 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.hours = []; $scope.selectedDate = new Date(); $scope.selectedDate.setHours(0, 0, 0, 0); for (var i = 0; i < 48; ++i) { var d = new Date(); d.setHours(i / 2, 30 * (i % 2), 0, 0); $scope.hours.push(d); } $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.getDayListByDay = function(date) { if ($scope.courses == null || date == null) { return null; } return $scope.courses.DayList.find(function(day) { return day.DateTime.getTime() == date.getTime(); }); }; $scope.getCoursesByDate = function(date) { var courses = $scope.getDayListByDay(date); if (courses == null) { return []; } return courses.CourseList; }; $scope.showCourse = function(course) { console.log(course); $mdDialog.show( $mdDialog.alert() .parent(angular.element(document.querySelector('#popupContainer'))) .clickOutsideToClose(true) .title(course.Name) .content("42") .ariaLabel(course.Name) .ok('Ok') ); }; $scope.onWeekReceived = function(data) { $scope.courses = data; if ($scope.weekId.id == null) { $scope.weekId.date = $scope.getMonday(new Date()); $scope.weekId.id = $scope.courses.Id; } $scope.setDates($scope.getMonday($scope.selectedDate)); }; $scope.loadWeeks = function() { var group = DataShareBusiness.getCurrentGroup(); if (group != null && $scope.getSheetCount() < 5) { var promiseLoadCourses = { id: "promiseLoadCourses", groups: ["coursesView"] }; if ($scope.weekId.id == null) { WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses) .then($scope.onWeekReceived); } else { var diff = Math.round(($scope.getMonday($scope.selectedDate) - $scope.weekId.date) / 604800000); var weekId = $scope.weekId.id + diff; WeeksBusiness.getWeek({ group_id: group.Id, type_id: group.Type, week_id: weekId }, promiseLoadCourses).then($scope.onWeekReceived); } } else { $scope.courses = null; } }; $scope.weekDiff = function(date1, date2) { return Math.round(($scope.getMonday(date1) - $scope.getMonday(date2)) / 604800000) }; $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, $scope.loadWeeks); $scope.$watch(function(){return $scope.selectedDate}, function(newValue, oldValue) { if (oldValue == null || newValue == oldValue) { return; } var diff = $scope.weekDiff($scope.selectedDate, oldValue); if (diff != 0) { $scope.loadWeeks(); } }); }]);