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

100 lines
2.3 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: function() {
let options = [];
let bestOptions = [];
2015-11-03 12:57:41 +01:00
// can not evaluate answer type free text
if (this.get('poll.isFreeText')) {
2015-11-03 12:57:41 +01:00
return [];
}
// can not evaluate a poll without users
if (Ember.isEmpty(this.get('poll.users'))) {
2015-11-03 12:57:41 +01:00
return [];
}
this.get('poll.users').forEach(function(user) {
user.get('selections').forEach(function(selection, i) {
if (options.length - 1 < i) {
2015-11-03 12:57:41 +01:00
options.push({
answers: [],
key: i,
score: 0
});
}
if (typeof options[i].answers[selection.get('type')] === 'undefined') {
2015-11-03 12:57:41 +01:00
options[i].answers[selection.get('type')] = 0;
}
options[i].answers[selection.get('type')]++;
switch (selection.get('type')) {
case 'yes':
options[i].score += 2;
break;
case 'maybe':
options[i].score += 1;
break;
case 'no':
options[i].score -= 2;
break;
}
});
});
options.sort(function(a, b) {
return a.score < b.score;
});
bestOptions.push(
options[0]
);
let i = 1;
2016-01-20 03:19:10 +01:00
while (true) {
2015-11-03 12:57:41 +01:00
if (
typeof options[i] !== 'undefined' &&
bestOptions[0].score === options[i].score
) {
bestOptions.push(
options[i]
);
2016-01-20 03:19:10 +01:00
} else {
2015-11-03 12:57:41 +01:00
break;
}
i++;
}
bestOptions.forEach((bestOption, i) => {
if (this.get('poll.isFindADate')) {
bestOptions[i].title = this.get('dates')[bestOption.key].title;
2016-01-20 03:19:10 +01:00
} else {
bestOptions[i].title = this.get('poll.options')[bestOption.key].title;
2015-11-03 12:57:41 +01:00
}
});
return bestOptions;
}.property('poll.users.@each'),
2016-01-20 03:19:10 +01:00
evaluationBestOptionsMultiple: 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;
}
}.property('evaluationBestOptions'),
2016-01-20 03:19:10 +01:00
evaluationLastParticipation: function() {
2015-11-03 12:57:41 +01:00
return this.get('sortedUsers.lastObject.creationDate');
}.property('sortedUsers.@each'),
2016-01-20 03:19:10 +01:00
evaluationParticipants: function() {
2015-11-03 12:57:41 +01:00
return this.get('poll.users.length');
}.property('poll.users.@each')
});