discourse/plugins/poll/assets/javascripts/components/poll-results-number.js.es6

26 lines
733 B
Plaintext
Raw Normal View History

import round from "discourse/lib/round";
2016-06-07 06:55:01 -04:00
import computed from 'ember-addons/ember-computed-decorators';
export default Em.Component.extend({
2016-06-07 06:55:01 -04:00
@computed("poll.options.@each.{html,votes}")
totalScore() {
return _.reduce(this.get("poll.options"), function(total, o) {
const value = parseInt(o.get("html"), 10),
votes = parseInt(o.get("votes"), 10);
return total + value * votes;
}, 0);
2016-06-07 06:55:01 -04:00
},
2016-06-07 06:55:01 -04:00
@computed("totalScore", "poll.voters")
average() {
const voters = this.get("poll.voters");
return voters === 0 ? 0 : round(this.get("totalScore") / voters, -2);
2016-06-07 06:55:01 -04:00
},
2016-06-07 06:55:01 -04:00
@computed("average")
averageRating() {
return I18n.t("poll.average_rating", { average: this.get("average") });
2016-06-07 06:55:01 -04:00
},
});