|
| 1 | +import eventController from "./eventController"; |
| 2 | +import {DateRange} from "../model/dateRange"; |
| 3 | +import {iCloudCalendarEvent} from "../iCloudJs/calendar"; |
| 4 | +import {CalendarViewDetail} from "../model/calendarViewDetail"; |
| 5 | +import {CalendarView} from "../plugin/calendarView"; |
| 6 | +import {Misc} from "../misc/misc"; |
| 7 | + |
| 8 | +class CalendarViewController { |
| 9 | + async getMarkdownPostProcessor(element, context){ |
| 10 | + const codeblocks = element.querySelectorAll("code"); |
| 11 | + const codeComponents = calendarViewController.checkCodeBlocks(codeblocks); |
| 12 | + if (codeComponents == null) return; |
| 13 | + const eventList = await calendarViewController.getEventList(codeComponents); |
| 14 | + const calendarViewData = calendarViewController.getCalendarViewData( new DateRange(new Date(codeComponents.from), new Date(codeComponents.to)), eventList); |
| 15 | + context.addChild(new CalendarView(codeComponents.codeBlock, calendarViewData)); |
| 16 | + } |
| 17 | + |
| 18 | + checkCodeBlocks(codeBlocks): {codeBlock, from, to} | null { |
| 19 | + if (codeBlocks.length == 0) return null; |
| 20 | + const codeBlock = codeBlocks.item(0); |
| 21 | + const codeText = codeBlock.innerText.replaceAll(" ", ""); |
| 22 | + const isCal = codeText.substring(0, 6) == "<ical>"; |
| 23 | + if (!isCal) return null; |
| 24 | + let from = calendarViewController.matchRegex("from:", codeText); |
| 25 | + if(from == undefined) return null; |
| 26 | + from = from.replaceAll("from:", ""); |
| 27 | + let to = calendarViewController.matchRegex("to:", codeText); |
| 28 | + if(to == undefined) to = from; |
| 29 | + else to = to.replaceAll("to:", ""); |
| 30 | + return {codeBlock, from, to}; |
| 31 | + } |
| 32 | + |
| 33 | + private matchRegex(prefix, text): string | undefined{ |
| 34 | + // Constructing the regular expression pattern |
| 35 | + const pattern = `${prefix}\\d{4}(\\/|-)\\d{1,2}(\\/|-)\\d{1,2}`; |
| 36 | + const matches = text.replaceAll(" ", "").match(pattern); |
| 37 | + if (matches == null) return undefined; |
| 38 | + return matches.filter(match => match.length > 4).first(); |
| 39 | + } |
| 40 | + |
| 41 | + private async getEventList(codeComponents: { from; to }): Promise<iCloudCalendarEvent[] | []> { |
| 42 | + const dateRange = new DateRange(new Date(codeComponents.from), new Date(codeComponents.to)); |
| 43 | + return await eventController.getEventsFromRange(dateRange); |
| 44 | + } |
| 45 | + |
| 46 | + private getCalendarViewData(dateRange: DateRange, eventList: iCloudCalendarEvent[] | []): {numOfCols, numOfRows, rowNeedsLabelMap, calendarViewDetails, startDate} { |
| 47 | + const calendarViewDetails: CalendarViewDetail[] = []; |
| 48 | + const noOverlapMap = this.manageEventOverlap(eventList, dateRange); |
| 49 | + const auxStruct = this.getAuxiliaryStructure(dateRange); |
| 50 | + const rowNeedsLabelMap = new Map<number, boolean>(); |
| 51 | + const filledRows = this.fillCalendarViewDetails(noOverlapMap, rowNeedsLabelMap, auxStruct, calendarViewDetails); |
| 52 | + |
| 53 | + return { |
| 54 | + numOfCols: auxStruct.numOfCols, |
| 55 | + numOfRows: filledRows, |
| 56 | + rowNeedsLabelMap, |
| 57 | + calendarViewDetails, |
| 58 | + startDate: dateRange.start |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private getAuxiliaryStructure(dateRange: DateRange) : {numOfCols, refiner, refinerMinutes, minTimeMilli, milliInDay} { |
| 63 | + const numOfConsideredHours = 24; |
| 64 | + // Every 15 mins |
| 65 | + const refiner = 2; |
| 66 | + const refinerMinutes = 60 / refiner; |
| 67 | + const numOfCols = numOfConsideredHours * refiner; |
| 68 | + |
| 69 | + const minTimeMilli = dateRange.start.getTime(); |
| 70 | + const milliInDay = 1000 * 3600 * 24; |
| 71 | + |
| 72 | + return { |
| 73 | + numOfCols, |
| 74 | + refiner, |
| 75 | + refinerMinutes, |
| 76 | + minTimeMilli, |
| 77 | + milliInDay |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private manageEventOverlap(eventList: iCloudCalendarEvent[] | [], dateRange: DateRange) { |
| 82 | + const date = new Date(dateRange.start); |
| 83 | + const dayDiff = dateRange.getDayDifference(); |
| 84 | + |
| 85 | + const noOverlapMap = new Map<Date, iCloudCalendarEvent[][]>(); |
| 86 | + for (let i = 0; i <= dayDiff; i++){ |
| 87 | + const dayEvents = this.filterEventsWithStartDate(eventList, date); |
| 88 | + this.checkOverlaps(dayEvents, noOverlapMap, date); |
| 89 | + date.setDate(date.getDate() + 1); |
| 90 | + } |
| 91 | + return noOverlapMap; |
| 92 | + } |
| 93 | + |
| 94 | + private filterEventsWithStartDate(eventList: iCloudCalendarEvent[], date: Date): iCloudCalendarEvent[] { |
| 95 | + return eventList.filter(event => { |
| 96 | + const eventDate = Misc.getDateFromICloudArray(event.startDate); |
| 97 | + return eventDate.toLocaleDateString() == date.toLocaleDateString(); |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + private checkOverlaps(dayEvents: iCloudCalendarEvent[], noOverlapMap, date: Date){ |
| 102 | + const sortedDayEvents = Misc.sortICloudCalendarEventList(dayEvents); |
| 103 | + const toCheckList = [...sortedDayEvents]; |
| 104 | + const eventRows = []; |
| 105 | + |
| 106 | + sortedDayEvents.forEach((dayEvent, dayEventsIndex) => { |
| 107 | + if (!toCheckList.contains(dayEvent)) return; |
| 108 | + toCheckList.remove(dayEvent); |
| 109 | + const noOverlapList: iCloudCalendarEvent[] = [dayEvent]; |
| 110 | + const dateRange = new DateRange(Misc.getDateFromICloudArray(dayEvent.startDate), Misc.getDateFromICloudArray(dayEvent.endDate)); |
| 111 | + for (let i = dayEventsIndex + 1; i < sortedDayEvents.length; i++){ |
| 112 | + const nextEvent = sortedDayEvents[i]; |
| 113 | + const nextDateRange = new DateRange(Misc.getDateFromICloudArray(nextEvent.startDate), Misc.getDateFromICloudArray(nextEvent.endDate)); |
| 114 | + if (dateRange.overlaps(nextDateRange)) continue; |
| 115 | + toCheckList.remove(nextEvent); |
| 116 | + noOverlapList.push(nextEvent); |
| 117 | + } |
| 118 | + this.propagateListOverlapCheck(noOverlapList, toCheckList); |
| 119 | + eventRows.push(noOverlapList); |
| 120 | + |
| 121 | + }) |
| 122 | + noOverlapMap.set(new Date(date), eventRows); |
| 123 | + } |
| 124 | + |
| 125 | + private propagateListOverlapCheck(noOverlapList: iCloudCalendarEvent[], toCheckList: iCloudCalendarEvent[]) { |
| 126 | + |
| 127 | + noOverlapList.forEach((noOverlapEvent, noOverlapIndex) => { |
| 128 | + const noOverlapEventDateRange = new DateRange(Misc.getDateFromICloudArray(noOverlapEvent.startDate), Misc.getDateFromICloudArray(noOverlapEvent.endDate)); |
| 129 | + for (let i = noOverlapIndex + 1; i < noOverlapList.length; i++){ |
| 130 | + const check = noOverlapList[i]; |
| 131 | + const checkDateRange = new DateRange(Misc.getDateFromICloudArray(check.startDate), Misc.getDateFromICloudArray(check.endDate)); |
| 132 | + if (noOverlapEventDateRange.overlaps(checkDateRange)){ |
| 133 | + noOverlapList.remove(check); |
| 134 | + toCheckList.push(check); |
| 135 | + } |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + private fillCalendarViewDetails(noOverlapMap, rowNeedsLabelMap, auxStruct, calendarViewDetails): number { |
| 141 | + let rowIndex = 0; |
| 142 | + |
| 143 | + Array.from(noOverlapMap.entries()).forEach((noOverlapEntry) => { |
| 144 | + const eventBlocks = noOverlapEntry[1]; |
| 145 | + if (eventBlocks.length == 0){ |
| 146 | + rowNeedsLabelMap.set(rowIndex, true); |
| 147 | + rowIndex += 1; |
| 148 | + return; |
| 149 | + } |
| 150 | + let isOverlap = false; |
| 151 | + eventBlocks.forEach(noOverlapArray => { |
| 152 | + noOverlapArray.forEach(noOverlapEvent => { |
| 153 | + if (isOverlap){ |
| 154 | + rowNeedsLabelMap.set(rowIndex, false); |
| 155 | + } else { |
| 156 | + rowNeedsLabelMap.set(rowIndex, true); |
| 157 | + } |
| 158 | + const eventStartTime = Misc.getDateFromICloudArray(noOverlapEvent.startDate) |
| 159 | + const eventEndTime = Misc.getDateFromICloudArray(noOverlapEvent.endDate) |
| 160 | + const fromCol = eventStartTime.getHours() * auxStruct.refiner + eventStartTime.getMinutes() / auxStruct.refinerMinutes |
| 161 | + const toCol = eventEndTime.getHours() * auxStruct.refiner + eventEndTime.getMinutes() / auxStruct.refinerMinutes |
| 162 | + const row = rowIndex; |
| 163 | + const title = noOverlapEvent.title; |
| 164 | + const calendarViewDetail = new CalendarViewDetail(title, row, fromCol, toCol) |
| 165 | + calendarViewDetails.push(calendarViewDetail); |
| 166 | + }) |
| 167 | + isOverlap = true; |
| 168 | + rowIndex += 1; |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + return rowIndex; |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +const calendarViewController = new CalendarViewController(); |
| 177 | +export default calendarViewController; |
0 commit comments