migrate UmbCollectionContext to generic UmbContextAlias

This commit is contained in:
Jacob Overgaard
2023-01-17 16:13:31 +01:00
parent 05b623cad9
commit eb8982a2ee
8 changed files with 35 additions and 23 deletions

View File

@@ -2,7 +2,7 @@ import { UUITextStyles } from '@umbraco-ui/uui-css';
import { css, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import type { UmbCollectionContext } from '../collection.context';
import { UmbCollectionContext, UMB_COLLECTION_CONTEXT_ALIAS } from '../collection.context';
import type { ManifestCollectionBulkAction } from '@umbraco-cms/models';
import { UmbLitElement } from '@umbraco-cms/element';
@@ -18,7 +18,7 @@ export class UmbCollectionBulkActionDeleteElement extends UmbLitElement {
constructor() {
super();
this.consumeContext('umbCollectionContext', (context) => {
this.consumeContext(UMB_COLLECTION_CONTEXT_ALIAS, (context) => {
this._collectionContext = context;
});
}

View File

@@ -1,7 +1,7 @@
import { UUITextStyles } from '@umbraco-ui/uui-css';
import { css, html, nothing } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import type { UmbCollectionContext } from './collection.context';
import { UmbCollectionContext, UMB_COLLECTION_CONTEXT_ALIAS } from './collection.context';
import type { MediaDetails } from '@umbraco-cms/models';
import { UmbLitElement } from '@umbraco-cms/element';
@@ -36,7 +36,7 @@ export class UmbCollectionSelectionActionsElement extends UmbLitElement {
constructor() {
super();
this.consumeContext('umbCollectionContext', (instance) => {
this.consumeContext(UMB_COLLECTION_CONTEXT_ALIAS, (instance) => {
this._collectionContext = instance;
this._observeCollectionContext();
});

View File

@@ -1,13 +1,12 @@
import { ContentTreeItem } from '@umbraco-cms/backend-api';
import { UmbTreeDataStore } from '@umbraco-cms/stores/store';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
import { UmbContextConsumerController } from '@umbraco-cms/context-api';
import { UmbContextAlias, UmbContextConsumerController } from '@umbraco-cms/context-api';
import { UniqueBehaviorSubject, UmbObserverController } from '@umbraco-cms/observable-api';
export class UmbCollectionContext<
DataType extends ContentTreeItem,
StoreType extends UmbTreeDataStore<DataType> = UmbTreeDataStore<DataType>
> {
private _host: UmbControllerHostInterface;
private _entityKey: string | null;
@@ -60,14 +59,18 @@ export class UmbCollectionContext<
this._dataObserver?.destroy();
if (this._entityKey) {
this._dataObserver = new UmbObserverController(this._host, this._store.getTreeItemChildren(this._entityKey), (nodes) => {
if(nodes) {
this.#data.next(nodes);
this._dataObserver = new UmbObserverController(
this._host,
this._store.getTreeItemChildren(this._entityKey),
(nodes) => {
if (nodes) {
this.#data.next(nodes);
}
}
});
);
} else {
this._dataObserver = new UmbObserverController(this._host, this._store.getTreeRoot(), (nodes) => {
if(nodes) {
if (nodes) {
this.#data.next(nodes);
}
});
@@ -107,3 +110,7 @@ export class UmbCollectionContext<
this.#data.unsubscribe();
}
}
export const UMB_COLLECTION_CONTEXT_ALIAS = new UmbContextAlias<UmbCollectionContext<any, any>>(
UmbCollectionContext.name
);

View File

@@ -4,7 +4,7 @@ import { customElement, state, property } from 'lit/decorators.js';
import { map } from 'rxjs';
import './collection-selection-actions.element';
import './collection-toolbar.element';
import type { UmbCollectionContext } from './collection.context';
import { UmbCollectionContext, UMB_COLLECTION_CONTEXT_ALIAS } from './collection.context';
import { createExtensionElement } from '@umbraco-cms/extensions-api';
import type { ManifestCollectionView, MediaDetails } from '@umbraco-cms/models';
import { umbExtensionsRegistry } from '@umbraco-cms/extensions-registry';
@@ -53,7 +53,7 @@ export class UmbCollectionElement extends UmbLitElement {
constructor() {
super();
this.consumeContext('umbCollectionContext', (instance) => {
this.consumeContext(UMB_COLLECTION_CONTEXT_ALIAS, (instance) => {
this._collectionContext = instance;
this._observeCollectionContext();
});
@@ -85,7 +85,7 @@ export class UmbCollectionElement extends UmbLitElement {
private _createRoutes(views: ManifestCollectionView[] | null) {
this._routes = [];
if(views) {
if (views) {
this._routes = views.map((view) => {
return {
path: `${view.meta.pathName}`,

View File

@@ -4,7 +4,10 @@ import { customElement, state } from 'lit/decorators.js';
import '../collection.element';
import { ifDefined } from 'lit-html/directives/if-defined.js';
import { UmbMediaStore, UmbMediaStoreItemType } from 'src/backoffice/media/media/media.store';
import { UmbCollectionContext } from 'src/backoffice/shared/collection/collection.context';
import {
UmbCollectionContext,
UMB_COLLECTION_CONTEXT_ALIAS,
} from 'src/backoffice/shared/collection/collection.context';
import type { ManifestDashboardCollection } from '@umbraco-cms/models';
import { UmbLitElement } from '@umbraco-cms/element';
@@ -37,7 +40,7 @@ export class UmbDashboardCollectionElement extends UmbLitElement {
const manifestMeta = this.manifest.meta as any;
this._entityType = manifestMeta.entityType as string;
this._collectionContext = new UmbCollectionContext(this, null, manifestMeta.storeAlias);
this.provideContext('umbCollectionContext', this._collectionContext);
this.provideContext(UMB_COLLECTION_CONTEXT_ALIAS, this._collectionContext);
}
}

View File

@@ -2,7 +2,7 @@ import { UUITextStyles } from '@umbraco-ui/uui-css';
import { css, html } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import type { UmbCollectionContext } from '../collection.context';
import { UmbCollectionContext, UMB_COLLECTION_CONTEXT_ALIAS } from '../collection.context';
import type { MediaDetails } from '@umbraco-cms/models';
import { UmbLitElement } from '@umbraco-cms/element';
@@ -77,8 +77,7 @@ export class UmbCollectionViewsMediaGridElement extends UmbLitElement {
document.addEventListener('dragenter', this._handleDragEnter.bind(this));
document.addEventListener('dragleave', this._handleDragLeave.bind(this));
document.addEventListener('drop', this._handleDrop.bind(this));
this.consumeContext('umbCollectionContext', (instance) => {
console.log('umbCollectionContext', instance);
this.consumeContext(UMB_COLLECTION_CONTEXT_ALIAS, (instance) => {
this._collectionContext = instance;
this._observeCollectionContext();
});

View File

@@ -1,7 +1,7 @@
import { UUITextStyles } from '@umbraco-ui/uui-css';
import { css, html } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import type { UmbCollectionContext } from '../collection.context';
import { UmbCollectionContext, UMB_COLLECTION_CONTEXT_ALIAS } from '../collection.context';
import type { MediaDetails } from '@umbraco-cms/models';
import { UmbLitElement } from '@umbraco-cms/element';
@@ -19,7 +19,7 @@ export class UmbCollectionViewMediaTableElement extends UmbLitElement {
constructor() {
super();
this.consumeContext('umbCollectionContext', (instance) => {
this.consumeContext(UMB_COLLECTION_CONTEXT_ALIAS, (instance) => {
this._collectionContext = instance;
this._observeCollectionContext();
});

View File

@@ -3,7 +3,10 @@ import { UUITextStyles } from '@umbraco-ui/uui-css/lib';
import { customElement } from 'lit/decorators.js';
import { ifDefined } from 'lit-html/directives/if-defined.js';
import type { UmbWorkspaceContentContext } from '../../workspace-content.context';
import { UmbCollectionContext } from 'src/backoffice/shared/collection/collection.context';
import {
UmbCollectionContext,
UMB_COLLECTION_CONTEXT_ALIAS,
} from 'src/backoffice/shared/collection/collection.context';
import { UmbMediaStore, UmbMediaStoreItemType } from 'src/backoffice/media/media/media.store';
import '../../../../../../shared/components/content-property/content-property.element';
@@ -42,7 +45,7 @@ export class UmbWorkspaceViewCollectionElement extends UmbLitElement {
this._workspaceContext.entityKey,
this._workspaceContext.getStore()?.storeAlias || '' // The store is available when the context is available.
);
this.provideContext('umbCollectionContext', this._collectionContext);
this.provideContext(UMB_COLLECTION_CONTEXT_ALIAS, this._collectionContext);
}
}