2017-01-10 18:02:20 -05:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, wrap-iife, prefer-arrow-callback, no-var, comma-dangle, object-shorthand, one-var, one-var-declaration-per-line, no-else-return, quotes, max-len */
|
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() {
|
|
|
|
$('.ajax-project-select').each(function(i, select) {
|
|
|
|
var placeholder;
|
2018-02-20 17:20:48 -05:00
|
|
|
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');
|
2017-05-30 00:35:49 -04:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
placeholder = "Search for project";
|
|
|
|
if (this.includeGroups) {
|
|
|
|
placeholder += " or group";
|
|
|
|
}
|
2017-08-07 16:55:50 -04:00
|
|
|
|
2017-11-17 06:30:09 -05:00
|
|
|
$(select).select2({
|
|
|
|
placeholder: placeholder,
|
|
|
|
minimumInputLength: 0,
|
|
|
|
query: (function (_this) {
|
|
|
|
return function (query) {
|
|
|
|
var finalCallback, projectsCallback;
|
|
|
|
finalCallback = function (projects) {
|
|
|
|
var data;
|
|
|
|
data = {
|
|
|
|
results: projects
|
|
|
|
};
|
|
|
|
return query.callback(data);
|
|
|
|
};
|
|
|
|
if (_this.includeGroups) {
|
|
|
|
projectsCallback = function (projects) {
|
|
|
|
var groupsCallback;
|
|
|
|
groupsCallback = function (groups) {
|
2016-07-24 16:45:11 -04:00
|
|
|
var data;
|
2017-11-17 06:30:09 -05:00
|
|
|
data = groups.concat(projects);
|
|
|
|
return finalCallback(data);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-11-17 06:30:09 -05:00
|
|
|
return Api.groups(query.term, {}, groupsCallback);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2017-11-17 06:30:09 -05:00
|
|
|
} else {
|
|
|
|
projectsCallback = finalCallback;
|
|
|
|
}
|
|
|
|
if (_this.groupId) {
|
|
|
|
return Api.groupProjects(_this.groupId, query.term, 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
id: function(project) {
|
|
|
|
if (simpleFilter) return project.id;
|
|
|
|
return JSON.stringify({
|
|
|
|
name: project.name,
|
|
|
|
url: project.web_url,
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-11-17 06:30:09 -05:00
|
|
|
},
|
|
|
|
text: function (project) {
|
|
|
|
return project.name_with_namespace || project.name;
|
|
|
|
},
|
|
|
|
dropdownCssClass: "ajax-project-dropdown"
|
|
|
|
});
|
|
|
|
if (simpleFilter) return select;
|
|
|
|
return new ProjectSelectComboButton(select);
|
|
|
|
});
|
|
|
|
}
|