@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.
138 lines (137 loc) • 4.96 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Input, Directive } from '@angular/core';
import { Subscription, Observable, merge } from 'rxjs';
import { take } from 'rxjs/operators';
import { TreeListComponent } from '../treelist.component';
import * as i0 from "@angular/core";
import * as i1 from "../treelist.component";
/**
* @hidden
*/
export class EditingDirectiveBase {
treelist;
/**
* The edit service that handles the operations.
*/
set editService(value) {
this.userEditService = value;
}
get editService() {
return this.userEditService || this.defaultEditService;
}
/**
* Gets or sets a function that determines the unique identifier
* for new items. The function receives the `item` and its `parent` as parameters
* and must return an ID.
*/
set newItemId(callback) {
this.idCallback = callback;
}
get newItemId() {
return this.idCallback;
}
/**
* A function that confirms if the `dataItem` will be removed.
*/
removeConfirmation;
subscriptions = new Subscription();
userEditService;
isNew;
parent;
dataItem;
idCallback;
constructor(treelist) {
this.treelist = treelist;
}
/**
* @hidden
*/
ngOnInit() {
this.subscriptions.add(this.treelist.add.subscribe(this.addHandler.bind(this)));
this.subscriptions.add(this.treelist.remove.subscribe(this.removeHandler.bind(this)));
this.subscriptions.add(this.treelist.cancel.subscribe(this.cancelHandler.bind(this)));
this.subscriptions.add(this.treelist.save.subscribe(this.saveHandler.bind(this)));
this.subscriptions.add(merge(this.treelist.dataStateChange, this.treelist.pageChange).subscribe(this.onStateChange.bind(this)));
}
/**
* @hidden
*/
ngOnDestroy() {
this.subscriptions.unsubscribe();
}
get defaultEditService() {
return this.treelist.localEditService;
}
addHandler({ parent }) {
this.parent = parent;
this.isNew = true;
if (parent) {
this.treelist.expand(parent);
}
this.treelist.addRow(this.createModel({ isNew: true }), parent);
}
saveHandler(args) {
const item = this.saveModel(args);
if (item) {
if (args.isNew) {
this.editService.create(item, args.parent, this.idCallback ? this.idCallback(item, args.parent) : null);
}
else {
this.editService.update(item);
}
}
this.treelist.closeRow(args.dataItem, args.isNew);
}
cancelHandler(args) {
this.closeEditor(args);
}
removeHandler({ dataItem, parent }) {
const removeItem = (shouldRemove) => {
if (shouldRemove) {
this.editService.remove(dataItem, parent);
}
};
if (this.removeConfirmation) {
const result = this.removeConfirmation(dataItem, parent);
if (result instanceof Promise) {
result.then(removeItem);
}
else if (result instanceof Observable) {
result.pipe(take(1)).subscribe(removeItem);
}
else {
removeItem(result);
}
}
else {
removeItem(true);
}
}
closeEditor(args = { dataItem: this.dataItem, isNew: this.isNew }) {
this.treelist.closeRow(args.dataItem, args.isNew);
this.clean();
}
clean() {
this.isNew = false;
this.dataItem = null;
this.parent = null;
}
onStateChange() {
this.closeEditor();
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditingDirectiveBase, deps: [{ token: i1.TreeListComponent }], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: EditingDirectiveBase, inputs: { editService: "editService", newItemId: "newItemId", removeConfirmation: "removeConfirmation" }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditingDirectiveBase, decorators: [{
type: Directive,
args: [{}]
}], ctorParameters: function () { return [{ type: i1.TreeListComponent }]; }, propDecorators: { editService: [{
type: Input
}], newItemId: [{
type: Input
}], removeConfirmation: [{
type: Input
}] } });