2018-10-09 11:25:53 -04:00
|
|
|
/* eslint-disable func-names, no-var, object-shorthand, one-var, no-else-return */
|
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';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
export default function projectSelect() {
|
2019-01-29 04:35:53 -05:00
|
|
|
import(/* webpackChunkName: 'select2' */ 'select2/select2')
|
|
|
|
.then(() => {
|
|
|
|
$('.ajax-project-select').each(function(i, select) {
|
|
|
|
var placeholder;
|
|
|
|
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');
|
|
|
|
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-01-29 04:35:53 -05:00
|
|
|
placeholder = 'Search for project';
|
|
|
|
if (this.includeGroups) {
|
|
|
|
placeholder += ' or group';
|
|
|
|
}
|
2017-08-07 16:55:50 -04:00
|
|
|
|
2019-01-29 04:35:53 -05:00
|
|
|
$(select).select2({
|
|
|
|
placeholder: placeholder,
|
|
|
|
minimumInputLength: 0,
|
|
|
|
query: (function(_this) {
|
|
|
|
return function(query) {
|
|
|
|
var finalCallback, projectsCallback;
|
|
|
|
finalCallback = function(projects) {
|
2016-07-24 16:45:11 -04:00
|
|
|
var data;
|
2019-01-29 04:35:53 -05:00
|
|
|
data = {
|
|
|
|
results: projects,
|
|
|
|
};
|
|
|
|
return query.callback(data);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2019-01-29 04:35:53 -05:00
|
|
|
if (_this.includeGroups) {
|
|
|
|
projectsCallback = function(projects) {
|
|
|
|
var groupsCallback;
|
|
|
|
groupsCallback = function(groups) {
|
|
|
|
var data;
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
projectsCallback,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Api.projects(
|
|
|
|
query.term,
|
|
|
|
{
|
|
|
|
order_by: _this.orderBy,
|
|
|
|
with_issues_enabled: _this.withIssuesEnabled,
|
|
|
|
with_merge_requests_enabled: _this.withMergeRequestsEnabled,
|
|
|
|
membership: !_this.allProjects,
|
|
|
|
},
|
|
|
|
projectsCallback,
|
|
|
|
);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2019-01-29 04:35:53 -05:00
|
|
|
})(this),
|
|
|
|
id: function(project) {
|
|
|
|
if (simpleFilter) return project.id;
|
|
|
|
return JSON.stringify({
|
|
|
|
name: project.name,
|
|
|
|
url: project.web_url,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
text: function(project) {
|
|
|
|
return project.name_with_namespace || project.name;
|
|
|
|
},
|
2018-08-14 17:32:24 -04:00
|
|
|
|
2019-01-29 04:35:53 -05:00
|
|
|
initSelection: function(el, callback) {
|
|
|
|
return Api.project(el.val()).then(({ data }) => callback(data));
|
|
|
|
},
|
2018-08-14 17:32:24 -04:00
|
|
|
|
2019-01-29 04:35:53 -05:00
|
|
|
allowClear: this.allowClear,
|
2018-08-14 17:32:24 -04:00
|
|
|
|
2019-01-29 04:35:53 -05:00
|
|
|
dropdownCssClass: 'ajax-project-dropdown',
|
|
|
|
});
|
|
|
|
if (simpleFilter) return select;
|
|
|
|
return new ProjectSelectComboButton(select);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
2017-11-17 06:30:09 -05:00
|
|
|
}
|