|
| 1 | + |
| 2 | +var icsFormatter = function() { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + if (navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('MSIE 10') == -1) { |
| 6 | + console.log('Unsupported Browser'); |
| 7 | + return; |
| 8 | + } |
| 9 | + |
| 10 | + var SEPARATOR = (navigator.appVersion.indexOf('Win') !== -1) ? '\r\n' : '\n'; |
| 11 | + var calendarEvents = []; |
| 12 | + var calendarStart = [ |
| 13 | + 'BEGIN:VCALENDAR', |
| 14 | + 'VERSION:2.0' |
| 15 | + ].join(SEPARATOR); |
| 16 | + var calendarEnd = SEPARATOR + 'END:VCALENDAR'; |
| 17 | + |
| 18 | + return { |
| 19 | + /** |
| 20 | + * Returns events array |
| 21 | + * @return {array} Events |
| 22 | + */ |
| 23 | + 'events': function() { |
| 24 | + return calendarEvents; |
| 25 | + }, |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns calendar |
| 29 | + * @return {string} Calendar in iCalendar format |
| 30 | + */ |
| 31 | + 'calendar': function() { |
| 32 | + return calendarStart + SEPARATOR + calendarEvents.join(SEPARATOR) + calendarEnd; |
| 33 | + }, |
| 34 | + |
| 35 | + /** |
| 36 | + * Add event to the calendar |
| 37 | + * @param {string} subject Subject/Title of event |
| 38 | + * @param {string} description Description of event |
| 39 | + * @param {string} location Location of event |
| 40 | + * @param {string} begin Beginning date of event |
| 41 | + * @param {string} stop Ending date of event |
| 42 | + */ |
| 43 | + 'addEvent': function(subject, description, location, begin, stop) { |
| 44 | + // I'm not in the mood to make these optional... So they are all required |
| 45 | + if (typeof subject === 'undefined' || |
| 46 | + typeof description === 'undefined' || |
| 47 | + typeof location === 'undefined' || |
| 48 | + typeof begin === 'undefined' || |
| 49 | + typeof stop === 'undefined' |
| 50 | + ) { |
| 51 | + return false; |
| 52 | + }; |
| 53 | + |
| 54 | + //TODO add time and time zone? use moment to format? |
| 55 | + var start_date = new Date(begin); |
| 56 | + var end_date = new Date(stop); |
| 57 | + |
| 58 | + var start_year = ("0000" + (start_date.getFullYear().toString())).slice(-4); |
| 59 | + var start_month = ("00" + ((start_date.getMonth() + 1).toString())).slice(-2); |
| 60 | + var start_day = ("00" + ((start_date.getDate()).toString())).slice(-2); |
| 61 | + var start_hours = ("00" + (start_date.getHours().toString())).slice(-2); |
| 62 | + var start_minutes = ("00" + (start_date.getMinutes().toString())).slice(-2); |
| 63 | + var start_seconds = ("00" + (start_date.getMinutes().toString())).slice(-2); |
| 64 | + |
| 65 | + var end_year = ("0000" + (end_date.getFullYear().toString())).slice(-4); |
| 66 | + var end_month = ("00" + ((end_date.getMonth() + 1).toString())).slice(-2); |
| 67 | + var end_day = ("00" + ((end_date.getDate()).toString())).slice(-2); |
| 68 | + var end_hours = ("00" + (end_date.getHours().toString())).slice(-2); |
| 69 | + var end_minutes = ("00" + (end_date.getMinutes().toString())).slice(-2); |
| 70 | + var end_seconds = ("00" + (end_date.getMinutes().toString())).slice(-2); |
| 71 | + |
| 72 | + // Since some calendars don't add 0 second events, we need to remove time if there is none... |
| 73 | + var start_time = ''; |
| 74 | + var end_time = ''; |
| 75 | + if (start_minutes + start_seconds + end_minutes + end_seconds != 0) { |
| 76 | + start_time = 'T' + start_hours + start_minutes + start_seconds; |
| 77 | + end_time = 'T' + end_hours + end_minutes + end_seconds; |
| 78 | + } |
| 79 | + |
| 80 | + var start = start_year + start_month + start_day + start_time; |
| 81 | + var end = end_year + end_month + end_day + end_time; |
| 82 | + |
| 83 | + var calendarEvent = [ |
| 84 | + 'BEGIN:VEVENT', |
| 85 | + 'CLASS:PUBLIC', |
| 86 | + 'DESCRIPTION:' + description, |
| 87 | + 'DTSTART;VALUE=DATE:' + start, |
| 88 | + 'DTEND;VALUE=DATE:' + end, |
| 89 | + 'LOCATION:' + location, |
| 90 | + 'SUMMARY;LANGUAGE=en-us:' + subject, |
| 91 | + 'TRANSP:TRANSPARENT', |
| 92 | + 'END:VEVENT' |
| 93 | + ].join(SEPARATOR); |
| 94 | + |
| 95 | + calendarEvents.push(calendarEvent); |
| 96 | + return calendarEvent; |
| 97 | + }, |
| 98 | + |
| 99 | + /** |
| 100 | + * Download calendar using the saveAs function from filesave.js |
| 101 | + * @param {string} filename Filename |
| 102 | + * @param {string} ext Extention |
| 103 | + */ |
| 104 | + 'download': function(filename, ext) { |
| 105 | + if (calendarEvents.length < 1) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + ext = (typeof ext !== 'undefined') ? ext : '.ics'; |
| 110 | + filename = (typeof filename !== 'undefined') ? filename : 'calendar'; |
| 111 | + var calendar = calendarStart + SEPARATOR + calendarEvents.join(SEPARATOR) + calendarEnd; |
| 112 | + |
| 113 | + window.open( "data:text/calendar;charset=utf8," + escape(calendar)); |
| 114 | + } |
| 115 | + }; |
| 116 | +}; |
0 commit comments