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

40 lines
1.1 KiB
Plaintext
Raw Normal View History

2016-04-01 05:33:25 +08:00
import evenRound from "discourse/plugins/poll/lib/even-round";
import computed from "ember-addons/ember-computed-decorators";
export default Em.Component.extend({
tagName: "ul",
classNames: ["results"],
2016-04-01 05:33:25 +08:00
@computed("poll.voters", "poll.type", "poll.options.[]")
options(voters, type) {
const options = this.get("poll.options");
let percentages = voters === 0 ?
Array(options.length).fill(0) :
_.map(options, o => 100 * o.get("votes") / voters);
// properly round percentages
if (type === "multiple") {
// when the poll is multiple choices, just "round down"
percentages = percentages.map(p => Math.floor(p));
} else {
// when the poll is single choice, adds up to 100%
percentages = evenRound(percentages);
}
2016-04-01 05:33:25 +08:00
options.forEach((option, i) => {
const percentage = percentages[i];
const style = new Ember.Handlebars.SafeString(`width: ${percentage}%`);
option.setProperties({
2015-05-06 23:49:55 +08:00
percentage,
2015-05-15 17:51:10 +08:00
style,
title: I18n.t("poll.option_title", { count: option.get("votes") })
});
});
2016-04-01 05:33:25 +08:00
return options;
}
});