<commit_before>$(document).ready(function () {
    ///////////////////////////////////
    // Trash & Restore Functionality //
    ///////////////////////////////////
    $(".trash-button, .restore-button").click(function (event) {
        event.preventDefault();
        var button = $(this),
            id = button.data('id'),
            buttonIcon = button.find('i'),
            parentRow = button.closest('tr'),
            pageType,
            message;

        buttonIcon.removeClass("fa-trash-o");
        buttonIcon.addClass("fa-spinner fa-pulse");
        if (window.location.href.indexOf("deleted") >= 0){
            // We're on the deleted page, trying to restore.
            pageType = "trash";
        } else {
            pageType = "active";
        }

        $.ajax({
            method: "POST",
            url: "/visualizations/scotus-mapper" + (pageType == 'trash' ? "/restore/" : "/delete/"),
            data: {pk: id},
            success: function () {
                $('.bootstrap-growl').alert("close");
                parentRow.fadeOut('slow');
                if (pageType == "trash") {
                    message = "Your item was restored successfully."
                } else {
                    message = "Your item was moved to the trash."
                }
                $.bootstrapGrowl(
                    message,
                    {
                        type: "success",
                        align: "center",
                        width: "auto",
                        delay: 2000,
                        allow_dismiss: false,
                        offset: {from: 'top', amount: 80}
                    }
                );
            },
            error: function () {
                buttonIcon.removeClass("fa-spinner fa-pulse");
                buttonIcon.addClass("fa-trash-o");
                if (pageType == "trash") {
                    message = "An error occurred. Unable to restore your item."
                } else {
                    message = "An error occurred. Unable to move your item to the trash."
                }
                $.bootstrapGrowl(
                    message,
                    {
                        type: "danger",
                        align: "center",
                        width: "auto",
                        delay: 2000,
                        allow_dismiss: false,
                        offset: {from: 'top', amount: 80}
                    }
                );
            }
        });
    });
    $(function () {
        // Initialize tooltips on this page.
        $('[data-toggle="tooltip"]').tooltip()
    });


    ///////////////////////////
    // New Viz Functionality //
    ///////////////////////////
    var authorityIDs = {};
    var updateAuthorityCache = function (suggestion, callback) {
        // Check if we have the ID in our cache. If so, do nothing. If not,
        // load up the cache.
        if (suggestion.id in authorityIDs) {
            // All good; do nothing; pass
        } else {
            // Get the IDs, add them as a new key.
            // Get a list of the IDs cited by the item and cache it. This cache
            // is needed to do the reverse lookup for authorities.
            $.ajax({
                'method': 'GET',
                'url': "/api/rest/v3/search/",
                'data': {q: "id:" + suggestion.id, 'format': 'json'}
            }).done(function (data) {
                authorityIDs[suggestion.id] = data.results[0].cites || [];
                callback(suggestion);
            });
        }
    };

    var identify = function (obj) {
        // Return the item ID so the system can have a unique id for it
        // in its caches.
        return obj.id;
    };
    var transform = function (response) {
        // Pull the results dict out of the main object.
        return response.results
    };
    var remotePrepare = function (query, settings) {
        // This query is anything with the case name typed in...
        // ...in the supreme court...
        // ...between 1945 and a year ago that has an SCDB id... OR
        // ...in the last year, with or without an SCDB id.
        var params = {
            court: 'scotus',
            q: '((dateFiled:[1945-01-01T00:00:00Z TO ' + last_year + 'Z] AND scdb_id:["" TO *]) OR (dateFiled:[' + last_year + 'Z TO *]))',
            format: 'json'
        };
        if (query.length > 0) {
            // Add a case name parameter, if the user has typed something.
            params.case_name = "(%QUERY) OR (%QUERY*)".replace(
                /%QUERY/g, $.trim(query));
        }

        var start_id = $('#id_cluster_start').val();
        if ($("#ending-cluster-typeahead-search").is(":focus")) {
            // Pass. No extra params required to do simple search.
        } else if ($("#ending-cluster-typeahead-authorities").is(":focus")) {
            // Append the authority IDs onto the end of the query.
            params.q += " AND id:(" + authorityIDs[start_id].join(" OR ") + ")";
        } else if ($("#ending-cluster-typeahead-citing").is(":focus")) {
            // Append the cited_by ID onto the end of the query.
            params.q += " AND cites:(" + start_id + ")";
        }

        return settings.url + $.param(params);
    };

    var searchResultsStartingCluster = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });
    var searchResultsEndingClusterSearch = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });
    var searchResultsEndingClusterAuthorities = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });
    var searchResultsEndingClusterCiting = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });

    var showFields = function (obj) {
        // Make a nice concatenation of citations, case name and year.
        var parts = [obj.caseName];
        if (obj.dateFiled) {
            parts.push(new Date(obj.dateFiled).getUTCFullYear());<commit_msg>Attempts to fix issue with citation count being wrong.<commit_after>$(document).ready(function () {
    ///////////////////////////////////
    // Trash & Restore Functionality //
    ///////////////////////////////////
    $(".trash-button, .restore-button").click(function (event) {
        event.preventDefault();
        var button = $(this),
            id = button.data('id'),
            buttonIcon = button.find('i'),
            parentRow = button.closest('tr'),
            pageType,
            message;

        buttonIcon.removeClass("fa-trash-o");
        buttonIcon.addClass("fa-spinner fa-pulse");
        if (window.location.href.indexOf("deleted") >= 0){
            // We're on the deleted page, trying to restore.
            pageType = "trash";
        } else {
            pageType = "active";
        }

        $.ajax({
            method: "POST",
            url: "/visualizations/scotus-mapper" + (pageType == 'trash' ? "/restore/" : "/delete/"),
            data: {pk: id},
            success: function () {
                $('.bootstrap-growl').alert("close");
                parentRow.fadeOut('slow');
                if (pageType == "trash") {
                    message = "Your item was restored successfully."
                } else {
                    message = "Your item was moved to the trash."
                }
                $.bootstrapGrowl(
                    message,
                    {
                        type: "success",
                        align: "center",
                        width: "auto",
                        delay: 2000,
                        allow_dismiss: false,
                        offset: {from: 'top', amount: 80}
                    }
                );
            },
            error: function () {
                buttonIcon.removeClass("fa-spinner fa-pulse");
                buttonIcon.addClass("fa-trash-o");
                if (pageType == "trash") {
                    message = "An error occurred. Unable to restore your item."
                } else {
                    message = "An error occurred. Unable to move your item to the trash."
                }
                $.bootstrapGrowl(
                    message,
                    {
                        type: "danger",
                        align: "center",
                        width: "auto",
                        delay: 2000,
                        allow_dismiss: false,
                        offset: {from: 'top', amount: 80}
                    }
                );
            }
        });
    });
    $(function () {
        // Initialize tooltips on this page.
        $('[data-toggle="tooltip"]').tooltip()
    });


    ///////////////////////////
    // New Viz Functionality //
    ///////////////////////////
    var dateFiledQ = '((dateFiled:[1945-01-01T00:00:00Z TO ' + last_year + 'Z] AND scdb_id:["" TO *]) OR (dateFiled:[' + last_year + 'Z TO *]))';
    var authorityIDs = {};
    var updateAuthorityCache = function (suggestion, callback) {
        // Check if we have the ID in our cache. If so, do nothing. If not,
        // load up the cache.
        if (suggestion.id in authorityIDs) {
            // All good; do nothing; pass
        } else {
            // Get the IDs, add them as a new key.
            // Get a list of the IDs cited by the item and cache it. This cache
            // is needed to do the reverse lookup for authorities.
            $.ajax({
                'method': 'GET',
                'url': "/api/rest/v3/search/",
                'data': {
                    q: dateFiledQ + " AND id:" + suggestion.id,
                    court: 'scotus',
                    format: 'json'
                }
            }).done(function (data) {
                authorityIDs[suggestion.id] = data.results[0].cites || [];
                callback(suggestion);
            });
        }
    };

    var identify = function (obj) {
        // Return the item ID so the system can have a unique id for it
        // in its caches.
        return obj.id;
    };
    var transform = function (response) {
        // Pull the results dict out of the main object.
        return response.results
    };
    var remotePrepare = function (query, settings) {
        // This query is anything with the case name typed in...
        // ...in the supreme court...
        // ...between 1945 and a year ago that has an SCDB id... OR
        // ...in the last year, with or without an SCDB id.
        var params = {
            court: 'scotus',
            q: dateFiledQ,
            format: 'json'
        };
        if (query.length > 0) {
            // Add a case name parameter, if the user has typed something.
            params.case_name = "(%QUERY) OR (%QUERY*)".replace(
                /%QUERY/g, $.trim(query));
        }

        var start_id = $('#id_cluster_start').val();
        if ($("#ending-cluster-typeahead-search").is(":focus")) {
            // Pass. No extra params required to do simple search.
        } else if ($("#ending-cluster-typeahead-authorities").is(":focus")) {
            // Append the authority IDs onto the end of the query.
            params.q += " AND id:(" + authorityIDs[start_id].join(" OR ") + ")";
        } else if ($("#ending-cluster-typeahead-citing").is(":focus")) {
            // Append the cited_by ID onto the end of the query.
            params.q += " AND cites:(" + start_id + ")";
        }

        return settings.url + $.param(params);
    };

    var searchResultsStartingCluster = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('caseName'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });
    var searchResultsEndingClusterSearch = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('caseName'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });
    var searchResultsEndingClusterAuthorities = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('caseName'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });
    var searchResultsEndingClusterCiting = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('caseName'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        sufficient: 21,
        identify: identify,
        remote: {
            url: '/api/rest/v3/search/?',
            prepare: remotePrepare,
            transform: transform
        }
    });

    var showFields = function (obj) {
        // Make a nice concatenation of citations, case name and year.
        var parts = [obj.caseName];
        if (obj.dateFiled) {
            parts.push(new Date(obj.dateFiled).getUTCFullYear());
{'fraction_non_alphanumeric': 0.13722627737226278, 'fraction_numerical': 0.006395550921098367, 'mean_word_length': 1.394474034620506, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, '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': '3622018', 'n_tokens_mistral': 3902, 'n_tokens_neox': 3683, 'n_words': 1010}