angular.module('app') .controller('HomeController', ['$scope', '$state', 'DataShareBusiness', 'WeeksBusiness', '$mdDialog', function($scope, $state, DataShareBusiness, WeeksBusiness, $mdDialog) { $scope.DataShareBusiness = DataShareBusiness; $scope.dateFormat = DataShareBusiness.DateFormat; $scope.timeFormat = DataShareBusiness.TimeFormat; $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); DataShareBusiness.EntitiesType = ($state.current.name == "rooms" ? "Rooms" : "Groups"); 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.isToday = function(date) { var today = new Date(); today.setHours(0, 0, 0, 0); return today.getTime() == date.getTime(); }; $scope.getSheetCount = function() { var sheetCount = function(group) { var c = (group[DataShareBusiness.EntitiesType] == null || group[DataShareBusiness.EntitiesType].length == 0) ? 1 : 0; if (group[DataShareBusiness.EntitiesType] != null) { group[DataShareBusiness.EntitiesType].forEach(function (group) { c += sheetCount(group); }); } return c; }; var g = DataShareBusiness.getCurrentGroup(); if (g == null) { g = {}; g[DataShareBusiness.EntitiesType] = DataShareBusiness[DataShareBusiness.EntitiesType]; } return sheetCount(g) }; $scope.getMonday = function(d) { d = new Date(d); var day = d.getDay(); var diff = d.getDate() - day + (day == 0 ? -6:1); 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) { $mdDialog.show({ controller: 'CourseController', templateUrl: 'views/modals/course.html', parent: angular.element(document.body), targetEvent: null, clickOutsideToClose: true, fullscreen: false, locals: { course: course } }); }; $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"], loaderGroups: ["sidebar"] }; 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[DataShareBusiness.EntitiesType]}, function() { DataShareBusiness.setFromSearchString($state.params.group); }); $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(); } }); $scope.$watch(function(){ return $state.params.group }, function(){ if (DataShareBusiness[DataShareBusiness.EntitiesType] != null) { DataShareBusiness.setFromSearchString($state.params.group); } }); $scope.goToRelativeWeek = function(relativeWeek) { $scope.selectedDate.setDate($scope.selectedDate.getDate() + (relativeWeek * 7)); $scope.selectedDate = new Date($scope.selectedDate); $scope.loadWeeks(); }; $scope.setDates($scope.getMonday($scope.selectedDate)); }]);