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

127 lines
4.5 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
/* eslint-disable func-names */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
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-03-11 19:13:27 +05:30
import { s__ } from './locale';
import ProjectSelectComboButton from './project_select_combo_button';
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
const projectSelect = () => {
2021-02-22 17:27:13 +05:30
loadCSSFile(gon.select2_css_path)
.then(() => {
2021-03-08 18:12:59 +05:30
$('.ajax-project-select').each(function (i, select) {
2021-02-22 17:27:13 +05:30
let placeholder;
const simpleFilter = $(select).data('simpleFilter') || false;
const isInstantiated = $(select).data('select2');
this.groupId = $(select).data('groupId');
this.userId = $(select).data('userId');
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-09-10 17:25:29 +05:30
2021-02-22 17:27:13 +05:30
placeholder = s__('ProjectSelect|Search for project');
2019-12-26 22:10:19 +05:30
if (this.includeGroups) {
2021-02-22 17:27:13 +05:30
placeholder += s__('ProjectSelect| or group');
2019-12-26 22:10:19 +05:30
}
2021-02-22 17:27:13 +05:30
$(select).select2({
placeholder,
minimumInputLength: 0,
2021-03-08 18:12:59 +05:30
query: (query) => {
2021-02-22 17:27:13 +05:30
let projectsCallback;
2021-03-08 18:12:59 +05:30
const finalCallback = function (projects) {
2021-02-22 17:27:13 +05:30
const data = {
results: projects,
};
return query.callback(data);
};
if (this.includeGroups) {
2021-03-08 18:12:59 +05:30
projectsCallback = function (projects) {
const groupsCallback = function (groups) {
2021-02-22 17:27:13 +05:30
const data = groups.concat(projects);
return finalCallback(data);
};
return Api.groups(query.term, {}, groupsCallback);
};
} else {
projectsCallback = finalCallback;
}
if (this.groupId) {
return Api.groupProjects(
this.groupId,
query.term,
{
with_issues_enabled: this.withIssuesEnabled,
with_merge_requests_enabled: this.withMergeRequestsEnabled,
with_shared: this.withShared,
include_subgroups: this.includeProjectsInSubgroups,
order_by: 'similarity',
2021-06-08 01:23:25 +05:30
simple: true,
2021-02-22 17:27:13 +05:30
},
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,
);
}
return Api.projects(
query.term,
{
order_by: this.orderBy,
with_issues_enabled: this.withIssuesEnabled,
with_merge_requests_enabled: this.withMergeRequestsEnabled,
membership: !this.allProjects,
},
projectsCallback,
);
},
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;
2020-05-24 23:13:21 +05:30
},
2018-11-20 20:47:30 +05:30
2021-02-22 17:27:13 +05:30
initSelection(el, callback) {
// eslint-disable-next-line promise/no-nesting
return Api.project(el.val()).then(({ data }) => callback(data));
},
2018-11-20 20:47:30 +05:30
2021-02-22 17:27:13 +05:30
allowClear: this.allowClear,
2018-11-20 20:47:30 +05:30
2021-02-22 17:27:13 +05:30
dropdownCssClass: 'ajax-project-dropdown',
});
if (isInstantiated || simpleFilter) return select;
return new ProjectSelectComboButton(select);
});
})
.catch(() => {});
2019-12-21 20:55:43 +05:30
};
2020-11-24 15:15:51 +05:30
export default () => {
if ($('.ajax-project-select').length) {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(projectSelect)
.catch(() => {});
}
};