83b866324067

web: underscorify a few things and fix the line comment form moving
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 03 Jul 2010 11:46:04 -0400
parents 431b3e74013f
children b38c46e3fdf6
branches/tags (none)
files review/static/comments.js

Changes

--- 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);