<commit_before>
"use strict"

$(document).ready(function() {

    var analyses = []
	var selectedAnalysisId = -1;
    var selectedAnalysis = null

    window.select = function(id) {

        if (selectedAnalysis != null)
            $(selectedAnalysis).removeClass("selected")

        var analysis = $('#id-' + id)
        selectedAnalysisId = id

        if (analysis.length === 0)
            return

        selectedAnalysis = analysis[0]

        analysis.addClass("selected").removeClass("unselected")
        $("body").addClass("selected")
    }
    
    window.scrollIntoView = function(item) {
    
        var itemTop = item.offset().top
        var itemBottom = itemTop + item.height() + parseInt(item.css('marginBottom')) + parseInt(item.css('marginTop'))
        var windowTop = document.body.scrollTop
        var windowBottom = windowTop + window.innerHeight

        //console.log(itemTop, itemBottom, windowTop, windowBottom)

        if (itemTop < windowTop && itemBottom < windowBottom)
            $("html, body").animate({ scrollTop: item.offset().top }, { duration: 'slow', easing: 'swing'});
        else if (itemBottom > windowBottom && item.height() < window.innerHeight)
            $("html, body").animate({ scrollTop: itemBottom - window.innerHeight + 10 }, { duration: 'slow', easing: 'swing'});
    
    }

    window.unselect = function() {

        $(analyses).removeClass("unselected")
        $(selectedAnalysis).removeClass("selected")
        $("body").removeClass("selected")

		selectedAnalysisId = -1
        selectedAnalysis = null
    }

    window.remove = function(id) {

        window.unselect()

        var analysis = $('#id-' + id)
        analysis.remove()
    }

    window.unselectByClickingBody = function()
    {
        window.unselect()
        jasp.analysisUnselected()
    }

    var selectedHandler = function(event) {

        var id = $(event.currentTarget).attr("id")
        var idAsInt = parseInt(id.substring(3))

        window.select(idAsInt)
        jasp.analysisSelected(idAsInt)

        event.stopPropagation()
    }

    window.analysisChanged = function(renderer, analysis) {

        var id = "id-" + analysis.id
        var results = analysis.results
        var status = analysis.status

        var item = $("#" + id)

        var newItem = $('<div id="' + id + '" class="jasp-analysis"></div>')
        newItem.click(selectedHandler)
        
        if (analysis.id == selectedAnalysisId)
        {
        	if (selectedAnalysis != null)
        		$(selectedAnalysis).removeClass("selected")

        	$(analyses).addClass("unselected")
        	$("body").addClass("selected")

        	selectedAnalysis = newItem[0]
        	$(selectedAnalysis).addClass("selected")
        }
        
        analyses.push(newItem[0])

        if (item.length !== 0)
        {
            analyses.filter(function(a) { return a !== item[0] })
            item.replaceWith(newItem);
        }
        else
        {
            var spacer = $("#spacer")
            spacer.before(newItem)
        }

        item = newItem

        renderer.render(item, results, status)
        
        if (selectedAnalysisId == analysis.id)
        	window.scrollIntoView(item);
    }

    $("body").click(window.unselectByClickingBody)
<commit_msg>Updated analysis selection behaviour in web view<commit_after>
"use strict"

$(document).ready(function() {

	var analyses = []
	var selectedAnalysisId = -1;
	var selectedAnalysis = null

	window.select = function(id) {

		if (selectedAnalysis != null)
			$(selectedAnalysis).removeClass("selected")

		var analysis = $('#id-' + id)
		selectedAnalysisId = id

		if (analysis.length === 0)
			return

		selectedAnalysis = analysis[0]

		analysis.addClass("selected").removeClass("unselected")
		$("body").addClass("selected")
	}
	
	window.scrollIntoView = function(item) {
	
		var itemTop = item.offset().top
		var itemBottom = itemTop + item.height() + parseInt(item.css('marginBottom')) + parseInt(item.css('marginTop'))
		var windowTop = document.body.scrollTop
		var windowBottom = windowTop + window.innerHeight

		//console.log(itemTop, itemBottom, windowTop, windowBottom)

		if (itemTop < windowTop && itemBottom < windowBottom)
			$("html, body").animate({ scrollTop: item.offset().top }, { duration: 'slow', easing: 'swing'});
		else if (itemBottom > windowBottom && item.height() < window.innerHeight)
			$("html, body").animate({ scrollTop: itemBottom - window.innerHeight + 10 }, { duration: 'slow', easing: 'swing'});
	
	}

	window.unselect = function() {

		$(analyses).removeClass("unselected")
		$(selectedAnalysis).removeClass("selected")
		$("body").removeClass("selected")

		selectedAnalysisId = -1
		selectedAnalysis = null
	}

	window.remove = function(id) {

		window.unselect()

		var analysis = $('#id-' + id)
		analysis.remove()
	}

	window.unselectByClickingBody = function(event) {

		if ($(event.target).is(".jasp-analysis *") == false) {
	
			window.unselect()
			jasp.analysisUnselected()
		}
	}

	var selectedHandler = function(event) {

		if ($(event.target).is(".toolbar") || $(event.target).is(".toolbar > *"))
			return

		var id = $(event.currentTarget).attr("id")
		var idAsInt = parseInt(id.substring(3))
		
		if (selectedAnalysisId == idAsInt) {
		
			window.unselect()
			jasp.analysisUnselected()
		}
		else {

			window.select(idAsInt)
			jasp.analysisSelected(idAsInt)
		}

	}

	window.analysisChanged = function(renderer, analysis) {

		var id = "id-" + analysis.id
		var results = analysis.results
		var status = analysis.status

		var item = $("#" + id)

		var newItem = $('<div id="' + id + '" class="jasp-analysis"></div>')
		newItem.click(selectedHandler)
		
		if (analysis.id == selectedAnalysisId)
		{
			if (selectedAnalysis != null)
				$(selectedAnalysis).removeClass("selected")

			$(analyses).addClass("unselected")
			$("body").addClass("selected")

			selectedAnalysis = newItem[0]
			$(selectedAnalysis).addClass("selected")
		}
		
		analyses.push(newItem[0])

		if (item.length !== 0)
		{
			analyses.filter(function(a) { return a !== item[0] })
			item.replaceWith(newItem);
		}
		else
		{
			var spacer = $("#spacer")
			spacer.before(newItem)
		}

		item = newItem

		renderer.render(item, results, status)
		
		if (selectedAnalysisId == analysis.id)
			window.scrollIntoView(item);
	}

	$("body").click(window.unselectByClickingBody)

{'fraction_non_alphanumeric': 0.150226668750977, 'fraction_numerical': 0.003439112083789276, 'mean_word_length': 2.9323909035033804, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 10, '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': '4412050', 'n_tokens_mistral': 2062, 'n_tokens_neox': 1865, 'n_words': 368}