2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable func-names */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2017-08-07 16:55:50 -04:00
|
|
|
import ProjectSelectComboButton from './project_select_combo_button';
|
2019-05-07 02:24:27 -04:00
|
|
|
import { s__ } from './locale';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2019-09-27 05:06:26 -04:00
|
|
|
const projectSelect = () => {
|
|
|
|
$('.ajax-project-select').each(function(i, select) {
|
2019-11-18 13:06:53 -05:00
|
|
|
let placeholder;
|
2019-09-27 05:06:26 -04:00
|
|
|
const simpleFilter = $(select).data('simpleFilter') || false;
|
2019-11-04 04:06:21 -05:00
|
|
|
const isInstantiated = $(select).data('select2');
|
2019-09-27 05:06:26 -04:00
|
|
|
this.groupId = $(select).data('groupId');
|
2019-11-04 04:06:21 -05:00
|
|
|
this.userId = $(select).data('userId');
|
2019-09-27 05:06:26 -04:00
|
|
|
this.includeGroups = $(select).data('includeGroups');
|
|
|
|
this.allProjects = $(select).data('allProjects') || false;
|
|
|
|
this.orderBy = $(select).data('orderBy') || 'id';
|
|
|
|
this.withIssuesEnabled = $(select).data('withIssuesEnabled');
|
|
|
|
this.withMergeRequestsEnabled = $(select).data('withMergeRequestsEnabled');
|
|
|
|
this.withShared =
|
|
|
|
$(select).data('withShared') === undefined ? true : $(select).data('withShared');
|
|
|
|
this.includeProjectsInSubgroups = $(select).data('includeProjectsInSubgroups') || false;
|
|
|
|
this.allowClear = $(select).data('allowClear') || false;
|
2017-05-30 00:35:49 -04:00
|
|
|
|
2019-09-27 05:06:26 -04:00
|
|
|
placeholder = s__('ProjectSelect|Search for project');
|
|
|
|
if (this.includeGroups) {
|
|
|
|
placeholder += s__('ProjectSelect| or group');
|
|
|
|
}
|
2017-08-07 16:55:50 -04:00
|
|
|
|
2019-09-27 05:06:26 -04:00
|
|
|
$(select).select2({
|
|
|
|
placeholder,
|
|
|
|
minimumInputLength: 0,
|
2019-11-05 19:06:13 -05:00
|
|
|
query: query => {
|
2019-11-18 13:06:53 -05:00
|
|
|
let projectsCallback;
|
|
|
|
const finalCallback = function(projects) {
|
|
|
|
const data = {
|
2019-11-05 19:06:13 -05:00
|
|
|
results: projects,
|
2019-09-27 05:06:26 -04:00
|
|
|
};
|
2019-11-05 19:06:13 -05:00
|
|
|
return query.callback(data);
|
2019-09-27 05:06:26 -04:00
|
|
|
};
|
2019-11-05 19:06:13 -05:00
|
|
|
if (this.includeGroups) {
|
|
|
|
projectsCallback = function(projects) {
|
2019-11-18 13:06:53 -05:00
|
|
|
const groupsCallback = function(groups) {
|
|
|
|
const data = groups.concat(projects);
|
2019-11-05 19:06:13 -05:00
|
|
|
return finalCallback(data);
|
|
|
|
};
|
|
|
|
return Api.groups(query.term, {}, groupsCallback);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
projectsCallback = finalCallback;
|
|
|
|
}
|
|
|
|
if (this.groupId) {
|
|
|
|
return Api.groupProjects(
|
|
|
|
this.groupId,
|
|
|
|
query.term,
|
|
|
|
{
|
2020-03-23 08:09:47 -04:00
|
|
|
search_namespaces: true,
|
2019-11-05 19:06:13 -05:00
|
|
|
with_issues_enabled: this.withIssuesEnabled,
|
|
|
|
with_merge_requests_enabled: this.withMergeRequestsEnabled,
|
|
|
|
with_shared: this.withShared,
|
|
|
|
include_subgroups: this.includeProjectsInSubgroups,
|
|
|
|
},
|
|
|
|
projectsCallback,
|
|
|
|
);
|
|
|
|
} else if (this.userId) {
|
|
|
|
return Api.userProjects(
|
|
|
|
this.userId,
|
|
|
|
query.term,
|
|
|
|
{
|
|
|
|
with_issues_enabled: this.withIssuesEnabled,
|
|
|
|
with_merge_requests_enabled: this.withMergeRequestsEnabled,
|
|
|
|
with_shared: this.withShared,
|
|
|
|
include_subgroups: this.includeProjectsInSubgroups,
|
|
|
|
},
|
|
|
|
projectsCallback,
|
|
|
|
);
|
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return Api.projects(
|
|
|
|
query.term,
|
|
|
|
{
|
|
|
|
order_by: this.orderBy,
|
|
|
|
with_issues_enabled: this.withIssuesEnabled,
|
|
|
|
with_merge_requests_enabled: this.withMergeRequestsEnabled,
|
|
|
|
membership: !this.allProjects,
|
|
|
|
},
|
|
|
|
projectsCallback,
|
|
|
|
);
|
2019-11-05 19:06:13 -05:00
|
|
|
},
|
2019-09-27 05:06:26 -04:00
|
|
|
id(project) {
|
|
|
|
if (simpleFilter) return project.id;
|
|
|
|
return JSON.stringify({
|
|
|
|
name: project.name,
|
|
|
|
url: project.web_url,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
text(project) {
|
|
|
|
return project.name_with_namespace || project.name;
|
|
|
|
},
|
2018-08-14 17:32:24 -04:00
|
|
|
|
2019-09-27 05:06:26 -04:00
|
|
|
initSelection(el, callback) {
|
|
|
|
return Api.project(el.val()).then(({ data }) => callback(data));
|
|
|
|
},
|
2018-08-14 17:32:24 -04:00
|
|
|
|
2019-09-27 05:06:26 -04:00
|
|
|
allowClear: this.allowClear,
|
2018-08-14 17:32:24 -04:00
|
|
|
|
2019-09-27 05:06:26 -04:00
|
|
|
dropdownCssClass: 'ajax-project-dropdown',
|
|
|
|
});
|
2019-11-04 04:06:21 -05:00
|
|
|
if (isInstantiated || simpleFilter) return select;
|
2019-09-27 05:06:26 -04:00
|
|
|
return new ProjectSelectComboButton(select);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default () =>
|
|
|
|
import(/* webpackChunkName: 'select2' */ 'select2/select2')
|
|
|
|
.then(projectSelect)
|
2019-01-29 04:35:53 -05:00
|
|
|
.catch(() => {});
|