Commit 00056c82f20ce900cec9b9fd17e3950e80fbb693
1 parent
32c7310f
Exists in
spb-stable
and in
3 other branches
Update votes ferom comments dynamically
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
26 additions
and
0 deletions
Show diff stats
app/assets/javascripts/notes.js.coffee
| @@ -232,6 +232,7 @@ class Notes | @@ -232,6 +232,7 @@ class Notes | ||
| 232 | ### | 232 | ### |
| 233 | addNote: (xhr, note, status) => | 233 | addNote: (xhr, note, status) => |
| 234 | @renderNote(note) | 234 | @renderNote(note) |
| 235 | + @updateVotes() | ||
| 235 | 236 | ||
| 236 | ### | 237 | ### |
| 237 | Called in response to the new note form being submitted | 238 | Called in response to the new note form being submitted |
| @@ -425,4 +426,7 @@ class Notes | @@ -425,4 +426,7 @@ class Notes | ||
| 425 | form = $(e.target).closest(".js-discussion-note-form") | 426 | form = $(e.target).closest(".js-discussion-note-form") |
| 426 | @removeDiscussionNoteForm(form) | 427 | @removeDiscussionNoteForm(form) |
| 427 | 428 | ||
| 429 | + updateVotes: -> | ||
| 430 | + (new NotesVotes).updateVotes() | ||
| 431 | + | ||
| 428 | @Notes = Notes | 432 | @Notes = Notes |
| @@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
| 1 | +class NotesVotes | ||
| 2 | + updateVotes: -> | ||
| 3 | + votes = $("#votes .votes") | ||
| 4 | + notes = $("#notes-list .note .vote") | ||
| 5 | + | ||
| 6 | + # only update if there is a vote display | ||
| 7 | + if votes.size() | ||
| 8 | + upvotes = notes.filter(".upvote").size() | ||
| 9 | + downvotes = notes.filter(".downvote").size() | ||
| 10 | + votesCount = upvotes + downvotes | ||
| 11 | + upvotesPercent = (if votesCount then (100.0 / votesCount * upvotes) else 0) | ||
| 12 | + downvotesPercent = (if votesCount then (100.0 - upvotesPercent) else 0) | ||
| 13 | + | ||
| 14 | + # change vote bar lengths | ||
| 15 | + votes.find(".bar-success").css "width", upvotesPercent + "%" | ||
| 16 | + votes.find(".bar-danger").css "width", downvotesPercent + "%" | ||
| 17 | + | ||
| 18 | + # replace vote numbers | ||
| 19 | + votes.find(".upvotes").text votes.find(".upvotes").text().replace(/\d+/, upvotes) | ||
| 20 | + votes.find(".downvotes").text votes.find(".downvotes").text().replace(/\d+/, downvotes) | ||
| 21 | + | ||
| 22 | +@NotesVotes = NotesVotes |