2017-04-20 07:11:51 -04:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, no-var, wrap-iife, one-var,
|
|
|
|
camelcase, one-var-declaration-per-line, quotes, object-shorthand,
|
|
|
|
prefer-arrow-callback, comma-dangle, consistent-return, yoda,
|
|
|
|
prefer-rest-params, prefer-spread, no-unused-vars, prefer-template,
|
|
|
|
promise/catch-or-return */
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
var slice = [].slice;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
window.GroupsSelect = (function() {
|
|
|
|
function GroupsSelect() {
|
|
|
|
$('.ajax-groups-select').each((function(_this) {
|
2017-03-30 08:14:32 -04:00
|
|
|
const self = _this;
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return function(i, select) {
|
|
|
|
var all_available, skip_groups;
|
2017-03-30 08:14:32 -04:00
|
|
|
const $select = $(select);
|
|
|
|
all_available = $select.data('all-available');
|
|
|
|
skip_groups = $select.data('skip-groups') || [];
|
|
|
|
|
|
|
|
$select.select2({
|
2017-03-11 02:30:44 -05:00
|
|
|
placeholder: "Search for a group",
|
2017-03-30 08:14:32 -04:00
|
|
|
multiple: $select.hasClass('multiselect'),
|
2017-03-11 02:30:44 -05:00
|
|
|
minimumInputLength: 0,
|
2017-03-30 08:14:32 -04:00
|
|
|
ajax: {
|
|
|
|
url: Api.buildUrl(Api.groupsPath),
|
|
|
|
dataType: 'json',
|
|
|
|
quietMillis: 250,
|
|
|
|
transport: function (params) {
|
|
|
|
$.ajax(params).then((data, status, xhr) => {
|
|
|
|
const results = data || [];
|
|
|
|
|
|
|
|
const headers = gl.utils.normalizeCRLFHeaders(xhr.getAllResponseHeaders());
|
|
|
|
const currentPage = parseInt(headers['X-PAGE'], 10) || 0;
|
|
|
|
const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0;
|
|
|
|
const more = currentPage < totalPages;
|
|
|
|
|
|
|
|
return {
|
|
|
|
results,
|
|
|
|
pagination: {
|
|
|
|
more,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}).then(params.success).fail(params.error);
|
|
|
|
},
|
|
|
|
data: function (search, page) {
|
|
|
|
return {
|
|
|
|
search,
|
|
|
|
page,
|
|
|
|
per_page: GroupsSelect.PER_PAGE,
|
|
|
|
all_available,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
results: function (data, page) {
|
|
|
|
if (data.length) return { results: [] };
|
|
|
|
|
2017-04-05 09:58:53 -04:00
|
|
|
const groups = data.length ? data : data.results || [];
|
2017-03-30 08:14:32 -04:00
|
|
|
const more = data.pagination ? data.pagination.more : false;
|
2017-04-05 09:58:53 -04:00
|
|
|
const results = groups.filter(group => skip_groups.indexOf(group.id) === -1);
|
2017-03-30 08:14:32 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
results,
|
|
|
|
page,
|
|
|
|
more,
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
2017-03-30 08:14:32 -04:00
|
|
|
},
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
initSelection: function(element, callback) {
|
|
|
|
var id;
|
|
|
|
id = $(element).val();
|
|
|
|
if (id !== "") {
|
|
|
|
return Api.group(id, callback);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
formatResult: function() {
|
|
|
|
var args;
|
|
|
|
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
2017-03-30 08:14:32 -04:00
|
|
|
return self.formatResult.apply(self, args);
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
formatSelection: function() {
|
|
|
|
var args;
|
|
|
|
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
2017-03-30 08:14:32 -04:00
|
|
|
return self.formatSelection.apply(self, args);
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
2017-03-30 08:14:32 -04:00
|
|
|
dropdownCssClass: "ajax-groups-dropdown select2-infinite",
|
2017-03-11 02:30:44 -05:00
|
|
|
// we do not want to escape markup since we are displaying html in results
|
|
|
|
escapeMarkup: function(m) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
});
|
2017-03-30 08:14:32 -04:00
|
|
|
|
|
|
|
self.dropdown = document.querySelector('.select2-infinite .select2-results');
|
|
|
|
|
|
|
|
$select.on('select2-loaded', self.forceOverflow.bind(self));
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GroupsSelect.prototype.formatResult = function(group) {
|
|
|
|
var avatar;
|
|
|
|
if (group.avatar_url) {
|
|
|
|
avatar = group.avatar_url;
|
|
|
|
} else {
|
|
|
|
avatar = gon.default_avatar_url;
|
|
|
|
}
|
|
|
|
return "<div class='group-result'> <div class='group-name'>" + group.full_name + "</div> <div class='group-path'>" + group.full_path + "</div> </div>";
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
GroupsSelect.prototype.formatSelection = function(group) {
|
|
|
|
return group.full_name;
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-30 08:14:32 -04:00
|
|
|
GroupsSelect.prototype.forceOverflow = function (e) {
|
2017-08-11 09:35:47 -04:00
|
|
|
this.dropdown.style.height = `${Math.floor(this.dropdown.scrollHeight)}px`;
|
2017-03-30 08:14:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
GroupsSelect.PER_PAGE = 20;
|
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return GroupsSelect;
|
|
|
|
})();
|