import { UmbDeselectedEvent, UmbSelectedEvent } from '@umbraco-cms/backoffice/event';
import { customElement, html, nothing, property } from '@umbraco-cms/backoffice/external/lit';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { UmbEntityContext } from '@umbraco-cms/backoffice/entity';
import type { UmbCollectionItemModel } from '@umbraco-cms/backoffice/collection';
@customElement('my-collection-view-item')
export class MyCollectionViewItemElement extends UmbLitElement {
#item?: UmbCollectionItemModel;
#entityContext?: UmbEntityContext;
@property({ type: Object })
get item(): UmbCollectionItemModel | undefined {
return this.#item;
}
set item(value: UmbCollectionItemModel | undefined) {
const oldValue = this.#item;
this.#item = value;
if (value) {
this.#entityContext = new UmbEntityContext(this);
this.#entityContext.setEntityType(value.entityType);
this.#entityContext.setUnique(value.unique);
} else {
this.#entityContext?.destroy();
this.#entityContext = undefined;
}
this.requestUpdate('item', oldValue);
}
@property({ type: Boolean })
selectable = false;
@property({ type: Boolean })
selected = false;
@property({ type: Boolean })
selectOnly = false;
@property({ type: String })
href?: string;
override render() {
if (!this.item) return nothing;
return html`
<div>
${this.selectable
? html`<input
type="checkbox"
.checked=${this.selected}
@change=${() => this.selected ? this.#onDeselected() : this.#onSelected()}/>`
: nothing}
${this.href && !this.selectOnly
? html`<a href=${this.href}>${this.item.name}</a>`
: html`<span>${this.item.name}</span>`}
<umb-entity-actions-bundle></umb-entity-actions-bundle>
</div>
`;
}
#onSelected() {
if (!this.item) return;
this.dispatchEvent(new UmbSelectedEvent(this.item.unique));
}
#onDeselected() {
if (!this.item) return;
this.dispatchEvent(new UmbDeselectedEvent(this.item.unique));
}
}
declare global {
interface HTMLElementTagNameMap {
'my-collection-view-item': MyCollectionViewItemElement;
}
}