<commit_before>"use strict";

/*global module, require*/

var d3 = require("d3"),
    dragClass = "drag-reorder";

/*
 Adds d3 drag behaviour to statically positioned elements.
 When dropped, if they are dropped onto an element in the same parent container, they will move above that element in the list.
 This may push all the other children in the container around.

 The elements will be moved at the end of the drag action.
 */

var findChildIndex = function(node) {
    var i = 1;
    while ((node = node.previousElementSibling)) {
	i++;
    }

    return i;
};

var findSiblingsBetween = function(first, second) {
    if (!first || !second) {
	return [];
    }

    var siblings = [],
	node = first.nextElementSibling;

    while (node && node !== second) {
	siblings.push(node);
	node = node.nextElementSibling;
    }

    return siblings;
};

module.exports = function(selection, callback) {
    var startIndex,
	height,
	movedDown,
	target,
	displaced,

	findDragTarget = function(parent, draggingElement) {
	    var draggingMid = draggingElement.offsetTop + (draggingElement.offsetHeight / 2),
		target = selection.filter(
		    function(d, i) {
			var ourBottom = this.offsetTop + this.offsetHeight;
			
			if (this === draggingElement) {
			    return false;
			    
			} else {
			    // The middle of the element being dragged is between our top and our bottom.
			    return (ourBottom > draggingMid) &&
				(this.offsetTop < draggingMid);
			}
		    }
	    );
	    
	    if (target.empty()) {
		return null;

	    } else
		return target[0][0];
	},
<commit_msg>Fixed a bug where, if a selection was added piecemeal, the various pieces wouldn't interact correctly. Added a comment to explain this.<commit_after>"use strict";

/*global module, require*/

var d3 = require("d3"),
    dragClass = "drag-reorder";

var findChildIndex = function(node) {
    var i = 1;
    while ((node = node.previousElementSibling)) {
	i++;
    }

    return i;
};

var findSiblingsBetween = function(first, second) {
    if (!first || !second) {
	return [];
    }

    var siblings = [],
	node = first.nextElementSibling;

    while (node && node !== second) {
	siblings.push(node);
	node = node.nextElementSibling;
    }

    return siblings;
};

/*
 Adds d3 drag behaviour to statically positioned elements.
 When dropped, if they are dropped onto an element in the same parent container, they will move above that element in the list.
 This may push all the other children in the container around.

 The elements will be moved at the end of the drag action.

 You may call this multiple times to add the drag handler to elements which have been added to the parent node.
 */
module.exports = function(selection, callback) {
    var startIndex,
	height,
	movedDown,
	target,
	displaced,

	findDragTarget = function(parent, draggingElement) {
	    var draggingMid = draggingElement.offsetTop + (draggingElement.offsetHeight / 2),
		target = parent.selectAll("." + dragClass)
		    .filter(
			function(d, i) {
			    var ourBottom = this.offsetTop + this.offsetHeight;
			    
			    if (this === draggingElement) {
				return false;
				
			    } else {
				// The middle of the element being dragged is between our top and our bottom.
				return (ourBottom > draggingMid) &&
				    (this.offsetTop < draggingMid);
			    }
			}
		    );
	    
	    if (target.empty()) {
		return null;

	    } else
		return target[0][0];
	},

{'fraction_non_alphanumeric': 0.10367990654205607, 'fraction_numerical': 0.0040887850467289715, 'mean_word_length': 2.959537572254335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5174123', 'n_tokens_mistral': 1117, 'n_tokens_neox': 993, 'n_words': 352}