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

108 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-11-03 12:57:41 +01:00
import Ember from 'ember';
import moment from 'moment';
2015-11-03 12:57:41 +01:00
export default Ember.Component.extend({
classNames: ['evaluation-summary'],
evaluationBestOptions: Ember.computed('poll.users.[]', 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 b.score - a.score;
2015-11-03 12:57:41 +01:00
});
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')) {
2016-03-20 15:30:54 +01:00
const date = this.get('dates').objectAt(bestOption.key);
const format = date.hasTime ? 'LLLL' : moment.localeData()
.longDateFormat('LLLL')
.replace(
moment.localeData().longDateFormat('LT'), '')
.trim();
bestOptions[i].title = date.title.format(format);
2016-01-20 03:19:10 +01:00
} else {
2016-03-20 15:30:54 +01:00
const option = this.get('poll.options').objectAt(bestOption.key);
bestOptions[i].title = option.get('title');
2015-11-03 12:57:41 +01:00
}
});
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
});