BigW Consortium Gitlab

groups_select.js 2.78 KB
Newer Older
1
import Api from './api';
2
import { normalizeCRLFHeaders } from './lib/utils/common_utils';
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
export default function groupsSelect() {
  // Needs to be accessible in rspec
  window.GROUP_SELECT_PER_PAGE = 20;
  $('.ajax-groups-select').each(function setAjaxGroupsSelect2() {
    const $select = $(this);
    const allAvailable = $select.data('all-available');
    const skipGroups = $select.data('skip-groups') || [];
    $select.select2({
      placeholder: 'Search for a group',
      multiple: $select.hasClass('multiselect'),
      minimumInputLength: 0,
      ajax: {
        url: Api.buildUrl(Api.groupsPath),
        dataType: 'json',
        quietMillis: 250,
        transport(params) {
          return $.ajax(params)
            .then((data, status, xhr) => {
              const results = data || [];
Fatih Acet committed
23

24 25 26 27
              const headers = normalizeCRLFHeaders(xhr.getAllResponseHeaders());
              const currentPage = parseInt(headers['X-PAGE'], 10) || 0;
              const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0;
              const more = currentPage < totalPages;
28 29 30

              return {
                results,
31 32 33
                pagination: {
                  more,
                },
34
              };
35 36 37 38 39 40 41 42 43 44 45 46 47 48
            })
            .then(params.success)
            .fail(params.error);
        },
        data(search, page) {
          return {
            search,
            page,
            per_page: window.GROUP_SELECT_PER_PAGE,
            all_available: allAvailable,
          };
        },
        results(data, page) {
          if (data.length) return { results: [] };
Fatih Acet committed
49

50 51 52
          const groups = data.length ? data : data.results || [];
          const more = data.pagination ? data.pagination.more : false;
          const results = groups.filter(group => skipGroups.indexOf(group.id) === -1);
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
          return {
            results,
            page,
            more,
          };
        },
      },
      // eslint-disable-next-line consistent-return
      initSelection(element, callback) {
        const id = $(element).val();
        if (id !== '') {
          return Api.group(id, callback);
        }
      },
      formatResult(object) {
        return `<div class='group-result'> <div class='group-name'>${object.full_name}</div> <div class='group-path'>${object.full_path}</div> </div>`;
      },
      formatSelection(object) {
        return object.full_name;
      },
      dropdownCssClass: 'ajax-groups-dropdown select2-infinite',
      // we do not want to escape markup since we are displaying html in results
      escapeMarkup(m) {
        return m;
      },
    });
80

81 82 83 84 85 86
    $select.on('select2-loaded', () => {
      const dropdown = document.querySelector('.select2-infinite .select2-results');
      dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
    });
  });
}