Skip to content

Commit ef453ad

Browse files
Docs: Collection example (#19593)
* add basic collection example * add card view example * update example readme * Add workspace view example with collection
1 parent e679124 commit ef453ad

File tree

18 files changed

+416
-1
lines changed

18 files changed

+416
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Collection Example
2+
3+
This example demonstrates how to register a collection with collection views.
4+
5+
The example includes:
6+
7+
- Collection Registration
8+
- Collection Repository
9+
- Collection Pagination
10+
- Table Collection View
11+
- Card Collection View
12+
- Collection as a Dashboard
13+
- Collection as a Workspace View
14+
15+
TODO: This example is not complete, it is missing the following features:
16+
17+
- Collection Action
18+
- Collection Filtering
19+
- Entity Actions
20+
- Selection + Bulk Actions
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { ExampleCollectionItemModel } from '../repository/types.js';
2+
import type { UmbDefaultCollectionContext } from '@umbraco-cms/backoffice/collection';
3+
import { UMB_COLLECTION_CONTEXT } from '@umbraco-cms/backoffice/collection';
4+
import { css, html, customElement, state, repeat } from '@umbraco-cms/backoffice/external/lit';
5+
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
6+
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
7+
8+
@customElement('example-card-collection-view')
9+
export class ExampleCardCollectionViewElement extends UmbLitElement {
10+
@state()
11+
private _items: Array<ExampleCollectionItemModel> = [];
12+
13+
#collectionContext?: UmbDefaultCollectionContext<ExampleCollectionItemModel>;
14+
15+
constructor() {
16+
super();
17+
18+
this.consumeContext(UMB_COLLECTION_CONTEXT, (instance) => {
19+
this.#collectionContext = instance;
20+
this.#observeCollectionItems();
21+
});
22+
}
23+
24+
#observeCollectionItems() {
25+
this.observe(this.#collectionContext?.items, (items) => (this._items = items || []), 'umbCollectionItemsObserver');
26+
}
27+
28+
override render() {
29+
return html`
30+
<div id="card-grid">
31+
${repeat(
32+
this._items,
33+
(item) => item.unique,
34+
(item) =>
35+
html` <uui-card>
36+
<uui-icon name="icon-newspaper"></uui-icon>
37+
<div>${item.name}</div>
38+
</uui-card>`,
39+
)}
40+
</div>
41+
`;
42+
}
43+
44+
static override styles = [
45+
UmbTextStyles,
46+
css`
47+
:host {
48+
display: flex;
49+
flex-direction: column;
50+
}
51+
52+
#card-grid {
53+
display: grid;
54+
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
55+
grid-auto-rows: 200px;
56+
gap: var(--uui-size-space-5);
57+
}
58+
59+
uui-card {
60+
display: flex;
61+
flex-direction: column;
62+
align-items: center;
63+
justify-content: center;
64+
text-align: center;
65+
height: 100%;
66+
67+
uui-icon {
68+
font-size: 2em;
69+
margin-bottom: var(--uui-size-space-4);
70+
}
71+
}
72+
`,
73+
];
74+
}
75+
76+
export { ExampleCardCollectionViewElement as element };
77+
78+
declare global {
79+
interface HTMLElementTagNameMap {
80+
'example-card-collection-view': ExampleCardCollectionViewElement;
81+
}
82+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { UMB_LANGUAGE_TABLE_COLLECTION_VIEW_ALIAS } from './manifests.js';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { EXAMPLE_COLLECTION_ALIAS } from '../constants.js';
2+
import { UMB_COLLECTION_ALIAS_CONDITION } from '@umbraco-cms/backoffice/collection';
3+
4+
export const manifests: Array<UmbExtensionManifest> = [
5+
{
6+
type: 'collectionView',
7+
alias: 'Example.CollectionView.Card',
8+
name: 'Example Card Collection View',
9+
js: () => import('./collection-view.element.js'),
10+
weight: 50,
11+
meta: {
12+
label: 'Card',
13+
icon: 'icon-grid',
14+
pathName: 'card',
15+
},
16+
conditions: [
17+
{
18+
alias: UMB_COLLECTION_ALIAS_CONDITION,
19+
match: EXAMPLE_COLLECTION_ALIAS,
20+
},
21+
],
22+
},
23+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const EXAMPLE_COLLECTION_ALIAS = 'Example.Collection';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { EXAMPLE_COLLECTION_ALIAS } from './constants.js';
2+
import { EXAMPLE_COLLECTION_REPOSITORY_ALIAS } from './repository/constants.js';
3+
import { manifests as cardViewManifests } from './card-view/manifests.js';
4+
import { manifests as repositoryManifests } from './repository/manifests.js';
5+
import { manifests as tableViewManifests } from './table-view/manifests.js';
6+
7+
export const manifests: Array<UmbExtensionManifest> = [
8+
{
9+
type: 'collection',
10+
kind: 'default',
11+
alias: EXAMPLE_COLLECTION_ALIAS,
12+
name: 'Example Collection',
13+
meta: {
14+
repositoryAlias: EXAMPLE_COLLECTION_REPOSITORY_ALIAS,
15+
},
16+
},
17+
...cardViewManifests,
18+
...repositoryManifests,
19+
...tableViewManifests,
20+
];
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { ExampleCollectionFilterModel, ExampleCollectionItemModel } from './types.js';
2+
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
3+
import type { UmbCollectionRepository } from '@umbraco-cms/backoffice/collection';
4+
5+
export class ExampleCollectionRepository
6+
extends UmbRepositoryBase
7+
implements UmbCollectionRepository<ExampleCollectionItemModel, ExampleCollectionFilterModel>
8+
{
9+
async requestCollection(args: ExampleCollectionFilterModel) {
10+
const skip = args.skip || 0;
11+
const take = args.take || 10;
12+
13+
// Simulating a data fetch. This would in most cases be replaced with an API call.
14+
let items = [
15+
{
16+
unique: '3e31e9c5-7d66-4c99-a9e5-d9f2b1e2b22f',
17+
entityType: 'example',
18+
name: 'Example Item 1',
19+
},
20+
{
21+
unique: 'bc9b6e24-4b11-4dd6-8d4e-7c4f70e59f3c',
22+
entityType: 'example',
23+
name: 'Example Item 2',
24+
},
25+
{
26+
unique: '5a2f4e3a-ef7e-470e-8c3c-3d859c02ae0d',
27+
entityType: 'example',
28+
name: 'Example Item 3',
29+
},
30+
{
31+
unique: 'f4c3d8b8-6d79-4c87-9aa9-56b1d8fda702',
32+
entityType: 'example',
33+
name: 'Example Item 4',
34+
},
35+
{
36+
unique: 'c9f0a8a3-1b49-4724-bde3-70e31592eb6e',
37+
entityType: 'example',
38+
name: 'Example Item 5',
39+
},
40+
];
41+
42+
// Simulating filtering based on the args
43+
if (args.filter) {
44+
items = items.filter((item) => item.name.toLowerCase().includes(args.filter!.toLowerCase()));
45+
}
46+
47+
// Get the total number of items before pagination
48+
const totalItems = items.length;
49+
50+
// Simulating pagination
51+
const start = skip;
52+
const end = start + take;
53+
items = items.slice(start, end);
54+
55+
const data = {
56+
items,
57+
total: totalItems,
58+
};
59+
60+
return { data };
61+
}
62+
}
63+
64+
export { ExampleCollectionRepository as api };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const EXAMPLE_COLLECTION_REPOSITORY_ALIAS = 'Example.Repository.Collection';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EXAMPLE_COLLECTION_REPOSITORY_ALIAS } from './constants.js';
2+
3+
export const manifests: Array<UmbExtensionManifest> = [
4+
{
5+
type: 'repository',
6+
alias: EXAMPLE_COLLECTION_REPOSITORY_ALIAS,
7+
name: 'Example Collection Repository',
8+
api: () => import('./collection.repository.js'),
9+
},
10+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { UmbCollectionFilterModel } from '@umbraco-cms/backoffice/collection';
2+
3+
export interface ExampleCollectionItemModel {
4+
unique: string;
5+
entityType: string;
6+
name: string;
7+
}
8+
9+
export interface ExampleCollectionFilterModel extends UmbCollectionFilterModel {}

0 commit comments

Comments
 (0)