Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Merge pull request #44 from jrjohnson/14-sortevents
Browse files Browse the repository at this point in the history
Sort events for the calendar
  • Loading branch information
stopfstedt committed Feb 23, 2016
2 parents 5ad8ef2 + 34f0320 commit e2f572f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
19 changes: 19 additions & 0 deletions addon/components/ilios-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import moment from 'moment';

const { Component, computed, RSVP, copy } = Ember;
const { PromiseArray } = DS;
const { sort } = computed;

export default Component.extend({
layout,
Expand Down Expand Up @@ -59,6 +60,24 @@ export default Component.extend({
promise: defer.promise
});
}),
sortedCalendarEvents: sort('compiledCalendarEvents', function(a, b){
let startDiff = moment(a.startDate).diff(moment(b.startDate));
if (startDiff !== 0) {
return startDiff;
}

let durrationA = moment(a.startDate).diff(moment(a.endDate));
let durrationB = moment(b.startDate).diff(moment(b.endDate));

let durationDiff = durrationA - durrationB;

if (durationDiff !== 0) {
return durationDiff;
}

return a.title - b.title;

}),
actions: {
changeView(newView){
this.sendAction('changeView', newView);
Expand Down
2 changes: 1 addition & 1 deletion addon/templates/components/ilios-calendar.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{{/if}}
<h1 class="{{if compiledCalendarEvents.isFulfilled 'loaded'}}">{{fa-icon 'spinner' spin=true}} {{loadingMessage}}</h1>
{{component (concat "ilios-calendar-" selectedView)
calendarEvents=compiledCalendarEvents.content
calendarEvents=sortedCalendarEvents
date=selectedDate
selectEvent='selectEvent'
changeDate='changeDate'
Expand Down
9 changes: 9 additions & 0 deletions tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export default Controller.extend({
events.pushObject(smallGroupsEvent);
}

let sortedMonthStartDate = moment().add(7, 'day').hour(10);
for(let i = 5; i > 0; i--) {
let thisEvent = this.createUserEventObject();
thisEvent.startDate = sortedMonthStartDate.clone().add(i, 'minutes');
thisEvent.name = 'Session: ' + i;
thisEvent.endDate = thisEvent.startDate.clone().add(1, 'hour');
events.pushObject(thisEvent);
}

let promise = RSVP.resolve(events);
return PromiseArray.create({
promise: promise
Expand Down

0 comments on commit e2f572f

Please sign in to comment.