diff --git a/assets/js/common/date.js b/assets/js/common/date.js index 12aa9823..fcc11959 100644 --- a/assets/js/common/date.js +++ b/assets/js/common/date.js @@ -103,6 +103,15 @@ const ComunicDate = { ComunicWeb.common.date = ComunicDate; +/** + * Add a day to a date + * + * @param {Date} date Target date + */ +function addOneDay(date) { + return new Date(date.getTime() + 1000*60*60*24); +} + /** * Get all the days of a specified range * @@ -115,8 +124,8 @@ function getDaysOfRange(start, end) { while (curr < end) { list.push(curr); - curr = new Date(curr.getTime() + 1000*60*60*24); + curr = addOneDay(curr); } return list; -} \ No newline at end of file +} diff --git a/assets/js/components/presence/PresenceHelper.js b/assets/js/components/presence/PresenceHelper.js index da7a13a4..d8c947ea 100644 --- a/assets/js/components/presence/PresenceHelper.js +++ b/assets/js/components/presence/PresenceHelper.js @@ -11,6 +11,10 @@ class Presence { this.month = month; this.day = day; } + + get date() { + return new Date(this.year, this.month - 1, this.day); + } } diff --git a/assets/js/pages/groups/pages/presence.js b/assets/js/pages/groups/pages/presence.js index 04d5166a..bcbff971 100644 --- a/assets/js/pages/groups/pages/presence.js +++ b/assets/js/pages/groups/pages/presence.js @@ -30,30 +30,64 @@ class GroupPresencePage { right: 'dayGridMonth,listMonth' }, initialView: 'dayGridMonth', - + // Data source events: async function(info, success, failure) { try { - const presence = await ForezPresenceHelper.GetList(group.id); - const users = await getUsers([...new Set(presence.map(e => e.userID))]); - success(presence.map((e) => { + let presences = await ForezPresenceHelper.GetList(group.id); + const users = await getUsers([...new Set(presences.map(e => e.userID))]); + + presences.sort((one, two) => { + if (one.userID < two.userID) + return -1; + + if (one.userID > two.userID) + return 1; + + return one.date - two.date; + }) + + presences.forEach((e) => e.end = addOneDay(e.date)); + + console.log(presences) + + for(let i = 0; i < presences.length; i++) { + while(true) { + if (presences.length == i + 1) + break; + + let curr = presences[i]; + let next = presences[i + 1]; + + if(curr.userID != next.userID || curr.year != next.year || curr.month != next.month || curr.day != next.day - 1) + break; + + curr.end = next.end; + presences.splice(i + 1, 1) + } + } + + const events = presences.map((e) => { return { title: users.get(e.userID).fullName, - start: new Date(e.year, e.month - 1, e.day), + start: e.date, + end: e.end, backgroundColor: "#0073b7", //Blue borderColor: "#0073b7", //Blue editable: e.userID == userID(), allDay: true, description: users.get(e.userID).fullName } - })); - + }); + + success(events); + } catch(e) { console.error(e); failure(e); } }, - + // Update events eventResize: async function(info) { try { @@ -63,7 +97,7 @@ class GroupPresencePage { notify(tr("Failed to update presence!"), "danger") } }, - + // Add new event dateClick: async function(info) { if (lastClick == null || new Date().getTime() - lastClick.getTime() > 500) @@ -71,10 +105,10 @@ class GroupPresencePage { lastClick = new Date() return; } - + try { await ForezPresenceHelper.AddDay(group.id, info.date.getFullYear(), info.date.getMonth() + 1, info.date.getDate()) - + calendar.getEventSources()[0].refetch() } catch(e) { console.error(e); @@ -82,7 +116,7 @@ class GroupPresencePage { } } }); - + calendar.render() } } \ No newline at end of file