# HG changeset patch # User Steve Losh # Date 1278171964 14400 # Node ID 83b86632406701878032b410acc3fb7f7a351c8e # Parent 431b3e74013fad7053c2cb818864523b63b3a7b8 web: underscorify a few things and fix the line comment form moving diff -r 431b3e74013f -r 83b866324067 review/static/comments.js --- a/review/static/comments.js Sat Jul 03 11:08:03 2010 -0400 +++ b/review/static/comments.js Sat Jul 03 11:46:04 2010 -0400 @@ -88,55 +88,47 @@ if ($(this).hasClass("chosen")) { $(this).removeClass("chosen"); - var newLines = ""; - jQuery.each(diff.find(".chosen .linenumber"), function() { - newLines += $(this).html() + ","; - }); + var newLines = _.reduce(diff.find(".chosen .linenumber"), "", + function(memo, lne) { return memo + $(lne).html() + ','; }); diff.find(".comment-form form .lines").val(newLines); lastSelected = null; - return false; + } else { + if (event.shiftKey && lastSelected) { + if (lastSelected && jQuery.contains(diff.get(0), lastSelected.get(0))) { + var lastNum = parseInt(lastSelected.find(".linenumber").html()); + _.each(_.range(currNum, lastNum, lastNum > currNum ? 1 : -1), + function(i) { diff.find(".line-" + i).addClass("chosen"); } + ); + } + } else { + $(this).addClass("chosen"); + } } - if (event.shiftKey && lastSelected) { - if (lastSelected && jQuery.contains(diff.get(0), lastSelected.get(0))) { - var lastNum = parseInt(lastSelected.find(".linenumber").html()); - if (lastNum > currNum) { - for (i = currNum; i < lastNum; i++) { - diff.find(".line-" + i).addClass("chosen"); - } - } else { - for (i = currNum; i > lastNum; i--) { - diff.find(".line-" + i).addClass("chosen"); - } - } - } - } else { - $(this).addClass("chosen"); - } - lastSelected = $(this); - var lines_chosen = diff.find(".chosen"); - var last_line = lines_chosen.last(); + var last_line_number = parseInt(lines_chosen.find(".linenumber").last().html()); var existing_forms = diff.find(".comment-form"); if (existing_forms.length) { - var existing_form = existing_forms.last(); - var existing_form_line_number = parseInt(existing_form.find(".lastlinenumber").html()); - - if (existing_form_line_number < currNum) { + if (_.isNaN(last_line_number)) { existing_forms.remove(); + } else { + var existing_form = existing_forms.last(); + var existing_form_line_number = parseInt(existing_form.find(".lastlinenumber").html()); + + if (existing_form_line_number != last_line_number) { + existing_forms.remove(); - var comment_form = RenderLineCommentForm($(this), currNum); - $(this).after(comment_form); - diff.find("label").inFieldLabels(); - } + var comment_form = RenderLineCommentForm($(this), last_line_number); + lines_chosen.last().after(comment_form); + diff.find("label").inFieldLabels(); + } - var newLines = ""; - jQuery.each(diff.find(".chosen .linenumber"), function() { - newLines += $(this).html() + ","; - }); - diff.find(".comment-form form .lines").val(newLines); + var newLines = _.reduce(diff.find(".chosen .linenumber"), "", + function(memo, lne) { return memo + $(lne).html() + ','; }); + diff.find(".comment-form form .lines").val(newLines); + } } else { var comment_form = RenderLineCommentForm($(this), currNum); $(this).after(comment_form);