MCPcopy Create free account
hub / github.com/codecombat/codecombat / updateAnswers

Method updateAnswers

app/views/play/modal/PollModal.js:59–95  ·  view source on GitHub ↗
(answered)

Source from the content-addressed store, hash-verified

57 }
58
59 updateAnswers (answered) {
60 let answer, left, votes
61 const myAnswer = ((left = this.userPollsRecord.get('polls')) != null ? left : {})[this.poll.id]
62 answered = (myAnswer != null)
63 this.$el.find('table, .random-gems-container-wrapper').toggleClass('answered', answered)
64 if (!answered) { return }
65 this.awardRandomGems()
66
67 // Count total votes and find the answer with the most votes.
68 let [maxVotes, totalVotes] = Array.from([0, 0])
69 for (answer of Array.from(this.poll.get('answers') || [])) {
70 votes = answer.votes || 0
71 if (answer.key === this.previousAnswer) { --votes }
72 if (answer.key === myAnswer) { ++votes }
73 answer.votes = votes
74 totalVotes += votes
75 maxVotes = Math.max(maxVotes, votes || 0)
76 }
77 this.previousAnswer = myAnswer
78 this.poll.set('answers', this.poll.get('answers')) // Update vote count locally (won't save to server).
79
80 // Update each answer cell according to its share of max and total votes.
81 for (answer of Array.from(this.poll.get('answers'))) {
82 const $answer = this.$el.find(`.answer[data-answer='${answer.key}']`)
83 $answer.toggleClass('selected', answer.key === myAnswer)
84 votes = answer.votes || 0
85 if (!totalVotes) { votes = (maxVotes = (totalVotes = 1)) } // If no votes yet, just pretend we voted for the first one.
86
87 const widthPercentage = ((100 * votes) / maxVotes) + '%'
88 const votePercentage = Math.round((100 * votes) / totalVotes) + '%'
89 $answer.find('.progress-bar').css('width', '0%').animate({ width: widthPercentage }, 'slow')
90 $answer.find('.vote-percentage').text(votePercentage)
91 if (me.isAdmin()) { $answer.find('.vote-count').text(votes) }
92 }
93
94 return this.trigger('vote-updated')
95 }
96
97 onClickAnswer (e) {
98 let left

Callers 2

afterRenderMethod · 0.95
onClickAnswerMethod · 0.95

Calls 4

awardRandomGemsMethod · 0.95
isAdminMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected