debian-mirror-gitlab/app/assets/javascripts/groups_select.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-10-24 23:57:45 +05:30
import { escape } from 'lodash';
2022-11-25 23:54:43 +05:30
import { groupsPath } from '~/vue_shared/components/group_select/utils';
2021-03-11 19:13:27 +05:30
import { __ } from '~/locale';
2017-09-10 17:25:29 +05:30
import Api from './api';
2021-02-22 17:27:13 +05:30
import { loadCSSFile } from './lib/utils/css_utils';
2021-04-17 20:07:23 +05:30
import { select2AxiosTransport } from './lib/utils/select2_utils';
2016-09-13 17:45:13 +05:30
2019-12-21 20:55:43 +05:30
const groupsSelect = () => {
2021-02-22 17:27:13 +05:30
loadCSSFile(gon.select2_css_path)
.then(() => {
// Needs to be accessible in rspec
window.GROUP_SELECT_PER_PAGE = 20;
2019-02-15 15:39:39 +05:30
2021-02-22 17:27:13 +05:30
$('.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-09-04 01:27:46 +05:30
const groupsFilter = $select.data('groupsFilter');
2022-04-04 11:22:00 +05:30
const minAccessLevel = $select.data('minAccessLevel');
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
$select.select2({
placeholder: __('Search for a group'),
allowClear: $select.hasClass('allowClear'),
multiple: $select.hasClass('multiselect'),
minimumInputLength: 0,
ajax: {
2021-09-04 01:27:46 +05:30
url: Api.buildUrl(groupsPath(groupsFilter, parentGroupID)),
2021-02-22 17:27:13 +05:30
dataType: 'json',
quietMillis: 250,
2021-04-17 20:07:23 +05:30
transport: select2AxiosTransport,
2021-02-22 17:27:13 +05:30
data(search, page) {
return {
search,
page,
per_page: window.GROUP_SELECT_PER_PAGE,
all_available: allAvailable,
2022-04-04 11:22:00 +05:30
min_access_level: minAccessLevel,
2021-02-22 17:27:13 +05:30
};
},
results(data, page) {
const groups = data.length ? data : data.results || [];
const more = data.pagination ? data.pagination.more : false;
2021-03-08 18:12:59 +05:30
const results = groups.filter((group) => skipGroups.indexOf(group.id) === -1);
2016-09-13 17:45:13 +05:30
2021-02-22 17:27:13 +05:30
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;
},
});
2016-09-13 17:45:13 +05:30
2021-02-22 17:27:13 +05:30
$select.on('select2-loaded', () => {
const dropdown = document.querySelector('.select2-infinite .select2-results');
dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
});
});
})
.catch(() => {});
2019-12-21 20:55:43 +05:30
};
2020-11-24 15:15:51 +05:30
export default () => {
if ($('.ajax-groups-select').length) {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(groupsSelect)
.catch(() => {});
}
};