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 */
|
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
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
(function() {
|
|
|
|
this.ProjectSelect = (function() {
|
|
|
|
function ProjectSelect() {
|
|
|
|
$('.js-projects-dropdown-toggle').each(function(i, dropdown) {
|
|
|
|
var $dropdown;
|
|
|
|
$dropdown = $(dropdown);
|
|
|
|
return $dropdown.glDropdown({
|
|
|
|
filterable: true,
|
|
|
|
filterRemote: true,
|
|
|
|
search: {
|
|
|
|
fields: ['name_with_namespace']
|
|
|
|
},
|
|
|
|
data: function(term, callback) {
|
|
|
|
var finalCallback, projectsCallback;
|
2016-12-14 15:32:01 -05:00
|
|
|
var orderBy = $dropdown.data('order-by');
|
2016-07-24 16:45:11 -04:00
|
|
|
finalCallback = function(projects) {
|
|
|
|
return callback(projects);
|
|
|
|
};
|
|
|
|
if (this.includeGroups) {
|
|
|
|
projectsCallback = function(projects) {
|
|
|
|
var groupsCallback;
|
|
|
|
groupsCallback = function(groups) {
|
|
|
|
var data;
|
|
|
|
data = groups.concat(projects);
|
|
|
|
return finalCallback(data);
|
|
|
|
};
|
2016-10-31 13:37:13 -04:00
|
|
|
return Api.groups(term, {}, groupsCallback);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
projectsCallback = finalCallback;
|
|
|
|
}
|
|
|
|
if (this.groupId) {
|
|
|
|
return Api.groupProjects(this.groupId, term, projectsCallback);
|
|
|
|
} else {
|
2017-03-06 00:03:21 -05:00
|
|
|
return Api.projects(term, { order_by: orderBy }, projectsCallback);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
url: function(project) {
|
|
|
|
return project.web_url;
|
|
|
|
},
|
|
|
|
text: function(project) {
|
|
|
|
return project.name_with_namespace;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$('.ajax-project-select').each(function(i, select) {
|
|
|
|
var placeholder;
|
|
|
|
this.groupId = $(select).data('group-id');
|
|
|
|
this.includeGroups = $(select).data('include-groups');
|
|
|
|
this.orderBy = $(select).data('order-by') || 'id';
|
2017-05-30 00:35:49 -04:00
|
|
|
this.withIssuesEnabled = $(select).data('with-issues-enabled');
|
|
|
|
this.withMergeRequestsEnabled = $(select).data('with-merge-requests-enabled');
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
placeholder = "Search for project";
|
|
|
|
if (this.includeGroups) {
|
|
|
|
placeholder += " or group";
|
|
|
|
}
|
2017-08-07 16:55:50 -04:00
|
|
|
|
|
|
|
$(select).select2({
|
2016-07-24 16:45:11 -04:00
|
|
|
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) {
|
|
|
|
var data;
|
|
|
|
data = groups.concat(projects);
|
|
|
|
return finalCallback(data);
|
|
|
|
};
|
2016-10-31 13:37:13 -04:00
|
|
|
return Api.groups(query.term, {}, groupsCallback);
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
projectsCallback = finalCallback;
|
|
|
|
}
|
|
|
|
if (_this.groupId) {
|
|
|
|
return Api.groupProjects(_this.groupId, query.term, projectsCallback);
|
|
|
|
} else {
|
2017-05-30 00:35:49 -04:00
|
|
|
return Api.projects(query.term, {
|
|
|
|
order_by: _this.orderBy,
|
|
|
|
with_issues_enabled: _this.withIssuesEnabled,
|
|
|
|
with_merge_requests_enabled: _this.withMergeRequestsEnabled
|
|
|
|
}, projectsCallback);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
id: function(project) {
|
2017-08-07 16:55:50 -04:00
|
|
|
return JSON.stringify({
|
|
|
|
name: project.name,
|
|
|
|
url: project.web_url,
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
},
|
|
|
|
text: function(project) {
|
|
|
|
return project.name_with_namespace || project.name;
|
|
|
|
},
|
|
|
|
dropdownCssClass: "ajax-project-dropdown"
|
|
|
|
});
|
2017-07-03 06:58:06 -04:00
|
|
|
|
2017-08-07 16:55:50 -04:00
|
|
|
return new ProjectSelectComboButton(select);
|
2017-07-03 06:58:06 -04:00
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ProjectSelect;
|
|
|
|
})();
|
2017-02-10 01:50:50 -05:00
|
|
|
}).call(window);
|