Commit 3372cf15 authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

cal.allDay flag

close #6
parent 1ffd7cd3
......@@ -73,6 +73,9 @@ Appointment date of beginning
#### options.end (Date Object, required)
Appointment date of end
#### options.allDay (Bool)
Appointment is for the whole day
#### options.stamp (Date Object)
Appointment date of creation
......
var a = {
newCalendar: function() {
'use strict';
var cal = {
domain: null,
prodid: '//sebbo.net//ical-generator//EN',
......@@ -45,7 +47,7 @@ var a = {
if(!e.start) {
throw 'event.start is a mandatory item.';
}
if(Object.prototype.toString.call(e.start) != '[object Date]') {
if(Object.prototype.toString.call(e.start) !== '[object Date]') {
throw 'event.start must be a Date Object.';
}
_event.start = e.start;
......@@ -54,13 +56,16 @@ var a = {
if(!e.end) {
throw 'event.end is a mandatory item.';
}
if(Object.prototype.toString.call(e.end) != '[object Date]') {
if(Object.prototype.toString.call(e.end) !== '[object Date]') {
throw 'event.end must be a Date Object.';
}
_event.end = e.end;
// allDay flag
_event.allDay = e.allDay ? true : false;
// Date Stamp
if(e.stamp && Object.prototype.toString.call(e.stamp) != '[object Date]') {
if(e.stamp && Object.prototype.toString.call(e.stamp) !== '[object Date]') {
throw 'event.stamp must be a Date Object.';
}
_event.stamp = e.stamp || new Date();
......@@ -103,12 +108,14 @@ var a = {
generate: function() {
var g;
var _formatDate = function(d, dateonly) {
var pad = function(i) {
function _formatDate(d, dateonly) {
var s;
function pad(i) {
return (i < 10 ? '0': '') + i;
};
}
var s = d.getFullYear();
s = d.getFullYear();
s += pad(d.getMonth() + 1);
s += pad(d.getDate());
......@@ -120,20 +127,21 @@ var a = {
}
return s;
};
}
var _escape = function(str) {
function _escape(str) {
return str.replace(/[\\;,\n]/g, function (match) {
if (match === '\n')
if (match === '\n') {
return '\\n';
}
return '\\' + match;
});
};
}
var _getUID = function(e) {
function _getUID(e) {
return _formatDate(e.start) + 'Z-' + e.uid + '@' + cal.domain;
};
}
// VCALENDAR and VERSION
g = 'BEGIN:VCALENDAR\nVERSION:2.0\n';
......@@ -156,14 +164,30 @@ var a = {
g += 'BEGIN:VEVENT\n';
g += 'UID:' + _getUID(e) + '\n';
g += 'DTSTAMP:' + _formatDate(e.stamp) + '\n';
g += 'DTSTART:' + _formatDate(e.start) + '\n';
g += 'DTEND:' + _formatDate(e.end) + '\n';
if(e.summary) g += 'SUMMARY:' + _escape(e.summary) + '\n';
if(e.location) g += 'LOCATION:' + _escape(e.location) + '\n';
if(e.description) g += 'DESCRIPTION:' + _escape(e.description )+ '\n';
if(e.organizer) g += 'ORGANIZER;CN="' + e.organizer.name.replace(/\"/g, '\\"') + '":mailto:' + e.organizer.email + '\n';
if(e.url) g += 'URL;VALUE=URI:' + e.url + '\n';
if(e.allDay) {
g += 'DTSTART;VALUE=DATE:' + _formatDate(e.start, true) + '\n';
g += 'DTEND;VALUE=DATE:' + _formatDate(e.end, true) + '\n';
}else{
g += 'DTSTART:' + _formatDate(e.start) + '\n';
g += 'DTEND:' + _formatDate(e.end) + '\n';
}
if(e.summary) {
g += 'SUMMARY:' + _escape(e.summary) + '\n';
}
if(e.location) {
g += 'LOCATION:' + _escape(e.location) + '\n';
}
if(e.description) {
g += 'DESCRIPTION:' + _escape(e.description )+ '\n';
}
if(e.organizer) {
g += 'ORGANIZER;CN="' + e.organizer.name.replace(/\"/g, '\\"') + '":mailto:' + e.organizer.email + '\n';
}
if(e.url) {
g += 'URL;VALUE=URI:' + e.url + '\n';
}
g += 'END:VEVENT\n';
});
......@@ -174,16 +198,24 @@ var a = {
return cal.methods;
},
save: function(path, cb) {
if(!cal.generated) cal.methods.generate();
if(!cal.generated) {
cal.methods.generate();
}
require('fs').writeFile(path, cal.generated, cb || function(){});
return cal.methods;
},
saveSync: function(path) {
if(!cal.generated) cal.methods.generate();
if(!cal.generated) {
cal.methods.generate();
}
/*jslint stupid: true */
return require('fs').writeFileSync(path, cal.generated);
},
serve: function(res) {
if(!cal.generated) cal.methods.generate();
if(!cal.generated) {
cal.methods.generate();
}
res.writeHead(200, {
'Content-Type': 'text/calendar',
......@@ -193,7 +225,9 @@ var a = {
return cal.methods;
},
toString: function() {
if(!cal.generated) cal.methods.generate();
if(!cal.generated) {
cal.methods.generate();
}
return cal.generated;
},
length: function() {
......
......@@ -4,8 +4,8 @@ PRODID:-//sebbo.net//ical-generator.tests//EN
BEGIN:VEVENT
UID:20131004T223930Z-123@sebbo.net
DTSTAMP:20131004T233453
DTSTART:20131004T223930
DTEND:20131004T231500
DTSTART;VALUE=DATE:20131004
DTEND;VALUE=DATE:20131006
SUMMARY:Sample Event
ORGANIZER;CN="Sebastian Pekarek":mailto:mail@sebbo.net
URL;VALUE=URI:http://sebbo.net/
......
......@@ -264,7 +264,7 @@ describe('ical-generator', function() {
summary: 'Simple Event'
});
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_01.ical', 'utf8'));
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_01.ics', 'utf8'));
});
it('case #2', function() {
......@@ -288,7 +288,7 @@ describe('ical-generator', function() {
description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\nbeep boop'
});
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_02.ical', 'utf8'));
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_02.ics', 'utf8'));
});
it('case #3', function() {
......@@ -305,7 +305,8 @@ describe('ical-generator', function() {
cal.addEvent({
uid: '123',
start: new Date("Fr Oct 04 2013 22:39:30"),
end: new Date("Fr Oct 04 2013 23:15:00"),
end: new Date("Fr Oct 06 2013 23:15:00"),
allDay: true,
stamp: new Date("Fr Oct 04 2013 23:34:53"),
summary: 'Sample Event',
organizer: {
......@@ -315,7 +316,7 @@ describe('ical-generator', function() {
url: 'http://sebbo.net/'
});
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_03.ical', 'utf8'));
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_03.ics', 'utf8'));
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment