diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/templating/file-system-tree.store.ts b/src/Umbraco.Web.UI.Client/src/backoffice/templating/file-system-tree.store.ts new file mode 100644 index 0000000000..eba5aee290 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/backoffice/templating/file-system-tree.store.ts @@ -0,0 +1,67 @@ +import { FileSystemTreeItemPresentationModel } from '@umbraco-cms/backoffice/backend-api'; +import { ArrayState, partialUpdateFrozenArray } from '@umbraco-cms/backoffice/observable-api'; +import { UmbStoreBase, UmbTreeStore } from '@umbraco-cms/backoffice/store'; + +/** + * @export + * @class UmbFileSystemTreeStore + * @extends {UmbStoreBase} + * @description - General Tree Data Store + */ +export class UmbFileSystemTreeStore extends UmbStoreBase implements UmbTreeStore { + #data = new ArrayState([], (x) => x.path); + + /** + * Appends items to the store + * @param {Array} items + * @memberof UmbFileSystemTreeStore + */ + appendItems(items: Array) { + this.#data.append(items); + } + + /** + * Updates an item in the store + * @param {string} path + * @param {Partial} data + * @memberof UmbFileSystemTreeStore + */ + updateItem(path: string, data: Partial) { + this.#data.next(partialUpdateFrozenArray(this.#data.getValue(), data, (entry) => entry.path === path)); + } + + /** + * Removes an item from the store + * @param {string} path + * @memberof UmbFileSystemTreeStore + */ + removeItem(path: string) { + this.#data.removeOne(path); + } + + /** + * An observable to observe the root items + * @memberof UmbFileSystemTreeStore + */ + rootItems = this.#data.getObservablePart((items) => items.filter((item) => item.path === item.name)); + + /** + * Returns an observable to observe the children of a given parent + * @param {(string | null)} parentPath + * @return {*} + * @memberof UmbFileSystemTreeStore + */ + childrenOf(parentPath: string | null) { + return this.#data.getObservablePart((items) => items.filter((item) => item.path?.startsWith(parentPath ?? ''))); + } + + /** + * Returns an observable to observe the items with the given keys + * @param {Array} paths + * @return {*} + * @memberof UmbFileSystemTreeStore + */ + items(paths: Array) { + return this.#data.getObservablePart((items) => items.filter((item) => paths.includes(item.path ?? ''))); + } +}