diff --git a/plugins/poll/assets/javascripts/components/poll-breakdown-option.js.es6 b/plugins/poll/assets/javascripts/components/poll-breakdown-option.js.es6 new file mode 100644 index 0000000000..5205adf561 --- /dev/null +++ b/plugins/poll/assets/javascripts/components/poll-breakdown-option.js.es6 @@ -0,0 +1,24 @@ +import I18n from "I18n"; +import Component from "@ember/component"; +import { htmlSafe } from "@ember/template"; +import discourseComputed from "discourse-common/utils/decorators"; +import { getColors } from "../lib/chart-colors"; + +export default Component.extend({ + tagName: "", + + @discourseComputed("option.votes", "totalVotes") + percent(votes, total) { + return I18n.toNumber((votes / total) * 100.0, { precision: 1 }); + }, + + @discourseComputed("optionsCount") + optionColors(optionsCount) { + return getColors(optionsCount); + }, + + @discourseComputed("optionColors", "index") + colorStyle(optionColors, index) { + return htmlSafe(`background: ${optionColors[index]};`); + } +}); diff --git a/plugins/poll/assets/javascripts/controllers/poll-breakdown.js.es6 b/plugins/poll/assets/javascripts/controllers/poll-breakdown.js.es6 index 6880b5befc..174436b66f 100644 --- a/plugins/poll/assets/javascripts/controllers/poll-breakdown.js.es6 +++ b/plugins/poll/assets/javascripts/controllers/poll-breakdown.js.es6 @@ -58,9 +58,9 @@ export default Controller.extend(ModalFunctionality, { })); }, - @discourseComputed("model.poll.options.length") - optionColors(optionsCount) { - return getColors(optionsCount); + @discourseComputed("model.poll.options") + totalVotes(options) { + return options.reduce((sum, option) => sum + option.votes, 0); }, onShow() { diff --git a/plugins/poll/assets/javascripts/discourse/templates/components/poll-breakdown-option.hbs b/plugins/poll/assets/javascripts/discourse/templates/components/poll-breakdown-option.hbs new file mode 100644 index 0000000000..b4f02e1fa8 --- /dev/null +++ b/plugins/poll/assets/javascripts/discourse/templates/components/poll-breakdown-option.hbs @@ -0,0 +1,5 @@ +
  • + + {{this.percent}}% + {{{@option.html}}} +
  • diff --git a/plugins/poll/assets/javascripts/discourse/templates/modal/poll-breakdown.hbs b/plugins/poll/assets/javascripts/discourse/templates/modal/poll-breakdown.hbs index 6010377da0..013d10f839 100644 --- a/plugins/poll/assets/javascripts/discourse/templates/modal/poll-breakdown.hbs +++ b/plugins/poll/assets/javascripts/discourse/templates/modal/poll-breakdown.hbs @@ -7,13 +7,14 @@ {{!-- TODO: I18n --}} {{this.model.poll.voters}} votes - {{!-- TODO --}} diff --git a/plugins/poll/assets/stylesheets/common/poll-breakdown.scss b/plugins/poll/assets/stylesheets/common/poll-breakdown.scss index 1513a3ccc7..02c50c6b8c 100644 --- a/plugins/poll/assets/stylesheets/common/poll-breakdown.scss +++ b/plugins/poll/assets/stylesheets/common/poll-breakdown.scss @@ -28,7 +28,6 @@ background: hsl(0, 0%, 92%); box-sizing: border-box; display: flex; - flex-basis: 30%; flex-direction: column; flex-shrink: 0; height: 100%; @@ -57,15 +56,30 @@ .poll-breakdown-option { align-items: center; display: grid; - grid: auto / auto-flow; column-gap: 0.66rem; - justify-content: flex-start; + grid-template-columns: 2.5rem 1fr; + justify-content: start; } .poll-breakdown-option-color { + align-self: end; border: 1px solid rgba(0, 0, 0, 0.15); - height: 0.5rem; - width: 0.8rem; + grid-column: 1; + height: 0.6rem; + justify-self: center; + width: 1.2rem; +} + +.poll-breakdown-option-count { + align-self: start; + font-size: 0.9rem; + grid-column: 1; + justify-self: center; +} + +.poll-breakdown-option-text { + grid-column: 2; + grid-row: 1/3; } .poll-breakdown-body {