Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

WeeksBusiness.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. (function()
  2. {
  3. angular.module('appSdk')
  4. .factory('WeeksBusiness', ['WeeksDataAccess', '$q', function (WeeksDataAccess, $q) {
  5. function isDateInCourse(course, date)
  6. {
  7. return course.BeginDate < date && course.EndDate > date;
  8. }
  9. var Business = {};
  10. Business.cachedWeeks = [];
  11. Business.currentWeekId = null;
  12. Business.cacheWeek = function(week, data)
  13. {
  14. data = {
  15. week: week,
  16. data: data,
  17. time: new Date()
  18. };
  19. Business.cachedWeeks.push(data);
  20. };
  21. Business.getCachedWeek = function(data)
  22. {
  23. var limitDate = new Date();
  24. limitDate = new Date(limitDate.getTime() - 10 * 60000);
  25. var week = Business.cachedWeeks.find(function(week)
  26. {
  27. return data.group_id == week.data.group_id &&
  28. data.type_id == week.data.type_id &&
  29. data.week_id == week.data.week_id &&
  30. limitDate <= week.time;
  31. });
  32. if (week != null) {
  33. return week.week;
  34. }
  35. return null;
  36. };
  37. Business.formatWeek = function(data)
  38. {
  39. data.DayList.forEach(function(day) {
  40. day.DateTime = new Date(day.DateTime);
  41. day.CourseList.forEach(function (course) {
  42. course.BeginDate = new Date(course.BeginDate);
  43. var dt = new Date(course.BeginDate);
  44. var secs = dt.getSeconds() + (60 * dt.getMinutes()) + (60 * 60 * dt.getHours());
  45. course.BeginPercent = secs / (60 * 60 * 24) * 100;
  46. course.BeginSeconds = secs;
  47. course.EndDate = new Date(course.EndDate);
  48. dt = new Date(course.EndDate);
  49. secs = dt.getSeconds() + (60 * dt.getMinutes()) + (60 * 60 * dt.getHours());
  50. course.EndPercent = secs / (60 * 60 * 24) * 100;
  51. course.EndSeconds = secs;
  52. });
  53. });
  54. data.DayList.forEach(function(day) {
  55. day.CourseList.forEach(function (course) {
  56. course.ParallelCoursesCount = 1;
  57. course.ParallelCoursesPosition = 0;
  58. day.CourseList.forEach(function (course2) {
  59. if (course.Id == course2.Id) {
  60. return;
  61. }
  62. var beginBeforeEndIn = (course2.BeginDate <= course.BeginDate && isDateInCourse(course, course2.EndDate));
  63. var beginInEndAfter = (course2.EndDate >= course.EndDate && isDateInCourse(course, course2.BeginDate));
  64. var beginBeforeEndAfter = (course2.BeginDate <= course.BeginDate && course2.EndDate >= course.EndDate);
  65. var beginInEndIn = (isDateInCourse(course, course2.EndDate) && isDateInCourse(course, course2.BeginDate));
  66. if (beginBeforeEndIn || beginInEndAfter || beginBeforeEndAfter || beginInEndIn) {
  67. ++course.ParallelCoursesCount;
  68. course2.ParallelCoursesPosition = Math.max(course.ParallelCoursesPosition, course2.ParallelCoursesPosition) + 1
  69. }
  70. });
  71. });
  72. });
  73. };
  74. Business.getWeek = function(data, promise)
  75. {
  76. var defer = $q.defer();
  77. var week = Business.getCachedWeek(data);
  78. if (week == null) {
  79. WeeksDataAccess.getWeek(data, promise)
  80. .then(function (week) {
  81. Business.formatWeek(week);
  82. Business.cacheWeek(week, data);
  83. defer.resolve(week);
  84. }, defer.reject);
  85. }
  86. else {
  87. defer.resolve(week);
  88. }
  89. return defer.promise;
  90. };
  91. Business.getCurrentWeek = function(data, promise)
  92. {
  93. data.week_id = Business.currentWeekId;
  94. var defer = $q.defer();
  95. var week = Business.getCachedWeek(data);
  96. if (week == null) {
  97. WeeksDataAccess.getCurrentWeek(data, promise)
  98. .then(function (week) {
  99. Business.formatWeek(week);
  100. Business.currentWeekId = week.Id;
  101. data.week_id = week.Id;
  102. Business.cacheWeek(week, data);
  103. defer.resolve(week);
  104. }, defer.reject);
  105. }
  106. else {
  107. defer.resolve(week);
  108. }
  109. return defer.promise;
  110. };
  111. return Business;
  112. }]);
  113. })();