javascript - 使用 fullcalendar 导入 iCal (ics)?

标签 javascript fullcalendar icalendar

为了使用 fullcalendar 加载 .ics 文件,需要做什么? 不幸的是我无法使用 php 或 .net。

最佳答案

我设法做到了。没有我想象的那么难。我用ical.js作为 ics 解析器。解析后,我得到一个json对象,其中包含ics中的所有信息。然后遍历它并根据the definition of FullCalendar Event object构造事件对象.

使用 FullCalendar v4 和重复事件进行更新

作为 v4 changed the initialization code下面的代码不计入重复事件,这里有一个适用于 v4 版本的代码:

$.get(calendarUrl).then(function (data) {
     // parse the ics data
     var jcalData = ICAL.parse(data.trim());
     var comp = new ICAL.Component(jcalData);
     var eventComps = comp.getAllSubcomponents("vevent");
     // map them to FullCalendar events
     var events = $.map(eventComps, function (item) {

        if (item.getFirstPropertyValue("class") == "PRIVATE") {
            return null;
        }
        else {
            var toreturn = {
                "title": item.getFirstPropertyValue("summary"),
                "location": item.getFirstPropertyValue("location"),
            };
            var rrule=item.getFirstPropertyValue("rrule");
            if(rrule!= null){ //event recurs
                toreturn.rrule={};
                if(rrule.freq) toreturn.rrule.freq=rrule.freq;
                if(rrule.parts.BYDAY) toreturn.rrule.byweekday=rrule.parts.BYDAY;
                if(rrule.until) toreturn.rrule.until=rrule.until.toString();
                if(rrule.until) toreturn.rrule.until=rrule.until.toString();
                if(rrule.interval) toreturn.rrule.interval=rrule.interval;
                var dtstart=item.getFirstPropertyValue("dtstart").toString();
                var dtend=item.getFirstPropertyValue("dtend").toString();
                toreturn.rrule.dtstart=dtstart;
                //count duration ms
                var startdate=new Date(dtstart);
                var enddate=new Date(dtend);
                toreturn.duration = enddate - startdate;
            }else{
                toreturn.start=item.getFirstPropertyValue("dtstart").toString();
                toreturn.end=item.getFirstPropertyValue("dtend").toString();
            }
            return toreturn;
        }
     });
     var calendarEl = document.getElementById('calendar');
     var calendar = new FullCalendar.Calendar(calendarEl, {

              plugins: [ 'interaction','dayGrid','rrule' ],
              defaultView: 'dayGridWeek',
              displayEventEnd: true,
              header: {
                  left: 'prev,next',
                  center: 'title',
                  right: 'dayGridDay,dayGridWeek,dayGridMonth'
              },
              events: events,
              eventRender: function (info) {
                // console.log(info.event);
                // append location
                if (info.event.extendedProps.location != null && info.event.extendedProps.location != "") {
                    info.el.append(info.event.extendedProps.location );
                }
              }
     });
     calendar.render();
});

原始答案的代码(v3 及更低版本):

$.get(calendarUrl).then(function (data) {
// parse the ics data
var jcalData = ICAL.parse(data.trim());
var comp = new ICAL.Component(jcalData);
var eventComps = comp.getAllSubcomponents("vevent");
// console.log(JSON.stringify(eventComps));
// map them to FullCalendar events
var events = $.map(eventComps, function (item) {
    if (item.getFirstPropertyValue("class") == "PRIVATE") {
        return null;
    }
    else {
        return {
            "title": item.getFirstPropertyValue("summary") + ";",
            "start": item.getFirstPropertyValue("dtstart").toJSDate(),
            "end": item.getFirstPropertyValue("dtend").toJSDate(),
            "location": item.getFirstPropertyValue("location")
        };
    }
});

// refresh the control
calendarCtrl.fullCalendar('destroy');
calendarCtrl.fullCalendar({
    events: events,
    timeFormat: "H:mm",
    displayEventEnd: true,
    eventRender: function (event, element) {
        // console.log(element);
        // append location
        if (event.location != null && event.location != "") {
            element.append("<span>" + event.location + "</span>");
        }
    },
    header: {
        left: 'title',
        center: '',
        right: 'today,month,basicWeek,listDay prev,next'
    }
});
});

关于javascript - 使用 fullcalendar 导入 iCal (ics)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9404685/

相关文章:

javascript - 定时更新新闻

java - 单击 jsp 页面上的链接打开文件

javascript - 在 .NET core MVC 应用程序中包含带 @ 符号的 Javascript 库

javascript - fullcalendar google隐藏过去的事件

date - 如何为重复发生的事件编写 ICS 文件?

iphone - 如何在 Iphone native 应用程序中同步 Google 日历、雅虎日历和 Toodledo?

javascript - 当某个元素加载时间过长时激活 div

fullcalendar - 我可以在 FullCalendar 中调整日 View 的高度吗?

javascript - Angular 8 和完整日历组件

api - 有开放的iCalender API吗?