<commit_before>/*********************************************
 *  Copyright (c) 2013 AnsibleWorks, Inc.
 *
 *  SearchHelper
 *
 *  All the parts for controlling the search widget on 
 *  related collections.
 *
 *  SearchInit({
 *      scope:       <scope>,
 *      set:         <model name (i.e. organizations) used in ng-repeat>
 *      url:         <default api url used to load data>
 *      list:        <list object used by ListGenerator>
 *      });   
 *
 */

angular.module('SearchHelper', ['RestServices', 'Utilities', 'RefreshHelper'])  
    .factory('SearchInit', ['Alert', 'Rest', 'Refresh', function(Alert, Rest, Refresh) {
    return function(params) {
        
        var scope = params.scope;
        var set = params.set;
        var defaultUrl = params.url;
        var list = params.list; 
        var iterator = (params.iterator) ? params.iterator : list.iterator;
        var sort_order; 

        // Set default values
        for (fld in list.fields) {
            if (list.fields[fld].key) {
               if (list.fields[fld].sourceModel) {
                  var fka = list.fields[fld].sourceModel + '__' + list.fields[fld].sourceField;
                  sort_order = (list.fields[fld].desc) ? '-' + fka : fka;
               }
               else {
                  sort_order = (list.fields[fld].desc) ? '-' + fld : fld; 
               }
               if (list.fields[fld].searchable == undefined || list.fields[fld].searchable == true) {
                  scope[iterator + 'SearchField'] = fld;
                  scope[iterator + 'SearchFieldLabel'] = list.fields[fld].label;
               }
               break;
            }
        }

        if (!scope[iterator + 'SearchField']) {
           // A field marked as key may not be 'searchable'
           for (fld in list.fields) {
               if (list.fields[fld].searchable == undefined || list.fields[fld].searchable == true) { 
                  scope[iterator + 'SearchField'] = fld;
                  scope[iterator + 'SearchFieldLabel'] = list.fields[fld].label;
                  break;
               }
           }
        }

        scope[iterator + 'SearchType'] = 'icontains';
        scope[iterator + 'SearchTypeLabel'] = 'Contains';
        scope[iterator + 'SearchParams'] = '';
        scope[iterator + 'SearchValue'] = '';
        scope[iterator + 'SelectShow'] = false;   // show/hide the Select
        scope[iterator + 'HideSearchType'] = false;
        scope[iterator + 'InputDisable'] = false; 

        var f = scope[iterator + 'SearchField']
        if (list.fields[f].searchType && ( list.fields[f].searchType == 'boolean' 
               || list.fields[f].searchType == 'select')) {
           scope[iterator + 'SelectShow'] = true;
           scope[iterator + 'SearchSelectOpts'] = list.fields[f].searchOptions;
        }
        if (list.fields[f].searchType && list.fields[f].searchType == 'int') {
           scope[iterator + 'HideSearchType'] = true;   
        }
        if (list.fields[f].searchType && list.fields[f].searchType == 'gtzero') {
              scope[iterator + "InputHide"] = true;
        }
      
        // Functions to handle search widget changes
        scope.setSearchField = function(iterator, fld, label) {
           scope[iterator + 'SearchFieldLabel'] = label;
           scope[iterator + 'SearchField'] = fld;
           scope[iterator + 'SearchValue'] = '';
           scope[iterator + 'SelectShow'] = false;
           scope[iterator + 'HideSearchType'] = false;
           scope[iterator + 'InputHide'] = false;
           scope[iterator + 'InputDisable'] = false;
           scope[iterator + 'SearchType'] = 'icontains';
           
           if (list.fields[fld].searchType && list.fields[fld].searchType == 'gtzero') {
              scope[iterator + "InputDisable"] = true;
           }
           else if (list.fields[fld].searchSingleValue){
              // Query a specific attribute for one specific value
              // searchSingleValue: true
              // searchType: 'boolean|int|etc.'
              // searchValue: < value to match for boolean use 'true'|'false' >
              scope[iterator + 'InputDisable'] = true;
              scope[iterator + "SearchValue"] = list.fields[fld].searchValue;
              // For boolean type, SearchValue must be an object
              if (list.fields[fld].searchType == 'boolean' && list.fields[fld].searchValue == 'true') {
                 scope[iterator + "SearchSelectValue"] = { value: 1 }; 
              }
              else if (list.fields[fld].searchType == 'boolean' && list.fields[fld].searchValue == 'false') {
                 scope[iterator + "SearchSelectValue"] = { value: 0 };
              }
              else {
                 scope[iterator + "SearchSelectValue"] = { value: list.fields[fld].searchValue };
              }
           }
           else if (list.fields[fld].searchType == 'in') {
              scope[iterator + "SearchType"] = 'in';
              scope[iterator + "SearchValue"] = list.fields[fld].searchValue;
              scope[iterator + "InputDisable"] = true;
           }
           else if (list.fields[fld].searchType && (list.fields[fld].searchType == 'boolean' 
                || list.fields[fld].searchType == 'select')) {
              scope[iterator + 'SelectShow'] = true;
              scope[iterator + 'SearchSelectOpts'] = list.fields[fld].searchOptions;
           }
           else if (list.fields[fld].searchType && list.fields[fld].searchType == 'int') {
              scope[iterator + 'HideSearchType'] = true;   
           }    
           scope.search(iterator);
           }

        scope.resetSearch = function(iterator) {
           // Respdond to click of reset button
           scope[iterator + "SearchValue"] = '';
           scope[iterator + "SearchSelectValue"] = '';
           scope[iterator + 'SelectShow'] = false;
           scope[iterator + 'HideSearchType'] = false;
           scope[iterator + 'InputHide'] = false;
           scope[iterator + 'InputDisable'] = false;
           for (fld in list.fields) {
               if (list.fields[fld].searchable == undefined || list.fields[fld].searchable == true) {
                  scope[iterator + 'SearchFieldLabel'] = list.fields[fld].label;
                  scope[iterator + 'SearchField'] = fld;
                  break;
               }
           }
           scope.search(iterator);
           }

        scope.setSearchType = function(iterator, type, label) {
           scope[iterator + 'SearchTypeLabel'] = label; 
           scope[iterator + 'SearchType'] = type;
           scope.search(iterator);
           }

        scope.search = function(iterator, page, load, spin) {
           // Page is optional. Added to accomodate back function on Job Events detail. 
           // Spin optional -set to false if spin not desired.
           // Load optional -set to false if loading message not desired

           scope[iterator + 'SearchSpin'] = (spin == undefined || spin == true) ? true : false;
           scope[iterator + 'Loading'] = (load == undefined || load == true) ? true : false;
           scope[iterator + 'SearchParms'] = '';
           var url = defaultUrl;
           
           if ( (scope[iterator + 'SelectShow'] == false && scope[iterator + 'SearchValue'] != '' && scope[iterator + 'SearchValue'] != undefined) ||
                (scope[iterator + 'SelectShow'] && scope[iterator + 'SearchSelectValue']) || 
                (list.fields[scope[iterator + 'SearchField']].searchType && list.fields[scope[iterator + 'SearchField']].searchType == 'gtzero') ) {
              
              if (list.fields[scope[iterator + 'SearchField']].searchField) {
                 scope[iterator + 'SearchParams'] = list.fields[scope[iterator + 'SearchField']].searchField + '__'; 
              }
              else if (list.fields[scope[iterator + 'SearchField']].sourceModel) {
                 // handle fields whose source is a related model e.g. inventories.organization<commit_msg>AC-559 Changed reset filter to clear the search and remove search keys from the URL displayed in the browser. Removing search keys from URL requires resetting window.location, which does to a page refresh. O'well.<commit_after>/*********************************************
 *  Copyright (c) 2013 AnsibleWorks, Inc.
 *
 *  SearchHelper
 *
 *  All the parts for controlling the search widget on 
 *  related collections.
 *
 *  SearchInit({
 *      scope:       <scope>,
 *      set:         <model name (i.e. organizations) used in ng-repeat>
 *      url:         <default api url used to load data>
 *      list:        <list object used by ListGenerator>
 *      });   
 *
 */

angular.module('SearchHelper', ['RestServices', 'Utilities', 'RefreshHelper'])  
    .factory('SearchInit', ['Alert', 'Rest', 'Refresh', '$location', function(Alert, Rest, Refresh, $location) {
    return function(params) {
        
        var scope = params.scope;
        var set = params.set;
        var defaultUrl = params.url;
        var list = params.list; 
        var iterator = (params.iterator) ? params.iterator : list.iterator;
        var sort_order; 

        // Set default values
        for (fld in list.fields) {
            if (list.fields[fld].key) {
               if (list.fields[fld].sourceModel) {
                  var fka = list.fields[fld].sourceModel + '__' + list.fields[fld].sourceField;
                  sort_order = (list.fields[fld].desc) ? '-' + fka : fka;
               }
               else {
                  sort_order = (list.fields[fld].desc) ? '-' + fld : fld; 
               }
               if (list.fields[fld].searchable == undefined || list.fields[fld].searchable == true) {
                  scope[iterator + 'SearchField'] = fld;
                  scope[iterator + 'SearchFieldLabel'] = list.fields[fld].label;
               }
               break;
            }
        }

        if (!scope[iterator + 'SearchField']) {
           // A field marked as key may not be 'searchable'
           for (fld in list.fields) {
               if (list.fields[fld].searchable == undefined || list.fields[fld].searchable == true) { 
                  scope[iterator + 'SearchField'] = fld;
                  scope[iterator + 'SearchFieldLabel'] = list.fields[fld].label;
                  break;
               }
           }
        }

        scope[iterator + 'SearchType'] = 'icontains';
        scope[iterator + 'SearchTypeLabel'] = 'Contains';
        scope[iterator + 'SearchParams'] = '';
        scope[iterator + 'SearchValue'] = '';
        scope[iterator + 'SelectShow'] = false;   // show/hide the Select
        scope[iterator + 'HideSearchType'] = false;
        scope[iterator + 'InputDisable'] = false; 

        var f = scope[iterator + 'SearchField']
        if (list.fields[f].searchType && ( list.fields[f].searchType == 'boolean' 
               || list.fields[f].searchType == 'select')) {
           scope[iterator + 'SelectShow'] = true;
           scope[iterator + 'SearchSelectOpts'] = list.fields[f].searchOptions;
        }
        if (list.fields[f].searchType && list.fields[f].searchType == 'int') {
           scope[iterator + 'HideSearchType'] = true;   
        }
        if (list.fields[f].searchType && list.fields[f].searchType == 'gtzero') {
              scope[iterator + "InputHide"] = true;
        }
      
        // Functions to handle search widget changes
        scope.setSearchField = function(iterator, fld, label) {
           scope[iterator + 'SearchFieldLabel'] = label;
           scope[iterator + 'SearchField'] = fld;
           scope[iterator + 'SearchValue'] = '';
           scope[iterator + 'SelectShow'] = false;
           scope[iterator + 'HideSearchType'] = false;
           scope[iterator + 'InputHide'] = false;
           scope[iterator + 'InputDisable'] = false;
           scope[iterator + 'SearchType'] = 'icontains';
           
           if (list.fields[fld].searchType && list.fields[fld].searchType == 'gtzero') {
              scope[iterator + "InputDisable"] = true;
           }
           else if (list.fields[fld].searchSingleValue){
              // Query a specific attribute for one specific value
              // searchSingleValue: true
              // searchType: 'boolean|int|etc.'
              // searchValue: < value to match for boolean use 'true'|'false' >
              scope[iterator + 'InputDisable'] = true;
              scope[iterator + "SearchValue"] = list.fields[fld].searchValue;
              // For boolean type, SearchValue must be an object
              if (list.fields[fld].searchType == 'boolean' && list.fields[fld].searchValue == 'true') {
                 scope[iterator + "SearchSelectValue"] = { value: 1 }; 
              }
              else if (list.fields[fld].searchType == 'boolean' && list.fields[fld].searchValue == 'false') {
                 scope[iterator + "SearchSelectValue"] = { value: 0 };
              }
              else {
                 scope[iterator + "SearchSelectValue"] = { value: list.fields[fld].searchValue };
              }
           }
           else if (list.fields[fld].searchType == 'in') {
              scope[iterator + "SearchType"] = 'in';
              scope[iterator + "SearchValue"] = list.fields[fld].searchValue;
              scope[iterator + "InputDisable"] = true;
           }
           else if (list.fields[fld].searchType && (list.fields[fld].searchType == 'boolean' 
                || list.fields[fld].searchType == 'select')) {
              scope[iterator + 'SelectShow'] = true;
              scope[iterator + 'SearchSelectOpts'] = list.fields[fld].searchOptions;
           }
           else if (list.fields[fld].searchType && list.fields[fld].searchType == 'int') {
              scope[iterator + 'HideSearchType'] = true;   
           }    
           scope.search(iterator);
           }

        scope.resetSearch = function(iterator) {
           // Respdond to click of reset button
           scope[iterator + "SearchValue"] = '';
           scope[iterator + "SearchSelectValue"] = '';
           scope[iterator + 'SelectShow'] = false;
           scope[iterator + 'HideSearchType'] = false;
           scope[iterator + 'InputHide'] = false;
           scope[iterator + 'InputDisable'] = false;
           // Force removal of search keys from the URL
           window.location = '/#' + $location.path();
           }

        scope.setSearchType = function(iterator, type, label) {
           scope[iterator + 'SearchTypeLabel'] = label; 
           scope[iterator + 'SearchType'] = type;
           scope.search(iterator);
           }

        scope.search = function(iterator, page, load, spin) {
           // Page is optional. Added to accomodate back function on Job Events detail. 
           // Spin optional -set to false if spin not desired.
           // Load optional -set to false if loading message not desired

           scope[iterator + 'SearchSpin'] = (spin == undefined || spin == true) ? true : false;
           scope[iterator + 'Loading'] = (load == undefined || load == true) ? true : false;
           scope[iterator + 'SearchParms'] = '';
           var url = defaultUrl;
           
           if ( (scope[iterator + 'SelectShow'] == false && scope[iterator + 'SearchValue'] != '' && scope[iterator + 'SearchValue'] != undefined) ||
                (scope[iterator + 'SelectShow'] && scope[iterator + 'SearchSelectValue']) || 
                (list.fields[scope[iterator + 'SearchField']].searchType && list.fields[scope[iterator + 'SearchField']].searchType == 'gtzero') ) {
              
              if (list.fields[scope[iterator + 'SearchField']].searchField) {
                 scope[iterator + 'SearchParams'] = list.fields[scope[iterator + 'SearchField']].searchField + '__'; 
              }
              else if (list.fields[scope[iterator + 'SearchField']].sourceModel) {
                 // handle fields whose source is a related model e.g. inventories.organization
{'fraction_non_alphanumeric': 0.14023403460724512, 'fraction_numerical': 0.0009336486991161459, 'mean_word_length': 1.9605675327068361, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1988907', 'n_tokens_mistral': 4293, 'n_tokens_neox': 4200, 'n_words': 977}