From 8ecde35df3983723ef64405848fea3c1a3d93dbd Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 7 Jun 2016 18:55:01 +0800 Subject: [PATCH] FEATURE: Add public type to polls. --- .../poll-results-number-voters.js.es6 | 31 +++++++++ .../components/poll-results-number.js.es6 | 16 +++-- .../poll-results-standard-voters.js.es6 | 25 ++++++++ .../javascripts/components/poll-voters.js.es6 | 13 ++++ .../javascripts/controllers/poll.js.es6 | 13 +++- .../components/poll-results-number.hbs | 4 ++ .../components/poll-results-standard.hbs | 4 ++ .../templates/components/poll-voters.hbs | 19 ++++++ .../javascripts/discourse/templates/poll.hbs | 4 +- .../initializers/extend-for-poll.js.es6 | 32 ++++++++-- .../poll/assets/javascripts/poll_dialect.js | 2 +- .../poll/assets/javascripts/views/poll.js.es6 | 9 +-- .../poll/assets/stylesheets/common/poll.scss | 18 +++++- plugins/poll/lib/polls_updater.rb | 7 ++- plugins/poll/plugin.rb | 49 ++++++++++++++- .../spec/controllers/polls_controller_spec.rb | 43 +++++++++++++ plugins/poll/spec/lib/polls_updater_spec.rb | 63 +++++++++++++++++++ 17 files changed, 322 insertions(+), 30 deletions(-) create mode 100644 plugins/poll/assets/javascripts/components/poll-results-number-voters.js.es6 create mode 100644 plugins/poll/assets/javascripts/components/poll-results-standard-voters.js.es6 create mode 100644 plugins/poll/assets/javascripts/components/poll-voters.js.es6 create mode 100644 plugins/poll/assets/javascripts/discourse/templates/components/poll-voters.hbs 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 000000000..63449af06 --- /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 42087e293..5718642b3 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 000000000..1e51dc23c --- /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 000000000..b580f1118 --- /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 14a507940..b986c4c9f 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 b6cf23314..48e73ff2f 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 d625b76cd..e412d61a7 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 000000000..afa3f26c4 --- /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 418c5b635..1d1e22ad1 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}}