-
Notifications
You must be signed in to change notification settings - Fork 139
Parsing iCalendar
This example uses the component model layer to retrieve specific VEVENT
s from a VCALENDAR component:
// Get the basic data out
var jCalData = ICAL.parse(someICalData);
var comp = new ICAL.Component(jCalData[1]);
// Fetch the VEVENT part
var vevent = comp.getFirstSubcomponent('vevent');
var event = new ICAL.Event(vevent);
console.log(event.summary, event.uid, event.description);
// Get start and end dates as local time on current machine
console.log(event.startDate.toJSDate(), event.endDate.toJSDate());
You can also fish out specific elements and look at those:
var vtz = comp.getFirstSubcomponent('vtimezone'),
var tz = new ICAL.Timezone(vtz);
...
In ical.js we have two patterns for recurrence expansion. On the high level, we have a RecurExpansion
class that will allow you to expand occurrences, taking into account recurrence exceptions (RDATE and EXDATE) and other factors.
Expansion will always begin from the start of the event, you can't simply start somewhere in between. You can iterate occurrences until your target range, and do any caching you might need on your end. This is needed due to certain rule parts in RRULEs that might limit expansion, e.g. if you have a COUNT rule with a complex pattern, we'll not know if we've exceeded the count unless we identify each occurrence.
let expand = new ICAL.RecurExpansion({
component: vevent,
dtstart: vevent.getFirstPropertyValue('dtstart')
});
// next is always an ICAL.Time or null
let next;
while (someCondition && (next = expand.next())) {
// do something with next
}
On the lower level, we have a recurrence iterator, which will simply iterate an RRULE. You can access it via the RRULE component:
let rrule = vevent.getFirstProperty('rrule');
let dtstart = vevent.getFirstPropertyValue('dtstart')
let iter = rrule.iterator(dtstart)
let next;
for (let next = iter.next(); next && !iter.completed; next = iter.next()) {
// do something with next
}