calendar.init.js 15 KB

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