123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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'h'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.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.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)
- {
- $mdDialog.show({
- controller: 'CourseController',
- templateUrl: 'views/modals/course.html',
- parent: angular.element(document.body),
- targetEvent: null,
- clickOutsideToClose: true,
- fullscreen: false,
- locals: {
- course: course
- }
- });
- /*$mdDialog.show(
- $mdDialog.alert()
- .parent(angular.element(document.querySelector('#popupContainer')))
- .clickOutsideToClose(true)
- .title(course.Name)
- .content("<a href=''>42</a>")
- .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();
- }
- });
-
- $scope.setDates($scope.getMonday($scope.selectedDate));
- }]);
|