summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes.js
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-09-18 08:56:54 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-09-18 08:56:54 +0300
commit2aafd7cf4e5dc28c0d4692d4a0b35a714186615b (patch)
tree6b6d7f59d1bc63792e4ebce84666a6302db2b7b9 /app/assets/javascripts/notes.js
parent249cb19d96cff52f70f930fbf13ee23ae5d66d3e (diff)
parent6aebb76b5d05bf7668a8f389a00648fdb405af7d (diff)
downloadgitlab-ce-2aafd7cf4e5dc28c0d4692d4a0b35a714186615b.tar.gz
Merge branch 'update-votes-when-adding-notes' of https://github.com/riyad/gitlabhq into riyad-update-votes-when-adding-notes
Diffstat (limited to 'app/assets/javascripts/notes.js')
-rw-r--r--app/assets/javascripts/notes.js43
1 files changed, 40 insertions, 3 deletions
diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js
index 4a55b41b1ab..e1ad1d2f9e8 100644
--- a/app/assets/javascripts/notes.js
+++ b/app/assets/javascripts/notes.js
@@ -21,15 +21,18 @@ var NoteList = {
this.getContent();
$("#notes-list, #new-notes-list").on("ajax:success", ".delete-note", function() {
- $(this).closest('li').fadeOut();
+ $(this).closest('li').fadeOut(function() {
+ $(this).remove();
+ NoteList.updateVotes();
+ });
});
$(".note-form-holder").on("ajax:before", function(){
- $(".submit_note").disable()
+ $(".submit_note").disable();
})
$(".note-form-holder").on("ajax:complete", function(){
- $(".submit_note").enable()
+ $(".submit_note").enable();
})
disableButtonIfEmptyField(".note-text", ".submit_note");
@@ -159,6 +162,8 @@ var NoteList = {
if (!this.reversed) {
this.initRefreshNew();
}
+ // make sure we are up to date
+ this.updateVotes();
},
@@ -198,6 +203,7 @@ var NoteList = {
replaceNewNotes:
function(html) {
$("#new-notes-list").html(html);
+ this.updateVotes();
},
/**
@@ -210,6 +216,37 @@ var NoteList = {
} else {
$("#new-notes-list").append(html);
}
+ this.updateVotes();
+ },
+
+ /**
+ * Recalculates the votes and updates them (if they are displayed at all).
+ *
+ * Assumes all relevant notes are displayed (i.e. there are no more notes to
+ * load via getMore()).
+ * Might produce inaccurate results when not all notes have been loaded and a
+ * recalculation is triggered (e.g. when deleting a note).
+ */
+ updateVotes:
+ function() {
+ var votes = $("#votes .votes");
+ var notes = $("#notes-list, #new-notes-list").find(".note.vote");
+
+ // only update if there is a vote display
+ if (votes.size()) {
+ var upvotes = notes.filter(".upvote").size();
+ var downvotes = notes.filter(".downvote").size();
+ var votesCount = upvotes + downvotes;
+ var upvotesPercent = votesCount ? (100.0 / votesCount * upvotes) : 0;
+ var downvotesPercent = votesCount ? (100.0 - upvotesPercent) : 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));
+ }
}
};