Adding additional stuff

This commit is contained in:
Travis Krause
2017-09-14 11:19:37 -05:00
parent 1425bdfad3
commit ce02e5d8a9
+65 -65
View File
@@ -1,7 +1,7 @@
/* global saveAs, Blob, BlobBuilder, console */ /* global saveAs, Blob, BlobBuilder, console */
/* exported ics */ /* exported ics */
var ics = function( uidDomain, prodId ) { var ics = function(uidDomain, prodId) {
'use strict'; 'use strict';
if (navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('MSIE 10') == -1) { if (navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('MSIE 10') == -1) {
@@ -16,7 +16,7 @@ var ics = function( uidDomain, prodId ) {
var calendarEvents = []; var calendarEvents = [];
var calendarStart = [ var calendarStart = [
'BEGIN:VCALENDAR', 'BEGIN:VCALENDAR',
'PRODID:'+prodId, 'PRODID:' + prodId,
'VERSION:2.0' 'VERSION:2.0'
].join(SEPARATOR); ].join(SEPARATOR);
var calendarEnd = SEPARATOR + 'END:VCALENDAR'; var calendarEnd = SEPARATOR + 'END:VCALENDAR';
@@ -47,7 +47,7 @@ var ics = function( uidDomain, prodId ) {
* @param {string} begin Beginning date of event * @param {string} begin Beginning date of event
* @param {string} stop Ending date of event * @param {string} stop Ending date of event
*/ */
'addEvent': function(subject, description, location, begin, stop, rrule) { 'addEvent': function(subject, description, location, begin, stop, rule) {
// I'm not in the mood to make these optional... So they are all required // I'm not in the mood to make these optional... So they are all required
if (typeof subject === 'undefined' || if (typeof subject === 'undefined' ||
typeof description === 'undefined' || typeof description === 'undefined' ||
@@ -58,58 +58,58 @@ var ics = function( uidDomain, prodId ) {
return false; return false;
} }
// validate rrule // validate rule
if (rrule) { if (rule) {
if (!rrule.rule) { if (!rule.rule) {
if (rrule.freq !== 'YEARLY' && rrule.freq !== 'MONTHLY' && rrule.freq !== 'WEEKLY' && rrule.freq !== 'DAILY') { if (rule.freq !== 'YEARLY' && rule.freq !== 'MONTHLY' && rule.freq !== 'WEEKLY' && rule.freq !== 'DAILY') {
throw "Recurrence rule frequency must be provided and be one of the following: 'YEARLY', 'MONTHLY', 'WEEKLY', or 'DAILY'"; throw "Recurrence rule frequency must be provided and be one of the following: 'YEARLY', 'MONTHLY', 'WEEKLY', or 'DAILY'";
}
if (rrule.until) {
if (isNaN(Date.parse(rrule.until))) {
throw "Recurrence rule 'until' must be a valid date string";
}
}
if (rrule.interval) {
if (isNaN(parseInt(rrule.interval))) {
throw "Recurrence rule 'interval' must be an integer";
}
}
if (rrule.count) {
if (isNaN(parseInt(rrule.count))) {
throw "Recurrence rule 'count' must be an integer";
}
}
if (typeof rrule.byday !== 'undefined') {
if ( (Object.prototype.toString.call(rrule.byday) !== '[object Array]') ) {
throw "Recurrence rule 'byday' must be an array";
} }
if (rrule.byday.length > 7) { if (rule.until) {
throw "Recurrence rule 'byday' array must not be longer than the 7 days in a week"; if (isNaN(Date.parse(rule.until))) {
throw "Recurrence rule 'until' must be a valid date string";
}
} }
// Filter any possible repeats if (rule.interval) {
rrule.byday = rrule.byday.filter(function(elem, pos) { if (isNaN(parseInt(rule.interval))) {
return rrule.byday.indexOf(elem) == pos; throw "Recurrence rule 'interval' must be an integer";
}); }
}
for (var d in rrule.byday) { if (rule.count) {
if (BYDAY_VALUES.indexOf(rrule.byday[d]) < 0) { if (isNaN(parseInt(rule.count))) {
throw "Recurrence rule 'byday' values must include only the following: 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'"; throw "Recurrence rule 'count' must be an integer";
}
} }
if (typeof rule.byday !== 'undefined') {
if ((Object.prototype.toString.call(rule.byday) !== '[object Array]')) {
throw "Recurrence rule 'byday' must be an array";
}
if (rule.byday.length > 7) {
throw "Recurrence rule 'byday' array must not be longer than the 7 days in a week";
}
// Filter any possible repeats
rule.byday = rule.byday.filter(function(elem, pos) {
return rule.byday.indexOf(elem) == pos;
});
for (var d in rule.byday) {
if (BYDAY_VALUES.indexOf(rule.byday[d]) < 0) {
throw "Recurrence rule 'byday' values must include only the following: 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'";
}
}
} }
} }
}
} }
//TODO add time and time zone? use moment to format? //TODO add time and time zone? use moment to format?
var start_date = new Date(begin); var start_date = new Date(begin);
var end_date = new Date(stop); var end_date = new Date(stop);
var now_date = new Date(); var now_date = new Date();
var start_year = ("0000" + (start_date.getFullYear().toString())).slice(-4); var start_year = ("0000" + (start_date.getFullYear().toString())).slice(-4);
var start_month = ("00" + ((start_date.getMonth() + 1).toString())).slice(-2); var start_month = ("00" + ((start_date.getMonth() + 1).toString())).slice(-2);
@@ -139,36 +139,36 @@ var ics = function( uidDomain, prodId ) {
start_time = 'T' + start_hours + start_minutes + start_seconds; start_time = 'T' + start_hours + start_minutes + start_seconds;
end_time = 'T' + end_hours + end_minutes + end_seconds; end_time = 'T' + end_hours + end_minutes + end_seconds;
} }
var now_time = 'T' + now_hours + now_minutes + now_seconds; var now_time = 'T' + now_hours + now_minutes + now_seconds;
var start = start_year + start_month + start_day + start_time; var start = start_year + start_month + start_day + start_time;
var end = end_year + end_month + end_day + end_time; var end = end_year + end_month + end_day + end_time;
var now = now_year + now_month + now_day + now_time; var now = now_year + now_month + now_day + now_time;
// recurrence rule vars // recurrence rule vars
var rruleString; var ruleString;
if (rrule) { if (rule) {
if (rrule.rule) { if (rule.rule) {
rruleString = rrule.rule; ruleString = rule.rule;
} else { } else {
rruleString = 'RRULE:FREQ=' + rrule.freq; ruleString = 'rule:FREQ=' + rule.freq;
if (rrule.until) { if (rule.until) {
var uDate = new Date(Date.parse(rrule.until)).toISOString(); var uDate = new Date(Date.parse(rule.until)).toISOString();
rruleString += ';UNTIL=' + uDate.substring(0, uDate.length - 13).replace(/[-]/g, '') + '000000Z'; ruleString += ';UNTIL=' + uDate.substring(0, uDate.length - 13).replace(/[-]/g, '') + '000000Z';
} }
if (rrule.interval) { if (rule.interval) {
rruleString += ';INTERVAL=' + rrule.interval; ruleString += ';INTERVAL=' + rule.interval;
} }
if (rrule.count) { if (rule.count) {
rruleString += ';COUNT=' + rrule.count; ruleString += ';COUNT=' + rule.count;
} }
if (rrule.byday && rrule.byday.length > 0) { if (rule.byday && rule.byday.length > 0) {
rruleString += ';BYDAY=' + rrule.byday.join(','); ruleString += ';BYDAY=' + rule.byday.join(',');
} }
} }
} }
@@ -176,7 +176,7 @@ var ics = function( uidDomain, prodId ) {
var calendarEvent = [ var calendarEvent = [
'BEGIN:VEVENT', 'BEGIN:VEVENT',
'UID:'+calendarEvents.length+"@"+uidDomain, 'UID:' + calendarEvents.length + "@" + uidDomain,
'CLASS:PUBLIC', 'CLASS:PUBLIC',
'DESCRIPTION:' + description, 'DESCRIPTION:' + description,
'DTSTAMP;VALUE=DATE-TIME:' + now, 'DTSTAMP;VALUE=DATE-TIME:' + now,
@@ -188,8 +188,8 @@ var ics = function( uidDomain, prodId ) {
'END:VEVENT' 'END:VEVENT'
]; ];
if (rruleString) { if (ruleString) {
calendarEvent.splice(4, 0, rruleString); calendarEvent.splice(4, 0, ruleString);
} }
calendarEvent = calendarEvent.join(SEPARATOR); calendarEvent = calendarEvent.join(SEPARATOR);