Commit 560b1ac5f39b87145e0882cad018f12cc8cf27b4

Authored by Dmitriy Zaporozhets
2 parents 249cb19d 64e76a87

Merge branch 'riyad-update-votes-when-adding-notes'

app/assets/javascripts/notes.js
... ... @@ -21,15 +21,18 @@ var NoteList = {
21 21 this.getContent();
22 22  
23 23 $("#notes-list, #new-notes-list").on("ajax:success", ".delete-note", function() {
24   - $(this).closest('li').fadeOut();
  24 + $(this).closest('li').fadeOut(function() {
  25 + $(this).remove();
  26 + NoteList.updateVotes();
  27 + });
25 28 });
26 29  
27 30 $(".note-form-holder").on("ajax:before", function(){
28   - $(".submit_note").disable()
  31 + $(".submit_note").disable();
29 32 })
30 33  
31 34 $(".note-form-holder").on("ajax:complete", function(){
32   - $(".submit_note").enable()
  35 + $(".submit_note").enable();
33 36 })
34 37  
35 38 disableButtonIfEmptyField(".note-text", ".submit_note");
... ... @@ -159,6 +162,8 @@ var NoteList = {
159 162 if (!this.reversed) {
160 163 this.initRefreshNew();
161 164 }
  165 + // make sure we are up to date
  166 + this.updateVotes();
162 167 },
163 168  
164 169  
... ... @@ -198,6 +203,7 @@ var NoteList = {
198 203 replaceNewNotes:
199 204 function(html) {
200 205 $("#new-notes-list").html(html);
  206 + this.updateVotes();
201 207 },
202 208  
203 209 /**
... ... @@ -210,6 +216,37 @@ var NoteList = {
210 216 } else {
211 217 $("#new-notes-list").append(html);
212 218 }
  219 + this.updateVotes();
  220 + },
  221 +
  222 + /**
  223 + * Recalculates the votes and updates them (if they are displayed at all).
  224 + *
  225 + * Assumes all relevant notes are displayed (i.e. there are no more notes to
  226 + * load via getMore()).
  227 + * Might produce inaccurate results when not all notes have been loaded and a
  228 + * recalculation is triggered (e.g. when deleting a note).
  229 + */
  230 + updateVotes:
  231 + function() {
  232 + var votes = $("#votes .votes");
  233 + var notes = $("#notes-list, #new-notes-list").find(".note.vote");
  234 +
  235 + // only update if there is a vote display
  236 + if (votes.size()) {
  237 + var upvotes = notes.filter(".upvote").size();
  238 + var downvotes = notes.filter(".downvote").size();
  239 + var votesCount = upvotes + downvotes;
  240 + var upvotesPercent = votesCount ? (100.0 / votesCount * upvotes) : 0;
  241 + var downvotesPercent = votesCount ? (100.0 - upvotesPercent) : 0;
  242 +
  243 + // change vote bar lengths
  244 + votes.find(".bar-success").css("width", upvotesPercent+"%");
  245 + votes.find(".bar-danger").css("width", downvotesPercent+"%");
  246 + // replace vote numbers
  247 + votes.find(".upvotes").text(votes.find(".upvotes").text().replace(/\d+/, upvotes));
  248 + votes.find(".downvotes").text(votes.find(".downvotes").text().replace(/\d+/, downvotes));
  249 + }
213 250 }
214 251 };
215 252  
... ...
app/assets/stylesheets/common.scss
... ... @@ -158,6 +158,18 @@ span.update-author {
158 158 padding: 6px;
159 159 }
160 160 }
  161 +
  162 + &.label-success {
  163 + background-color: #8D8;
  164 + color: #333;
  165 + text-shadow: 0 1px 1px white;
  166 + }
  167 +
  168 + &.label-error {
  169 + background-color: #D88;
  170 + color: #333;
  171 + text-shadow: 0 1px 1px white;
  172 + }
161 173 }
162 174  
163 175 .event_label {
... ...
app/assets/stylesheets/sections/notes.scss
... ... @@ -71,6 +71,11 @@
71 71 border-top: 1px solid #eee;
72 72 }
73 73  
  74 +/* mark vote notes */
  75 +.voting_notes .note {
  76 + padding: 8px 0;
  77 +}
  78 +
74 79 .notes-status {
75 80 margin: 18px;
76 81 }
... ...
app/helpers/notes_helper.rb
... ... @@ -6,4 +6,12 @@ module NotesHelper
6 6 def loading_new_notes?
7 7 params[:loading_new].present?
8 8 end
  9 +
  10 + def note_vote_class(note)
  11 + if note.upvote?
  12 + "vote upvote"
  13 + elsif note.downvote?
  14 + "vote downvote"
  15 + end
  16 + end
9 17 end
... ...
app/views/issues/show.html.haml
... ... @@ -61,4 +61,4 @@
61 61 = markdown @issue.description
62 62  
63 63  
64   -.issue_notes#notes= render "notes/notes_with_form", tid: @issue.id, tt: "issue"
  64 +.issue_notes.voting_notes#notes= render "notes/notes_with_form", tid: @issue.id, tt: "issue"
... ...
app/views/merge_requests/_show.html.haml
... ... @@ -15,7 +15,7 @@
15 15 %i.icon-list-alt
16 16 Diff
17 17  
18   -.merge_request_notes#notes{ class: (controller.action_name == 'show') ? "" : "hide" }
  18 +.merge_request_notes.voting_notes#notes{ class: (controller.action_name == 'show') ? "" : "hide" }
19 19 = render("notes/notes_with_form", tid: @merge_request.id, tt: "merge_request")
20 20 .merge-request-diffs
21 21 = render "merge_requests/show/diffs" if @diffs
... ...
app/views/notes/_note.html.haml
1   -%li{id: dom_id(note), class: "note"}
  1 +%li{id: dom_id(note), class: "note #{note_vote_class(note)}"}
2 2 = image_tag gravatar_icon(note.author.email), class: "avatar s32"
3 3 %div.note-author
4 4 %strong= note.author_name
... ... @@ -6,8 +6,16 @@
6 6 %cite.cgray
7 7 = time_ago_in_words(note.updated_at)
8 8 ago
  9 + - if note.upvote?
  10 + %span.label.label-success
  11 + %i.icon-thumbs-up
  12 + \+1
  13 + - if note.downvote?
  14 + %span.label.label-error
  15 + %i.icon-thumbs-down
  16 + \-1
9 17 - if(note.author_id == current_user.id) || can?(current_user, :admin_note, @project)
10   - = link_to [@project, note], confirm: 'Are you sure?', method: :delete, remote: true, class: "cred delete-note btn very_small" do
  18 + = link_to [@project, note], confirm: 'Are you sure?', method: :delete, remote: true, class: "cred delete-note btn very_small" do
11 19 %i.icon-trash
12 20 Remove
13 21  
... ...