Output now passes ical validation

- A few required fields were missing for proper ical validation: PRODID,
UID, DTSTAMP
- Added optional params to configure prodid and uid
- Fixed bug: Start and End times were reporting DATE instead of
DATE-TIME, so some consumers would ignore event times
- Fixed bug: If start/end times only contained hours, no time would be
reported at all
- Added a new method: "build" which simply returns the ical contents
instead of trying to trigger a download
This commit is contained in:
Adam Brown
2016-07-31 02:00:23 -07:00
parent d0359a5e06
commit fd2d0a81aa
+33 -4
View File
@@ -1,7 +1,7 @@
/* global saveAs, Blob, BlobBuilder, console */
/* exported ics */
var ics = function() {
var ics = function( uidDomain, prodId ) {
'use strict';
if (navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('MSIE 10') == -1) {
@@ -9,10 +9,14 @@ var ics = function() {
return;
}
if (typeof uidDomain === 'undefined') { uidDomain = 'default'; }
if (typeof prodId === 'undefined') { prodId = 'Calendar'; }
var SEPARATOR = (navigator.appVersion.indexOf('Win') !== -1) ? '\r\n' : '\n';
var calendarEvents = [];
var calendarStart = [
'BEGIN:VCALENDAR',
'PRODID:'+prodId,
'VERSION:2.0'
].join(SEPARATOR);
var calendarEnd = SEPARATOR + 'END:VCALENDAR';
@@ -56,6 +60,7 @@ var ics = function() {
//TODO add time and time zone? use moment to format?
var start_date = new Date(begin);
var end_date = new Date(stop);
var now_date = new Date();
var start_year = ("0000" + (start_date.getFullYear().toString())).slice(-4);
var start_month = ("00" + ((start_date.getMonth() + 1).toString())).slice(-2);
@@ -70,24 +75,35 @@ var ics = function() {
var end_hours = ("00" + (end_date.getHours().toString())).slice(-2);
var end_minutes = ("00" + (end_date.getMinutes().toString())).slice(-2);
var end_seconds = ("00" + (end_date.getMinutes().toString())).slice(-2);
var now_year = ("0000" + (now_date.getFullYear().toString())).slice(-4);
var now_month = ("00" + ((now_date.getMonth() + 1).toString())).slice(-2);
var now_day = ("00" + ((now_date.getDate()).toString())).slice(-2);
var now_hours = ("00" + (now_date.getHours().toString())).slice(-2);
var now_minutes = ("00" + (now_date.getMinutes().toString())).slice(-2);
var now_seconds = ("00" + (now_date.getMinutes().toString())).slice(-2);
// Since some calendars don't add 0 second events, we need to remove time if there is none...
var start_time = '';
var end_time = '';
if (start_minutes + start_seconds + end_minutes + end_seconds != 0) {
if (start_hours + start_minutes + start_seconds + end_hours + end_minutes + end_seconds != 0) {
start_time = 'T' + start_hours + start_minutes + start_seconds;
end_time = 'T' + end_hours + end_minutes + end_seconds;
}
var now_time = 'T' + now_hours + now_minutes + now_seconds;
var start = start_year + start_month + start_day + start_time;
var end = end_year + end_month + end_day + end_time;
var now = now_year + now_month + now_day + now_time;
var calendarEvent = [
'BEGIN:VEVENT',
'UID:'+calendarEvents.length+"@"+uidDomain,
'CLASS:PUBLIC',
'DESCRIPTION:' + description,
'DTSTART;VALUE=DATE:' + start,
'DTEND;VALUE=DATE:' + end,
'DTSTAMP;VALUE=DATE-TIME:' + now,
'DTSTART;VALUE=DATE-TIME:' + start,
'DTEND;VALUE=DATE-TIME:' + end,
'LOCATION:' + location,
'SUMMARY;LANGUAGE=en-us:' + subject,
'TRANSP:TRANSPARENT',
@@ -121,6 +137,19 @@ var ics = function() {
blob = bb.getBlob('text/x-vCalendar;charset=' + document.characterSet);
}
saveAs(blob, filename + ext);
return calendar;
},
/**
* Build and return the ical contents
*/
'build': function() {
if (calendarEvents.length < 1) {
return false;
}
var calendar = calendarStart + SEPARATOR + calendarEvents.join(SEPARATOR) + calendarEnd;
return calendar;
}
};