2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-08-17 17:09:56 -04:00
|
|
|
import { escape } from 'lodash';
|
2021-02-01 10:08:56 -05:00
|
|
|
import { __ } from '~/locale';
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2020-11-18 04:09:02 -05:00
|
|
|
import { loadCSSFile } from './lib/utils/css_utils';
|
2021-03-11 22:08:56 -05:00
|
|
|
import { select2AxiosTransport } from './lib/utils/select2_utils';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2021-06-08 17:10:05 -04:00
|
|
|
const groupsPath = (groupsFilter, parentGroupID) => {
|
|
|
|
switch (groupsFilter) {
|
|
|
|
case 'descendant_groups':
|
|
|
|
return Api.descendantGroupsPath.replace(':id', parentGroupID);
|
|
|
|
case 'subgroups':
|
|
|
|
return Api.subgroupsPath.replace(':id', parentGroupID);
|
|
|
|
default:
|
|
|
|
return Api.groupsPath;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-26 14:06:29 -04:00
|
|
|
const groupsSelect = () => {
|
2020-11-18 04:09:02 -05:00
|
|
|
loadCSSFile(gon.select2_css_path)
|
|
|
|
.then(() => {
|
|
|
|
// Needs to be accessible in rspec
|
|
|
|
window.GROUP_SELECT_PER_PAGE = 20;
|
2018-11-27 11:52:58 -05:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
$('.ajax-groups-select').each(function setAjaxGroupsSelect2() {
|
|
|
|
const $select = $(this);
|
|
|
|
const allAvailable = $select.data('allAvailable');
|
|
|
|
const skipGroups = $select.data('skipGroups') || [];
|
|
|
|
const parentGroupID = $select.data('parentId');
|
2021-06-08 17:10:05 -04:00
|
|
|
const groupsFilter = $select.data('groupsFilter');
|
2017-03-30 08:14:32 -04:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
$select.select2({
|
|
|
|
placeholder: __('Search for a group'),
|
|
|
|
allowClear: $select.hasClass('allowClear'),
|
|
|
|
multiple: $select.hasClass('multiselect'),
|
|
|
|
minimumInputLength: 0,
|
|
|
|
ajax: {
|
2021-06-08 17:10:05 -04:00
|
|
|
url: Api.buildUrl(groupsPath(groupsFilter, parentGroupID)),
|
2020-11-18 04:09:02 -05:00
|
|
|
dataType: 'json',
|
|
|
|
quietMillis: 250,
|
2021-03-11 22:08:56 -05:00
|
|
|
transport: select2AxiosTransport,
|
2020-11-18 04:09:02 -05:00
|
|
|
data(search, page) {
|
|
|
|
return {
|
|
|
|
search,
|
|
|
|
page,
|
|
|
|
per_page: window.GROUP_SELECT_PER_PAGE,
|
|
|
|
all_available: allAvailable,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
results(data, page) {
|
|
|
|
const groups = data.length ? data : data.results || [];
|
|
|
|
const more = data.pagination ? data.pagination.more : false;
|
2020-12-23 19:10:25 -05:00
|
|
|
const results = groups.filter((group) => skipGroups.indexOf(group.id) === -1);
|
2017-03-30 08:14:32 -04:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
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'>${escape(
|
|
|
|
object.full_name,
|
|
|
|
)}</div> <div class='group-path'>${object.full_path}</div> </div>`;
|
|
|
|
},
|
|
|
|
formatSelection(object) {
|
|
|
|
return escape(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;
|
|
|
|
},
|
|
|
|
});
|
2017-03-30 08:14:32 -04:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
$select.on('select2-loaded', () => {
|
|
|
|
const dropdown = document.querySelector('.select2-infinite .select2-results');
|
|
|
|
dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
2019-09-26 14:06:29 -04:00
|
|
|
};
|
|
|
|
|
2020-09-15 11:10:08 -04:00
|
|
|
export default () => {
|
|
|
|
if ($('.ajax-groups-select').length) {
|
|
|
|
import(/* webpackChunkName: 'select2' */ 'select2/select2')
|
|
|
|
.then(groupsSelect)
|
|
|
|
.catch(() => {});
|
|
|
|
}
|
|
|
|
};
|