diff --git a/ics.js b/ics.js index 083d963..3e518e5 100644 --- a/ics.js +++ b/ics.js @@ -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; } };