All files / ee/app/assets/javascripts ldap_groups_select.js

0% Statements 0/19
0% Branches 0/2
0% Functions 0/14
0% Lines 0/19

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                                                                                                                         
/* eslint-disable consistent-return, func-names */
 
import $ from 'jquery';
import Api from 'ee/api';
import { loadCSSFile } from '~/lib/utils/css_utils';
import { __ } from '~/locale';
 
export default function initLDAPGroupsSelect() {
  const ldapGroupResult = function (group) {
    return group.cn;
  };
  const groupFormatSelection = function (group) {
    return group.cn;
  };
  import(/* webpackChunkName: 'select2' */ 'select2/select2')
    .then(() => {
      // eslint-disable-next-line promise/no-nesting
      loadCSSFile(gon.select2_css_path)
        .then(() => {
          $('.ajax-ldap-groups-select').each((i, select) => {
            $(select).select2({
              id(group) {
                return group.cn;
              },
              placeholder: __('Search for a LDAP group'),
              minimumInputLength: 1,
              query(query) {
                const provider = $('#ldap_group_link_provider').val();
                return Api.ldapGroups(query.term, provider, (groups) => {
                  const data = {
                    results: groups,
                  };
                  return query.callback(data);
                });
              },
              initSelection(element, callback) {
                const id = $(element).val();
                if (id !== '') {
                  return callback({
                    cn: id,
                  });
                }
              },
              formatResult: ldapGroupResult,
              formatSelection: groupFormatSelection,
              dropdownCssClass: 'ajax-groups-dropdown',
              formatNoMatches() {
                return __('Match not found; try refining your search query.');
              },
            });
          });
        })
        .catch(() => {});
    })
    .catch(() => {});
 
  $('#ldap_group_link_provider').on('change', () => {
    $('.ajax-ldap-groups-select').select2('data', null);
  });
}