2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-03-27 19:54:05 +05:30
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-09-10 17:25:29 +05:30
|
|
|
import Api from './api';
|
2020-02-01 01:16:34 +05:30
|
|
|
import { escape } from 'lodash';
|
2018-03-27 19:54:05 +05:30
|
|
|
import { normalizeHeaders } from './lib/utils/common_utils';
|
2019-09-04 21:01:54 +05:30
|
|
|
import { __ } from '~/locale';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
const 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('allAvailable');
|
|
|
|
const skipGroups = $select.data('skipGroups') || [];
|
|
|
|
const parentGroupID = $select.data('parentId');
|
|
|
|
const groupsPath = parentGroupID
|
|
|
|
? Api.subgroupsPath.replace(':id', parentGroupID)
|
|
|
|
: Api.groupsPath;
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
$select.select2({
|
|
|
|
placeholder: __('Search for a group'),
|
|
|
|
allowClear: $select.hasClass('allowClear'),
|
|
|
|
multiple: $select.hasClass('multiselect'),
|
|
|
|
minimumInputLength: 0,
|
|
|
|
ajax: {
|
|
|
|
url: Api.buildUrl(groupsPath),
|
|
|
|
dataType: 'json',
|
|
|
|
quietMillis: 250,
|
|
|
|
transport(params) {
|
|
|
|
axios[params.type.toLowerCase()](params.url, {
|
|
|
|
params: params.data,
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
const results = res.data || [];
|
|
|
|
const headers = normalizeHeaders(res.headers);
|
|
|
|
const currentPage = parseInt(headers['X-PAGE'], 10) || 0;
|
|
|
|
const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0;
|
|
|
|
const more = currentPage < totalPages;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
params.success({
|
|
|
|
results,
|
|
|
|
pagination: {
|
|
|
|
more,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(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: [] };
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
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);
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-21 20:55:43 +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) {
|
2020-02-01 01:16:34 +05:30
|
|
|
return `<div class='group-result'> <div class='group-name'>${escape(
|
|
|
|
object.full_name,
|
|
|
|
)}</div> <div class='group-path'>${object.full_path}</div> </div>`;
|
2019-12-21 20:55:43 +05:30
|
|
|
},
|
|
|
|
formatSelection(object) {
|
2020-02-01 01:16:34 +05:30
|
|
|
return escape(object.full_name);
|
2019-12-21 20:55:43 +05:30
|
|
|
},
|
|
|
|
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
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
$select.on('select2-loaded', () => {
|
|
|
|
const dropdown = document.querySelector('.select2-infinite .select2-results');
|
|
|
|
dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default () =>
|
|
|
|
import(/* webpackChunkName: 'select2' */ 'select2/select2')
|
|
|
|
.then(groupsSelect)
|
2019-03-02 22:35:43 +05:30
|
|
|
.catch(() => {});
|