All files / app/assets/javascripts/filtered_search dropdown_ajax_filter.js

63.63% Statements 14/22
50% Branches 5/10
37.5% Functions 3/8
63.63% Lines 14/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                  7x   7x   7x 7x 7x   7x           7x                                                     2x 2x   2x   2x           2x 2x     2x              
import { createAlert } from '~/alert';
import { __ } from '~/locale';
import AjaxFilter from './droplab/plugins/ajax_filter';
import DropdownUtils from './dropdown_utils';
import FilteredSearchDropdown from './filtered_search_dropdown';
import FilteredSearchTokenizer from './filtered_search_tokenizer';
 
export default class DropdownAjaxFilter extends FilteredSearchDropdown {
  constructor(options = {}) {
    const { tokenKeys, endpoint, symbol } = options;
 
    super(options);
 
    this.tokenKeys = tokenKeys;
    this.endpoint = endpoint;
    this.symbol = symbol;
 
    this.config = {
      AjaxFilter: this.ajaxFilterConfig(),
    };
  }
 
  ajaxFilterConfig() {
    return {
      endpoint: this.endpoint,
      searchKey: 'search',
      searchValueFunction: this.getSearchInput.bind(this),
      loadingTemplate: this.loadingTemplate,
      onError() {
        createAlert({
          message: __('An error occurred fetching the dropdown data.'),
        });
      },
    };
  }
 
  itemClicked(e) {
    super.itemClicked(e, (selected) => {
      const title = selected.querySelector('.dropdown-light-content').innerText.trim();
 
      return DropdownUtils.getEscapedText(title);
    });
  }
 
  renderContent(forceShowList = false) {
    this.droplab.changeHookList(this.hookId, this.dropdown, [AjaxFilter], this.config);
    super.renderContent(forceShowList);
  }
 
  getSearchInput() {
    const query = DropdownUtils.getSearchInput(this.input);
    const { lastToken } = FilteredSearchTokenizer.processTokens(query, this.tokenKeys.getKeys());
 
    let value = lastToken || '';
 
    Iif (value[0] === this.symbol) {
      value = value.slice(1);
    }
 
    // Removes the first character if it is a quotation so that we can search
    // with multiple words
    Eif (value[0] === '"' || value[0] === "'") {
      value = value.slice(1);
    }
 
    return value;
  }
 
  init() {
    this.droplab.addHook(this.input, this.dropdown, [AjaxFilter], this.config).init();
  }
}