Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

home.controller.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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: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.getSheetCount = function()
  24. {
  25. var sheetCount = function(group)
  26. {
  27. var c = (group.Groups == null || group.Groups.length == 0) ? 1 : 0;
  28. if (group.Groups != null) {
  29. group.Groups.forEach(function (group) {
  30. c += sheetCount(group);
  31. });
  32. }
  33. return c;
  34. };
  35. return sheetCount(DataShareBusiness.getCurrentGroupOrRoot())
  36. };
  37. $scope.getMonday = function(d)
  38. {
  39. d = new Date(d);
  40. var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
  41. d = new Date(d.setDate(diff));
  42. d.setHours(0, 0, 0, 0);
  43. return d;
  44. };
  45. $scope.setDates = function(monday)
  46. {
  47. $scope.days[0].date = new Date(monday);
  48. for (var i = 1; i < $scope.days.length; ++i) {
  49. $scope.days[i].date = new Date(monday.setDate(monday.getDate() + 1));
  50. }
  51. };
  52. $scope.getDayListByDay = function(date)
  53. {
  54. if ($scope.courses == null || date == null) {
  55. return null;
  56. }
  57. return $scope.courses.DayList.find(function(day)
  58. {
  59. return day.DateTime.getTime() == date.getTime();
  60. });
  61. };
  62. $scope.getCoursesByDate = function(date)
  63. {
  64. var courses = $scope.getDayListByDay(date);
  65. if (courses == null) {
  66. return [];
  67. }
  68. return courses.CourseList;
  69. };
  70. $scope.showCourse = function(course)
  71. {
  72. console.log(course);
  73. $mdDialog.show(
  74. $mdDialog.alert()
  75. .parent(angular.element(document.querySelector('#popupContainer')))
  76. .clickOutsideToClose(true)
  77. .title(course.Name)
  78. .content("<a href=''>42</a>")
  79. .ariaLabel(course.Name)
  80. .ok('Ok')
  81. );
  82. };
  83. $scope.onWeekReceived = function(data)
  84. {
  85. $scope.courses = data;
  86. if ($scope.weekId.id == null) {
  87. $scope.weekId.date = $scope.getMonday(new Date());
  88. $scope.weekId.id = $scope.courses.Id;
  89. }
  90. $scope.setDates($scope.getMonday($scope.selectedDate));
  91. };
  92. $scope.loadWeeks = function()
  93. {
  94. var group = DataShareBusiness.getCurrentGroup();
  95. if (group != null && $scope.getSheetCount() < 5) {
  96. var promiseLoadCourses = {
  97. id: "promiseLoadCourses",
  98. groups: ["coursesView"]
  99. };
  100. if ($scope.weekId.id == null) {
  101. WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses)
  102. .then($scope.onWeekReceived);
  103. }
  104. else {
  105. var diff = Math.round(($scope.getMonday($scope.selectedDate) - $scope.weekId.date) / 604800000);
  106. var weekId = $scope.weekId.id + diff;
  107. WeeksBusiness.getWeek({
  108. group_id: group.Id,
  109. type_id: group.Type,
  110. week_id: weekId
  111. }, promiseLoadCourses).then($scope.onWeekReceived);
  112. }
  113. }
  114. else {
  115. $scope.courses = null;
  116. }
  117. };
  118. $scope.weekDiff = function(date1, date2)
  119. {
  120. return Math.round(($scope.getMonday(date1) - $scope.getMonday(date2)) / 604800000)
  121. };
  122. $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, $scope.loadWeeks);
  123. $scope.$watch(function(){return $scope.selectedDate}, function(newValue, oldValue)
  124. {
  125. if (oldValue == null || newValue == oldValue) {
  126. return;
  127. }
  128. var diff = $scope.weekDiff($scope.selectedDate, oldValue);
  129. if (diff != 0) {
  130. $scope.loadWeeks();
  131. }
  132. });
  133. }]);