@progress/kendo-angular-grid
Version:
Kendo UI Grid for Angular - high performance data grid with paging, filtering, virtualization, CRUD, and more.
121 lines (120 loc) • 4.76 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 { GridComponent } from '../grid.component';
import { LocalEditService } from './local-edit.service';
import { Subscription, Observable } from 'rxjs';
import { LocalDataChangesService } from '../editing/local-data-changes.service';
import { take } from 'rxjs/operators';
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;
}
/**
* A function that is called to confirm if the `dataItem` will be removed.
*/
removeConfirmation;
subscriptions = new Subscription();
defaultEditService;
userEditService;
constructor(grid, localDataChangesService) {
this.grid = grid;
this.localDataChangesService = localDataChangesService;
this.defaultEditService = this.createDefaultService();
}
/**
* @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() {
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 result = this.removeConfirmation(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
}] } });