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-24 18:14:24 -05:00
|
|
|
@computed("poll.voters", "poll.type", "poll.options.[]")
|
|
|
|
options(voters, type) {
|
2016-07-28 11:04:45 -04:00
|
|
|
const options = this.get("poll.options").slice(0).sort((a, b) => {
|
|
|
|
return a.get("votes") < b.get("votes") ? 1 : 0;
|
|
|
|
});
|
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
|
2015-11-24 18:14:24 -05:00
|
|
|
if (type === "multiple") {
|
2015-11-23 09:28:24 -05:00
|
|
|
// 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];
|
2016-05-10 13:45:58 -04:00
|
|
|
const style = new 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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-24 18:14:24 -05:00
|
|
|
return options;
|
2015-11-19 12:23:38 -05:00
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
});
|