controllers accept multiple types

This commit is contained in:
Niels Lyngsø
2023-08-03 11:19:21 +02:00
parent 6b22b78bee
commit d9960a43d3
6 changed files with 59 additions and 14 deletions

View File

@@ -125,6 +125,41 @@ describe('UmbBaseExtensionsController', () => {
}
);
});
it('consumed multiple types', (done) => {
const manifestExtra = {
type: 'extension-type-extra',
name: 'test-extension-extra',
alias: 'Umb.Test.Extension.Extra',
};
testExtensionRegistry.register(manifestExtra);
let count = 0;
const extensionController = new UmbTestExtensionsController(
hostElement,
testExtensionRegistry,
['extension-type', 'extension-type-extra'],
null,
(permitted) => {
count++;
if (count === 1) {
// First callback gives just one. We need to make a feature to gather changes to only reply after a computation cycle if we like to avoid this.
expect(permitted.length).to.eq(1);
}
if (count === 2) {
expect(permitted.length).to.eq(2);
}
if (count === 3) {
expect(permitted.length).to.eq(3);
expect(permitted[0].alias).to.eq('Umb.Test.Extension.A');
expect(permitted[1].alias).to.eq('Umb.Test.Extension.B');
expect(permitted[2].alias).to.eq('Umb.Test.Extension.Extra');
done();
extensionController.destroy();
}
}
);
});
});
describe('Manifests without conditions overwrites another', () => {

View File

@@ -22,7 +22,7 @@ export abstract class UmbBaseExtensionsController<
MyPermittedControllerType extends ControllerType = PermittedControllerType<ControllerType>
> extends UmbBaseController {
#extensionRegistry: UmbExtensionRegistry<ManifestType>;
#type: ManifestTypeName;
#type: ManifestTypeName | Array<ManifestTypeName>;
#filter: undefined | null | ((manifest: ManifestType) => boolean);
#onChange: (permittedManifests: Array<MyPermittedControllerType>, controller: MyPermittedControllerType) => void;
protected _extensions: Array<ControllerType> = [];
@@ -31,7 +31,7 @@ export abstract class UmbBaseExtensionsController<
constructor(
host: UmbControllerHost,
extensionRegistry: UmbExtensionRegistry<ManifestType>,
type: ManifestTypeName,
type: ManifestTypeName | Array<ManifestTypeName>,
filter: undefined | null | ((manifest: ManifestType) => boolean),
onChange: (permittedManifests: Array<MyPermittedControllerType>, controller: MyPermittedControllerType) => void
) {
@@ -42,7 +42,9 @@ export abstract class UmbBaseExtensionsController<
this.#onChange = onChange;
}
protected _init() {
let source = this.#extensionRegistry.extensionsOfType<ManifestTypeName, ManifestType>(this.#type);
let source = Array.isArray(this.#type)
? this.#extensionRegistry.extensionsOfTypes<ManifestType>(this.#type as string[])
: this.#extensionRegistry.extensionsOfType<ManifestTypeName, ManifestType>(this.#type as ManifestTypeName);
if (this.#filter) {
source = source.pipe(map((extensions: Array<ManifestType>) => extensions.filter(this.#filter!)));
}

View File

@@ -27,7 +27,7 @@ export class UmbExtensionElementController<
* @memberof UmbElementExtensionController
* @example
* ```ts
* const controller = new UmbElementExtensionController(host, alias, onPermissionChanged);
* const controller = new UmbElementExtensionController(host, extensionRegistry, alias, onPermissionChanged);
* controller.props = { foo: 'bar' };
* ```
* Is equivalent to:

View File

@@ -41,7 +41,7 @@ export class UmbExtensionsElementController<
constructor(
host: UmbControllerHost,
extensionRegistry: UmbExtensionRegistry<ManifestTypes>,
type: ManifestTypeName,
type: ManifestTypeName | Array<ManifestTypeName>,
filter: undefined | null | ((manifest: ManifestType) => boolean),
onChange: (permittedManifests: Array<MyPermittedControllerType>, controller: MyPermittedControllerType) => void,
defaultElement?: string

View File

@@ -29,7 +29,7 @@ export class UmbExtensionsManifestController<
constructor(
host: UmbControllerHost,
extensionRegistry: UmbExtensionRegistry<ManifestTypes>,
type: ManifestTypeName,
type: ManifestTypeName | Array<ManifestTypeName>,
filter: null | ((manifest: ManifestType) => boolean),
onChange: (permittedManifests: Array<MyPermittedControllerType>, controller: MyPermittedControllerType) => void
) {

View File

@@ -1,18 +1,12 @@
import { UUITextStyles } from '@umbraco-cms/backoffice/external/uui';
import { css, html, nothing, customElement, property, state, repeat } from '@umbraco-cms/backoffice/external/lit';
import { map } from '@umbraco-cms/backoffice/external/rxjs';
import type {
PageComponent,
UmbRoute,
UmbRouterSlotInitEvent,
UmbRouterSlotChangeEvent,
} from '@umbraco-cms/backoffice/router';
import type { UmbRoute, UmbRouterSlotInitEvent, UmbRouterSlotChangeEvent } from '@umbraco-cms/backoffice/router';
import {
ManifestWorkspaceEditorView,
ManifestWorkspaceViewCollection,
umbExtensionsRegistry,
} from '@umbraco-cms/backoffice/extension-registry';
import { createExtensionElement } from '@umbraco-cms/backoffice/extension-api';
import { UmbExtensionsElementController, createExtensionElement } from '@umbraco-cms/backoffice/extension-api';
import { UmbLitElement } from '@umbraco-cms/internal/lit-element';
import { componentHasManifestProperty } from '@umbraco-cms/backoffice/utils';
@@ -75,6 +69,20 @@ export class UmbWorkspaceEditorElement extends UmbLitElement {
@state()
private _activePath?: string;
constructor() {
super();
new UmbExtensionsElementController(
this,
umbExtensionsRegistry,
['workspaceEditorView', 'workspaceViewCollection'],
null,
(workspaceViews) => {
this._workspaceViews = workspaceViews;
this._createRoutes();
}
);
}
private _observeWorkspaceViews() {
this.observe(
umbExtensionsRegistry.extensionsOfTypes<ManifestWorkspaceEditorView>([