Commit 9c1c0793 authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

ICalAlarm.attach / ICalAlarm.description + tests

parent 481d2472
......@@ -146,6 +146,65 @@ var ICalAlarm = function(_data, event) {
};
/**
* Set/Get Attachment
*
* @param {Object|String} File-URI or Object
* @since 0.2.1
* @returns {ICalAlarm|Object}
*/
this.attach = function(_attach) {
if(!_attach) {
return data.attach;
}
var attach= null;
if(typeof _attach === 'string') {
attach = {
uri: _attach,
mime: null
};
}
else if(typeof _attach === 'object') {
attach = {
uri: _attach.uri,
mime: _attach.mime || null
};
}
else {
// @todo update readme url
throw '`attach` needs to be a valid formed string or an object. See https://github.com/sebbo2002/ical-generator#';
}
if(!attach.uri) {
throw '`attach.uri` is empty!';
}
data.attach = {
uri: attach.uri,
mime: attach.mime
};
return this;
};
/**
* Set/Get the alarm description
*
* @param description Description
* @since 0.2.1
* @returns {ICalAlarm|String}
*/
this.description = function(description) {
if(!description) {
return data.description;
}
data.description = description;
return this;
};
/**
* Export Event to iCal
*
......@@ -200,13 +259,32 @@ var ICalAlarm = function(_data, event) {
g += 'DURATION:' + tools.duration(data.interval) + '\n';
}
// ATTACH
if(data.type === 'audio' && data.attach && data.attach.mime) {
g += 'ATTACH;FMTTYPE=' + data.attach.mime + ':' + data.attach.uri + '\n';
}
else if(data.type === 'audio' && data.attach ) {
g += 'ATTACH;VALUE=URI:' + data.attach.uri + '\n';
}
else if(data.type === 'audio') {
g += 'ATTACH;VALUE=URI:Basso\n';
}
// DESCRIPTION
if(data.type === 'display' && data.description) {
g += 'DESCRIPTION:' + tools.escape(data.description) + '\n';
}
else if(data.type === 'display') {
g += 'DESCRIPTION:' + tools.escape(event.summary()) + '\n';
}
g += 'END:VALARM\n';
return g;
};
for(i in _data) {
if(_data.hasOwnProperty(i) && ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat', 'interval'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat', 'interval', 'attach', 'description'].indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -15,10 +15,12 @@ ACTION:DISPLAY
TRIGGER:-PT10M
REPEAT:2
DURATION:PT1M
DESCRIPTION:Sample Event
END:VALARM
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT1H
DESCRIPTION:I'm a reminder :)
END:VALARM
URL;VALUE=URI:http://sebbo.net/
METHOD:ADD
......
......@@ -1095,7 +1095,8 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
},
{
type: 'display',
trigger: 60 * 60
trigger: 60 * 60,
description: 'I\'m a reminder :)'
}
],
method: 'add',
......@@ -1541,6 +1542,107 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
});
describe('attach()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.attach('https://sebbo.net/beep.aud'));
});
it('getter should return value', function() {
var t = {uri: 'https://example.com/alarm.aud', mime: 'audio/basic'},
e = ical().createEvent().createAlarm().attach(t);
assert.deepEqual(e.attach(), t);
e.attach('https://www.example.com/beep.aud');
assert.deepEqual(e.attach(), {
uri: 'https://www.example.com/beep.aud',
mime: null
});
e.attach({
uri: 'https://www.example.com/beep.aud'
});
assert.deepEqual(e.attach(), {
uri: 'https://www.example.com/beep.aud',
mime: null
});
});
it('should throw error withour uri', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.attach({mime: 'audio/basic'});
}, /`attach.uri`/);
});
it('should throw error when unknown format', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.attach(Infinity);
}, /`attach`/);
});
it('should change something', function() {
var cal = ical(),
e = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
}),
a = e.createAlarm({
type: 'audio',
trigger: 300
});
assert.ok(cal.toString().indexOf('\nATTACH;VALUE=URI:Basso') > -1);
a.attach('https://example.com/beep.aud');
assert.ok(cal.toString().indexOf('\nATTACH;VALUE=URI:https://example.com/beep.aud') > -1);
a.attach({
uri: 'https://example.com/beep.aud',
mime: 'audio/basic'
});
assert.ok(cal.toString().indexOf('\nATTACH;FMTTYPE=audio/basic:https://example.com/beep.aud') > -1);
});
});
describe('description()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.description('Hey Ho!'));
});
it('getter should return value', function() {
var e = ical().createEvent().createAlarm().description('blablabla');
assert.deepEqual(e.description(), 'blablabla');
});
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: 300, description: 'Huibuh!'});
assert.ok(cal.toString().indexOf('\nDESCRIPTION:Huibuh') > -1);
});
it('should fallback to event summary', 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: 300});
assert.ok(cal.toString().indexOf('\nDESCRIPTION:Example Event') > -1);
});
});
describe('generate()', function() {
it('shoult throw an error without type', function() {
var a = ical().createEvent({
......
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