Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

home.controller.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. DataShareBusiness.EntitiesType = ($state.current.name == "rooms" ? "Rooms" : "Groups");
  19. for (var i = 0; i < 48; ++i) {
  20. var d = new Date();
  21. d.setHours(i / 2, 30 * (i % 2), 0, 0);
  22. $scope.hours.push(d);
  23. }
  24. $scope.isToday = function(date)
  25. {
  26. var today = new Date();
  27. today.setHours(0, 0, 0, 0);
  28. return today.getTime() == date.getTime();
  29. };
  30. $scope.getSheetCount = function()
  31. {
  32. var sheetCount = function(group)
  33. {
  34. var c = (group[DataShareBusiness.EntitiesType] == null || group[DataShareBusiness.EntitiesType].length == 0) ? 1 : 0;
  35. if (group[DataShareBusiness.EntitiesType] != null) {
  36. group[DataShareBusiness.EntitiesType].forEach(function (group) {
  37. c += sheetCount(group);
  38. });
  39. }
  40. return c;
  41. };
  42. var g = DataShareBusiness.getCurrentGroup();
  43. if (g == null) {
  44. g = {};
  45. g[DataShareBusiness.EntitiesType] = DataShareBusiness[DataShareBusiness.EntitiesType];
  46. }
  47. return sheetCount(g)
  48. };
  49. $scope.getMonday = function(d)
  50. {
  51. d = new Date(d);
  52. var day = d.getDay();
  53. var diff = d.getDate() - day + (day == 0 ? -6:1);
  54. d = new Date(d.setDate(diff));
  55. d.setHours(0, 0, 0, 0);
  56. return d;
  57. };
  58. $scope.setDates = function(monday)
  59. {
  60. $scope.days[0].date = new Date(monday);
  61. for (var i = 1; i < $scope.days.length; ++i) {
  62. $scope.days[i].date = new Date(monday.setDate(monday.getDate() + 1));
  63. }
  64. };
  65. $scope.getDayListByDay = function(date)
  66. {
  67. if ($scope.courses == null || date == null) {
  68. return null;
  69. }
  70. return $scope.courses.DayList.find(function(day)
  71. {
  72. return day.DateTime.getTime() == date.getTime();
  73. });
  74. };
  75. $scope.getCoursesByDate = function(date)
  76. {
  77. var courses = $scope.getDayListByDay(date);
  78. if (courses == null) {
  79. return [];
  80. }
  81. return courses.CourseList;
  82. };
  83. $scope.showCourse = function(course)
  84. {
  85. $mdDialog.show({
  86. controller: 'CourseController',
  87. templateUrl: 'views/modals/course.html',
  88. parent: angular.element(document.body),
  89. targetEvent: null,
  90. clickOutsideToClose: true,
  91. fullscreen: false,
  92. locals: {
  93. course: course
  94. }
  95. });
  96. };
  97. $scope.onWeekReceived = function(data)
  98. {
  99. $scope.courses = data;
  100. if ($scope.weekId.id == null) {
  101. $scope.weekId.date = $scope.getMonday(new Date());
  102. $scope.weekId.id = $scope.courses.Id;
  103. }
  104. $scope.setDates($scope.getMonday($scope.selectedDate));
  105. };
  106. $scope.loadWeeks = function()
  107. {
  108. var group = DataShareBusiness.getCurrentGroup();
  109. if (group != null && $scope.getSheetCount() < 5) {
  110. var promiseLoadCourses = {
  111. id: "promiseLoadCourses",
  112. groups: ["coursesView"],
  113. loaderGroups: ["sidebar"]
  114. };
  115. if ($scope.weekId.id == null) {
  116. WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses)
  117. .then($scope.onWeekReceived);
  118. }
  119. else {
  120. var diff = Math.round(($scope.getMonday($scope.selectedDate) - $scope.weekId.date) / 604800000);
  121. var weekId = $scope.weekId.id + diff;
  122. WeeksBusiness.getWeek({
  123. group_id: group.Id,
  124. type_id: group.Type,
  125. week_id: weekId
  126. }, promiseLoadCourses).then($scope.onWeekReceived);
  127. }
  128. }
  129. else {
  130. $scope.courses = null;
  131. }
  132. };
  133. $scope.weekDiff = function(date1, date2)
  134. {
  135. return Math.round(($scope.getMonday(date1) - $scope.getMonday(date2)) / 604800000)
  136. };
  137. $scope.$watch(function(){return DataShareBusiness[DataShareBusiness.EntitiesType]}, function()
  138. {
  139. DataShareBusiness.setFromSearchString($state.params.group);
  140. });
  141. $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, $scope.loadWeeks);
  142. $scope.$watch(function(){return $scope.selectedDate}, function(newValue, oldValue)
  143. {
  144. if (oldValue == null || newValue == oldValue) {
  145. return;
  146. }
  147. var diff = $scope.weekDiff($scope.selectedDate, oldValue);
  148. if (diff != 0) {
  149. $scope.loadWeeks();
  150. }
  151. });
  152. $scope.$watch(function(){ return $state.params.group }, function(){
  153. if (DataShareBusiness[DataShareBusiness.EntitiesType] != null) {
  154. DataShareBusiness.setFromSearchString($state.params.group);
  155. }
  156. });
  157. $scope.goToRelativeWeek = function(relativeWeek)
  158. {
  159. $scope.selectedDate.setDate($scope.selectedDate.getDate() + (relativeWeek * 7));
  160. $scope.selectedDate = new Date($scope.selectedDate);
  161. $scope.loadWeeks();
  162. };
  163. $scope.setDates($scope.getMonday($scope.selectedDate));
  164. }]);