UNPKG

@progress/kendo-angular-grid

Version:

Kendo UI Grid for Angular - high performance data grid with paging, filtering, virtualization, CRUD, and more.

150 lines (149 loc) 6.36 kB
/**----------------------------------------------------------------------------------------- * 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, inject } from '@angular/core'; import { GridComponent } from '../grid.component'; import { LocalEditService } from './local-edit.service'; import { Subscription, Observable, Subject } from 'rxjs'; import { LocalDataChangesService } from '../editing/local-data-changes.service'; import { take } from 'rxjs/operators'; import { DialogService } from '@progress/kendo-angular-dialog'; import { checkIcon, xIcon } from '@progress/kendo-svg-icons'; import { LocalizationService } from '@progress/kendo-angular-l10n'; import * as i0 from "@angular/core"; import * as i1 from "../grid.component"; import * as i2 from "../editing/local-data-changes.service"; /** * @hidden */ export class EditingDirectiveBase { grid; localDataChangesService; // Consider adding support for the dependency injection of the service to allow for specifying a generic service without code. // The Input should still be kept. /** * The edit service that will handle the operations. */ set editService(value) { this.userEditService = value; } get editService() { return this.userEditService || this.defaultEditService; } removeConfirmationSubject = new Subject(); dialogService; localization; defaultRemoveConfirmation = (_item) => { const dialog = this.dialogService.open({ appendTo: this.grid.dialogContainer, title: this.localization.get('removeConfirmationDialogTitle'), content: this.localization.get('removeConfirmationDialogContent'), actions: [{ text: this.localization.get('removeConfirmationDialogConfirmText'), themeColor: 'primary', svgIcon: checkIcon, icon: 'check' }, { text: this.localization.get('removeConfirmationDialogRejectText'), svgIcon: xIcon, icon: 'x' }], }); dialog.result.pipe(take(1)).subscribe((e) => this.removeConfirmationSubject.next(e.text === this.localization.get('removeConfirmationDialogConfirmText'))); return this.removeConfirmationSubject; }; /** * A function that is called to confirm if the `dataItem` will be removed. * Setting to `true` will result in displaying a default confirmation dialog. */ removeConfirmation = false; subscriptions = new Subscription(); defaultEditService; userEditService; constructor(grid, localDataChangesService) { this.grid = grid; this.localDataChangesService = localDataChangesService; this.defaultEditService = this.createDefaultService(); this.dialogService = inject(DialogService); this.localization = inject(LocalizationService); } /** * @hidden */ ngOnInit() { this.subscriptions.add(this.grid.add.subscribe(this.addHandler.bind(this))); this.subscriptions.add(this.grid.remove.subscribe(this.removeHandler.bind(this))); this.subscriptions.add(this.grid.cancel.subscribe(this.cancelHandler.bind(this))); this.subscriptions.add(this.grid.save.subscribe(this.saveHandler.bind(this))); this.subscriptions.add(this.grid.dataStateChange.subscribe(this.onStateChange.bind(this))); } /** * @hidden */ ngOnDestroy() { this.subscriptions.unsubscribe(); } createDefaultService() { return new LocalEditService(this.grid, this.localDataChangesService); } addHandler(_args) { this.grid.addRow(this.createModel({ isNew: true })); } saveHandler(args) { const item = this.saveModel(args); if (item) { if (args.isNew) { this.editService.create(item); } else { this.editService.update(item); } } this.grid.closeRow(args.rowIndex); } cancelHandler({ rowIndex }) { this.closeEditor(rowIndex); } removeHandler({ dataItem }) { const removeItem = (shouldRemove) => { if (shouldRemove) { this.editService.remove(dataItem); } }; if (this.removeConfirmation) { const confirmationCallback = typeof this.removeConfirmation === 'boolean' ? this.defaultRemoveConfirmation : this.removeConfirmation; const result = confirmationCallback(dataItem); if (result instanceof Promise) { result.then(removeItem); } else if (result instanceof Observable) { result.pipe(take(1)).subscribe(removeItem); } else { removeItem(result); } } else { removeItem(true); } } onStateChange() { this.closeEditor(); } closeEditor(rowIndex) { this.grid.closeRow(rowIndex); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditingDirectiveBase, deps: [{ token: i1.GridComponent }, { token: i2.LocalDataChangesService }], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: EditingDirectiveBase, selector: "[kendoGridEditingDirectiveBase]", inputs: { editService: "editService", removeConfirmation: "removeConfirmation" }, ngImport: i0 }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditingDirectiveBase, decorators: [{ type: Directive, args: [{ selector: '[kendoGridEditingDirectiveBase]' }] }], ctorParameters: function () { return [{ type: i1.GridComponent }, { type: i2.LocalDataChangesService }]; }, propDecorators: { editService: [{ type: Input }], removeConfirmation: [{ type: Input }] } });