<commit_before>/* This is the central Dissemin JavaScript library */
/* Every function should be inside this file */


/* ***
 * Miscellaneous
 * *** */

/* On AJAX errors, we like to be informed via Sentry */
$(document).ajaxError( function (event, jqXHR, ajaxSettings, thrownError) {
    try {
        Sentry.captureMessage(thrownError || jqXHR.statusText, {
            extra: {
                type: ajaxSettings.type,
                url: ajaxSettings.url,
                data: ajaxSettings.data,
                status: jqXHR.status,
                error: thrownError || jqXHR.statusText,
                response: jqXHR.responseText.substring(0, 100)
            }
        });
    }
    catch (e)
    {
        console.log(thrownError);
        console.log(ajaxSettings);
    }
});

/* Returns the current csrf token from the cookie. This is the recommend method by django: https://docs.djangoproject.com/en/2.2/ref/csrf/#ajax */
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i].trim();
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}


/* ***
 * Navigation
 * *** */

/* When changing language in dropdown, submit the form */
$(function() {
    $("#select-language").change(function() {
        $("#select-language").submit();
    });
});

/* This is the ORCID Logout. Pass the orcid_base_domain as parameter */
function orcidLogout (orcid_base_domain) {
    $.ajax({ url: 'https://' + orcid_base_domaon + '/userStatus.json?logUserOut=true',
        dataType: 'jsonp',
        success: function(result,status,xhr) {
                    window.location.href = '{% url "account_logout" %}';
                },
        error: function (xhr, status, error) {
                    window.location.href = '{% url "account_logout" %}';
                }
    })
}


/* ***
 * Search
 * *** */

/* Our strategy is: Reload a part of the page and process any messages that get delivered */

/* Prevent standard behaviour of form and execute some JS */

$(function () {
    $('#searchPapers').submit(function (e) {
        e.preventDefault();

        // slighty fade current results that are going to be replaced
        $('#paperSearchResults').css('opacity', '0.5');
        // turn bird on
        $('#paperSearchWaitingArea').toggleClass('d-none d-flex');

        // call with ajax. it's easy
        $.ajax ({
            contentType : 'application/json',
            data : $(this).serializeArray(),
            dataType : 'json',
            method : 'GET',
            success : function (result) {
                $('#paperSearchResults').html(result.listPapers);
                $('#searchNotifications').html(result.messages);
                // update pie
                updateStats(result.stats);
                // update number of search results
                $('#nbPapersFound').text(
                    interpolate(
                        ngettext(
                            '%s paper found',
                            '%s papers found',
                            result.nb_results
                        ),
                        [formatNumbersThousands(result.nb_results)]
                    )
                );
            },
            timeout : 5000, // 5 seconds
        });

        // turn bird off
        $('#paperSearchWaitingArea').toggleClass('d-none d-flex');
        $('#paperSearchResults').css('opacity', '');

    });
});


/* Refreshes to profil of a user from ORCID.
 * This function is here, because it is related to the search */

$(function () {
    $('#refetchPublications').submit( function () {
        var obj = $(this);
        var researcher_pk = obj.attr('data-researcher-pk');
        var ajax_url = Urls['refetch-researcher'](researcher_pk);

        // slighty fade current results that are going to be replaced
        $('#paperSearchResults').css('opacity', '0.5');
        // turn bird on
        $('#paperSearchWaitingArea').toggleClass('d-none d-flex');

        $.ajax({
            contentType : 'application/json',
            data : null,
            dataType : 'json',
            success : function (result) {
                $('#paperSearchResults').html(result.listPapers);
                $('#searchNotifications').html(result.messages);
                // update pie
                updateStats(result.stats);
                // update number of search results
                $('#nbPapersFound').text(
                    interpolate(
                        ngettext(
                            '%s paper found',
                            '%s papers found',
                            result.nb_results
                        ),
                        [formatNumbersThousands(result.nb_results)]
                    )
                );
            },
            timeout : 5000, // 5 seconds
            url : ajax_url
        });

        // turn bird off
        $('#paperSearchWaitingArea').toggleClass('d-none d-flex');
        $('#paperSearchResults').css('opacity', '');

    });
});

/* When a message on search is closed, move it from inbox toarchive to not display it again
 * This function is here, because it is related to the search */
$(function () {
    $('.messageAlert').on('closed.bs.alert', function () {
        var obj = $(this);
        var message_pk = obj.attr('data-message-pk');
        var url = Urls['inbox-read'](message_pk);

        $.ajax({
            data: {
                "csrfmiddlewaretoken": getCookie('csrftoken')
            },
            method : 'POST',
            url : url
        });
    });
});

/* Claim and unclaim items to or from profile
 * This function is here, because it is related to the search */

$(function () {
    $('.buttonClaimUnclaim').submit( function () {
        var obj = $(this);<commit_msg>Redundant code<commit_after>/* This is the central Dissemin JavaScript library */
/* Every function should be inside this file */


/* ***
 * Miscellaneous
 * *** */

/* On AJAX errors, we like to be informed via Sentry */
$(document).ajaxError( function (event, jqXHR, ajaxSettings, thrownError) {
    try {
        Sentry.captureMessage(thrownError || jqXHR.statusText, {
            extra: {
                type: ajaxSettings.type,
                url: ajaxSettings.url,
                data: ajaxSettings.data,
                status: jqXHR.status,
                error: thrownError || jqXHR.statusText,
                response: jqXHR.responseText.substring(0, 100)
            }
        });
    }
    catch (e)
    {
        console.log(thrownError);
        console.log(ajaxSettings);
    }
});

/* Returns the current csrf token from the cookie. This is the recommend method by django: https://docs.djangoproject.com/en/2.2/ref/csrf/#ajax */
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i].trim();
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}


/* ***
 * Navigation
 * *** */

/* When changing language in dropdown, submit the form */
$(function() {
    $("#select-language").change(function() {
        $("#select-language").submit();
    });
});

/* This is the ORCID Logout. Pass the orcid_base_domain as parameter */
function orcidLogout (orcid_base_domain) {
    $.ajax({ url: 'https://' + orcid_base_domaon + '/userStatus.json?logUserOut=true',
        dataType: 'jsonp',
        success: function(result,status,xhr) {
                    window.location.href = '{% url "account_logout" %}';
                },
        error: function (xhr, status, error) {
                    window.location.href = '{% url "account_logout" %}';
                }
    })
}


/* ***
 * Search
 * *** */

/* Our strategy is: Reload a part of the page and insert any messages that get delivered */

function updateSearch (ajax_url, data) {
    // slighty fade current results that are going to be replaced
    $('#paperSearchResults').css('opacity', '0.5');
    // turn bird on
    $('#paperSearchWaitingArea').toggleClass('d-none d-flex');

    // call with ajax. it's easy
    $.ajax ({
        contentType : 'application/json',
        data : data,
        dataType : 'json',
        method : 'GET',
        success : function (result) {
            $('#paperSearchResults').html(result.listPapers);
            $('#searchNotifications').html(result.messages);
            // update pie
            updateStats(result.stats);
            // update number of search results
            $('#nbPapersFound').text(
                interpolate(
                    ngettext(
                        '%s paper found',
                        '%s papers found',
                        result.nb_results
                    ),
                    [formatNumbersThousands(result.nb_results)]
                )
            );
        },
        timeout : 5000, // 5 seconds
        url : ajax_url
    });

    // turn bird off
    $('#paperSearchWaitingArea').toggleClass('d-none d-flex');
    // remove opacity
    $('#paperSearchResults').css('opacity', '');
}

/* Prevent standard behaviour of form and execute some JS */
$(function () {
    $('#searchPapers').submit(function (e) {
        e.preventDefault();

        var ajax_url =  null // We pass just null, to keep the view we currently use
        var data = $(this).serializeArray();

        updateSearch(ajax_url, data);
    });
});


/* Refreshes to profil of a user from ORCID.
 * This function is here, because it is related to the search */
$(function () {
    $('#refetchPublications').submit( function () {
        var obj = $(this);
        var researcher_pk = obj.attr('data-researcher-pk');
        var ajax_url = Urls['refetch-researcher'](researcher_pk);

        updateSearch(ajax_url);
    });
});

/* When a message on search is closed, move it from inbox toarchive to not display it again
 * This function is here, because it is related to the search */
$(function () {
    $('.messageAlert').on('closed.bs.alert', function () {
        var obj = $(this);
        var message_pk = obj.attr('data-message-pk');
        var url = Urls['inbox-read'](message_pk);

        $.ajax({
            data: {
                "csrfmiddlewaretoken": getCookie('csrftoken')
            },
            method : 'POST',
            url : url
        });
    });
});

/* Claim and unclaim items to or from profile
 * This function is here, because it is related to the search */

$(function () {
    $('.buttonClaimUnclaim').submit( function () {
        var obj = $(this);
{'fraction_non_alphanumeric': 0.13679454239390448, 'fraction_numerical': 0.0036324975635687075, 'mean_word_length': 1.8319116909182138, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '4972788', 'n_tokens_mistral': 3124, 'n_tokens_neox': 3013, 'n_words': 800}