Commit a140a9d1 authored by Tim Zallmann's avatar Tim Zallmann

Merge branch 'winh-eslint-promise/no-nesting-admin_email_select.js' into 'master'

Remove nested Promise from admin email select

See merge request gitlab-org/gitlab!17689
parents 2a5e7ffa 60c86cf8
/* eslint-disable no-var, func-names, one-var, no-else-return */
import $ from 'jquery';
import Api from '~/api';
import { sprintf, __ } from '~/locale';
function AdminEmailSelect() {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('.ajax-admin-email-select').each(
(function(_this) {
return function(i, select) {
return $(select).select2({
placeholder: __('Select group or project'),
multiple: $(select).hasClass('multiselect'),
minimumInputLength: 0,
query(query) {
const groupsFetch = Api.groups(query.term, {});
const projectsFetch = Api.projects(query.term, {
order_by: 'id',
membership: false,
});
return Promise.all([projectsFetch, groupsFetch]).then(([projects, groups]) => {
var all, data;
all = {
id: 'all',
};
data = [all].concat(groups, projects);
return query.callback({
results: data,
});
});
},
id(object) {
if (object.path_with_namespace) {
return `project-${object.id}`;
} else if (object.path) {
return `group-${object.id}`;
} else {
return 'all';
}
},
formatResult(...args) {
return _this.formatResult(...args);
},
formatSelection(...args) {
return _this.formatSelection(...args);
},
dropdownCssClass: 'ajax-admin-email-dropdown',
escapeMarkup(m) {
return m;
},
});
};
})(this),
);
})
.catch(() => {});
}
AdminEmailSelect.prototype.formatResult = function(object) {
if (object.path_with_namespace) {
return `<div class='project-result'> <div class='project-name'>${object.name}</div> <div class='project-path'>${object.path_with_namespace}</div> </div>`;
} else if (object.path) {
return `<div class='group-result'> <div class='group-name'>${object.name}</div> <div class='group-path'>${object.path}</div> </div>`;
} else {
return `<div class='group-result'> <div class='group-name'>${__(
'All',
)}</div> <div class='group-path'>${__('All groups and projects')}</div> </div>`;
const formatResult = selectedItem => {
if (selectedItem.path_with_namespace) {
return `<div class='project-result'> <div class='project-name'>${selectedItem.name}</div> <div class='project-path'>${selectedItem.path_with_namespace}</div> </div>`;
} else if (selectedItem.path) {
return `<div class='group-result'> <div class='group-name'>${selectedItem.name}</div> <div class='group-path'>${selectedItem.path}</div> </div>`;
}
return `<div class='group-result'> <div class='group-name'>${__(
'All',
)}</div> <div class='group-path'>${__('All groups and projects')}</div> </div>`;
};
AdminEmailSelect.prototype.formatSelection = function(object) {
if (object.path_with_namespace) {
return sprintf(__('Project: %{name}'), { name: object.name });
} else if (object.path) {
return sprintf(__('Group: %{name}'), { name: object.name });
} else {
return __('All groups and projects');
const formatSelection = selectedItem => {
if (selectedItem.path_with_namespace) {
return sprintf(__('Project: %{name}'), { name: selectedItem.name });
} else if (selectedItem.path) {
return sprintf(__('Group: %{name}'), { name: selectedItem.name });
}
return __('All groups and projects');
};
export default AdminEmailSelect;
const AdminEmailSelect = () => {
$('.ajax-admin-email-select').each((i, select) =>
$(select).select2({
placeholder: __('Select group or project'),
multiple: $(select).hasClass('multiselect'),
minimumInputLength: 0,
query(query) {
const groupsFetch = Api.groups(query.term, {});
const projectsFetch = Api.projects(query.term, {
order_by: 'id',
membership: false,
});
return Promise.all([projectsFetch, groupsFetch]).then(([projects, groups]) => {
const all = {
id: 'all',
};
const data = [all].concat(groups, projects);
return query.callback({
results: data,
});
});
},
id(object) {
if (object.path_with_namespace) {
return `project-${object.id}`;
} else if (object.path) {
return `group-${object.id}`;
}
return 'all';
},
formatResult(...args) {
return formatResult(...args);
},
formatSelection(...args) {
return formatSelection(...args);
},
dropdownCssClass: 'ajax-admin-email-dropdown',
escapeMarkup(m) {
return m;
},
}),
);
};
export default () =>
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(AdminEmailSelect)
.catch(() => {});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment