2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-02-09 13:25:58 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2018-02-09 13:25:58 -05:00
|
|
|
import { normalizeHeaders } from './lib/utils/common_utils';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-10-27 05:05:03 -04:00
|
|
|
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);
|
2018-02-20 17:20:48 -05:00
|
|
|
const allAvailable = $select.data('allAvailable');
|
|
|
|
const skipGroups = $select.data('skipGroups') || [];
|
2017-10-27 05:05:03 -04:00
|
|
|
$select.select2({
|
|
|
|
placeholder: 'Search for a group',
|
2018-08-03 11:36:53 -04:00
|
|
|
allowClear: $select.hasClass('allowClear'),
|
2017-10-27 05:05:03 -04:00
|
|
|
multiple: $select.hasClass('multiselect'),
|
|
|
|
minimumInputLength: 0,
|
|
|
|
ajax: {
|
|
|
|
url: Api.buildUrl(Api.groupsPath),
|
|
|
|
dataType: 'json',
|
|
|
|
quietMillis: 250,
|
|
|
|
transport(params) {
|
2018-02-09 13:25:58 -05:00
|
|
|
axios[params.type.toLowerCase()](params.url, {
|
|
|
|
params: params.data,
|
|
|
|
})
|
2018-10-24 15:17:03 -04:00
|
|
|
.then(res => {
|
2018-02-09 13:25:58 -05:00
|
|
|
const results = res.data || [];
|
|
|
|
const headers = normalizeHeaders(res.headers);
|
2017-10-27 05:05:03 -04:00
|
|
|
const currentPage = parseInt(headers['X-PAGE'], 10) || 0;
|
|
|
|
const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0;
|
|
|
|
const more = currentPage < totalPages;
|
2017-03-30 08:14:32 -04:00
|
|
|
|
2018-02-09 13:25:58 -05:00
|
|
|
params.success({
|
2017-03-30 08:14:32 -04:00
|
|
|
results,
|
2017-10-27 05:05:03 -04:00
|
|
|
pagination: {
|
|
|
|
more,
|
|
|
|
},
|
2018-02-09 13:25:58 -05:00
|
|
|
});
|
2018-10-24 15:17:03 -04:00
|
|
|
})
|
|
|
|
.catch(params.error);
|
2017-10-27 05:05:03 -04:00
|
|
|
},
|
|
|
|
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-07-24 16:45:11 -04:00
|
|
|
|
2017-10-27 05:05:03 -04:00
|
|
|
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);
|
2017-03-30 08:14:32 -04:00
|
|
|
|
2017-10-27 05:05:03 -04: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) {
|
2018-10-24 15:17:03 -04:00
|
|
|
return `<div class='group-result'> <div class='group-name'>${
|
|
|
|
object.full_name
|
|
|
|
}</div> <div class='group-path'>${object.full_path}</div> </div>`;
|
2017-10-27 05:05:03 -04:00
|
|
|
},
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
});
|
2017-03-30 08:14:32 -04:00
|
|
|
|
2017-10-27 05:05:03 -04:00
|
|
|
$select.on('select2-loaded', () => {
|
|
|
|
const dropdown = document.querySelector('.select2-infinite .select2-results');
|
|
|
|
dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|