From 2180b6ef24fbf03218fae9d1bfd9fd50e4f956d4 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 29 Oct 2024 17:03:27 +0100 Subject: [PATCH] render create options --- .../create-user-collection-action.element.ts | 127 +++++++++--------- 1 file changed, 65 insertions(+), 62 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/action/create-user-collection-action.element.ts b/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/action/create-user-collection-action.element.ts index 4ffcc353d0..6ddc1b57cc 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/action/create-user-collection-action.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/action/create-user-collection-action.element.ts @@ -1,13 +1,7 @@ -import { UMB_CREATE_USER_MODAL } from '../../modals/create/create-user-modal.token.js'; -import type { UmbUserKindType } from '../../utils/index.js'; -import { UmbUserKind } from '../../utils/index.js'; import { html, customElement, state } from '@umbraco-cms/backoffice/external/lit'; import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; -import { UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal'; -import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity'; -import { UMB_ENTITY_CONTEXT } from '@umbraco-cms/backoffice/entity'; -import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action'; -import { UmbRequestReloadChildrenOfEntityEvent } from '@umbraco-cms/backoffice/entity-action'; +import { UmbExtensionsApiInitializer } from '@umbraco-cms/backoffice/extension-api'; +import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry'; const elementName = 'umb-create-user-collection-action-button'; @customElement(elementName) @@ -15,83 +9,92 @@ export class UmbCollectionActionButtonElement extends UmbLitElement { @state() private _popoverOpen = false; - async #onClick(event: Event, kind: UmbUserKindType) { - event.stopPropagation(); - const modalManager = await this.getContext(UMB_MODAL_MANAGER_CONTEXT); - const entityContext = await this.getContext(UMB_ENTITY_CONTEXT); + @state() + private _multipleOptions = false; - const unique = entityContext.getUnique(); - const entityType = entityContext.getEntityType(); + #apiControllers: Array = []; + #createLabel = this.localize.term('general_create'); - if (unique === undefined) throw new Error('Missing unique'); - if (!entityType) throw new Error('Missing entityType'); - - const modalContext = modalManager.open(this, UMB_CREATE_USER_MODAL, { - data: { - user: { - kind, - }, - }, - }); - - modalContext - ?.onSubmit() - .then(() => { - this.#requestReloadChildrenOfEntity({ entityType, unique }); - }) - .catch(async () => { - // modal is closed after creation instead of navigating to the new user. - // We therefore need to reload the children of the entity - this.#requestReloadChildrenOfEntity({ entityType, unique }); - }); - } - - async #requestReloadChildrenOfEntity({ entityType, unique }: UmbEntityModel) { - const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT); - const event = new UmbRequestReloadChildrenOfEntityEvent({ - entityType, - unique, - }); - - eventContext.dispatchEvent(event); - } - - #onPopoverToggle(event: ToggleEvent) { + #onPopoverToggle(event: PointerEvent) { // TODO: This ignorer is just neede for JSON SCHEMA TO WORK, As its not updated with latest TS jet. // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore this._popoverOpen = event.newState === 'open'; } - override render() { - const label = this.localize.term('general_create'); + async #onClick(event: Event, controller: any) { + event.stopPropagation(); + await controller.api.execute(); + } + constructor() { + super(); + + new UmbExtensionsApiInitializer( + this, + umbExtensionsRegistry, + 'entityCreateOptionAction', + [], + undefined, + (controllers) => { + this.#apiControllers = controllers; + this._multipleOptions = controllers.length > 1; + }, + ); + } + + override render() { + return this._multipleOptions ? this.#renderMultiOptionAction() : this.#renderSingleOptionAction(); + } + + #renderSingleOptionAction() { + return html` this.#onClick(event, this.#apiControllers[0])}>`; + } + + #renderMultiOptionAction() { return html` - - ${label} + + ${this.#createLabel} + ${this.#renderDropdown()} + `; + } + + #renderDropdown() { + return html` - this.#onClick(event, UmbUserKind.DEFAULT)}> - - - this.#onClick(event, UmbUserKind.API)}> - - + ${this.#apiControllers.map((controller) => this.#renderMenuItem(controller))} `; } + + #renderMenuItem(controller: any) { + const label = controller.manifest.meta.label + ? this.localize.string(controller.manifest.meta.label) + : controller.manifest.meta.name; + + return html` + this.#onClick(event, controller)}> + + + `; + } } export { UmbCollectionActionButtonElement as element };