Skip to content

Commit 10f83df

Browse files
author
matthias
committed
Initial commit of the js Files + Changes to README
1 parent c38225d commit 10f83df

File tree

3 files changed

+175
-1
lines changed

3 files changed

+175
-1
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
11
icsFormatter
22
============
33

4-
My fork + changes of/to https://github.com/nwcell/ics.js
4+
My fork of the original ics.js
5+
6+
I took out the FileSaver.js and Blob.js parts as it better fit my needs.
7+
Please have a look at the original repository!! -- https://github.com/nwcell/ics.js
8+
(At least thats where I got my fork)
9+
10+
PLEASE NOTE: I only tested this simplification with single events and on Chrome/Firefox !!
11+
12+
13+
My changes where applied to the download() function - have a look yourself :-)
14+
15+
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:
16+
17+
Example:
18+
---------
19+
var buildICSEntry = function( javascriptExampleDateObject ){
20+
21+
var calEntry = icsFormatter();
22+
23+
var title = 'sometitlestring';
24+
var place = 'our secret meeting place';
25+
var begin = new Date(javascriptExampleDateObject);
26+
var end = new Date(Beginn.getTime() + 30*60000);
27+
28+
var description = 'A very long and boring description of what is the agenda of this super exclusiv pow-wow';
29+
30+
calEntry.addEvent(title,description, place, begin.toUTCString(), begin.toUTCString());
31+
calEntry.download('ourSecretMeeting');
32+
}
33+
34+
35+
/* Original Readme End */
36+
37+
Credits
38+
------------------
39+
* [Travis Krause](https://github.com/nwcell): Me
40+
* [Kyle Hornberg](https://github.com/khornberg): Added multi event functionality and made everything a package firendly
41+
42+
/* Original Readme End */

demo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
4+
/* See the README or send me a mail if you need help to get this one running :-) */
5+
6+
7+
var buildICSEntry = function( javascriptExampleDateObject ){
8+
9+
var calEntry = icsFormatter();
10+
11+
var title = 'sometitlestring';
12+
var place = 'our secret meeting place';
13+
var begin = new Date(javascriptExampleDateObject);
14+
var end = new Date(Beginn.getTime() + 30*60000);
15+
16+
var description = 'A very long and boring description of what is the agenda of this super exclusiv pow-wow';
17+
18+
calEntry.addEvent(title,description, place, begin.toUTCString(), begin.toUTCString());
19+
calEntry.download('ourSecretMeeting');
20+
}

icsFormatter.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)