Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
fix(week-view): show recurring events in the weekview. Closes #252
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lewis committed Jan 9, 2016
1 parent c594f39 commit 4615973
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docs/examples/recurring-events/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ angular
{
title: 'Recurs monthly',
type: 'warning',
startsAt: moment().startOf('month').toDate(),
startsAt: moment().toDate(),
recursOn: 'month'
},
{
title: 'Recurs yearly',
type: 'info',
startsAt: moment().startOf('month').toDate(),
startsAt: moment().toDate(),
recursOn: 'year'
}
];
Expand All @@ -23,7 +23,7 @@ angular
// See here: https://github.com/mattlewis92/angular-bootstrap-calendar/issues/127#issuecomment-136022090

vm.calendarView = 'month';
vm.viewDate = moment().startOf('month').toDate();
vm.viewDate = moment().toDate();
vm.isCellOpen = true;

});
42 changes: 30 additions & 12 deletions src/services/calendarHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ angular
return moment(oldEnd).add(diffInSeconds);
}

function eventIsInPeriod(event, periodStart, periodEnd) {
function getRecurringEventPeriod(eventPeriod, recursOn, containerPeriodStart) {

var eventStart = moment(event.startsAt);
var eventEnd = moment(event.endsAt || event.startsAt);
periodStart = moment(periodStart);
periodEnd = moment(periodEnd);
var eventStart = moment(eventPeriod.start);
var eventEnd = moment(eventPeriod.end);
var periodStart = moment(containerPeriodStart);

if (angular.isDefined(event.recursOn)) {
if (angular.isDefined(recursOn)) {

switch (event.recursOn) {
switch (recursOn) {
case 'year':
eventStart.set({
year: periodStart.year()
Expand All @@ -46,13 +45,26 @@ angular
break;

default:
throw new Error('Invalid value (' + event.recursOn + ') given for recurs on. Can only be year or month.');
throw new Error('Invalid value (' + recursOn + ') given for recurs on. Can only be year or month.');
}

eventEnd = adjustEndDateFromStartDiff(event.startsAt, eventStart, eventEnd);
eventEnd = adjustEndDateFromStartDiff(eventPeriod.start, eventStart, eventEnd);

}

return {start: eventStart, end: eventEnd};

}

function eventIsInPeriod(event, periodStart, periodEnd) {

periodStart = moment(periodStart);
periodEnd = moment(periodEnd);

var eventPeriod = getRecurringEventPeriod({start: event.startsAt, end: event.endsAt || event.startsAt}, event.recursOn, periodStart);
var eventStart = eventPeriod.start;
var eventEnd = eventPeriod.end;

return (eventStart.isAfter(periodStart) && eventStart.isBefore(periodEnd)) ||
(eventEnd.isAfter(periodStart) && eventEnd.isBefore(periodEnd)) ||
(eventStart.isBefore(periodStart) && eventEnd.isAfter(periodEnd)) ||
Expand Down Expand Up @@ -183,12 +195,18 @@ angular

var eventsSorted = filterEventsInPeriod(events, startOfWeek, endOfWeek).map(function(event) {

var eventStart = moment(event.startsAt).startOf('day');
var eventEnd = moment(event.endsAt || event.startsAt).startOf('day');
var weekViewStart = moment(startOfWeek).startOf('day');
var weekViewEnd = moment(endOfWeek).startOf('day');
var offset, span;

var eventPeriod = getRecurringEventPeriod({
start: moment(event.startsAt).startOf('day'),
end: moment(event.endsAt || event.startsAt).startOf('day')
}, event.recursOn, weekViewStart);

var eventStart = eventPeriod.start;
var eventEnd = eventPeriod.end;

var offset, span;
if (eventStart.isBefore(weekViewStart) || eventStart.isSame(weekViewStart)) {
offset = 0;
} else {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/services/calendarHelper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,19 @@ describe('calendarHelper', function() {

});

describe('recurring events', function() {

it('should display recuring events', function() {
weekView = calendarHelper.getWeekView([{
startsAt: new Date(2016, 0, 9, 1),
recursOn: 'month'
}], new Date(2016, 1, 9, 1));
expect(weekView.events[0].daySpan).to.equal(1);
expect(weekView.events[0].dayOffset).to.equal(2);
});

});

});

describe('getDayView', function() {
Expand Down

0 comments on commit 4615973

Please sign in to comment.