CE backport for reference in gfm_autocomplete

Currently not used by any CE code
This commit is contained in:
Heinrich Lee Yu 2018-12-21 16:49:44 +08:00
parent b83be50327
commit 12b3a20322
2 changed files with 51 additions and 9 deletions

View file

@ -221,13 +221,13 @@ class GfmAutoComplete {
displayTpl(value) { displayTpl(value) {
let tmpl = GfmAutoComplete.Loading.template; let tmpl = GfmAutoComplete.Loading.template;
if (value.title != null) { if (value.title != null) {
tmpl = GfmAutoComplete.Issues.templateFunction(value.id, value.title); tmpl = GfmAutoComplete.Issues.templateFunction(value);
} }
return tmpl; return tmpl;
}, },
data: GfmAutoComplete.defaultLoadingData, data: GfmAutoComplete.defaultLoadingData,
// eslint-disable-next-line no-template-curly-in-string insertTpl: GfmAutoComplete.Issues.insertTemplateFunction,
insertTpl: '${atwho-at}${id}', skipSpecialCharacterTest: true,
callbacks: { callbacks: {
...this.getDefaultCallbacks(), ...this.getDefaultCallbacks(),
beforeSave(issues) { beforeSave(issues) {
@ -238,6 +238,7 @@ class GfmAutoComplete {
return { return {
id: i.iid, id: i.iid,
title: sanitize(i.title), title: sanitize(i.title),
reference: i.reference,
search: `${i.iid} ${i.title}`, search: `${i.iid} ${i.title}`,
}; };
}); });
@ -287,13 +288,13 @@ class GfmAutoComplete {
displayTpl(value) { displayTpl(value) {
let tmpl = GfmAutoComplete.Loading.template; let tmpl = GfmAutoComplete.Loading.template;
if (value.title != null) { if (value.title != null) {
tmpl = GfmAutoComplete.Issues.templateFunction(value.id, value.title); tmpl = GfmAutoComplete.Issues.templateFunction(value);
} }
return tmpl; return tmpl;
}, },
data: GfmAutoComplete.defaultLoadingData, data: GfmAutoComplete.defaultLoadingData,
// eslint-disable-next-line no-template-curly-in-string insertTpl: GfmAutoComplete.Issues.insertTemplateFunction,
insertTpl: '${atwho-at}${id}', skipSpecialCharacterTest: true,
callbacks: { callbacks: {
...this.getDefaultCallbacks(), ...this.getDefaultCallbacks(),
beforeSave(merges) { beforeSave(merges) {
@ -304,6 +305,7 @@ class GfmAutoComplete {
return { return {
id: m.iid, id: m.iid,
title: sanitize(m.title), title: sanitize(m.title),
reference: m.reference,
search: `${m.iid} ${m.title}`, search: `${m.iid} ${m.title}`,
}; };
}); });
@ -397,7 +399,7 @@ class GfmAutoComplete {
displayTpl(value) { displayTpl(value) {
let tmpl = GfmAutoComplete.Loading.template; let tmpl = GfmAutoComplete.Loading.template;
if (value.title != null) { if (value.title != null) {
tmpl = GfmAutoComplete.Issues.templateFunction(value.id, value.title); tmpl = GfmAutoComplete.Issues.templateFunction(value);
} }
return tmpl; return tmpl;
}, },
@ -596,8 +598,12 @@ GfmAutoComplete.Labels = {
}; };
// Issues, MergeRequests and Snippets // Issues, MergeRequests and Snippets
GfmAutoComplete.Issues = { GfmAutoComplete.Issues = {
templateFunction(id, title) { insertTemplateFunction(value) {
return `<li><small>${id}</small> ${_.escape(title)}</li>`; // eslint-disable-next-line no-template-curly-in-string
return value.reference || '${atwho-at}${id}';
},
templateFunction({ id, title, reference }) {
return `<li><small>${reference || id}</small> ${_.escape(title)}</li>`;
}, },
}; };
// Milestones // Milestones

View file

@ -205,4 +205,40 @@ describe('GfmAutoComplete', function() {
expect(GfmAutoComplete.isLoading({ title: 'Foo' })).toBe(false); expect(GfmAutoComplete.isLoading({ title: 'Foo' })).toBe(false);
}); });
}); });
describe('Issues.insertTemplateFunction', function() {
it('should return default template', function() {
expect(GfmAutoComplete.Issues.insertTemplateFunction({ id: 5, title: 'Some Issue' })).toBe(
'${atwho-at}${id}', // eslint-disable-line no-template-curly-in-string
);
});
it('should return reference when reference is set', function() {
expect(
GfmAutoComplete.Issues.insertTemplateFunction({
id: 5,
title: 'Some Issue',
reference: 'grp/proj#5',
}),
).toBe('grp/proj#5');
});
});
describe('Issues.templateFunction', function() {
it('should return html with id and title', function() {
expect(GfmAutoComplete.Issues.templateFunction({ id: 5, title: 'Some Issue' })).toBe(
'<li><small>5</small> Some Issue</li>',
);
});
it('should replace id with reference if reference is set', function() {
expect(
GfmAutoComplete.Issues.templateFunction({
id: 5,
title: 'Some Issue',
reference: 'grp/proj#5',
}),
).toBe('<li><small>grp/proj#5</small> Some Issue</li>');
});
});
}); });