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-19 12:23:38 -05:00
|
|
|
@computed("poll.voters", "poll.options.[]")
|
|
|
|
options() {
|
|
|
|
const options = this.get("poll.options");
|
2015-05-15 05:08:51 -04:00
|
|
|
const voters = this.get("poll.voters");
|
2015-11-19 12:23:38 -05:00
|
|
|
const percentages = voters === 0 ?
|
|
|
|
Array(options.length).fill(0) :
|
|
|
|
evenRound(_.map(options, o => 100 * o.get("votes") / voters));
|
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
|
|
|
|
|
|
|
});
|