UNPKG

@progress/kendo-angular-treeview

Version:
161 lines (160 loc) 6.42 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ import { Directive, EventEmitter, HostBinding, Input, Output } from '@angular/core'; import { TreeViewComponent } from '../treeview.component'; import { isBoolean, isPresent, noop } from '../utils'; import { Subscription } from 'rxjs'; import { isChanged } from '@progress/kendo-angular-common'; import { NavigationService } from '../navigation/navigation.service'; import * as i0 from "@angular/core"; import * as i1 from "../treeview.component"; import * as i2 from "../navigation/navigation.service"; /** * A directive which manages the in-memory selection state of the TreeView node * ([see example]({% slug selection_treeview %})). */ export class SelectDirective { treeView; navigationService; /** * @hidden */ set isSelected(value) { this.treeView.isSelected = value; } /** * Defines the item key that will be stored in the `selectedKeys` collection. */ selectKey; /** * Defines the current selection mode * ([see example](slug:selection_treeview#toc-modes)). */ selection; /** * Defines the collection that will store the selected keys * ([see example](slug:selection_treeview#toc-modes)). */ selectedKeys; /** * Fires when the `selectedKeys` collection was updated. */ selectedKeysChange = new EventEmitter(); get getAriaMultiselectable() { return this.options.mode === 'multiple'; } subscriptions = new Subscription(); get options() { const defaultOptions = { enabled: true, mode: 'single' }; if (!isPresent(this.selection) || typeof this.selection === 'string') { return defaultOptions; } const selectionSettings = isBoolean(this.selection) ? { enabled: this.selection } : this.selection; return Object.assign(defaultOptions, selectionSettings); } selectActions = { 'multiple': (e) => this.selectMultiple(e), 'single': (e) => this.selectSingle(e) }; /** * Reflectes the internal `selectedKeys` state. */ state = new Set(); /** * Holds the last emitted `selectedKeys` collection. */ lastChange; constructor(treeView, navigationService) { this.treeView = treeView; this.navigationService = navigationService; this.subscriptions.add(this.treeView.selectionChange.subscribe(this.select.bind(this))); this.treeView.isSelected = (dataItem, index) => (this.state.has(this.itemKey({ dataItem, index }))); this.navigationService.deselectAllButCurrentItem.subscribe((node) => { this.selectSingle(node); }); } ngOnChanges(changes) { if (isChanged('selectedKeys', changes, false) && changes['selectedKeys'].currentValue !== this.lastChange) { this.state = new Set(changes['selectedKeys'].currentValue); } const isSelectionBooleanTrue = typeof this.selection === 'boolean' && this.selection; this.navigationService.selection = isSelectionBooleanTrue ? 'single' : this.selection?.mode; } ngOnDestroy() { this.subscriptions.unsubscribe(); } itemKey(e) { if (!this.selectKey) { return e.index; } if (typeof this.selectKey === 'string') { return e.dataItem[this.selectKey]; } if (typeof this.selectKey === 'function') { return this.selectKey(e); } } select(e) { const { enabled, mode } = this.options; const performSelection = this.selectActions[mode] || noop; if (!enabled) { return; } performSelection(e); } selectSingle(node) { const key = this.itemKey(node); if (!this.state.has(key)) { this.state.clear(); this.state.add(key); this.notify(); } } selectMultiple(node) { const key = this.itemKey(node); const isSelected = this.state.has(key); if (!isPresent(key)) { return; } if (isSelected) { this.state.delete(key); } else { this.state.add(key); } this.notify(); } notify() { this.lastChange = Array.from(this.state); this.selectedKeysChange.emit(this.lastChange); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SelectDirective, deps: [{ token: i1.TreeViewComponent }, { token: i2.NavigationService }], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: SelectDirective, isStandalone: true, selector: "[kendoTreeViewSelectable]", inputs: { isSelected: "isSelected", selectKey: ["selectBy", "selectKey"], selection: ["kendoTreeViewSelectable", "selection"], selectedKeys: "selectedKeys" }, outputs: { selectedKeysChange: "selectedKeysChange" }, host: { properties: { "attr.aria-multiselectable": "this.getAriaMultiselectable" } }, usesOnChanges: true, ngImport: i0 }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SelectDirective, decorators: [{ type: Directive, args: [{ selector: '[kendoTreeViewSelectable]', standalone: true }] }], ctorParameters: function () { return [{ type: i1.TreeViewComponent }, { type: i2.NavigationService }]; }, propDecorators: { isSelected: [{ type: Input }], selectKey: [{ type: Input, args: ['selectBy'] }], selection: [{ type: Input, args: ['kendoTreeViewSelectable'] }], selectedKeys: [{ type: Input }], selectedKeysChange: [{ type: Output }], getAriaMultiselectable: [{ type: HostBinding, args: ['attr.aria-multiselectable'] }] } });