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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = "dd MMM yyyy";
  6. $scope.hourFormat = "HH'h'mm";
  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. /*$mdDialog.show(
  90. $mdDialog.alert()
  91. .parent(angular.element(document.querySelector('#popupContainer')))
  92. .clickOutsideToClose(true)
  93. .title(course.Name)
  94. .content("<a href=''>42</a>")
  95. .ariaLabel(course.Name)
  96. .ok('Ok')
  97. );*/
  98. };
  99. $scope.onWeekReceived = function(data)
  100. {
  101. $scope.courses = data;
  102. if ($scope.weekId.id == null) {
  103. $scope.weekId.date = $scope.getMonday(new Date());
  104. $scope.weekId.id = $scope.courses.Id;
  105. }
  106. $scope.setDates($scope.getMonday($scope.selectedDate));
  107. };
  108. $scope.loadWeeks = function()
  109. {
  110. var group = DataShareBusiness.getCurrentGroup();
  111. if (group != null && $scope.getSheetCount() < 5) {
  112. var promiseLoadCourses = {
  113. id: "promiseLoadCourses",
  114. groups: ["coursesView"]
  115. };
  116. if ($scope.weekId.id == null) {
  117. WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses)
  118. .then($scope.onWeekReceived);
  119. }
  120. else {
  121. var diff = Math.round(($scope.getMonday($scope.selectedDate) - $scope.weekId.date) / 604800000);
  122. var weekId = $scope.weekId.id + diff;
  123. WeeksBusiness.getWeek({
  124. group_id: group.Id,
  125. type_id: group.Type,
  126. week_id: weekId
  127. }, promiseLoadCourses).then($scope.onWeekReceived);
  128. }
  129. }
  130. else {
  131. $scope.courses = null;
  132. }
  133. };
  134. $scope.weekDiff = function(date1, date2)
  135. {
  136. return Math.round(($scope.getMonday(date1) - $scope.getMonday(date2)) / 604800000)
  137. };
  138. $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, $scope.loadWeeks);
  139. $scope.$watch(function(){return $scope.selectedDate}, function(newValue, oldValue)
  140. {
  141. if (oldValue == null || newValue == oldValue) {
  142. return;
  143. }
  144. var diff = $scope.weekDiff($scope.selectedDate, oldValue);
  145. if (diff != 0) {
  146. $scope.loadWeeks();
  147. }
  148. });
  149. $scope.setDates($scope.getMonday($scope.selectedDate));
  150. }]);