Skip to content
Snippets Groups Projects
Commit eda515db authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

person-select: add two override properties for customizing the query params and text format

buildUrlData() allows to change the search query based on the search terms and
formatPerson() allows formatting the search results differently.
parent c66817d0
No related branches found
No related tags found
No related merge requests found
Pipeline #230018 passed
...@@ -44,6 +44,13 @@ Or directly via CDN: ...@@ -44,6 +44,13 @@ Or directly via CDN:
- example auth property: `{token: "THE_BEARER_TOKEN"}` - example auth property: `{token: "THE_BEARER_TOKEN"}`
- note: most often this should be an attribute that is not set directly, but subscribed at a provider - note: most often this should be an attribute that is not set directly, but subscribed at a provider
## Override Properties
- `buildUrlData` - A function which gets passed the select and the current search context and
should return the query parameters used for searching.
- `formatPerson` - A function which takes the select and a person object and should
return the text representation displayed to the user.
## Local development ## Local development
```bash ```bash
......
...@@ -178,10 +178,8 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -178,10 +178,8 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
jqXHR.setRequestHeader('Authorization', 'Bearer ' + that.auth.token); jqXHR.setRequestHeader('Authorization', 'Bearer ' + that.auth.token);
that.isSearching = true; that.isSearching = true;
}, },
data: function (params) { data: (params) => {
return { return this.buildUrlData(this, params);
search: params.term.trim(),
};
}, },
processResults: function (data) { processResults: function (data) {
that.$('#person-select-dropdown').addClass('select2-bug'); that.$('#person-select-dropdown').addClass('select2-bug');
...@@ -192,7 +190,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -192,7 +190,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
transformed.forEach((person) => { transformed.forEach((person) => {
results.push({ results.push({
id: person['@id'], id: person['@id'],
text: that.generateOptionText(person), text: that.formatPerson(that, person),
}); });
}); });
...@@ -267,7 +265,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -267,7 +265,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
const identifier = transformed['@id']; const identifier = transformed['@id'];
const option = new Option( const option = new Option(
that.generateOptionText(transformed), that.formatPerson(this, transformed),
identifier, identifier,
true, true,
true true
...@@ -295,7 +293,29 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -295,7 +293,29 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
return true; return true;
} }
generateOptionText(person) { /**
* Gets passed the select2 params (https://select2.org/data-sources/ajax#jquery-ajax-options)
* and should return an object containing the query parameters send to the server.
*
* @param {object} select
* @param {object} params
* @returns {object}
*/
buildUrlData(select, params) {
return {
search: params.term.trim(),
};
}
/**
* Gets passed a person object and should return a string representation that will
* will be shown to the user.
*
* @param {object} select
* @param {object} person
* @returns {string}
*/
formatPerson(select, person) {
let text = person['givenName'] ?? ''; let text = person['givenName'] ?? '';
if (person['familyName']) { if (person['familyName']) {
text += ` ${person['familyName']}`; text += ` ${person['familyName']}`;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment