2015-11-19 12:23:38 -05:00
|
|
|
import evenRound from "discourse/plugins/poll/lib/even-round";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
export default Em.Component.extend({
|
2015-06-03 10:49:20 -04:00
|
|
|
tagName: "ul",
|
2015-04-23 13:33:29 -04:00
|
|
|
classNames: ["results"],
|
|
|
|
|
2015-11-23 09:28:24 -05:00
|
|
|
@computed("poll.voters", "poll.options.[]", "poll.type")
|
2015-11-19 12:23:38 -05:00
|
|
|
options() {
|
|
|
|
const options = this.get("poll.options");
|
2015-05-15 05:08:51 -04:00
|
|
|
const voters = this.get("poll.voters");
|
2015-11-23 09:28:24 -05:00
|
|
|
|
2015-11-23 05:34:24 -05:00
|
|
|
let percentages = voters === 0 ?
|
2015-11-19 12:23:38 -05:00
|
|
|
Array(options.length).fill(0) :
|
2015-11-23 05:34:24 -05:00
|
|
|
_.map(options, o => 100 * o.get("votes") / voters);
|
|
|
|
|
2015-11-23 09:28:24 -05:00
|
|
|
// properly round percentages
|
|
|
|
if (this.get("poll.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%
|
2015-11-23 05:34:24 -05:00
|
|
|
percentages = evenRound(percentages);
|
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-11-19 12:23:38 -05:00
|
|
|
options.forEach((option, i) => {
|
|
|
|
const percentage = percentages[i];
|
|
|
|
const style = new Ember.Handlebars.SafeString(`width: ${percentage}%`);
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
option.setProperties({
|
2015-05-06 11:49:55 -04:00
|
|
|
percentage,
|
2015-05-15 05:51:10 -04:00
|
|
|
style,
|
2015-05-15 05:08:51 -04:00
|
|
|
title: I18n.t("poll.option_title", { count: option.get("votes") })
|
2015-04-23 13:33:29 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.get("poll.options");
|
2015-11-19 12:23:38 -05:00
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
});
|