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

resource-select: use functions instead of events to override behaviour

This is inspired by how lion web components handle this, and much cleaner.
parent 3a3b68e2
No related branches found
No related tags found
No related merge requests found
Pipeline #97185 passed
...@@ -39,13 +39,15 @@ Or directly via CDN: ...@@ -39,13 +39,15 @@ Or directly via CDN:
- example auth property: `{token: "THE_BEARER_TOKEN"}` - example auth property: `{token: "THE_BEARER_TOKEN"}`
- note: most often this should be a property that is not set directly, but subscribed at a provider - note: most often this should be a property that is not set directly, but subscribed at a provider
## Override Properties
- `buildUrl` - A function which takes the select and the base URL, can return a
different URL for fetching the list of resources.
- `formatResource` - A function which takes the select and a resource, should
return the text used for displaying the resource.
## Events ## Events
- `change` - Gets dispatched when either `value` or `valueObject` change. - `change` - Gets dispatched when either `value` or `valueObject` change.
- `event.detail.value` - Same as the `value` property - `event.detail.value` - Same as the `value` property
- `event.detail.object` - Same as the `valueObject` property - `event.detail.object` - Same as the `valueObject` property
- `build-url` - Gets dispatched when during URL building.
- `event.detail.url` - Set this to the URL representing the collection endpoint
- `format-resource` - Gets dispatched when creating the display test for earch resource.
- `event.detail.object` - The resource that which should be represented by the text
- `event.detail.text` - Set this to the text that should be displayed
...@@ -78,17 +78,15 @@ export class ResourceSelectDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -78,17 +78,15 @@ export class ResourceSelectDemo extends ScopedElementsMixin(DBPLitElement) {
} }
render() { render() {
let buildUrl = (event) => { let buildUrl = (select, url) => {
let select = event.target;
let url = new URL(select.resourcePath, select.entryPointUrl).href;
url += '/' + encodeURIComponent(select.auth['person-id']); url += '/' + encodeURIComponent(select.auth['person-id']);
url += '/organizations'; url += '/organizations';
url += '?' + new URLSearchParams({lang: select.lang}).toString(); url += '?' + new URLSearchParams({lang: select.lang}).toString();
event.detail.url = url; return url;
}; };
let formatResource = (event) => { let formatResource = (select, resource) => {
event.detail.text = event.detail.object.name; return resource['name'];
}; };
let change = (event) => { let change = (event) => {
...@@ -113,8 +111,8 @@ export class ResourceSelectDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -113,8 +111,8 @@ export class ResourceSelectDemo extends ScopedElementsMixin(DBPLitElement) {
entry-point-url="${this.entryPointUrl}" entry-point-url="${this.entryPointUrl}"
resource-path="base/people" resource-path="base/people"
@change="${change}" @change="${change}"
@build-url="${buildUrl}" .buildUrl="${buildUrl}"
@format-resource="${formatResource}"></dbp-resource-select> .formatResource="${formatResource}"></dbp-resource-select>
</div> </div>
</div> </div>
</form> </form>
......
...@@ -86,6 +86,14 @@ export class ResourceSelect extends AdapterLitElement { ...@@ -86,6 +86,14 @@ export class ResourceSelect extends AdapterLitElement {
} }
} }
buildUrl(select, url) {
return url;
}
formatResource(select, resource) {
return resource.name ?? resource['@id'];
}
_getUrl() { _getUrl() {
if (this.entryPointUrl === null) { if (this.entryPointUrl === null) {
return null; return null;
...@@ -94,26 +102,12 @@ export class ResourceSelect extends AdapterLitElement { ...@@ -94,26 +102,12 @@ export class ResourceSelect extends AdapterLitElement {
if (this.resourcePath !== null) { if (this.resourcePath !== null) {
url = new URL(this.resourcePath, this.entryPointUrl).href; url = new URL(this.resourcePath, this.entryPointUrl).href;
} }
let detail = { url = this.buildUrl(this, url);
url: url, return url;
};
const event = new CustomEvent('build-url', {
detail: detail,
});
this.dispatchEvent(event);
return detail.url;
} }
_getText(resource) { _getText(resource) {
let detail = { return this.formatResource(this, resource);
text: resource.name ?? null,
object: resource,
};
const event = new CustomEvent('format-resource', {
detail: detail,
});
this.dispatchEvent(event);
return detail.text;
} }
_setValue(value) { _setValue(value) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment