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

102 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-12-13 13:39:08 +05:30
/* eslint-disable func-names, no-var, object-shorthand, one-var, no-else-return */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import Api from './api';
import ProjectSelectComboButton from './project_select_combo_button';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
export default function projectSelect() {
$('.ajax-project-select').each(function(i, select) {
var placeholder;
2018-03-27 19:54:05 +05:30
const simpleFilter = $(select).data('simpleFilter') || false;
this.groupId = $(select).data('groupId');
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');
2019-02-15 15:39:39 +05:30
this.withShared =
$(select).data('withShared') === undefined ? true : $(select).data('withShared');
this.includeProjectsInSubgroups = $(select).data('includeProjectsInSubgroups') || false;
2018-11-20 20:47:30 +05:30
this.allowClear = $(select).data('allowClear') || false;
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
placeholder = 'Search for project';
2018-03-17 18:26:18 +05:30
if (this.includeGroups) {
2018-12-13 13:39:08 +05:30
placeholder += ' or group';
2018-03-17 18:26:18 +05:30
}
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
$(select).select2({
placeholder: placeholder,
minimumInputLength: 0,
2018-12-13 13:39:08 +05:30
query: (function(_this) {
return function(query) {
2018-03-17 18:26:18 +05:30
var finalCallback, projectsCallback;
2018-12-13 13:39:08 +05:30
finalCallback = function(projects) {
2018-03-17 18:26:18 +05:30
var data;
data = {
2018-12-13 13:39:08 +05:30
results: projects,
2018-03-17 18:26:18 +05:30
};
return query.callback(data);
};
if (_this.includeGroups) {
2018-12-13 13:39:08 +05:30
projectsCallback = function(projects) {
2018-03-17 18:26:18 +05:30
var groupsCallback;
2018-12-13 13:39:08 +05:30
groupsCallback = function(groups) {
2016-09-13 17:45:13 +05:30
var data;
2018-03-17 18:26:18 +05:30
data = groups.concat(projects);
return finalCallback(data);
2016-09-13 17:45:13 +05:30
};
2018-03-17 18:26:18 +05:30
return Api.groups(query.term, {}, groupsCallback);
2016-09-13 17:45:13 +05:30
};
2018-03-17 18:26:18 +05:30
} else {
projectsCallback = finalCallback;
}
if (_this.groupId) {
2018-12-13 13:39:08 +05:30
return Api.groupProjects(
_this.groupId,
query.term,
{
with_issues_enabled: _this.withIssuesEnabled,
with_merge_requests_enabled: _this.withMergeRequestsEnabled,
2019-02-15 15:39:39 +05:30
with_shared: _this.withShared,
include_subgroups: _this.includeProjectsInSubgroups,
2018-12-13 13:39:08 +05:30
},
projectsCallback,
);
2018-03-17 18:26:18 +05:30
} else {
2018-12-13 13:39:08 +05:30
return Api.projects(
query.term,
{
order_by: _this.orderBy,
with_issues_enabled: _this.withIssuesEnabled,
with_merge_requests_enabled: _this.withMergeRequestsEnabled,
membership: !_this.allProjects,
},
projectsCallback,
);
2018-03-17 18:26:18 +05:30
}
};
})(this),
id: function(project) {
if (simpleFilter) return project.id;
return JSON.stringify({
name: project.name,
url: project.web_url,
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
},
2018-12-13 13:39:08 +05:30
text: function(project) {
2018-03-17 18:26:18 +05:30
return project.name_with_namespace || project.name;
},
2018-11-20 20:47:30 +05:30
initSelection: function(el, callback) {
return Api.project(el.val()).then(({ data }) => callback(data));
},
allowClear: this.allowClear,
2018-12-13 13:39:08 +05:30
dropdownCssClass: 'ajax-project-dropdown',
2018-03-17 18:26:18 +05:30
});
if (simpleFilter) return select;
return new ProjectSelectComboButton(select);
});
}