Commit 19c1193f authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

ICalAlarm.trigger()

parent 07b5b6d5
......@@ -2,7 +2,7 @@
module.exports = {};
module.exports.formatDate = function _formatDate(d, dateonly, floating) {
module.exports.formatDate = function formatDate(d, dateonly, floating) {
var s;
function pad(i) {
......@@ -35,4 +35,38 @@ module.exports.escape = function escape(str) {
return '\\' + match;
});
};
module.exports.duration = function duration(seconds) {
var string = '';
// < 0
if(seconds < 0) {
string = '-';
seconds *= -1;
}
string += 'PT';
// HOURS
if(seconds >= 3600) {
string += Math.floor(seconds / 3600) + 'H';
seconds %= 3600;
}
// MINUTES
if(seconds >= 60) {
string += Math.floor(seconds / 60) + 'M';
seconds %= 60;
}
// SECONDS
if(string > 0) {
string += seconds + 'S';
}
else if(!string) {
string = '0S';
}
return string;
};
\ No newline at end of file
......@@ -66,16 +66,14 @@ var ICalAlarm = function(_data, event) {
if(trigger instanceof Date) {
data.trigger = trigger;
return this;
}
else if(typeof trigger === 'number') {
if(typeof trigger === 'number' && isFinite(trigger)) {
data.trigger = -1 * trigger;
}
else {
throw '`trigger` is not correct, must be either typeof `Number` or `Date`!';
return this;
}
data.trigger = -1 * trigger;
return this;
throw '`trigger` is not correct, must be either typeof `Number` or `Date`!';
};
/**
......@@ -90,7 +88,7 @@ var ICalAlarm = function(_data, event) {
return data.trigger;
}
return this.trigger(trigger instanceof Number ? -1 * trigger : trigger);
return this.trigger(typeof trigger === 'number' ? -1 * trigger : trigger);
};
/**
......@@ -140,10 +138,10 @@ var ICalAlarm = function(_data, event) {
g += 'TRIGGER;VALUE=DATE-TIME:' + tools.formatDate(data.trigger) + '\n';
}
else if(data.trigger > 0) {
g += 'TRIGGER;RELATED=END:P5M\n'; // @todo
g += 'TRIGGER;RELATED=END:' + tools.duration(data.trigger) + '\n';
}
else {
g += 'TRIGGER:-P10M\n'; // @todo
g += 'TRIGGER:' + tools.duration(data.trigger) + '\n';
}
g += 'END:VALARM\n';
......
......@@ -1349,5 +1349,97 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
assert.ok(cal.toString().indexOf('ACTION:DISPLAY') > -1);
});
});
describe('trigger()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.trigger(60 * 10));
});
it('getter should return value', function() {
var e = ical().createEvent().createAlarm().trigger(300),
now = new Date();
assert.equal(e.trigger(), 300);
assert.equal(e.triggerAfter(), -300);
// Date
e.trigger(now);
assert.deepEqual(e.trigger(), now);
});
it('should throw error when trigger not allowed', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.trigger(Infinity);
}, /`trigger`/);
assert.throws(function() {
a.trigger('hi');
}, /`trigger`/);
assert.throws(function() {
a.trigger(true);
}, /`trigger`/);
});
it('should change something', function() {
var cal = ical(),
event = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
}),
trigger = new Date('2015-02-01T13:38:45Z'),
alarm;
alarm = event.createAlarm({type: 'display', trigger: 60 * 10});
assert.ok(cal.toString().indexOf('TRIGGER:-PT10M') > -1);
alarm.trigger(trigger);
assert.ok(cal.toString().indexOf('TRIGGER;VALUE=DATE-TIME:20150201T133845Z') > -1);
});
});
describe('triggerAfter()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAlarm();
assert.deepEqual(a, a.triggerAfter(60 * 10));
});
it('getter should return value', function() {
var e = ical().createEvent().createAlarm().triggerAfter(300);
assert.equal(e.triggerAfter(), 300);
assert.equal(e.trigger(), -300);
});
it('should throw error when trigger not allowed', function() {
var a = ical().createEvent().createAlarm();
assert.throws(function() {
a.triggerAfter(Infinity);
}, /`trigger`/);
assert.throws(function() {
a.triggerAfter('hi');
}, /`trigger`/);
assert.throws(function() {
a.triggerAfter(true);
}, /`trigger`/);
});
it('should change something', function() {
var cal = ical(),
event = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
}),
trigger = new Date('2015-02-01T13:38:45Z'),
alarm;
alarm = event.createAlarm({type: 'display', triggerAfter: 60 * 10});
assert.ok(cal.toString().indexOf('TRIGGER;RELATED=END:PT10M') > -1);
alarm.triggerAfter(trigger);
assert.ok(cal.toString().indexOf('TRIGGER;VALUE=DATE-TIME:20150201T133845Z') > -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