All files / ee/app/assets/javascripts/pages/admin/emails/show admin_email_select.js

0% Statements 0/33
0% Branches 0/12
0% Functions 0/15
0% Lines 0/33

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 74 75 76 77 78 79 80 81 82 83                                                                                                                                                                     
import $ from 'jquery';
import Api from '~/api';
import { sanitizeItem } from '~/frequent_items/utils';
import { loadCSSFile } from '~/lib/utils/css_utils';
import { sprintf, __ } from '~/locale';
 
const formatResult = (selectedItem) => {
  if (selectedItem.path_with_namespace) {
    return `<div class='project-result'> <div class='project-name'>${selectedItem.name}</div> <div class='project-path'>${selectedItem.path_with_namespace}</div> </div>`;
  } else if (selectedItem.path) {
    return `<div class='group-result'> <div class='group-name'>${selectedItem.name}</div> <div class='group-path'>${selectedItem.path}</div> </div>`;
  }
  return `<div class='group-result'> <div class='group-name'>${__(
    'All',
  )}</div> <div class='group-path'>${__('All groups and projects')}</div> </div>`;
};
 
const formatSelection = (selectedItem) => {
  if (selectedItem.path_with_namespace) {
    return sprintf(__('Project: %{name}'), { name: selectedItem.name });
  } else if (selectedItem.path) {
    return sprintf(__('Group: %{name}'), { name: selectedItem.name });
  }
  return __('All groups and projects');
};
 
const QueryAdmin = (query) => {
  const groupsFetch = Api.groups(query.term, {});
  const projectsFetch = Api.projects(query.term, {
    order_by: 'id',
    membership: false,
  });
  return Promise.all([projectsFetch, groupsFetch]).then(([projects, groups]) => {
    const all = {
      id: 'all',
    };
    const data = [all].concat(groups, projects.data).map(sanitizeItem);
    return query.callback({
      results: data,
    });
  });
};
 
const AdminEmailSelect = () => {
  loadCSSFile(gon.select2_css_path)
    .then(() => {
      $('.ajax-admin-email-select').each((i, select) =>
        $(select).select2({
          placeholder: __('Select group or project'),
          multiple: $(select).hasClass('multiselect'),
          minimumInputLength: 0,
          query(query) {
            QueryAdmin(query);
          },
          id(object) {
            if (object.path_with_namespace) {
              return `project-${object.id}`;
            } else if (object.path) {
              return `group-${object.id}`;
            }
            return 'all';
          },
          formatResult(...args) {
            return formatResult(...args);
          },
          formatSelection(...args) {
            return formatSelection(...args);
          },
          dropdownCssClass: 'ajax-admin-email-dropdown',
          escapeMarkup(m) {
            return m;
          },
        }),
      );
    })
    .catch(() => {});
};
 
export default () =>
  import(/* webpackChunkName: 'select2' */ 'select2/select2')
    .then(AdminEmailSelect)
    .catch(() => {});