Skip to content

Commit

Permalink
direct usage of selection manager instead of proxy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
madsrasmussen committed Nov 28, 2023
1 parent 11efe83 commit e0c2e61
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/packages/core/collection/collection-default.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class UmbDefaultCollectionContext<ItemType = any, FilterModelType extends
});

public readonly pagination = new UmbPaginationManager();
public readonly selection = new UmbSelectionManager();
public readonly selection = new UmbSelectionManager(this);

constructor(host: UmbControllerHostElement, config: UmbCollectionConfiguration = { pageSize: 50 }) {
super(host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class UmbSectionPickerModalElement extends UmbModalBaseElement<
@state()
private _sections: Array<ManifestSection> = [];

#selectionManager = new UmbSelectionManager();
#selectionManager = new UmbSelectionManager(this);

#submit() {
this.modalContext?.submit({
Expand All @@ -38,7 +38,8 @@ export class UmbSectionPickerModalElement extends UmbModalBaseElement<
this.observe(
umbExtensionsRegistry.extensionsOfType('section'),
(sections: Array<ManifestSection>) => (this._sections = sections),
), 'umbSectionsObserver';
),
'umbSectionsObserver';
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ export class UmbTreeItemContextBase<TreeItemType extends UmbTreeItemModelBase>

public select() {
if (this.unique === undefined) throw new Error('Could not select, unique key is missing');
this.treeContext?.select(this.unique);
this.treeContext?.selection.select(this.unique);
}

public deselect() {
if (this.unique === undefined) throw new Error('Could not deselect, unique key is missing');
this.treeContext?.deselect(this.unique);
this.treeContext?.selection.deselect(this.unique);
}

#consumeContexts() {
Expand Down Expand Up @@ -138,7 +138,7 @@ export class UmbTreeItemContextBase<TreeItemType extends UmbTreeItemModelBase>
#observeIsSelectable() {
if (!this.treeContext) return;
this.observe(
this.treeContext.selectable,
this.treeContext.selection.selectable,
(value) => {
this.#isSelectableContext.next(value);

Expand All @@ -156,7 +156,7 @@ export class UmbTreeItemContextBase<TreeItemType extends UmbTreeItemModelBase>
if (!this.treeContext || !this.unique) return;

this.observe(
this.treeContext.selection.pipe(map((selection) => selection.includes(this.unique!))),
this.treeContext.selection.selection.pipe(map((selection) => selection.includes(this.unique!))),
(isSelected) => {
this.#isSelected.next(isSelected);
},
Expand Down
57 changes: 3 additions & 54 deletions src/packages/core/tree/tree.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,10 @@ import { type UmbControllerHostElement } from '@umbraco-cms/backoffice/controlle
import { UmbExtensionApiInitializer } from '@umbraco-cms/backoffice/extension-api';
import { ProblemDetails } from '@umbraco-cms/backoffice/backend-api';
import { UmbSelectionManager } from '@umbraco-cms/backoffice/utils';
import { UmbSelectionChangeEvent } from '@umbraco-cms/backoffice/event';

// TODO: update interface
export interface UmbTreeContext<TreeItemType extends UmbTreeItemModelBase> extends UmbBaseController {
readonly selectable: Observable<boolean>;
readonly selection: Observable<Array<string | null>>;
setSelectable(value: boolean): void;
getSelectable(): boolean;
setMultiple(value: boolean): void;
getMultiple(): boolean;
setSelection(value: Array<string | null>): void;
getSelection(): Array<string | null>;
select(unique: string | null): void;
deselect(unique: string | null): void;
selection: UmbSelectionManager;
requestChildrenOf: (parentUnique: string | null) => Promise<{
data?: UmbPagedData<TreeItemType>;
error?: ProblemDetails;
Expand All @@ -38,17 +28,11 @@ export class UmbTreeContextBase<TreeItemType extends UmbTreeItemModelBase>
extends UmbBaseController
implements UmbTreeContext<TreeItemType>
{
#selectionManager = new UmbSelectionManager();

#selectable = new UmbBooleanState(false);
public readonly selectable = this.#selectable.asObservable();

public readonly multiple = this.#selectionManager.multiple;
public readonly selection = this.#selectionManager.selection;

public repository?: UmbTreeRepository<TreeItemType>;
public selectableFilter?: (item: TreeItemType) => boolean = () => true;

public readonly selection = new UmbSelectionManager(this._host);

#treeAlias?: string;

#initResolver?: () => void;
Expand Down Expand Up @@ -82,41 +66,6 @@ export class UmbTreeContextBase<TreeItemType extends UmbTreeItemModelBase>
return this.#treeAlias;
}

public setSelectable(value: boolean) {
this.#selectable.next(value);
}

public getSelectable() {
return this.#selectable.getValue();
}

public setMultiple(value: boolean) {
this.#selectionManager.setMultiple(value);
}

public getMultiple() {
return this.#selectionManager.getMultiple();
}

public setSelection(value: Array<string | null>) {
this.#selectionManager.setSelection(value);
}

public getSelection() {
return this.#selectionManager.getSelection();
}

public select(unique: string | null) {
if (!this.getSelectable()) return;
this.#selectionManager.select(unique);
this._host.getHostElement().dispatchEvent(new UmbSelectionChangeEvent());
}

public deselect(unique: string | null) {
this.#selectionManager.deselect(unique);
this._host.getHostElement().dispatchEvent(new UmbSelectionChangeEvent());
}

public async requestTreeRoot() {
await this.#init;
return this.repository!.requestTreeRoot();
Expand Down
12 changes: 6 additions & 6 deletions src/packages/core/tree/tree.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ export class UmbTreeElement extends UmbLitElement {

@property({ type: Boolean, reflect: true })
get selectable() {
return this.#treeContext.getSelectable();
return this.#treeContext.selection.getSelectable();
}
set selectable(newVal) {
this.#treeContext.setSelectable(newVal);
this.#treeContext.selection.setSelectable(newVal);
}

@property({ type: Array })
get selection() {
return this.#treeContext.getSelection();
return this.#treeContext.selection.getSelection();
}
set selection(newVal) {
if (!Array.isArray(newVal)) return;
this.#treeContext?.setSelection(newVal);
this.#treeContext?.selection.setSelection(newVal);
}

@property({ type: Boolean, reflect: true })
get multiple() {
return this.#treeContext.getMultiple();
return this.#treeContext.selection.getMultiple();
}
set multiple(newVal) {
this.#treeContext.setMultiple(newVal);
this.#treeContext.selection.setMultiple(newVal);
}

// TODO: what is the best name for this functionality?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class UmbLanguagePickerModalElement extends UmbModalBaseElement<
private _languages: Array<LanguageResponseModel> = [];

#languageRepository = new UmbLanguageRepository(this);
#selectionManager = new UmbSelectionManager();
#selectionManager = new UmbSelectionManager(this);

connectedCallback(): void {
super.connectedCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class UmbUserGroupPickerModalElement extends UmbModalBaseElement<any, any
@state()
private _userGroups: Array<UserGroupResponseModel> = [];

#selectionManager = new UmbSelectionManager();
#selectionManager = new UmbSelectionManager(this);
#userGroupCollectionRepository = new UmbUserGroupCollectionRepository(this);

connectedCallback(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class UmbUserPickerModalElement extends UmbModalBaseElement<UmbUserPicker
@state()
private _users: Array<UserItemResponseModel> = [];

#selectionManager = new UmbSelectionManager();
#selectionManager = new UmbSelectionManager(this);
#userCollectionRepository = new UmbUserCollectionRepository(this);

connectedCallback(): void {
Expand Down
36 changes: 35 additions & 1 deletion src/shared/utils/selection-manager.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import { UmbBaseController } from '@umbraco-cms/backoffice/class-api';
import { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbSelectionChangeEvent } from '@umbraco-cms/backoffice/event';
import { UmbArrayState, UmbBooleanState } from '@umbraco-cms/backoffice/observable-api';

/**
* Manages the selection of items.
* @export
* @class UmbSelectionManager
*/
export class UmbSelectionManager {
export class UmbSelectionManager extends UmbBaseController {
#selectable = new UmbBooleanState(false);
public readonly selectable = this.#selectable.asObservable();

#selection = new UmbArrayState(<Array<string | null>>[], (x) => x);
public readonly selection = this.#selection.asObservable();

#multiple = new UmbBooleanState(false);
public readonly multiple = this.#multiple.asObservable();

constructor(host: UmbControllerHost) {
super(host);
}

/**
* Returns whether items can be selected.
* @return {*}
* @memberof UmbSelectionManager
*/
public getSelectable() {
return this.#selectable.getValue();
}

/**
* Sets whether items can be selected.
* @param {boolean} value
* @memberof UmbSelectionManager
*/
public setSelectable(value: boolean) {
this.#selectable.next(value);
}

/**
* Returns the current selection.
* @return {*}
Expand All @@ -27,6 +55,7 @@ export class UmbSelectionManager {
* @memberof UmbSelectionManager
*/
public setSelection(value: Array<string | null>) {
if (this.getSelectable() === false) return;
if (value === undefined) throw new Error('Value cannot be undefined');
this.#selection.next(value);
}
Expand Down Expand Up @@ -55,6 +84,7 @@ export class UmbSelectionManager {
* @memberof UmbSelectionManager
*/
public toggleSelect(unique: string | null) {
if (this.getSelectable() === false) return;
this.isSelected(unique) ? this.deselect(unique) : this.select(unique);
}

Expand All @@ -64,8 +94,10 @@ export class UmbSelectionManager {
* @memberof UmbSelectionManager
*/
public select(unique: string | null) {
if (this.getSelectable() === false) return;
const newSelection = this.getMultiple() ? [...this.getSelection(), unique] : [unique];
this.#selection.next(newSelection);
this._host.getHostElement().dispatchEvent(new UmbSelectionChangeEvent());
}

/**
Expand All @@ -74,8 +106,10 @@ export class UmbSelectionManager {
* @memberof UmbSelectionManager
*/
public deselect(unique: string | null) {
if (this.getSelectable() === false) return;
const newSelection = this.getSelection().filter((x) => x !== unique);
this.#selection.next(newSelection);
this._host.getHostElement().dispatchEvent(new UmbSelectionChangeEvent());
}

/**
Expand Down

0 comments on commit e0c2e61

Please sign in to comment.