UNPKG

@progress/kendo-angular-treelist

Version:

Kendo UI TreeList for Angular - Display hierarchical data in an Angular tree grid view that supports sorting, filtering, paging, and much more.

91 lines (90 loc) 4.56 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ import { ChangeDetectorRef, Directive, ElementRef, Renderer2 } from '@angular/core'; import { CheckBoxComponent } from '@progress/kendo-angular-inputs'; import { SelectionService } from '../../selection/selection.service'; import { Subscription } from 'rxjs'; import { isPresent } from '@progress/kendo-angular-common'; import { ViewCollection } from '../../data/data.collection'; import * as i0 from "@angular/core"; import * as i1 from "@progress/kendo-angular-inputs"; import * as i2 from "../../selection/selection.service"; /** * @hidden */ export class TreeListSelectAllCheckboxDirective { host; selectionService; cdr; sub = new Subscription(); setState = true; constructor(host, hostEl, selectionService, cdr, renderer) { this.host = host; this.selectionService = selectionService; this.cdr = cdr; this.sub = selectionService.changes.subscribe(eventData => { // omit setting the state when the selection change comes from clicking the checkbox, and clear the flag if (!this.setState) { this.setState = true; return; } const view = eventData.sender.view; let total; ViewCollection.loadView({ fields: Object.assign({}, view.fieldAccessor(), { hasFooter: false, pageable: false, isVirtual: false }), loaded: view.loaded, selectionState: view.selectionState }).subscribe(view => { if (!view) { return; } total = view.data.length; }); const allSelected = total === selectionService.state.length; const noneSelected = selectionService.state.length === 0; const someSelected = !noneSelected && !allSelected; if (allSelected && host.checkedState !== true) { host.checkedState = true; } else if (someSelected && host.checkedState !== 'indeterminate') { host.checkedState = 'indeterminate'; } else if (noneSelected && host.checkedState !== false) { host.checkedState = false; } cdr.detectChanges(); }); this.sub.add(selectionService.selectAllCheckedStateChange.subscribe((selectAllCheckedState) => { host.checkedState = selectAllCheckedState; cdr.detectChanges(); })); // set flag for conditional checkbox state logic ['pointerdown', 'keydown'].forEach(eventName => this.sub.add(renderer.listen(hostEl.nativeElement, eventName, (e) => { const isSpace = e.key && e.key === ' '; const spaceOrPointer = !isPresent(e.key) || isSpace; spaceOrPointer && (this.setState = false); }))); } ngAfterViewInit() { this.host.checkedState = this.selectionService.selectAllCheckedState; this.cdr.detectChanges(); } ngOnDestroy() { this.sub && this.sub.unsubscribe(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TreeListSelectAllCheckboxDirective, deps: [{ token: i1.CheckBoxComponent }, { token: i0.ElementRef }, { token: i2.SelectionService }, { token: i0.ChangeDetectorRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.14", type: TreeListSelectAllCheckboxDirective, isStandalone: true, selector: "[kendoTreeListSelectAllCheckbox]", ngImport: i0 }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TreeListSelectAllCheckboxDirective, decorators: [{ type: Directive, args: [{ selector: '[kendoTreeListSelectAllCheckbox]', standalone: true }] }], ctorParameters: () => [{ type: i1.CheckBoxComponent }, { type: i0.ElementRef }, { type: i2.SelectionService }, { type: i0.ChangeDetectorRef }, { type: i0.Renderer2 }] });