<commit_before>/*
 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/**
 * A directive that displays a list of projects in a table.
 *
 * @see ProjectListController
 */
angular.module('sb.search').directive('searchResults',
    function ($log, $parse, Criteria, $injector, Preference) {
        'use strict';

        return {
            restrict: 'A',
            scope: true,
            link: function ($scope, $element, args) {

                // Extract the resource type.
                var resourceName = args.searchResource;
                var pageSize = args.searchPageSize ||
                    Preference.get('page_size');
                var searchWithoutCriteria =
                    args.searchWithoutCriteria === 'true';
                var criteria = [];

                $scope.isSearching = false;
                $scope.searchResults = [];

                /**
                 * The field to sort on.
                 *
                 * @type {string}
                 */
                $scope.sortField = 'id';

                /**
                 * The direction to sort on.
                 *
                 * @type {string}
                 */
                $scope.sortDirection = 'asc';

                /**
                 * Handle error result.
                 */
                function handleErrorResult() {
                    $scope.isSearching = false;
                }

                /**
                 * Handle search result.
                 *
                 * @param results
                 */
                function handleSearchResult(results) {
                    $scope.searchResults = results;
                    $scope.isSearching = false;
                }

                /**
                 * Toggle the filter ID and direction in the UI.
                 *
                 * @param fieldName
                 */
                $scope.toggleFilter = function (fieldName) {
                    if ($scope.sortField === fieldName) {
                        $scope.sortDirection =
                                $scope.sortDirection === 'asc' ? 'desc' : 'asc';
                    } else {
                        $scope.sortField = fieldName;
                        $scope.sortDirection = 'desc';
                    }
                    updateResults();
                };

                /**
                 * Update the results when the criteria change
                 */
                function updateResults() {

                    // Extract the valid criteria from the provided ones.
                    $scope.validCriteria = Criteria
                        .filterCriteria(resourceName, criteria);

                    // You have criteria, but they may not be valid.
                    $scope.hasCriteria = criteria.length > 0;

                    // You have criteria, and all of them are valid for
                    // this resource.
                    $scope.hasValidCriteria =
                        searchWithoutCriteria ||
                        ($scope.validCriteria.length === criteria.length &&
                            $scope.hasCriteria);

                    // No need to search if our criteria aren't valid.
                    if (!$scope.hasValidCriteria) {
                        $scope.searchResults = [];
                        $scope.isSearching = false;
                        return;
                    }

                    var params = Criteria.mapCriteria(resourceName,
                        $scope.validCriteria);
                    var resource = $injector.get(resourceName);

                    if (!resource) {
                        $log.error('Invalid resource name: ' +
                            resourceName);
                        return;
                    }

                    // Apply paging.
                    params.limit = pageSize;
                    params.sort_field = $scope.sortField;
                    params.sort_dir = $scope.sortDirection;

                    // If we don't actually have search criteria, issue a
                    // browse. Otherwise, issue a search.
                    if (!params.hasOwnProperty('q')) {
                        resource.query(params,
                            handleSearchResult,
                            handleErrorResult);
                    } else {
                        resource.search(params,
                            handleSearchResult,
                            handleErrorResult);
                    }
                }

                // Watch for changing criteria<commit_msg>Removed sort field from search.<commit_after>/*
 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/**
 * A directive that displays a list of projects in a table.
 *
 * @see ProjectListController
 */
angular.module('sb.search').directive('searchResults',
    function ($log, $parse, Criteria, $injector, Preference) {
        'use strict';

        return {
            restrict: 'A',
            scope: true,
            link: function ($scope, $element, args) {

                // Extract the resource type.
                var resourceName = args.searchResource;
                var pageSize = args.searchPageSize ||
                    Preference.get('page_size');
                var searchWithoutCriteria =
                    args.searchWithoutCriteria === 'true';
                var criteria = [];

                $scope.isSearching = false;
                $scope.searchResults = [];

                /**
                 * The field to sort on.
                 *
                 * @type {string}
                 */
                $scope.sortField = 'id';

                /**
                 * The direction to sort on.
                 *
                 * @type {string}
                 */
                $scope.sortDirection = 'asc';

                /**
                 * Handle error result.
                 */
                function handleErrorResult() {
                    $scope.isSearching = false;
                }

                /**
                 * Handle search result.
                 *
                 * @param results
                 */
                function handleSearchResult(results) {
                    $scope.searchResults = results;
                    $scope.isSearching = false;
                }

                /**
                 * Toggle the filter ID and direction in the UI.
                 *
                 * @param fieldName
                 */
                $scope.toggleFilter = function (fieldName) {
                    if ($scope.sortField === fieldName) {
                        $scope.sortDirection =
                                $scope.sortDirection === 'asc' ? 'desc' : 'asc';
                    } else {
                        $scope.sortField = fieldName;
                        $scope.sortDirection = 'desc';
                    }
                    updateResults();
                };

                /**
                 * Update the results when the criteria change
                 */
                function updateResults() {

                    // Extract the valid criteria from the provided ones.
                    $scope.validCriteria = Criteria
                        .filterCriteria(resourceName, criteria);

                    // You have criteria, but they may not be valid.
                    $scope.hasCriteria = criteria.length > 0;

                    // You have criteria, and all of them are valid for
                    // this resource.
                    $scope.hasValidCriteria =
                        searchWithoutCriteria ||
                        ($scope.validCriteria.length === criteria.length &&
                            $scope.hasCriteria);

                    // No need to search if our criteria aren't valid.
                    if (!$scope.hasValidCriteria) {
                        $scope.searchResults = [];
                        $scope.isSearching = false;
                        return;
                    }

                    var params = Criteria.mapCriteria(resourceName,
                        $scope.validCriteria);
                    var resource = $injector.get(resourceName);

                    if (!resource) {
                        $log.error('Invalid resource name: ' +
                            resourceName);
                        return;
                    }

                    // Apply paging.
                    params.limit = pageSize;

                    // If we don't actually have search criteria, issue a
                    // browse. Otherwise, issue a search.
                    if (!params.hasOwnProperty('q')) {
                        params.sort_field = $scope.sortField;
                        params.sort_dir = $scope.sortDirection;

                        resource.query(params,
                            handleSearchResult,
                            handleErrorResult);
                    } else {
                        resource.search(params,
                            handleSearchResult,
                            handleErrorResult);
                    }
                }

                // Watch for changing criteria
{'fraction_non_alphanumeric': 0.08203012568358438, 'fraction_numerical': 0.0017269500143912502, 'mean_word_length': 1.043520878259165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6428071', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2087, 'n_words': 635}