app.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. 'use strict';
  2. /* eslint-disable */
  3. /* eslint-env jquery */
  4. /* global moment, tui, chance */
  5. /* global findCalendar, CalendarList, ScheduleList, generateSchedule */
  6. (function(window, Calendar) {
  7. var cal, resizeThrottled;
  8. var useCreationPopup = false;
  9. var useDetailPopup = true;
  10. var datePicker, selectedCalendar;
  11. cal = new Calendar('#calendar', {
  12. defaultView: 'month',
  13. useCreationPopup: useCreationPopup,
  14. useDetailPopup: useDetailPopup,
  15. calendars: CalendarList,
  16. template: {
  17. milestone: function(model) {
  18. return '<span class="calendar-font-icon ic-milestone-b"></span> <span style="background-color: ' + model.bgColor + '">' + model.title + '</span>';
  19. },
  20. allday: function(schedule) {
  21. return getTimeTemplate(schedule, true);
  22. },
  23. time: function(schedule) {
  24. return getTimeTemplate(schedule, false);
  25. }
  26. }
  27. });
  28. // event handlers
  29. cal.on({
  30. 'clickMore': function(e) {
  31. console.log('clickMore', e);
  32. },
  33. 'clickSchedule': function(e) {
  34. console.log('clickSchedule', e);
  35. },
  36. 'clickDayname': function(date) {
  37. console.log('clickDayname', date);
  38. },
  39. 'beforeCreateSchedule': function(e) {
  40. console.log('beforeCreateSchedule', e);
  41. // saveNewSchedule(e);
  42. },
  43. 'beforeUpdateSchedule': function(e) {
  44. var schedule = e.schedule;
  45. var changes = e.changes;
  46. console.log('beforeUpdateSchedule', e);
  47. if (changes && !changes.isAllDay && schedule.category === 'allday') {
  48. changes.category = 'time';
  49. }
  50. // 调用ajax更新日期
  51. changeCheckJobDate(schedule, changes);
  52. cal.updateSchedule(schedule.id, schedule.calendarId, changes);
  53. refreshScheduleVisibility();
  54. },
  55. 'beforeDeleteSchedule': function(e) {
  56. console.log('beforeDeleteSchedule', e);
  57. cal.deleteSchedule(e.schedule.id, e.schedule.calendarId);
  58. },
  59. 'afterRenderSchedule': function(e) {
  60. var schedule = e.schedule;
  61. // var element = cal.getElement(schedule.id, schedule.calendarId);
  62. // console.log('afterRenderSchedule', element);
  63. },
  64. 'clickTimezonesCollapseBtn': function(timezonesCollapsed) {
  65. console.log('timezonesCollapsed', timezonesCollapsed);
  66. if (timezonesCollapsed) {
  67. cal.setTheme({
  68. 'week.daygridLeft.width': '77px',
  69. 'week.timegridLeft.width': '77px'
  70. });
  71. } else {
  72. cal.setTheme({
  73. 'week.daygridLeft.width': '60px',
  74. 'week.timegridLeft.width': '60px'
  75. });
  76. }
  77. return true;
  78. }
  79. });
  80. /**
  81. * Get time template for time and all-day
  82. * @param {Schedule} schedule - schedule
  83. * @param {boolean} isAllDay - isAllDay or hasMultiDates
  84. * @returns {string}
  85. */
  86. function getTimeTemplate(schedule, isAllDay) {
  87. var html = [];
  88. var start = moment(schedule.start.toUTCString());
  89. if (!isAllDay) {
  90. html.push('<strong>' + start.format('HH:mm') + '</strong> ');
  91. }
  92. if (schedule.isPrivate) {
  93. html.push('<span class="calendar-font-icon ic-lock-b"></span>');
  94. html.push(' Private');
  95. } else {
  96. if (schedule.isReadOnly) {
  97. html.push('<span class="calendar-font-icon ic-readonly-b"></span>');
  98. } else if (schedule.recurrenceRule) {
  99. html.push('<span class="calendar-font-icon ic-repeat-b"></span>');
  100. } else if (schedule.attendees.length) {
  101. html.push('<span class="calendar-font-icon ic-user-b"></span>');
  102. } else if (schedule.location) {
  103. html.push('<span class="calendar-font-icon ic-location-b"></span>');
  104. }
  105. html.push(' ' + schedule.title);
  106. }
  107. return html.join('');
  108. }
  109. /**
  110. * A listener for click the menu
  111. * @param {Event} e - click event
  112. */
  113. function onClickMenu(e) {
  114. var target = $(e.target).closest('a[role="menuitem"]')[0];
  115. var action = getDataAction(target);
  116. var options = cal.getOptions();
  117. var viewName = '';
  118. console.log(target);
  119. console.log(action);
  120. switch (action) {
  121. case 'toggle-daily':
  122. viewName = 'day';
  123. break;
  124. case 'toggle-weekly':
  125. viewName = 'week';
  126. break;
  127. case 'toggle-monthly':
  128. options.month.visibleWeeksCount = 0;
  129. viewName = 'month';
  130. break;
  131. case 'toggle-weeks2':
  132. options.month.visibleWeeksCount = 2;
  133. viewName = 'month';
  134. break;
  135. case 'toggle-weeks3':
  136. options.month.visibleWeeksCount = 3;
  137. viewName = 'month';
  138. break;
  139. case 'toggle-narrow-weekend':
  140. options.month.narrowWeekend = !options.month.narrowWeekend;
  141. options.week.narrowWeekend = !options.week.narrowWeekend;
  142. viewName = cal.getViewName();
  143. target.querySelector('input').checked = options.month.narrowWeekend;
  144. break;
  145. case 'toggle-start-day-1':
  146. options.month.startDayOfWeek = options.month.startDayOfWeek ? 0 : 1;
  147. options.week.startDayOfWeek = options.week.startDayOfWeek ? 0 : 1;
  148. viewName = cal.getViewName();
  149. target.querySelector('input').checked = options.month.startDayOfWeek;
  150. break;
  151. case 'toggle-workweek':
  152. options.month.workweek = !options.month.workweek;
  153. options.week.workweek = !options.week.workweek;
  154. viewName = cal.getViewName();
  155. target.querySelector('input').checked = !options.month.workweek;
  156. break;
  157. default:
  158. break;
  159. }
  160. cal.setOptions(options, true);
  161. cal.changeView(viewName, true);
  162. setDropdownCalendarType();
  163. setRenderRangeText();
  164. setSchedules();
  165. }
  166. function onClickNavi(e) {
  167. var action = getDataAction(e.target);
  168. switch (action) {
  169. case 'move-prev':
  170. cal.prev();
  171. break;
  172. case 'move-next':
  173. cal.next();
  174. break;
  175. case 'move-today':
  176. cal.today();
  177. break;
  178. default:
  179. return;
  180. }
  181. setRenderRangeText();
  182. setSchedules();
  183. }
  184. function onNewSchedule() {
  185. var title = $('#new-schedule-title').val();
  186. var location = $('#new-schedule-location').val();
  187. var isAllDay = document.getElementById('new-schedule-allday').checked;
  188. var start = datePicker.getStartDate();
  189. var end = datePicker.getEndDate();
  190. var calendar = selectedCalendar ? selectedCalendar : CalendarList[0];
  191. if (!title) {
  192. return;
  193. }
  194. cal.createSchedules([{
  195. id: String(chance.guid()),
  196. calendarId: calendar.id,
  197. title: title,
  198. isAllDay: isAllDay,
  199. start: start,
  200. end: end,
  201. category: isAllDay ? 'allday' : 'time',
  202. dueDateClass: '',
  203. color: calendar.color,
  204. bgColor: calendar.bgColor,
  205. dragBgColor: calendar.bgColor,
  206. borderColor: calendar.borderColor,
  207. raw: {
  208. location: location
  209. },
  210. state: 'Busy'
  211. }]);
  212. $('#modal-new-schedule').modal('hide');
  213. }
  214. function onChangeNewScheduleCalendar(e) {
  215. var target = $(e.target).closest('a[role="menuitem"]')[0];
  216. var calendarId = getDataAction(target);
  217. changeNewScheduleCalendar(calendarId);
  218. }
  219. function changeNewScheduleCalendar(calendarId) {
  220. var calendarNameElement = document.getElementById('calendarName');
  221. var calendar = findCalendar(calendarId);
  222. var html = [];
  223. html.push('<span class="calendar-bar" style="background-color: ' + calendar.bgColor + '; border-color:' + calendar.borderColor + ';"></span>');
  224. html.push('<span class="calendar-name">' + calendar.name + '</span>');
  225. calendarNameElement.innerHTML = html.join('');
  226. selectedCalendar = calendar;
  227. }
  228. function createNewSchedule(event) {
  229. var start = event.start ? new Date(event.start.getTime()) : new Date();
  230. var end = event.end ? new Date(event.end.getTime()) : moment().add(1, 'hours').toDate();
  231. if (useCreationPopup) {
  232. cal.openCreationPopup({
  233. start: start,
  234. end: end
  235. });
  236. }
  237. }
  238. function saveNewSchedule(scheduleData) {
  239. var calendar = scheduleData.calendar || findCalendar(scheduleData.calendarId);
  240. var schedule = {
  241. id: String(chance.guid()),
  242. title: scheduleData.title,
  243. isAllDay: scheduleData.isAllDay,
  244. start: scheduleData.start,
  245. end: scheduleData.end,
  246. category: scheduleData.isAllDay ? 'allday' : 'time',
  247. dueDateClass: '',
  248. color: calendar.color,
  249. bgColor: calendar.bgColor,
  250. dragBgColor: calendar.bgColor,
  251. borderColor: calendar.borderColor,
  252. location: scheduleData.location,
  253. raw: {
  254. class: scheduleData.raw['class']
  255. },
  256. state: scheduleData.state
  257. };
  258. if (calendar) {
  259. schedule.calendarId = calendar.id;
  260. schedule.color = calendar.color;
  261. schedule.bgColor = calendar.bgColor;
  262. schedule.borderColor = calendar.borderColor;
  263. }
  264. cal.createSchedules([schedule]);
  265. refreshScheduleVisibility();
  266. }
  267. function onChangeCalendars(e) {
  268. var calendarId = e.target.value;
  269. var checked = e.target.checked;
  270. var viewAll = document.querySelector('.lnb-calendars-item input');
  271. var calendarElements = Array.prototype.slice.call(document.querySelectorAll('#calendarList input'));
  272. var allCheckedCalendars = true;
  273. if (calendarId === 'all') {
  274. allCheckedCalendars = checked;
  275. calendarElements.forEach(function(input) {
  276. var span = input.parentNode;
  277. input.checked = checked;
  278. span.style.backgroundColor = checked ? span.style.borderColor : 'transparent';
  279. });
  280. CalendarList.forEach(function(calendar) {
  281. calendar.checked = checked;
  282. });
  283. } else {
  284. findCalendar(calendarId).checked = checked;
  285. allCheckedCalendars = calendarElements.every(function(input) {
  286. return input.checked;
  287. });
  288. if (allCheckedCalendars) {
  289. viewAll.checked = true;
  290. } else {
  291. viewAll.checked = false;
  292. }
  293. }
  294. refreshScheduleVisibility();
  295. }
  296. // 更新日历图
  297. function changeCheckJobDate(schedule, changes) {
  298. console.log(schedule)
  299. if(schedule.calendarId === "5"){
  300. alert("已完成的任务不可更改日期")
  301. return
  302. }
  303. // 处理日期Tue Sep 14 2021 00:00:00, Tue Sep 14 2021 23:59:59
  304. const startTime = moment(new Date(changes.start)).format("YYYY-MM-DD")
  305. const endTime = moment(new Date(changes.end)).format("YYYY-MM-DD")
  306. $.ajax({
  307. type: "put",
  308. url: '/api/check/jobs/' + schedule.id,
  309. method: 'PUT',
  310. data: JSON.stringify({ id:schedule.id, startTime: startTime, endTime: endTime }),
  311. dataType: 'json',
  312. headers: getHeader(),
  313. success: function (response) {
  314. alert("日期更改成功")
  315. // 更新localStorage的数据
  316. ScheduleList.forEach((item) => {
  317. if(item.id == schedule.id){
  318. // alert("找:" + new Date(changes.start) + "到:" + new Date(changes.end))
  319. item.start = new Date(changes.start);
  320. item.end = new Date(changes.end);
  321. return;
  322. }
  323. })
  324. localStorage.setItem('scheduleList', JSON.stringify(ScheduleList))
  325. },
  326. error: function (xhr,textStatus,errorThrown) {
  327. ajaxError(xhr);
  328. }
  329. });
  330. }
  331. function refreshScheduleVisibility() {
  332. var calendarElements = Array.prototype.slice.call(document.querySelectorAll('#calendarList input'));
  333. CalendarList.forEach(function(calendar) {
  334. cal.toggleSchedules(calendar.id, !calendar.checked, false);
  335. });
  336. cal.render(true);
  337. calendarElements.forEach(function(input) {
  338. var span = input.nextElementSibling;
  339. span.style.backgroundColor = input.checked ? span.style.borderColor : 'transparent';
  340. });
  341. }
  342. function setDropdownCalendarType() {
  343. var calendarTypeName = document.getElementById('calendarTypeName');
  344. var calendarTypeIcon = document.getElementById('calendarTypeIcon');
  345. var options = cal.getOptions();
  346. var type = cal.getViewName();
  347. var iconClassName;
  348. if (type === 'day') {
  349. type = 'Daily';
  350. iconClassName = 'calendar-icon ic_view_day';
  351. } else if (type === 'week') {
  352. type = 'Weekly';
  353. iconClassName = 'calendar-icon ic_view_week';
  354. } else if (options.month.visibleWeeksCount === 2) {
  355. type = '2 weeks';
  356. iconClassName = 'calendar-icon ic_view_week';
  357. } else if (options.month.visibleWeeksCount === 3) {
  358. type = '3 weeks';
  359. iconClassName = 'calendar-icon ic_view_week';
  360. } else {
  361. type = 'Monthly';
  362. iconClassName = 'calendar-icon ic_view_month';
  363. }
  364. calendarTypeName.innerHTML = type;
  365. calendarTypeIcon.className = iconClassName;
  366. }
  367. function currentCalendarDate(format) {
  368. var currentDate = moment([cal.getDate().getFullYear(), cal.getDate().getMonth(), cal.getDate().getDate()]);
  369. return currentDate.format(format);
  370. }
  371. function setRenderRangeText() {
  372. var renderRange = document.getElementById('renderRange');
  373. var options = cal.getOptions();
  374. var viewName = cal.getViewName();
  375. var html = [];
  376. if (viewName === 'day') {
  377. html.push(currentCalendarDate('YYYY.MM.DD'));
  378. } else if (viewName === 'month' &&
  379. (!options.month.visibleWeeksCount || options.month.visibleWeeksCount > 4)) {
  380. html.push(currentCalendarDate('YYYY.MM'));
  381. } else {
  382. html.push(moment(cal.getDateRangeStart().getTime()).format('YYYY.MM.DD'));
  383. html.push(' ~ ');
  384. html.push(moment(cal.getDateRangeEnd().getTime()).format(' MM.DD'));
  385. }
  386. renderRange.innerHTML = html.join('');
  387. }
  388. function setSchedules() {
  389. cal.clear();
  390. generateSchedule(cal.getViewName(), cal.getDateRangeStart(), cal.getDateRangeEnd());
  391. cal.createSchedules(ScheduleList);
  392. refreshScheduleVisibility();
  393. }
  394. function setEventListener() {
  395. $('#menu-navi').on('click', onClickNavi);
  396. $('.dropdown-menu a[role="menuitem"]').on('click', onClickMenu);
  397. $('#lnb-calendars').on('change', onChangeCalendars);
  398. $('#btn-save-schedule').on('click', onNewSchedule);
  399. $('#btn-new-schedule').on('click', createNewSchedule);
  400. $('#dropdownMenu-calendars-list').on('click', onChangeNewScheduleCalendar);
  401. window.addEventListener('resize', resizeThrottled);
  402. }
  403. function getDataAction(target) {
  404. return target.dataset ? target.dataset.action : target.getAttribute('data-action');
  405. }
  406. resizeThrottled = tui.util.throttle(function() {
  407. cal.render();
  408. }, 50);
  409. window.cal = cal;
  410. setDropdownCalendarType();
  411. setRenderRangeText();
  412. setSchedules();
  413. setEventListener();
  414. })(window, tui.Calendar);
  415. // set calendars
  416. (function() {
  417. var calendarList = document.getElementById('calendarList');
  418. var html = [];
  419. CalendarList.forEach(function(calendar) {
  420. html.push('<div class="lnb-calendars-item"><label>' +
  421. '<input type="checkbox" class="tui-full-calendar-checkbox-round" value="' + calendar.id + '" checked>' +
  422. '<span style="border-color: ' + calendar.borderColor + '; background-color: ' + calendar.borderColor + ';"></span>' +
  423. '<span>' + calendar.name + '</span>' +
  424. '</label></div>'
  425. );
  426. });
  427. calendarList.innerHTML = html.join('\n');
  428. })();