<commit_before>var GOVUK = GOVUK || {};
GOVUK.transactionsExplorer = GOVUK.transactionsExplorer || {};

GOVUK.transactionsExplorer.loadSearchData = function(dataUrl, callback) {
    $.ajax({
        method: "GET",
        url: dataUrl,
        dataType: "json"
    }).done(callback);
};

GOVUK.transactionsExplorer.scoreService = (function () {
    var SEARCH_FIELDS = [
        "agencyOrBodyAbbreviation",
        "service",
        "departmentAbbreviation",
        "agencyOrBody",
        "department",
        "keywords",
        "category"
    ];

    var scoreService = function (searchTerm, service) {
        var score = 0;
        $.each(SEARCH_FIELDS, function (i, field) {
            var valueToSearch,
                termToUse = searchTerm.toLowerCase();
            if (field === "keywords") {
                valueToSearch = service[field].join(' ').toLowerCase();
            } else {
                valueToSearch = service[field].toLowerCase();
            }

            if (valueToSearch.search(termToUse) >= 0) {
                //FIXME this is pretty inefficient but $ doesn't provide a reduce
                score = service.transactionsPerYear || 1;
            }

        });
        return score;
    };

    return scoreService;
}());

GOVUK.transactionsExplorer.search = (function () {
    var data = [],
        loaded = false,
        cachedQuery = undefined;

    var load = function () {
        GOVUK.transactionsExplorer.loadSearchData("search.json", function (loadedData) {
            data = loadedData;
            loaded = true;
            if (cachedQuery) {
                GOVUK.transactionsExplorer.search.performSearch(cachedQuery, data);
            }
        });
    };

    var searchServices = function (query, services) {
        var scoredServices = $.map(services, function (service, index) {
            return {
                service: service,
                score: GOVUK.transactionsExplorer.scoreService(query, service)
            };
        });
        var matchedServices = $.grep(scoredServices, function(n, i) {
            return (n.score > 0);
        }).sort(function (first, next) {
            if (first.score > next.score) return -1;
            if (first.score < next.score) return 1;
            if (first.score === next.score) return 0;
        });
        return $.map(matchedServices, function(scoredService, index) {
            return scoredService.service;
        });
    };

    var performSearch = function (query) {
        if (loaded) {
            var results = GOVUK.transactionsExplorer.search.searchServices(query, data);
            GOVUK.transactionsExplorer.searchResultsTable.update(results);
        } else {
            cachedQuery = query;
        }

        $("table tbody").highlight(query);
    };

    return {
        load: load,
        searchServices: searchServices,
        performSearch: performSearch
    };
}());

GOVUK.transactionsExplorer.wireSearchForm = function(ids, search, keyword) {
    var searchBox = $(ids.inputId),
        loaded = false;

    if (keyword) {
        search.load();
        loaded = true;
        searchBox.val(keyword);
        search.performSearch(keyword);
    }

    searchBox.on('focus', function (event) {
        if (!loaded) {
            search.load();
            loaded = true;
        }
    });
};

GOVUK.transactionsExplorer.searchResultsTable = (function () {
    var table = undefined,
        ROW_TEMPLATE =  "<tr class='i-get-removed'>" + 
                            "<th class='js-row-header'></th>" + 
                            "<td class='js-row-abbr'></td>" +
                            "<td class='js-row-category'></td>" + 
                            "<td class='js-row-transaction'></td>" + 
                            "<td class='js-row-transactions'></td>" +
                        "</tr>";
    
    var wireTable = function (id) {
        table = $(id);
    };
    
    var rowHeader = function (serviceName, link) {
        if (link) {
            return '<a href="' + link  + '">' + serviceName + '</a>';
        } else {
            return serviceName;
        }
    };
    
    var transactionLink = function (transactionLink) {
        if (transactionLink) {
            return '<a rel="external" href="' + transactionLink + '">Access service</a>'; 
        } else {
            return "&nbsp;";
        }
    };

    var abbreviation = function (abbreviation, title) {
        if (!abbreviation) { return title; }
        return '<abbr title="' + title + '">' + abbreviation + '</abbr>'
    }

    var transactionsPerYear = function (transactionsPerYear) {
        if (transactionsPerYear) {
            return transactionsPerYear.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        } else {
            return "&nbsp;";
        }
    };

    var update = function (services) {
        var rows = [];

        $.each(services, function (i, service) {
            var row = $(ROW_TEMPLATE);
            row.find('.js-row-header').html(rowHeader(service['service'], service['detailsLink']));
            row.find('.js-row-abbr').html(abbreviation(service['agencyOrBodyAbbreviation'],service['agencyOrBody']));
            row.find('.js-row-category').html(service['category']);
            row.find('.js-row-transaction').html(transactionLink(service['transactionLink']));
            row.find('.js-row-transactions').html(transactionsPerYear(service['transactionsPerYear']));
        
            rows.push('<tr>' + row.html() + '</tr>');
        });
        table.find('tbody').html(rows.join(''));
    };

    return {
        wireTable: wireTable,
        update: update
    };
}());

GOVUK.transactionsExplorer.getSearchKeyword = function (documentSearchString) {
    var searchParam = undefined;
    if (documentSearchString) {
        var matchedUrlParams = documentSearchString.match(/(?:keyword)=([^&]+)/);
        if (matchedUrlParams && matchedUrlParams.length === 2) {
            searchParam = decodeURIComponent(matchedUrlParams[1]).replace(/\+/g, ' ');
        }
    }
    return searchParam;
};

GOVUK.transactionsExplorer.initSearch = function () {
    $(function () {
    GOVUK.transactionsExplorer.wireSearchForm({
        formId: '#search',
        inputId: '#search-box',
        buttonId: '#search-button'
        },
        GOVUK.transactionsExplorer.search,
        GOVUK.transactionsExplorer.getSearchKeyword(document.location.search));
    GOVUK.transactionsExplorer.searchResultsTable.wireTable('#transactions-table');
    });
};

<commit_msg>Display message for queries with no results<commit_after>var GOVUK = GOVUK || {};
GOVUK.transactionsExplorer = GOVUK.transactionsExplorer || {};

GOVUK.transactionsExplorer.loadSearchData = function(dataUrl, callback) {
    $.ajax({
        method: "GET",
        url: dataUrl,
        dataType: "json"
    }).done(callback);
};

GOVUK.transactionsExplorer.scoreService = (function () {
    var SEARCH_FIELDS = [
        "agencyOrBodyAbbreviation",
        "service",
        "departmentAbbreviation",
        "agencyOrBody",
        "department",
        "keywords",
        "category"
    ];

    var scoreService = function (searchTerm, service) {
        var score = 0;
        $.each(SEARCH_FIELDS, function (i, field) {
            var valueToSearch,
                termToUse = searchTerm.toLowerCase();
            if (field === "keywords") {
                valueToSearch = service[field].join(' ').toLowerCase();
            } else {
                valueToSearch = service[field].toLowerCase();
            }

            if (valueToSearch.search(termToUse) >= 0) {
                //FIXME this is pretty inefficient but $ doesn't provide a reduce
                score = service.transactionsPerYear || 1;
            }

        });
        return score;
    };

    return scoreService;
}());

GOVUK.transactionsExplorer.search = (function () {
    var data = [],
        loaded = false,
        cachedQuery = undefined;

    var load = function () {
        GOVUK.transactionsExplorer.loadSearchData("search.json", function (loadedData) {
            data = loadedData;
            loaded = true;
            if (cachedQuery) {
                GOVUK.transactionsExplorer.search.performSearch(cachedQuery, data);
            }
        });
    };

    var searchServices = function (query, services) {
        var scoredServices = $.map(services, function (service, index) {
            return {
                service: service,
                score: GOVUK.transactionsExplorer.scoreService(query, service)
            };
        });
        var matchedServices = $.grep(scoredServices, function(n, i) {
            return (n.score > 0);
        }).sort(function (first, next) {
            if (first.score > next.score) return -1;
            if (first.score < next.score) return 1;
            if (first.score === next.score) return 0;
        });
        return $.map(matchedServices, function(scoredService, index) {
            return scoredService.service;
        });
    };

    var performSearch = function (query) {
        if (loaded) {
            var results = GOVUK.transactionsExplorer.search.searchServices(query, data);
            GOVUK.transactionsExplorer.searchResultsTable.update(results);
        } else {
            cachedQuery = query;
        }

        $("table tbody").highlight(query);
    };

    return {
        load: load,
        searchServices: searchServices,
        performSearch: performSearch
    };
}());

GOVUK.transactionsExplorer.wireSearchForm = function(ids, search, keyword) {
    var searchBox = $(ids.inputId),
        loaded = false;

    if (keyword) {
        search.load();
        loaded = true;
        searchBox.val(keyword);
        search.performSearch(keyword);
    }

    searchBox.on('focus', function (event) {
        if (!loaded) {
            search.load();
            loaded = true;
        }
    });
};

GOVUK.transactionsExplorer.searchResultsTable = (function () {
    var table = undefined,
        ROW_TEMPLATE =  "<tr class='i-get-removed'>" + 
                            "<th class='js-row-header'></th>" + 
                            "<td class='js-row-abbr'></td>" +
                            "<td class='js-row-category'></td>" + 
                            "<td class='js-row-transaction'></td>" + 
                            "<td class='js-row-transactions'></td>" +
                        "</tr>",
        NO_RESULTS_TEMPLATE = "<tr><th colspan='5'>No results for that query</th></tr>";
    
    var wireTable = function (id) {
        table = $(id);
    };
    
    var rowHeader = function (serviceName, link) {
        if (link) {
            return '<a href="' + link  + '">' + serviceName + '</a>';
        } else {
            return serviceName;
        }
    };
    
    var transactionLink = function (transactionLink) {
        if (transactionLink) {
            return '<a rel="external" href="' + transactionLink + '">Access service</a>'; 
        } else {
            return "&nbsp;";
        }
    };

    var abbreviation = function (abbreviation, title) {
        if (!abbreviation) { return title; }
        return '<abbr title="' + title + '">' + abbreviation + '</abbr>'
    }

    var transactionsPerYear = function (transactionsPerYear) {
        if (transactionsPerYear) {
            return transactionsPerYear.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        } else {
            return "&nbsp;";
        }
    };

    var update = function (services) {
        var rows = [];

        $.each(services, function (i, service) {
            var row = $(ROW_TEMPLATE);
            row.find('.js-row-header').html(rowHeader(service['service'], service['detailsLink']));
            row.find('.js-row-abbr').html(abbreviation(service['agencyOrBodyAbbreviation'],service['agencyOrBody']));
            row.find('.js-row-category').html(service['category']);
            row.find('.js-row-transaction').html(transactionLink(service['transactionLink']));
            row.find('.js-row-transactions').html(transactionsPerYear(service['transactionsPerYear']));
        
            rows.push('<tr>' + row.html() + '</tr>');
        });

        if (rows.length === 0) {
            table.find('tbody').html(NO_RESULTS_TEMPLATE);
        } else {
            table.find('tbody').html(rows.join(''));
        }

    };

    return {
        wireTable: wireTable,
        update: update
    };
}());

GOVUK.transactionsExplorer.getSearchKeyword = function (documentSearchString) {
    var searchParam = undefined;
    if (documentSearchString) {
        var matchedUrlParams = documentSearchString.match(/(?:keyword)=([^&]+)/);
        if (matchedUrlParams && matchedUrlParams.length === 2) {
            searchParam = decodeURIComponent(matchedUrlParams[1]).replace(/\+/g, ' ');
        }
    }
    return searchParam;
};

GOVUK.transactionsExplorer.initSearch = function () {
    $(function () {
    GOVUK.transactionsExplorer.wireSearchForm({
        formId: '#search',
        inputId: '#search-box',
        buttonId: '#search-button'
        },
        GOVUK.transactionsExplorer.search,
        GOVUK.transactionsExplorer.getSearchKeyword(document.location.search));
    GOVUK.transactionsExplorer.searchResultsTable.wireTable('#transactions-table');
    });
};


{'fraction_non_alphanumeric': 0.14289998500524817, 'fraction_numerical': 0.001649422702054281, 'mean_word_length': 2.042655109489051, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 53, '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': '3353199', 'n_tokens_mistral': 3856, 'n_tokens_neox': 3708, 'n_words': 684}