diff --git a/README.md b/README.md index 894fb78..23c7c48 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,42 @@ icsFormatter ============ -My fork + changes of/to https://github.com/nwcell/ics.js +My fork of the original ics.js + +I took out the FileSaver.js and Blob.js parts as it better fit my needs. +Please have a look at the original repository!! -- https://github.com/nwcell/ics.js +(At least thats where I got my fork) + +PLEASE NOTE: I only tested this simplification with single events and on Chrome/Firefox !! + + +My changes where applied to the download() function - have a look yourself :-) + +My suggested use is to have a onclick or controller/service function (in case you use Angular or stuff) that builds your nice calendar data: + +Example: +--------- +var buildICSEntry = function( javascriptExampleDateObject ){ + + var calEntry = icsFormatter(); + + var title = 'sometitlestring'; + var place = 'our secret meeting place'; + var begin = new Date(javascriptExampleDateObject); + var end = new Date(Beginn.getTime() + 30*60000); + + var description = 'A very long and boring description of what is the agenda of this super exclusiv pow-wow'; + + calEntry.addEvent(title,description, place, begin.toUTCString(), begin.toUTCString()); + calEntry.download('ourSecretMeeting'); + } + + +/* Original Readme End */ + +Credits +------------------ +* [Travis Krause](https://github.com/nwcell): Me +* [Kyle Hornberg](https://github.com/khornberg): Added multi event functionality and made everything a package firendly + +/* Original Readme End */ \ No newline at end of file diff --git a/demo.js b/demo.js new file mode 100644 index 0000000..bf8dfd1 --- /dev/null +++ b/demo.js @@ -0,0 +1,20 @@ + + + +/* See the README or send me a mail if you need help to get this one running :-) */ + + +var buildICSEntry = function( javascriptExampleDateObject ){ + + var calEntry = icsFormatter(); + + var title = 'sometitlestring'; + var place = 'our secret meeting place'; + var begin = new Date(javascriptExampleDateObject); + var end = new Date(Beginn.getTime() + 30*60000); + + var description = 'A very long and boring description of what is the agenda of this super exclusiv pow-wow'; + + calEntry.addEvent(title,description, place, begin.toUTCString(), begin.toUTCString()); + calEntry.download('ourSecretMeeting'); + } \ No newline at end of file diff --git a/icsFormatter.js b/icsFormatter.js new file mode 100644 index 0000000..62a87b2 --- /dev/null +++ b/icsFormatter.js @@ -0,0 +1,116 @@ + +var icsFormatter = function() { + 'use strict'; + + if (navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('MSIE 10') == -1) { + console.log('Unsupported Browser'); + return; + } + + var SEPARATOR = (navigator.appVersion.indexOf('Win') !== -1) ? '\r\n' : '\n'; + var calendarEvents = []; + var calendarStart = [ + 'BEGIN:VCALENDAR', + 'VERSION:2.0' + ].join(SEPARATOR); + var calendarEnd = SEPARATOR + 'END:VCALENDAR'; + + return { + /** + * Returns events array + * @return {array} Events + */ + 'events': function() { + return calendarEvents; + }, + + /** + * Returns calendar + * @return {string} Calendar in iCalendar format + */ + 'calendar': function() { + return calendarStart + SEPARATOR + calendarEvents.join(SEPARATOR) + calendarEnd; + }, + + /** + * Add event to the calendar + * @param {string} subject Subject/Title of event + * @param {string} description Description of event + * @param {string} location Location of event + * @param {string} begin Beginning date of event + * @param {string} stop Ending date of event + */ + 'addEvent': function(subject, description, location, begin, stop) { + // I'm not in the mood to make these optional... So they are all required + if (typeof subject === 'undefined' || + typeof description === 'undefined' || + typeof location === 'undefined' || + typeof begin === 'undefined' || + typeof stop === 'undefined' + ) { + return false; + }; + + //TODO add time and time zone? use moment to format? + var start_date = new Date(begin); + var end_date = new Date(stop); + + var start_year = ("0000" + (start_date.getFullYear().toString())).slice(-4); + var start_month = ("00" + ((start_date.getMonth() + 1).toString())).slice(-2); + var start_day = ("00" + ((start_date.getDate()).toString())).slice(-2); + var start_hours = ("00" + (start_date.getHours().toString())).slice(-2); + var start_minutes = ("00" + (start_date.getMinutes().toString())).slice(-2); + var start_seconds = ("00" + (start_date.getMinutes().toString())).slice(-2); + + var end_year = ("0000" + (end_date.getFullYear().toString())).slice(-4); + var end_month = ("00" + ((end_date.getMonth() + 1).toString())).slice(-2); + var end_day = ("00" + ((end_date.getDate()).toString())).slice(-2); + 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); + + // 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) { + start_time = 'T' + start_hours + start_minutes + start_seconds; + end_time = 'T' + end_hours + end_minutes + end_seconds; + } + + var start = start_year + start_month + start_day + start_time; + var end = end_year + end_month + end_day + end_time; + + var calendarEvent = [ + 'BEGIN:VEVENT', + 'CLASS:PUBLIC', + 'DESCRIPTION:' + description, + 'DTSTART;VALUE=DATE:' + start, + 'DTEND;VALUE=DATE:' + end, + 'LOCATION:' + location, + 'SUMMARY;LANGUAGE=en-us:' + subject, + 'TRANSP:TRANSPARENT', + 'END:VEVENT' + ].join(SEPARATOR); + + calendarEvents.push(calendarEvent); + return calendarEvent; + }, + + /** + * Download calendar using the saveAs function from filesave.js + * @param {string} filename Filename + * @param {string} ext Extention + */ + 'download': function(filename, ext) { + if (calendarEvents.length < 1) { + return false; + } + + ext = (typeof ext !== 'undefined') ? ext : '.ics'; + filename = (typeof filename !== 'undefined') ? filename : 'calendar'; + var calendar = calendarStart + SEPARATOR + calendarEvents.join(SEPARATOR) + calendarEnd; + + window.open( "data:text/calendar;charset=utf8," + escape(calendar)); + } + }; +};