-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend Ionic2-Calender to display recurring events. #682
base: ionic8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import {Injectable} from '@angular/core'; | ||
import {Observable, Subject} from 'rxjs'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import * as moment from 'moment' | ||
import 'moment-recur-ts' | ||
|
||
import {ICalendarComponent, IView, CalendarMode, QueryMode} from './calendar.interface'; | ||
import { ICalendarComponent, IView, CalendarMode, QueryMode, IEvent, IOccurence } from './calendar.interface'; | ||
|
||
@Injectable() | ||
export class CalendarService { | ||
|
@@ -159,4 +161,41 @@ export class CalendarService { | |
update() { | ||
this.slideUpdated.next(); | ||
} | ||
|
||
getEventOccurences(event: IEvent, utcStartTime: number, utcEndTime: number): IOccurence[] { | ||
let occurences: IOccurence[] = [] | ||
if( event.startTime > event.endTime) { | ||
return occurences | ||
} | ||
if (!event.rruleFreq) { | ||
let eventUTCStartTime: number | ||
let eventUTCEndTime: number | ||
if (event.allDay) { | ||
eventUTCStartTime = event.startTime.getTime() | ||
eventUTCEndTime = event.endTime.getTime() | ||
} else { | ||
eventUTCStartTime = Date.UTC(event.startTime.getFullYear(), event.startTime.getMonth(), event.startTime.getDate()) | ||
eventUTCEndTime = Date.UTC(event.endTime.getFullYear(), event.endTime.getMonth(), event.endTime.getDate() + 1) | ||
} | ||
if( eventUTCEndTime > utcStartTime && eventUTCStartTime < utcEndTime ) { | ||
occurences.push({ | ||
eventUTCStartTime: eventUTCStartTime, | ||
eventUTCEndTime: eventUTCEndTime | ||
}) | ||
} | ||
return occurences | ||
} | ||
if(event.rruleUntil && event.rruleUntil.getTime() < utcStartTime ){ | ||
return occurences; | ||
} | ||
for( let match of moment(event.startTime.getDate()).recur(utcStartTime,utcEndTime).every(event.rruleInterval, event.rruleFreq).all('L') ){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems not correct, the range should be
Also instead of introducing a separate rruleUntil field on IEvent, could we just leverage the existing endTime field? Basically startTime and endTime field determine the total period of recurring event, rruleFreq and rruleInterval determine what's the pattern of the recurring event. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you fix this logic? utcStartTime and utcEndTime is the range, for example, month start and month end. But the recurring event could start and end in the middle of the month. So it should be like this: moment().recur(max(utcStartTime, event.startTime.getDate()), min(utcEndTime, event.rruleUntil.getDate())).every(xxxxx) |
||
let matchDate = new Date(match) | ||
let eventUTCStartTime = Date.UTC(matchDate.getFullYear(), matchDate.getMonth(), matchDate.getDate()) | ||
occurences.push({ | ||
eventUTCStartTime: eventUTCStartTime, | ||
eventUTCEndTime: eventUTCStartTime + 86399999 | ||
}) | ||
} | ||
return occurences | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of introducing a separate rruleUntil field on IEvent, could we just leverage the existing endTime field? Basically startTime and endTime field determine the total period of recurring event, rruleFreq and rruleInterval determine what's the pattern of the recurring event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think it is a good idea. startTime and endTime determinates the start and end time of the event.
By recurring events only the time counts not the date.
If I want to create a recurring event for all thusday 13:00 - 14:00 until 2/11/2025 I need all 3 fields: startTime endTime and rruleUntil also. Only for allDay == true events can you ignore endTime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if endTime is needed, maybe I didn't get the logic in below code. eventUTCEndTime is only set based on eventUTCStartTime.
eventUTCEndTime: eventUTCStartTime + 86400000
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moment(eventTime).recure(start,end) delivers the list of dates when an event recures.
So eventUTCStartTime is not the start time of the event but the start time of the day on which the event happens. That is why I've selected as eventUTCEndTime the end time of this day. May be adding 86399999 is correct. ( I'm a chemist not a mathematician :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sense