<commit_before>$(document).ready(function(){
    var canvasPos, xCanvasPos, yCanvasPos;
    var xCanvasPosPre, yCanvasPosPre;
    var points = new Array();
    var canvas, xCanvas, yCanvas;
    var startTime = -1;
    var selectedPointIndex = -1;
    var selectedPointYTreeIndex = -1;
    var highlightedPointIndex = -1;
    var selectedXNodeIndex = -1;
	var maxPoints = 200;
	var xTree = new Array();
	var yTree = new Array();
	var yTreePoints = new Array();
    var selectRect = null;
    var isMouseDown = false;
    var isMouseDownXCanvas = false;
    var isMouseDownYCanvas = false;
	var xRangeLeft = -1;
	var xRangeRight = -1;
    var xCanvasXOffset = 0;
    var xCanvasYOffset = 0;
    var yCanvasXOffset = 0;
    var yCanvasYOffset = 0;
    var xCanvasXOffsetMax = 0;
    var xCanvasYOffsetMax = 0;
    var xCanvasXOffsetMin = 0;
    var xCanvasYOffsetMin = 0;
    var yCanvasXOffsetMax = 0;
    var yCanvasYOffsetMax = 0;
    var yCanvasXOffsetMin = 0;
    var yCanvasYOffsetMin = 0;
    
    //get images to draw
    var redRect10px = document.getElementById("rect_red_10px");
    var greenRect10px = document.getElementById("rect_green_10px");
    var whiteRect10px = document.getElementById("rect_white_10px");
    var redCirc10px = document.getElementById("circ_red_10px");
    var whiteCirc10px = document.getElementById("circ_white_10px");

    //set up action listeners
    canvas = document.getElementById('rangeQueryCanvas');
    xCanvas = document.getElementById('xTreeCanvas');
    yCanvas = document.getElementById('yTreeCanvas');
	var clearButton = document.getElementById('rangeQueryClear');
	clearButton.onclick=function(){
        xCanvasXOffset = 0;
        xCanvasYOffset = 0;
        clearRangeQueryCanvas();
    };
    canvas.addEventListener('mousedown', function(){ mouseDown()}, false);
    canvas.addEventListener('mouseup', function(){ mouseUp()}, false);
    canvas.addEventListener('mousemove', function(e) {
        canvasPos = getCanvasPos(e, 'rangeQueryCanvas');
		xCanvasPos = null;
		yCanvasPos = null;
        mouseMove();
        redraw();
    }, false);
    canvas.addEventListener('mouseout', function(e) {
        canvasPos = null;
		xCanvasPos = null;
		yCanvasPos = null;
        startTime = -1;
        selectedPointIndex = -1;
        highlightedPointIndex = -1;
        redraw();
    }, false);
    xCanvas.addEventListener('mousemove', function(e) {
        xCanvasPos = getCanvasPos(e, 'xTreeCanvas');
		canvasPos = null;
		yCanvasPos = null;
        
        if(isMouseDownXCanvas){
            if(xCanvasPos.x > 0 && xCanvasPos.y > 0){
                xCanvasXOffset += xCanvasPos.x - xCanvasPosPre.x;
                xCanvasYOffset += xCanvasPos.y - xCanvasPosPre.y;
                xCanvasXOffset = Math.max(Math.min(xCanvasXOffset, xCanvasXOffsetMax), xCanvasXOffsetMin);
                xCanvasYOffset = Math.max(Math.min(xCanvasYOffset, xCanvasYOffsetMax), xCanvasYOffsetMin);
            } else {
                isMouseDownXCanvas = false
            }
        }
        
        redraw();
        xCanvasPosPre=xCanvasPos;
    }, false);
    yCanvas.addEventListener('mousemove', function(e) {
        yCanvasPos = getCanvasPos(e, 'yTreeCanvas');
		canvasPos = null;
		xCanvasPos = null;
        redraw();
        yCanvasPosPre=yCanvasPos;
    }, false);
    xCanvas.addEventListener('mousedown', function(e){ isMouseDownXCanvas = true}, false);
    xCanvas.addEventListener('mouseup', function(e){ 
        isMouseDownXCanvas = false;
        clickXCanvas();
    }, false);

    function getCanvasPos(e, canvasId){
		var c = document.getElementById(canvasId);
        var rect = c.getBoundingClientRect();
        var pos = {x: e.clientX - rect.left, y: e.clientY - rect.top};
        if (pos.x > rect.width - 5 || pos.y > rect.height - 5){ //don't register position when its close to the border
            pos = {x: -1, y: -1};
        }
        return pos;
    }

    function mouseDown(){
        isMouseDown = true;
        startTime = new Date().getTime();
        selectedPointIndex = -1;
		if(canvasPos != null){
			for(var i = 0; i < points.length; i++){
				if(Math.abs(points[i].x - canvasPos.x) <= 4 && Math.abs(points[i].y - canvasPos.y) <= 4){
					selectedPointIndex = i;
				}
			}
			if(selectedPointIndex < 0){
				selectRect = {x1:canvasPos.x, y1:canvasPos.y, x2:canvasPos.x, y2:canvasPos.y};
			}
		}
    }

    function mouseUp(){
        isMouseDown = false;
        addPoint();
        startTime = -1;
        selectedPointIndex = -1;
    }

    function addPoint()
    {
		if(canvasPos == null){
			return;
		}
        endTime = new Date().getTime();
        var newPointAdded = false;
        if(selectedPointIndex < 0 && endTime - startTime < 200){
			if(points.length > maxPoints){
				return;
			}
            points[points.length] = canvasPos;
            newPointAdded = true;
        } else {
            endTime = new Date().getTime();
            if(startTime > 0){
                if(endTime - startTime < 200){
                    points.splice(selectedPointIndex, 1);
                    selectedPointIndex = -1;
                }
            }
        }
        //remove duplicate points
        var selectedPoint = selectedPointIndex;
        if(selectedPoint < 0 && newPointAdded){
            //ignore newly added point
            selectedPoint = points.length - 1
        }
        if(selectedPoint >= 0){
            for(var i = points.length - 1; i >= 0; i--){
                if(i != selectedPoint && Math.abs(points[i].x - canvasPos.x) <= 8 && Math.abs(points[i].y - canvasPos.y) <= 8){
                    points.splice(i, 1);
                }
            }
        }
		constructXTree();
        redraw();
    }

    function mouseMove(){
        endTime = new Date().getTime();
        if(startTime > 0){
            movePoint();
        }
        if(canvasPos != null && selectedPointIndex < 0 && selectRect != null && isMouseDown && endTime - startTime > 200 && startTime > 0 && canvasPos.x > 0 && canvasPos.y > 0){
            selectRect.y2 = canvasPos.y;
            selectRect.x2 = canvasPos.x;
        }
    }

    function movePoint(){
        if(selectedPointIndex >= 0 && canvasPos != null && canvasPos.x > 1 && canvasPos.y > 1){
            //var i = 0;
            //while(i < points.length){
            //}
            points[selectedPointIndex] = canvasPos;
			constructXTree();
        }
    }

    function redraw()
    {
		fixScale();
        clearCanvas();
        var ctx=canvas.getContext("2d");
        ctx.save();
        
		calcHoverColors();
		
        if(selectRect != null){
            var x = Math.min(selectRect.x1, selectRect.x2);
            var y = Math.min(selectRect.y1, selectRect.y2);
            var xDiff = Math.max(selectRect.x1, selectRect.x2) - x;<commit_msg>deselects node when adding, removing, or moving a point<commit_after>$(document).ready(function(){
    var canvasPos, xCanvasPos, yCanvasPos;
    var xCanvasPosPre, yCanvasPosPre;
    var points = new Array();
    var canvas, xCanvas, yCanvas;
    var startTime = -1;
    var selectedPointIndex = -1;
    var selectedPointYTreeIndex = -1;
    var highlightedPointIndex = -1;
    var selectedXNodeIndex = -1;
	var maxPoints = 200;
	var xTree = new Array();
	var yTree = new Array();
	var yTreePoints = new Array();
    var selectRect = null;
    var isMouseDown = false;
    var isMouseDownXCanvas = false;
    var isMouseDownYCanvas = false;
	var xRangeLeft = -1;
	var xRangeRight = -1;
    var xCanvasXOffset = 0;
    var xCanvasYOffset = 0;
    var yCanvasXOffset = 0;
    var yCanvasYOffset = 0;
    var xCanvasXOffsetMax = 0;
    var xCanvasYOffsetMax = 0;
    var xCanvasXOffsetMin = 0;
    var xCanvasYOffsetMin = 0;
    var yCanvasXOffsetMax = 0;
    var yCanvasYOffsetMax = 0;
    var yCanvasXOffsetMin = 0;
    var yCanvasYOffsetMin = 0;
    
    //get images to draw
    var redRect10px = document.getElementById("rect_red_10px");
    var greenRect10px = document.getElementById("rect_green_10px");
    var whiteRect10px = document.getElementById("rect_white_10px");
    var redCirc10px = document.getElementById("circ_red_10px");
    var whiteCirc10px = document.getElementById("circ_white_10px");

    //set up action listeners
    canvas = document.getElementById('rangeQueryCanvas');
    xCanvas = document.getElementById('xTreeCanvas');
    yCanvas = document.getElementById('yTreeCanvas');
	var clearButton = document.getElementById('rangeQueryClear');
	clearButton.onclick=function(){
        xCanvasXOffset = 0;
        xCanvasYOffset = 0;
        selectedXNodeIndex = -1;
        clearRangeQueryCanvas();
    };
    canvas.addEventListener('mousedown', function(){ mouseDown()}, false);
    canvas.addEventListener('mouseup', function(){ mouseUp()}, false);
    canvas.addEventListener('mousemove', function(e) {
        canvasPos = getCanvasPos(e, 'rangeQueryCanvas');
		xCanvasPos = null;
		yCanvasPos = null;
        mouseMove();
        redraw();
    }, false);
    canvas.addEventListener('mouseout', function(e) {
        canvasPos = null;
		xCanvasPos = null;
		yCanvasPos = null;
        startTime = -1;
        selectedPointIndex = -1;
        highlightedPointIndex = -1;
        redraw();
    }, false);
    xCanvas.addEventListener('mousemove', function(e) {
        xCanvasPos = getCanvasPos(e, 'xTreeCanvas');
		canvasPos = null;
		yCanvasPos = null;
        
        if(isMouseDownXCanvas){
            if(xCanvasPos.x > 0 && xCanvasPos.y > 0){
                xCanvasXOffset += xCanvasPos.x - xCanvasPosPre.x;
                xCanvasYOffset += xCanvasPos.y - xCanvasPosPre.y;
                xCanvasXOffset = Math.max(Math.min(xCanvasXOffset, xCanvasXOffsetMax), xCanvasXOffsetMin);
                xCanvasYOffset = Math.max(Math.min(xCanvasYOffset, xCanvasYOffsetMax), xCanvasYOffsetMin);
            } else {
                isMouseDownXCanvas = false
            }
        }
        
        redraw();
        xCanvasPosPre=xCanvasPos;
    }, false);
    yCanvas.addEventListener('mousemove', function(e) {
        yCanvasPos = getCanvasPos(e, 'yTreeCanvas');
		canvasPos = null;
		xCanvasPos = null;
        redraw();
        yCanvasPosPre=yCanvasPos;
    }, false);
    xCanvas.addEventListener('mousedown', function(e){ isMouseDownXCanvas = true}, false);
    xCanvas.addEventListener('mouseup', function(e){ 
        isMouseDownXCanvas = false;
        clickXCanvas();
    }, false);

    function getCanvasPos(e, canvasId){
		var c = document.getElementById(canvasId);
        var rect = c.getBoundingClientRect();
        var pos = {x: e.clientX - rect.left, y: e.clientY - rect.top};
        if (pos.x > rect.width - 5 || pos.y > rect.height - 5){ //don't register position when its close to the border
            pos = {x: -1, y: -1};
        }
        return pos;
    }

    function mouseDown(){
        isMouseDown = true;
        startTime = new Date().getTime();
        selectedPointIndex = -1;
		if(canvasPos != null){
			for(var i = 0; i < points.length; i++){
				if(Math.abs(points[i].x - canvasPos.x) <= 4 && Math.abs(points[i].y - canvasPos.y) <= 4){
					selectedPointIndex = i;
				}
			}
			if(selectedPointIndex < 0){
				selectRect = {x1:canvasPos.x, y1:canvasPos.y, x2:canvasPos.x, y2:canvasPos.y};
			}
		}
    }

    function mouseUp(){
        isMouseDown = false;
        addPoint();
        startTime = -1;
        selectedPointIndex = -1;
    }

    function addPoint()
    {
		if(canvasPos == null){
			return;
		}
        endTime = new Date().getTime();
        var newPointAdded = false;
        if(selectedPointIndex < 0 && endTime - startTime < 200){
			if(points.length > maxPoints){
				return;
			}
            points[points.length] = canvasPos;
            newPointAdded = true;
            selectedXNodeIndex = -1;
        } else {
            endTime = new Date().getTime();
            if(startTime > 0){
                if(endTime - startTime < 200){
                    points.splice(selectedPointIndex, 1);
                    selectedPointIndex = -1;
                }
            }
            selectedXNodeIndex = -1;
        }
        //remove duplicate points
        var selectedPoint = selectedPointIndex;
        if(selectedPoint < 0 && newPointAdded){
            //ignore newly added point
            selectedPoint = points.length - 1
        }
        if(selectedPoint >= 0){
            for(var i = points.length - 1; i >= 0; i--){
                if(i != selectedPoint && Math.abs(points[i].x - canvasPos.x) <= 8 && Math.abs(points[i].y - canvasPos.y) <= 8){
                    points.splice(i, 1);
                }
            }
        }
		constructXTree();
        redraw();
    }

    function mouseMove(){
        endTime = new Date().getTime();
        if(startTime > 0){
            movePoint();
        }
        if(canvasPos != null && selectedPointIndex < 0 && selectRect != null && isMouseDown && endTime - startTime > 200 && startTime > 0 && canvasPos.x > 0 && canvasPos.y > 0){
            selectRect.y2 = canvasPos.y;
            selectRect.x2 = canvasPos.x;
        }
    }

    function movePoint(){
        if(selectedPointIndex >= 0 && canvasPos != null && canvasPos.x > 1 && canvasPos.y > 1){
            points[selectedPointIndex] = canvasPos;
            selectedXNodeIndex = -1;
			constructXTree();
        }
    }

    function redraw()
    {
		fixScale();
        clearCanvas();
        var ctx=canvas.getContext("2d");
        ctx.save();
        
		calcHoverColors();
		
        if(selectRect != null){
            var x = Math.min(selectRect.x1, selectRect.x2);
            var y = Math.min(selectRect.y1, selectRect.y2);
            var xDiff = Math.max(selectRect.x1, selectRect.x2) - x;
{'fraction_non_alphanumeric': 0.1229335081203117, 'fraction_numerical': 0.015221032699730536, 'mean_word_length': 2.2509469696969697, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 35, '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': '4473338', 'n_tokens_mistral': 4397, 'n_tokens_neox': 4122, 'n_words': 918}