Commit 3f0d0117 authored by Sebastian's avatar Sebastian
Browse files

Merge branch 'develop'

parents f69806a1 525c1eeb
...@@ -30,8 +30,8 @@ module.exports.formatDate = function formatDate(d, dateonly, floating) { ...@@ -30,8 +30,8 @@ module.exports.formatDate = function formatDate(d, dateonly, floating) {
// For information about this format, see RFC 5545, section 3.3.5 // For information about this format, see RFC 5545, section 3.3.5
// https://tools.ietf.org/html/rfc5545#section-3.3.5 // https://tools.ietf.org/html/rfc5545#section-3.3.5
module.exports.formatDateTZ = function formatDateTZ(property, date, eventData) { module.exports.formatDateTZ = function formatDateTZ(property, date, eventData) {
var tzParam = ''; var tzParam = '',
var floating = eventData.floating; floating = eventData.floating;
if(eventData.timezone) { if(eventData.timezone) {
tzParam = ';TZID=' + eventData.timezone; tzParam = ';TZID=' + eventData.timezone;
...@@ -106,7 +106,9 @@ module.exports.toJSON = function(object, attributes, options) { ...@@ -106,7 +106,9 @@ module.exports.toJSON = function(object, attributes, options) {
return; return;
} }
var value = object[attribute](); var value = object[attribute](),
newObj;
if(options.hooks[attribute]) { if(options.hooks[attribute]) {
value = options.hooks[attribute](value); value = options.hooks[attribute](value);
} }
...@@ -117,7 +119,7 @@ module.exports.toJSON = function(object, attributes, options) { ...@@ -117,7 +119,7 @@ module.exports.toJSON = function(object, attributes, options) {
result[attribute] = value; result[attribute] = value;
if(Array.isArray(result[attribute])) { if(Array.isArray(result[attribute])) {
var newObj = []; newObj = [];
result[attribute].forEach(function(object) { result[attribute].forEach(function(object) {
newObj.push(object.toJSON()); newObj.push(object.toJSON());
}); });
......
...@@ -38,9 +38,13 @@ var ICalAlarm = function(_data, event) { ...@@ -38,9 +38,13 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|String} * @returns {ICalAlarm|String}
*/ */
this.type = function(type) { this.type = function(type) {
if(!type) { if(type === undefined) {
return data.type; return data.type;
} }
if(!type) {
data.type = null;
return this;
}
if(vars.types.indexOf(type) === -1) { if(vars.types.indexOf(type) === -1) {
throw '`type` is not correct, must be either `display` or `audio`!'; throw '`type` is not correct, must be either `display` or `audio`!';
...@@ -59,13 +63,21 @@ var ICalAlarm = function(_data, event) { ...@@ -59,13 +63,21 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|Number|Date} * @returns {ICalAlarm|Number|Date}
*/ */
this.trigger = function(trigger) { this.trigger = function(trigger) {
if(!trigger && data.trigger instanceof Date) { if(trigger === undefined && data.trigger instanceof Date) {
return data.trigger; return data.trigger;
} }
if(!trigger) { if(trigger === undefined && data.trigger) {
return -1 * data.trigger; return -1 * data.trigger;
} }
if(trigger === undefined) {
return null;
}
if(!trigger) {
data.trigger = null;
return this;
}
if(trigger instanceof Date) { if(trigger instanceof Date) {
data.trigger = trigger; data.trigger = trigger;
return this; return this;
...@@ -87,7 +99,7 @@ var ICalAlarm = function(_data, event) { ...@@ -87,7 +99,7 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|Number|Date} * @returns {ICalAlarm|Number|Date}
*/ */
this.triggerAfter = function(trigger) { this.triggerAfter = function(trigger) {
if(!trigger) { if(trigger === undefined) {
return data.trigger; return data.trigger;
} }
...@@ -113,9 +125,13 @@ var ICalAlarm = function(_data, event) { ...@@ -113,9 +125,13 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|Number} * @returns {ICalAlarm|Number}
*/ */
this.repeat = function(repeat) { this.repeat = function(repeat) {
if(!repeat) { if(repeat === undefined) {
return data.repeat; return data.repeat;
} }
if(!repeat) {
data.repeat = null;
return this;
}
if(typeof repeat !== 'number' || !isFinite(repeat)) { if(typeof repeat !== 'number' || !isFinite(repeat)) {
throw '`repeat` is not correct, must be numeric!'; throw '`repeat` is not correct, must be numeric!';
...@@ -134,9 +150,13 @@ var ICalAlarm = function(_data, event) { ...@@ -134,9 +150,13 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|Number|Null} * @returns {ICalAlarm|Number|Null}
*/ */
this.interval = function(interval) { this.interval = function(interval) {
if(!interval) { if(interval === undefined) {
return data.interval; return data.interval;
} }
if(!interval) {
data.interval = null;
return this;
}
if(typeof interval !== 'number' || !isFinite(interval)) { if(typeof interval !== 'number' || !isFinite(interval)) {
throw '`interval` is not correct, must be numeric!'; throw '`interval` is not correct, must be numeric!';
...@@ -155,9 +175,13 @@ var ICalAlarm = function(_data, event) { ...@@ -155,9 +175,13 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|Object} * @returns {ICalAlarm|Object}
*/ */
this.attach = function(_attach) { this.attach = function(_attach) {
if(!_attach) { if(_attach === undefined) {
return data.attach; return data.attach;
} }
if(!_attach) {
data.attach = null;
return this;
}
var attach = null; var attach = null;
if(typeof _attach === 'string') { if(typeof _attach === 'string') {
...@@ -196,9 +220,13 @@ var ICalAlarm = function(_data, event) { ...@@ -196,9 +220,13 @@ var ICalAlarm = function(_data, event) {
* @returns {ICalAlarm|String} * @returns {ICalAlarm|String}
*/ */
this.description = function(description) { this.description = function(description) {
if(!description) { if(description === undefined) {
return data.description; return data.description;
} }
if(!description) {
data.description = null;
return this;
}
data.description = description; data.description = description;
return this; return this;
......
...@@ -70,11 +70,11 @@ var ICalAttendee = function(_data, event) { ...@@ -70,11 +70,11 @@ var ICalAttendee = function(_data, event) {
* @returns {ICalAttendee|String} * @returns {ICalAttendee|String}
*/ */
this.name = function(name) { this.name = function(name) {
if(!name) { if(name === undefined) {
return data.name; return data.name;
} }
data.name = name; data.name = name || null;
return this; return this;
}; };
...@@ -124,6 +124,10 @@ var ICalAttendee = function(_data, event) { ...@@ -124,6 +124,10 @@ var ICalAttendee = function(_data, event) {
if(status === undefined) { if(status === undefined) {
return data.status; return data.status;
} }
if(!status) {
data.status = null;
return this;
}
data.status = getAllowedStatus(status); data.status = getAllowedStatus(status);
return this; return this;
...@@ -141,6 +145,10 @@ var ICalAttendee = function(_data, event) { ...@@ -141,6 +145,10 @@ var ICalAttendee = function(_data, event) {
if(type === undefined) { if(type === undefined) {
return data.type; return data.type;
} }
if(!type) {
data.type = null;
return this;
}
data.type = getAllowedType(type); data.type = getAllowedType(type);
return this; return this;
}; };
...@@ -154,9 +162,16 @@ var ICalAttendee = function(_data, event) { ...@@ -154,9 +162,16 @@ var ICalAttendee = function(_data, event) {
* @returns {ICalAttendee|String} * @returns {ICalAttendee|String}
*/ */
this.delegatedTo = function(delegatedTo) { this.delegatedTo = function(delegatedTo) {
if(!delegatedTo) { if(delegatedTo === undefined) {
return data.delegatedTo; return data.delegatedTo;
} }
if(!delegatedTo) {
data.delegatedTo = null;
if(data.status === 'DELEGATED') {
data.status = null;
}
return this;
}
data.delegatedTo = delegatedTo; data.delegatedTo = delegatedTo;
data.status = 'DELEGATED'; data.status = 'DELEGATED';
...@@ -172,11 +187,11 @@ var ICalAttendee = function(_data, event) { ...@@ -172,11 +187,11 @@ var ICalAttendee = function(_data, event) {
* @returns {ICalAttendee|String} * @returns {ICalAttendee|String}
*/ */
this.delegatedFrom = function(delegatedFrom) { this.delegatedFrom = function(delegatedFrom) {
if(!delegatedFrom) { if(delegatedFrom === undefined) {
return data.delegatedFrom; return data.delegatedFrom;
} }
data.delegatedFrom = delegatedFrom; data.delegatedFrom = delegatedFrom || null;
return this; return this;
}; };
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
*/ */
var ICalCalendar = function(_data) { var ICalCalendar = function(_data) {
var data = {}, var data = {},
attributes = ['domain', 'prodId', 'name', 'timezone', 'ttl', 'url', 'events'], attributes = ['domain', 'prodId', 'name', 'description', 'timezone', 'ttl', 'url', 'events'],
generate, generate,
i; i;
...@@ -34,6 +34,11 @@ var ICalCalendar = function(_data) { ...@@ -34,6 +34,11 @@ var ICalCalendar = function(_data) {
g += 'X-WR-CALNAME:' + data.name + '\r\n'; g += 'X-WR-CALNAME:' + data.name + '\r\n';
} }
// Description
if(data.description) {
g += 'X-WR-CALDESC:' + data.description + '\r\n';
}
// Timezone // Timezone
if(data.timezone) { if(data.timezone) {
g += 'TIMEZONE-ID:' + data.timezone + '\r\n'; g += 'TIMEZONE-ID:' + data.timezone + '\r\n';
...@@ -188,11 +193,28 @@ var ICalCalendar = function(_data) { ...@@ -188,11 +193,28 @@ var ICalCalendar = function(_data) {
* @returns {ICalCalendar} * @returns {ICalCalendar}
*/ */
this.name = function(name) { this.name = function(name) {
if(!name) { if(name === undefined) {
return data.name; return data.name;
} }
data.name = name.toString(); data.name = name ? name.toString() : null;
return this;
};
/**
* Set/Get your feed's description…
*
* @param [description] Description
* @since 0.2.7
* @returns {ICalCalendar}
*/
this.description = function(description) {
if(description === undefined) {
return data.description;
}
data.description = description ? description.toString() : null;
return this; return this;
}; };
...@@ -222,11 +244,11 @@ var ICalCalendar = function(_data) { ...@@ -222,11 +244,11 @@ var ICalCalendar = function(_data) {
* @returns {ICalCalendar} * @returns {ICalCalendar}
*/ */
this.timezone = function(timezone) { this.timezone = function(timezone) {
if(!timezone) { if(timezone === undefined) {
return data.timezone; return data.timezone;
} }
data.timezone = timezone.toString(); data.timezone = timezone ? timezone.toString() : null;
return this; return this;
}; };
...@@ -240,7 +262,7 @@ var ICalCalendar = function(_data) { ...@@ -240,7 +262,7 @@ var ICalCalendar = function(_data) {
* @returns {ICalCalendar} * @returns {ICalCalendar}
*/ */
this.url = function(url) { this.url = function(url) {
if(!url) { if(url === undefined) {
return data.url; return data.url;
} }
...@@ -259,7 +281,7 @@ var ICalCalendar = function(_data) { ...@@ -259,7 +281,7 @@ var ICalCalendar = function(_data) {
* @returns {ICalCalendar} * @returns {ICalCalendar}
*/ */
this.ttl = function(ttl) { this.ttl = function(ttl) {
if(!ttl) { if(ttl === undefined) {
return data.ttl; return data.ttl;
} }
...@@ -296,7 +318,7 @@ var ICalCalendar = function(_data) { ...@@ -296,7 +318,7 @@ var ICalCalendar = function(_data) {
// update errors to 0.1.x version // update errors to 0.1.x version
try { try {
e = new ICalEvent(event); e = new ICalEvent(event, this);
} }
catch(err) { catch(err) {
throw err.replace(/`([\w.]+)`/i, 'event.$1'); throw err.replace(/`([\w.]+)`/i, 'event.$1');
...@@ -316,7 +338,7 @@ var ICalCalendar = function(_data) { ...@@ -316,7 +338,7 @@ var ICalCalendar = function(_data) {
*/ */
this.createEvent = function(eventData) { this.createEvent = function(eventData) {
var ICalEvent = require('./event.js'), var ICalEvent = require('./event.js'),
event = new ICalEvent(eventData); event = new ICalEvent(eventData, this);
data.events.push(event); data.events.push(event);
return event; return event;
......
...@@ -6,12 +6,16 @@ ...@@ -6,12 +6,16 @@
* @module event * @module event
* @constructor ICalEvent Event * @constructor ICalEvent Event
*/ */
var ICalEvent = function(_data) { var ICalEvent = function(_data, calendar) {
var attributes = ['id', 'uid', 'sequence', 'start', 'end', 'timezone', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url'], var attributes = ['id', 'uid', 'sequence', 'start', 'end', 'timezone', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url'],
vars, vars,
i, i,
data; data;
if(!calendar) {
throw '`calendar` option required!';
}
vars = { vars = {
allowedMethods: ['PUBLISH', 'REQUEST', 'REPLY', 'ADD', 'CANCEL', 'REFRESH', 'COUNTER', 'DECLINECOUNTER'], allowedMethods: ['PUBLISH', 'REQUEST', 'REPLY', 'ADD', 'CANCEL', 'REFRESH', 'COUNTER', 'DECLINECOUNTER'],
allowedRepeatingFreq: ['SECONDLY', 'MINUTELY', 'HOURLY', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY'], allowedRepeatingFreq: ['SECONDLY', 'MINUTELY', 'HOURLY', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY'],
...@@ -23,7 +27,7 @@ var ICalEvent = function(_data) { ...@@ -23,7 +27,7 @@ var ICalEvent = function(_data) {
sequence: 0, sequence: 0,
start: null, start: null,
end: null, end: null,
timezone: null, timezone: undefined,
stamp: new Date(), stamp: new Date(),
allDay: false, allDay: false,
floating: false, floating: false,
...@@ -126,10 +130,14 @@ var ICalEvent = function(_data) { ...@@ -126,10 +130,14 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|Date} * @returns {ICalEvent|Date}
*/ */
this.end = function(end) { this.end = function(end) {
if(!end) { if(end === undefined) {
return data.end; return data.end;
} }
if(!end) {
data.end = null;
return this;
}
if(typeof end === 'string') { if(typeof end === 'string') {
end = new Date(end); end = new Date(end);
} }
...@@ -157,12 +165,17 @@ var ICalEvent = function(_data) { ...@@ -157,12 +165,17 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|String} * @returns {ICalEvent|String}
*/ */
this.timezone = function(timezone) { this.timezone = function(timezone) {
if(!timezone) { if(timezone === undefined && data.timezone !== undefined) {
return data.timezone; return data.timezone;
} }
if(timezone === undefined) {
return calendar.timezone();
}
data.timezone = timezone.toString(); data.timezone = timezone ? timezone.toString() : null;
data.floating = false; if(data.timezone) {
data.floating = false;
}
return this; return this;
}; };
...@@ -209,7 +222,7 @@ var ICalEvent = function(_data) { ...@@ -209,7 +222,7 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|Boolean} * @returns {ICalEvent|Boolean}
*/ */
this.allDay = function(allDay) { this.allDay = function(allDay) {
if(!allDay) { if(allDay === undefined) {
return data.allDay; return data.allDay;
} }
...@@ -227,12 +240,14 @@ var ICalEvent = function(_data) { ...@@ -227,12 +240,14 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|Boolean} * @returns {ICalEvent|Boolean}
*/ */
this.floating = function(floating) { this.floating = function(floating) {
if(!floating) { if(floating === undefined) {
return data.floating; return data.floating;
} }
data.floating = !!floating; data.floating = !!floating;
data.timezone = null; if(data.floating) {
data.timezone = null;
}
return this; return this;
}; };
...@@ -245,9 +260,13 @@ var ICalEvent = function(_data) { ...@@ -245,9 +260,13 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|Object} * @returns {ICalEvent|Object}
*/ */
this.repeating = function(repeating) { this.repeating = function(repeating) {
if(!repeating) { if(repeating === undefined) {
return data.repeating; return data.repeating;
} }
if(!repeating) {
data.repeating = null;
return this;
}
if(!repeating.freq || vars.allowedRepeatingFreq.indexOf(repeating.freq.toUpperCase()) === -1) { if(!repeating.freq || vars.allowedRepeatingFreq.indexOf(repeating.freq.toUpperCase()) === -1) {
throw '`repeating.freq` is a mandatory item, and must be one of the following: ' + vars.allowedRepeatingFreq.join(', ') + '!'; throw '`repeating.freq` is a mandatory item, and must be one of the following: ' + vars.allowedRepeatingFreq.join(', ') + '!';
...@@ -341,11 +360,11 @@ var ICalEvent = function(_data) { ...@@ -341,11 +360,11 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|String} * @returns {ICalEvent|String}
*/ */
this.summary = function(summary) { this.summary = function(summary) {
if(!summary) { if(summary === undefined) {
return data.summary; return data.summary;
} }
data.summary = summary.toString(); data.summary = summary ? summary.toString() : '';
return this; return this;
}; };
...@@ -358,11 +377,11 @@ var ICalEvent = function(_data) { ...@@ -358,11 +377,11 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|String} * @returns {ICalEvent|String}
*/ */
this.location = function(location) { this.location = function(location) {
if(!location) { if(location === undefined) {
return data.location; return data.location;
} }
data.location = location.toString(); data.location = location ? location.toString() : null;
return this; return this;
}; };
...@@ -375,11 +394,11 @@ var ICalEvent = function(_data) { ...@@ -375,11 +394,11 @@ var ICalEvent = function(_data) {
* @returns {ICalEvent|String} * @returns {ICalEvent|String}