2025-10-10 11:46:48 +02:00
|
|
|
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
|
|
|
|
|
import {
|
|
|
|
|
UmbDocumentItemRepository,
|
|
|
|
|
UmbDocumentSearchRepository,
|
|
|
|
|
UmbDocumentTreeRepository,
|
2025-10-14 18:11:34 +02:00
|
|
|
type UmbDocumentSearchItemModel,
|
2025-10-10 11:46:48 +02:00
|
|
|
type UmbDocumentSearchRequestArgs,
|
2025-10-14 18:11:34 +02:00
|
|
|
type UmbDocumentTreeItemModel,
|
|
|
|
|
type UmbDocumentTreeRootModel,
|
2025-10-10 11:46:48 +02:00
|
|
|
} from '@umbraco-cms/backoffice/document';
|
|
|
|
|
import { UMB_DOCUMENT_TYPE_ENTITY_TYPE } from '@umbraco-cms/backoffice/document-type';
|
|
|
|
|
import type {
|
|
|
|
|
UmbPickerSearchableDataSource,
|
|
|
|
|
UmbPickerTreeDataSource,
|
|
|
|
|
} from '@umbraco-cms/backoffice/picker-data-source';
|
|
|
|
|
import type { UmbSearchRequestArgs } from '@umbraco-cms/backoffice/search';
|
|
|
|
|
import type {
|
|
|
|
|
UmbTreeAncestorsOfRequestArgs,
|
|
|
|
|
UmbTreeChildrenOfRequestArgs,
|
|
|
|
|
UmbTreeRootItemsRequestArgs,
|
|
|
|
|
} from '@umbraco-cms/backoffice/tree';
|
2025-11-13 10:57:18 +01:00
|
|
|
import { getConfigValue } from '@umbraco-cms/backoffice/utils';
|
|
|
|
|
|
|
|
|
|
type ExampleDocumentPickerConfigCollectionModel = Array<{ alias: 'filter'; value: string }>;
|
2025-10-10 11:46:48 +02:00
|
|
|
|
|
|
|
|
export class ExampleDocumentPickerPropertyEditorDataSource
|
|
|
|
|
extends UmbControllerBase
|
2025-10-14 18:11:34 +02:00
|
|
|
implements
|
|
|
|
|
UmbPickerTreeDataSource<UmbDocumentTreeItemModel, UmbDocumentTreeRootModel>,
|
|
|
|
|
UmbPickerSearchableDataSource<UmbDocumentSearchItemModel>
|
2025-10-10 11:46:48 +02:00
|
|
|
{
|
|
|
|
|
#tree = new UmbDocumentTreeRepository(this);
|
|
|
|
|
#item = new UmbDocumentItemRepository(this);
|
|
|
|
|
#search = new UmbDocumentSearchRepository(this);
|
2025-11-13 10:57:18 +01:00
|
|
|
#config: ExampleDocumentPickerConfigCollectionModel = [];
|
2025-10-10 11:46:48 +02:00
|
|
|
|
2025-10-14 18:11:34 +02:00
|
|
|
treePickableFilter: (treeItem: UmbDocumentTreeItemModel) => boolean = (treeItem) => !!treeItem.unique;
|
|
|
|
|
|
2025-11-13 10:57:18 +01:00
|
|
|
setConfig(config: ExampleDocumentPickerConfigCollectionModel) {
|
2025-10-10 11:46:48 +02:00
|
|
|
// TODO: add examples for all config options
|
|
|
|
|
this.#config = config;
|
2025-10-14 18:11:34 +02:00
|
|
|
this.#applyPickableFilterFromConfig();
|
2025-10-10 11:46:48 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 10:57:18 +01:00
|
|
|
getConfig(): ExampleDocumentPickerConfigCollectionModel {
|
2025-10-10 11:46:48 +02:00
|
|
|
return this.#config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestTreeRoot() {
|
|
|
|
|
return this.#tree.requestTreeRoot();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestTreeRootItems(args: UmbTreeRootItemsRequestArgs) {
|
|
|
|
|
return this.#tree.requestTreeRootItems(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestTreeItemsOf(args: UmbTreeChildrenOfRequestArgs) {
|
|
|
|
|
return this.#tree.requestTreeItemsOf(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestTreeItemAncestors(args: UmbTreeAncestorsOfRequestArgs) {
|
|
|
|
|
return this.#tree.requestTreeItemAncestors(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestItems(uniques: Array<string>) {
|
|
|
|
|
return this.#item.requestItems(uniques);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
search(args: UmbSearchRequestArgs) {
|
2025-10-14 18:11:34 +02:00
|
|
|
const allowedContentTypes = this.#getAllowedDocumentTypesConfig();
|
|
|
|
|
const combinedArgs: UmbDocumentSearchRequestArgs = { ...args, allowedContentTypes };
|
|
|
|
|
|
|
|
|
|
return this.#search.search(combinedArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#getAllowedDocumentTypesConfig() {
|
2025-11-13 10:57:18 +01:00
|
|
|
const filterString = getConfigValue(this.#config, 'filter');
|
2025-10-10 11:46:48 +02:00
|
|
|
const filterArray = filterString ? filterString.split(',') : [];
|
|
|
|
|
const allowedContentTypes: UmbDocumentSearchRequestArgs['allowedContentTypes'] = filterArray.map(
|
|
|
|
|
(unique: string) => ({
|
|
|
|
|
entityType: UMB_DOCUMENT_TYPE_ENTITY_TYPE,
|
|
|
|
|
unique,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2025-10-14 18:11:34 +02:00
|
|
|
return allowedContentTypes;
|
|
|
|
|
}
|
2025-10-10 11:46:48 +02:00
|
|
|
|
2025-10-14 18:11:34 +02:00
|
|
|
#applyPickableFilterFromConfig() {
|
|
|
|
|
const allowedDocumentTypes = this.#getAllowedDocumentTypesConfig();
|
|
|
|
|
if (!allowedDocumentTypes || allowedDocumentTypes.length === 0) return;
|
|
|
|
|
this.treePickableFilter = (treeItem: UmbDocumentTreeItemModel) =>
|
|
|
|
|
allowedDocumentTypes.some((entityType) => entityType.unique === treeItem.documentType.unique);
|
2025-10-10 11:46:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { ExampleDocumentPickerPropertyEditorDataSource as api };
|