Commit 07b5b6d5 authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

ICalAlarm: basic stuff & type()

parent 0067a085
'use strict';
/**
* @author Sebastian Pekarek
* @module alarm
* @constructor ICalAlarm Alarm
*/
var ICalAlarm = function(_data, event) {
var vars,
i,
data;
if(!event) {
throw '`event` option required!';
}
vars = {
types: ['display', 'audio']
};
data = {
type: null,
trigger: null,
repeat: null,
repeatInterval: null,
attach: null,
description: null
};
/**
* Set/Get the alarm type
*
* @param type Type
* @since 0.2.1
* @returns {ICalAlarm|String}
*/
this.type = function(type) {
if(!type) {
return data.type;
}
if(vars.types.indexOf(type) === -1) {
throw '`type` is not correct, must be either `display` or `audio`!';
}
data.type = type;
return this;
};
/**
* Set/Get seconds before event to trigger alarm
*
* @param {Number|Date} trigger Seconds before alarm triggeres
* @since 0.2.1
* @returns {ICalAlarm|Number|Date}
*/
this.trigger = function(trigger) {
if(!trigger && data.trigger instanceof Date) {
return data.trigger;
}
if(!trigger) {
return -1 * data.trigger;
}
if(trigger instanceof Date) {
data.trigger = trigger;
}
else if(typeof trigger === 'number') {
data.trigger = -1 * trigger;
}
else {
throw '`trigger` is not correct, must be either typeof `Number` or `Date`!';
}
data.trigger = -1 * trigger;
return this;
};
/**
* Set/Get seconds after event to trigger alarm
*
* @param {Number|Date} trigger Seconds after alarm triggeres
* @since 0.2.1
* @returns {ICalAlarm|Number|Date}
*/
this.triggerAfter = function(trigger) {
if(!trigger) {
return data.trigger;
}
return this.trigger(trigger instanceof Number ? -1 * trigger : trigger);
};
/**
* Set/Get seconds before event to trigger alarm
*
* @param {Number|Date} trigger Seconds before alarm triggeres
* @since 0.2.1
* @alias trigger
* @returns {ICalAlarm|Number|Date}
*/
this.triggerBefore = this.trigger;
this.repeat = function() {
};
/**
* Export Event to iCal
*
* @since 0.2.0
* @returns {String}
*/
this.generate = function() {
var tools = require('./_tools.js'),
g = 'BEGIN:VALARM\n';
/*
REPEAT:4
DURATION:PT15M
ATTACH;FMTTYPE=audio/basic:ftp://host.com/pub/sounds/bell-01.aud
*/
if(!data.type) {
throw 'No value for `type` in ICalAlarm given!';
}
if(!data.trigger) {
throw 'No value for `trigger` in ICalAlarm given!';
}
// ACTION
g += 'ACTION:' + data.type.toUpperCase() + '\n';
if(data.trigger instanceof Date) {
g += 'TRIGGER;VALUE=DATE-TIME:' + tools.formatDate(data.trigger) + '\n';
}
else if(data.trigger > 0) {
g += 'TRIGGER;RELATED=END:P5M\n'; // @todo
}
else {
g += 'TRIGGER:-P10M\n'; // @todo
}
g += 'END:VALARM\n';
return g;
};
for(i in _data) {
if(_data.hasOwnProperty(i) && ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat'].indexOf(i) > -1) {
this[i](_data[i]);
}
}
};
module.exports = ICalAlarm;
\ No newline at end of file
......@@ -30,6 +30,7 @@ var ICalEvent = function(_data) {
description: null,
organizer: null,
attendees: [],
alarms: [],
method: null,
status: null,
url: null
......@@ -378,6 +379,41 @@ var ICalEvent = function(_data) {
};
/**
* Create a new Alarm and return the alarm object…
*
* @param [alarmData] Alarm-Options
* @since 0.2.1
* @returns {ICalAlarm}
*/
this.createAlarm = function(alarmData) {
var ICalAlarm = require('./alarm.js'),
alarm = new ICalAlarm(alarmData, this);
data.alarms.push(alarm);
return alarm;
};
/**
* Get all alarms or add alarms…
*
* @since 0.2.0
* @returns {ICalAlarms[]|ICalEvent}
*/
this.alarms = function(alarms) {
if(!alarms) {
return data.alarms;
}
var cal = this;
alarms.forEach(function(e) {
cal.createAlarm(e);
});
return cal;
};
/**
* Set/Get the event's method
*
......@@ -536,7 +572,7 @@ var ICalEvent = function(_data) {
for(i in _data) {
if(_data.hasOwnProperty(i) && ['id', 'uid', 'start', 'end', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'method', 'status', 'url'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && ['id', 'uid', 'start', 'end', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url'].indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -794,6 +794,46 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
});
describe('createAlarm()', function() {
it('should return a ICalAlarm instance', function() {
var cal = ical(),
event = cal.createEvent(),
ICalAlarm = require('../lib/alarm.js');
assert.ok(event.createAlarm() instanceof ICalAlarm);
});
it('should pass data to instance', function() {
var cal = ical(),
event = cal.createEvent(),
attendee = event.createAlarm({type: 'audio'});
assert.equal(attendee.type(), 'audio');
});
});
describe('alarms()', function() {
it('getter should return an array of alarms…', function() {
var cal = ical(),
event = cal.createEvent(),
alarm;
assert.equal(event.alarms().length, 0);
alarm = event.createAlarm();
assert.equal(event.alarms().length, 1);
assert.deepEqual(event.alarms()[0], alarm);
});
it('setter should add alarms and return this', function() {
var cal = ical(),
event = cal.createEvent(),
foo = event.alarms([{type: 'audio'}, {type: 'display'}]);
assert.equal(event.alarms().length, 2);
assert.deepEqual(foo, event);
});
});
describe('method()', function() {
it('setter should return this', function() {
var e = ical().createEvent();
......@@ -1270,4 +1310,44 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
});
});
describe('ICalAlarm', function() {
it('shouldn\'t work without event reference', function() {
var ICalAlarm = require('../lib/alarm.js');
assert.throws(function() {
new ICalAlarm({type: 'display'});
}, /`event`/);
});
describe('type()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.type('display'));
});
it('getter should return value', function() {
var e = ical().createEvent().createAlarm().type('display');
assert.equal(e.type(), 'display');
});
it('should throw error when type not allowed', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.type('BANANA');
}, /`type`/);
});
it('should change something', function() {
var cal = ical(),
event = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
});
event.createAlarm({type: 'display', trigger: 60 * 10});
assert.ok(cal.toString().indexOf('ACTION:DISPLAY') > -1);
});
});
});
});
\ No newline at end of file
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