diff --git a/plugins/poll/assets/javascripts/components/poll-results-number-voters.js.es6 b/plugins/poll/assets/javascripts/components/poll-results-number-voters.js.es6 new file mode 100644 index 00000000000..63449af066a --- /dev/null +++ b/plugins/poll/assets/javascripts/components/poll-results-number-voters.js.es6 @@ -0,0 +1,31 @@ +import computed from 'ember-addons/ember-computed-decorators'; +import User from 'discourse/models/user'; +import PollVoters from 'discourse/plugins/poll/components/poll-voters'; + +export default PollVoters.extend({ + @computed("pollsVoters", "poll.options", "showMore", "isExpanded", "numOfVotersToShow") + users(pollsVoters, options, showMore, isExpanded, numOfVotersToShow) { + var users = []; + var voterIds = []; + const shouldLimit = showMore && !isExpanded; + + options.forEach(option => { + option.voter_ids.forEach(voterId => { + if (shouldLimit) { + if (!(users.length > numOfVotersToShow - 1)) { + users.push(pollsVoters[voterId]); + } + } else { + users.push(pollsVoters[voterId]); + } + }) + }); + + return users; + }, + + @computed("pollsVoters", "numOfVotersToShow") + showMore(pollsVoters, numOfVotersToShow) { + return !(Object.keys(pollsVoters).length < numOfVotersToShow); + } +}); diff --git a/plugins/poll/assets/javascripts/components/poll-results-number.js.es6 b/plugins/poll/assets/javascripts/components/poll-results-number.js.es6 index 42087e2933b..5718642b327 100644 --- a/plugins/poll/assets/javascripts/components/poll-results-number.js.es6 +++ b/plugins/poll/assets/javascripts/components/poll-results-number.js.es6 @@ -1,23 +1,27 @@ import round from "discourse/lib/round"; +import computed from 'ember-addons/ember-computed-decorators'; export default Em.Component.extend({ tagName: "span", - totalScore: function() { + @computed("poll.options.@each.{html,votes}") + totalScore() { return _.reduce(this.get("poll.options"), function(total, o) { const value = parseInt(o.get("html"), 10), votes = parseInt(o.get("votes"), 10); return total + value * votes; }, 0); - }.property("poll.options.@each.{html,votes}"), + }, - average: function() { + @computed("totalScore", "poll.voters") + average() { const voters = this.get("poll.voters"); return voters === 0 ? 0 : round(this.get("totalScore") / voters, -2); - }.property("totalScore", "poll.voters"), + }, - averageRating: function() { + @computed("average") + averageRating() { return I18n.t("poll.average_rating", { average: this.get("average") }); - }.property("average"), + }, }); diff --git a/plugins/poll/assets/javascripts/components/poll-results-standard-voters.js.es6 b/plugins/poll/assets/javascripts/components/poll-results-standard-voters.js.es6 new file mode 100644 index 00000000000..1e51dc23c44 --- /dev/null +++ b/plugins/poll/assets/javascripts/components/poll-results-standard-voters.js.es6 @@ -0,0 +1,25 @@ +import computed from 'ember-addons/ember-computed-decorators'; +import User from 'discourse/models/user'; +import PollVoters from 'discourse/plugins/poll/components/poll-voters'; + +export default PollVoters.extend({ + @computed("pollsVoters", "option.voter_ids", "showMore", "isExpanded", "numOfVotersToShow") + users(pollsVoters, voterIds, showMore, isExpanded, numOfVotersToShow) { + var users = []; + + if (showMore && !isExpanded) { + voterIds = voterIds.slice(0, numOfVotersToShow); + } + + voterIds.forEach(voterId => { + users.push(pollsVoters[voterId]); + }); + + return users; + }, + + @computed("option.votes", "numOfVotersToShow") + showMore(numOfVotes, numOfVotersToShow) { + return !(numOfVotes < numOfVotersToShow); + } +}); diff --git a/plugins/poll/assets/javascripts/components/poll-voters.js.es6 b/plugins/poll/assets/javascripts/components/poll-voters.js.es6 new file mode 100644 index 00000000000..b580f111821 --- /dev/null +++ b/plugins/poll/assets/javascripts/components/poll-voters.js.es6 @@ -0,0 +1,13 @@ +export default Ember.Component.extend({ + layoutName: "components/poll-voters", + tagName: 'ul', + classNames: ["poll-voters-list"], + isExpanded: false, + numOfVotersToShow: 20, + + actions: { + toggleExpand() { + this.toggleProperty("isExpanded"); + } + } +}); diff --git a/plugins/poll/assets/javascripts/controllers/poll.js.es6 b/plugins/poll/assets/javascripts/controllers/poll.js.es6 index 14a5079408f..b986c4c9fc2 100644 --- a/plugins/poll/assets/javascripts/controllers/poll.js.es6 +++ b/plugins/poll/assets/javascripts/controllers/poll.js.es6 @@ -6,6 +6,7 @@ export default Ember.Controller.extend({ isNumber: Ember.computed.equal("poll.type", "number"), isRandom : Ember.computed.equal("poll.order", "random"), isClosed: Ember.computed.equal("poll.status", "closed"), + pollsVoters: Ember.computed.alias("post.polls_voters"), // shows the results when // - poll is closed @@ -145,8 +146,16 @@ export default Ember.Controller.extend({ options: this.get("selectedOptions"), } }).then(results => { - this.setProperties({ vote: results.vote, showResults: true }); - this.set("model", Em.Object.create(results.poll)); + const poll = results.poll; + const votes = results.vote; + const currentUser = this.currentUser; + + this.setProperties({ vote: votes, showResults: true }); + this.set("model", Em.Object.create(poll)); + + if (poll.public) { + this.get("pollsVoters")[currentUser.get("id")] = currentUser; + } }).catch(() => { bootbox.alert(I18n.t("poll.error_while_casting_votes")); }).finally(() => { diff --git a/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-number.hbs b/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-number.hbs index b6cf23314d9..48e73ff2fc8 100644 --- a/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-number.hbs +++ b/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-number.hbs @@ -1 +1,5 @@ {{{averageRating}}} + +{{#if poll.public}} + {{poll-results-number-voters poll=poll pollsVoters=pollsVoters}} +{{/if}} diff --git a/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-standard.hbs b/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-standard.hbs index d625b76cd2d..e412d61a772 100644 --- a/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-standard.hbs +++ b/plugins/poll/assets/javascripts/discourse/templates/components/poll-results-standard.hbs @@ -9,5 +9,9 @@
+ + {{#if poll.public}} + {{poll-results-standard-voters option=option pollsVoters=pollsVoters}} + {{/if}} {{/each}} diff --git a/plugins/poll/assets/javascripts/discourse/templates/components/poll-voters.hbs b/plugins/poll/assets/javascripts/discourse/templates/components/poll-voters.hbs new file mode 100644 index 00000000000..afa3f26c4b9 --- /dev/null +++ b/plugins/poll/assets/javascripts/discourse/templates/components/poll-voters.hbs @@ -0,0 +1,19 @@ +
+ {{#each users as |user|}} +
  • + + {{avatar user imageSize="tiny"}} + +
  • + {{/each}} + +
    + {{#if showMore}} + {{#if isExpanded}} + {{fa-icon "chevron-up"}} + {{else}} + {{fa-icon "chevron-down"}} + {{/if}} + {{/if}} +
    +
    diff --git a/plugins/poll/assets/javascripts/discourse/templates/poll.hbs b/plugins/poll/assets/javascripts/discourse/templates/poll.hbs index 418c5b6355f..1d1e22ad182 100644 --- a/plugins/poll/assets/javascripts/discourse/templates/poll.hbs +++ b/plugins/poll/assets/javascripts/discourse/templates/poll.hbs @@ -2,9 +2,9 @@
    {{#if showingResults}} {{#if isNumber}} - {{poll-results-number poll=poll}} + {{poll-results-number poll=poll pollsVoters=pollsVoters}} {{else}} - {{poll-results-standard poll=poll}} + {{poll-results-standard poll=poll pollsVoters=pollsVoters}} {{/if}} {{else}}