summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes_votes.js.coffee
blob: b31eb9ac9de7a014a719814003a696f49e042023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class NotesVotes
  updateVotes: ->
    votes = $("#votes .votes")
    notes = $("#notes-list .note .vote")

    # only update if there is a vote display
    if votes.size()
      upvotes = notes.filter(".upvote").size()
      downvotes = notes.filter(".downvote").size()
      votesCount = upvotes + downvotes
      upvotesPercent = (if votesCount then (100.0 / votesCount * upvotes) else 0)
      downvotesPercent = (if votesCount then (100.0 - upvotesPercent) else 0)

      # change vote bar lengths
      votes.find(".bar-success").css "width", upvotesPercent + "%"
      votes.find(".bar-danger").css "width", downvotesPercent + "%"

      # replace vote numbers
      votes.find(".upvotes").text votes.find(".upvotes").text().replace(/\d+/, upvotes)
      votes.find(".downvotes").text votes.find(".downvotes").text().replace(/\d+/, downvotes)

@NotesVotes = NotesVotes