decide.nolog.cz/app/components/poll-evaluation-summary.js

86 lines
2 KiB
JavaScript
Raw Normal View History

2015-11-03 12:57:41 +01:00
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['evaluation-summary'],
evaluationBestOptions: Ember.computed('poll.users.[]', function() {
2015-11-03 12:57:41 +01:00
// can not evaluate answer type free text
if (this.get('poll.isFreeText')) {
return undefined;
2015-11-03 12:57:41 +01:00
}
// can not evaluate a poll without users
if (Ember.isEmpty(this.get('poll.users'))) {
return undefined;
2015-11-03 12:57:41 +01:00
}
let answers = this.get('poll.answers').reduce((answers, answer) => {
answers[answer.get('type')] = 0;
return answers;
}, {});
let evaluation = this.get('poll.options').map((option) => {
return {
answers: Ember.copy(answers),
option,
score: 0
};
});
let bestOptions = [];
this.get('poll.users').forEach(function(user) {
user.get('selections').forEach(function(selection, i) {
evaluation[i].answers[selection.get('type')]++;
2015-11-03 12:57:41 +01:00
switch (selection.get('type')) {
case 'yes':
evaluation[i].score += 2;
2015-11-03 12:57:41 +01:00
break;
case 'maybe':
evaluation[i].score += 1;
2015-11-03 12:57:41 +01:00
break;
case 'no':
evaluation[i].score -= 2;
2015-11-03 12:57:41 +01:00
break;
}
});
});
evaluation.sort(function(a, b) {
return b.score - a.score;
2015-11-03 12:57:41 +01:00
});
let bestScore = evaluation[0].score;
for (let i = 0; i < evaluation.length; i++) {
2015-11-03 12:57:41 +01:00
if (
bestScore === evaluation[i].score
2015-11-03 12:57:41 +01:00
) {
bestOptions.push(
evaluation[i]
2015-11-03 12:57:41 +01:00
);
2016-01-20 03:19:10 +01:00
} else {
2015-11-03 12:57:41 +01:00
break;
}
}
return bestOptions;
}),
2015-11-03 12:57:41 +01:00
evaluationBestOptionsMultiple: Ember.computed('evaluationBestOptions', function() {
2015-11-03 12:57:41 +01:00
if (this.get('evaluationBestOptions.length') > 1) {
return true;
2016-01-20 03:19:10 +01:00
} else {
2015-11-03 12:57:41 +01:00
return false;
}
}),
2015-11-03 12:57:41 +01:00
evaluationLastParticipation: Ember.computed('sortedUsers.[]', function() {
2015-11-03 12:57:41 +01:00
return this.get('sortedUsers.lastObject.creationDate');
}),
2015-11-03 12:57:41 +01:00
evaluationParticipants: Ember.computed('poll.users.[]', function() {
2015-11-03 12:57:41 +01:00
return this.get('poll.users.length');
})
2015-11-03 12:57:41 +01:00
});