factory.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. starter.factory('Schedule', function ($resource, $cordovaCalendar, $q, cfg, formatFilter, showPopup) {
  2. var schedule = {};
  3. var recurrenceByDayData = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
  4. var week = ['日', '一', '二', '三', '四', '五', '六'];
  5. function getRecurrenceByDay (days) {
  6. var arr = [];
  7. _.each(days, function (item) {
  8. arr.push(recurrenceByDayData[item])
  9. })
  10. return arr.join(',');
  11. }
  12. function createEventOptions (content, start_date, end_date, repeat_type, freq, notification_type) {
  13. var options = {
  14. title: content,
  15. notes: 'Linker+',
  16. startDate: new Date(start_date),
  17. endDate: new Date(start_date)
  18. }
  19. if (repeat_type == 1) {
  20. options.recurrence = 'daily';
  21. options.recurrenceInterval = freq;
  22. } else if (repeat_type == 2) {
  23. options.recurrence = 'weekly';
  24. options.recurrenceByDay = getRecurrenceByDay(freq);
  25. } else if (repeat_type == 3) {
  26. options.recurrence = 'monthly'
  27. options.recurrenceByMonthDay = freq.join(',');
  28. } else if (repeat_type == 4) {
  29. options.recurrence = 'yearly'
  30. }
  31. if (notification_type == 1) {
  32. options.firstReminderMinutes = 0;
  33. } else if (notification_type == 2) {
  34. options.firstReminderMinutes = 5;
  35. } else if (notification_type == 3) {
  36. options.firstReminderMinutes = 15;
  37. } else if (notification_type == 4) {
  38. options.firstReminderMinutes = 30;
  39. } else if (notification_type == 5) {
  40. options.firstReminderMinutes = 60;
  41. } else if (notification_type == 6) {
  42. options.firstReminderMinutes = 120;
  43. } else if (notification_type == 7) {
  44. options.firstReminderMinutes = 300;
  45. }
  46. if (end_date) {
  47. if (!(end_date instanceof Date)) {
  48. end_date = new Date(end_date);
  49. }
  50. options.recurrenceEndDate = end_date;
  51. }
  52. return options;
  53. }
  54. schedule.scheduleDetails = {};
  55. schedule.schedule = $resource(formatFilter('{0}schedule/schedule/:id/', cfg.api));
  56. schedule.schedulemember = $resource(formatFilter('{0}schedule/schedule/:id/', cfg.api));
  57. schedule.notificationType = {
  58. "0":"不提醒",
  59. "1":"事件发生时",
  60. "2":"提前5分钟",
  61. "3":"提前15分钟",
  62. "4":"提前30分钟",
  63. "5":"提前1小时",
  64. "6":"提前2小时",
  65. "7":"提前5小时"
  66. }
  67. schedule.repeatType = {
  68. "0":"永不",
  69. "1":"每天",
  70. "2":"每周",
  71. "3":"每月",
  72. "4":"每年"
  73. }
  74. schedule.weekDay = {
  75. "1":"周一",
  76. "2":"周二",
  77. "3":"周三",
  78. "4":"周四",
  79. "5":"周五",
  80. "6":"周六",
  81. "0":"周日"
  82. }
  83. schedule.checkSchedule = function (schedules) {
  84. if (schedules.content == null && schedules.content == '') {
  85. showPopup.PopupWindow(0, '请输入日程内容', false);
  86. return false;
  87. // } else if (schedules.schedulemembers.length == 0) {
  88. // showPopup.PopupWindow(0, '请选择成员', false);
  89. // return false;
  90. } else {
  91. return true;
  92. }
  93. }
  94. schedule.createEventWithOptions = function (content, start_date, end_date, repeat_type, freq, notification_type) {
  95. var deferred = $q.defer();
  96. var options = createEventOptions(content, start_date, end_date, repeat_type, freq, notification_type);
  97. $cordovaCalendar.createEventWithOptions(options).then(function (result) {
  98. deferred.resolve(true);
  99. }, function (err) {
  100. showPopup.PopupWindow(0, '日程创建失败,请重试');
  101. deferred.resolve(false);
  102. });
  103. return deferred.promise;
  104. }
  105. schedule.deleteEvent = function (content, start_date) {
  106. var deferred = $q.defer();
  107. var options = createEventOptions(content, start_date);
  108. options.endDate = options.endDate.setMonth(options.endDate.getMonth() + 1);
  109. $cordovaCalendar.deleteEvent(options);
  110. }
  111. schedule.getRepeatFreqTitle = function (type, freq) {
  112. var title = '';
  113. //0:永不,1:每天 2:每周,3:每月,4:每年
  114. if (typeof freq === 'string')
  115. freq = freq.split(',');
  116. if (type == '0') {
  117. title = '不重复';
  118. } else if (type == '1') {
  119. title = '每' + freq + '天一次';
  120. } else if (type == '2') {
  121. _.each(freq, function (item) {
  122. title += '周' + week[item] + '、';
  123. })
  124. title = '每' + title.substring(0, title.lastIndexOf('、'));
  125. } else if (type == '3') {
  126. _.each(freq, function (item) {
  127. title += item + '日、';
  128. })
  129. // title = '每月' + freq.join('日、');
  130. title = '每月' + title.substring(0, title.lastIndexOf('、'));
  131. } else if (type == '4') {
  132. title = '每年';
  133. }
  134. return title;
  135. }
  136. schedule.getDateTimeTitle = function (date) {
  137. var title = '';
  138. if (!(date instanceof Date))
  139. date = new Date(date);
  140. var minutes_str = date.getMinutes().toString();
  141. var hours_str = date.getHours().toString();
  142. if (parseInt(minutes_str) < 10) {
  143. minutes_str = '0' + minutes_str;
  144. }
  145. if (parseInt(hours_str) < 10) {
  146. hours_str = '0' + hours_str;
  147. }
  148. year_str = date.getFullYear() + '年';
  149. title = year_str + (date.getMonth() + 1) + '月' + date.getDate() + '日 ' + ' 周' + week[date.getDay()] + ' ' + hours_str + ':' + minutes_str;
  150. return title;
  151. }
  152. schedule.getMemberTitle = function (member) {
  153. return _.map(member, function(user) {
  154. return user.username;
  155. }).join('、');
  156. }
  157. return schedule;
  158. });