You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

home.controller.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. angular.module('app')
  2. .controller('HomeController', ['$scope', '$state', 'DataShareBusiness', 'WeeksBusiness', '$mdDialog',
  3. function($scope, $state, DataShareBusiness, WeeksBusiness, $mdDialog) {
  4. $scope.DataShareBusiness = DataShareBusiness;
  5. $scope.dateFormat = DataShareBusiness.DateFormat;
  6. $scope.timeFormat = DataShareBusiness.TimeFormat;
  7. $scope.courses = null;
  8. $scope.weekId = {
  9. id: null,
  10. date: null
  11. };
  12. $scope.days = [{day:"Monday", date: null}, {day:"Tuesday", date: null}, {day:"Wednesday", date: null},
  13. {day:"Thursday", date: null}, {day:"Friday", date: null}, {day:"Saturday", date: null},
  14. {day:"Sunday", date: null}];
  15. $scope.hours = [];
  16. $scope.selectedDate = new Date();
  17. $scope.selectedDate.setHours(0, 0, 0, 0);
  18. for (var i = 0; i < 48; ++i) {
  19. var d = new Date();
  20. d.setHours(i / 2, 30 * (i % 2), 0, 0);
  21. $scope.hours.push(d);
  22. }
  23. $scope.isToday = function(date)
  24. {
  25. var today = new Date();
  26. today.setHours(0, 0, 0, 0);
  27. return today.getTime() == date.getTime();
  28. };
  29. $scope.getSheetCount = function()
  30. {
  31. var sheetCount = function(group)
  32. {
  33. var c = (group.Groups == null || group.Groups.length == 0) ? 1 : 0;
  34. if (group.Groups != null) {
  35. group.Groups.forEach(function (group) {
  36. c += sheetCount(group);
  37. });
  38. }
  39. return c;
  40. };
  41. return sheetCount(DataShareBusiness.getCurrentGroupOrRoot())
  42. };
  43. $scope.getMonday = function(d)
  44. {
  45. d = new Date(d);
  46. var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
  47. d = new Date(d.setDate(diff));
  48. d.setHours(0, 0, 0, 0);
  49. return d;
  50. };
  51. $scope.setDates = function(monday)
  52. {
  53. $scope.days[0].date = new Date(monday);
  54. for (var i = 1; i < $scope.days.length; ++i) {
  55. $scope.days[i].date = new Date(monday.setDate(monday.getDate() + 1));
  56. }
  57. };
  58. $scope.getDayListByDay = function(date)
  59. {
  60. if ($scope.courses == null || date == null) {
  61. return null;
  62. }
  63. return $scope.courses.DayList.find(function(day)
  64. {
  65. return day.DateTime.getTime() == date.getTime();
  66. });
  67. };
  68. $scope.getCoursesByDate = function(date)
  69. {
  70. var courses = $scope.getDayListByDay(date);
  71. if (courses == null) {
  72. return [];
  73. }
  74. return courses.CourseList;
  75. };
  76. $scope.showCourse = function(course)
  77. {
  78. $mdDialog.show({
  79. controller: 'CourseController',
  80. templateUrl: 'views/modals/course.html',
  81. parent: angular.element(document.body),
  82. targetEvent: null,
  83. clickOutsideToClose: true,
  84. fullscreen: false,
  85. locals: {
  86. course: course
  87. }
  88. });
  89. };
  90. $scope.onWeekReceived = function(data)
  91. {
  92. $scope.courses = data;
  93. if ($scope.weekId.id == null) {
  94. $scope.weekId.date = $scope.getMonday(new Date());
  95. $scope.weekId.id = $scope.courses.Id;
  96. }
  97. $scope.setDates($scope.getMonday($scope.selectedDate));
  98. };
  99. $scope.loadWeeks = function()
  100. {
  101. var group = DataShareBusiness.getCurrentGroup();
  102. if (group != null && $scope.getSheetCount() < 5) {
  103. var promiseLoadCourses = {
  104. id: "promiseLoadCourses",
  105. groups: ["coursesView"]
  106. };
  107. if ($scope.weekId.id == null) {
  108. WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses)
  109. .then($scope.onWeekReceived);
  110. }
  111. else {
  112. var diff = Math.round(($scope.getMonday($scope.selectedDate) - $scope.weekId.date) / 604800000);
  113. var weekId = $scope.weekId.id + diff;
  114. WeeksBusiness.getWeek({
  115. group_id: group.Id,
  116. type_id: group.Type,
  117. week_id: weekId
  118. }, promiseLoadCourses).then($scope.onWeekReceived);
  119. }
  120. }
  121. else {
  122. $scope.courses = null;
  123. }
  124. };
  125. $scope.weekDiff = function(date1, date2)
  126. {
  127. return Math.round(($scope.getMonday(date1) - $scope.getMonday(date2)) / 604800000)
  128. };
  129. $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, $scope.loadWeeks);
  130. $scope.$watch(function(){return $scope.selectedDate}, function(newValue, oldValue)
  131. {
  132. if (oldValue == null || newValue == oldValue) {
  133. return;
  134. }
  135. var diff = $scope.weekDiff($scope.selectedDate, oldValue);
  136. if (diff != 0) {
  137. $scope.loadWeeks();
  138. }
  139. });
  140. $scope.goToRelativeWeek = function(relativeWeek)
  141. {
  142. $scope.selectedDate.setDate($scope.selectedDate.getDate() + (relativeWeek * 7));
  143. $scope.loadWeeks();
  144. };
  145. $scope.setDates($scope.getMonday($scope.selectedDate));
  146. }]);