UNPKG

@progress/kendo-angular-listview

Version:

Kendo UI Angular listview component

120 lines (119 loc) 3.94 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ import { Injectable, EventEmitter, NgZone } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { isPresent } from '../utils'; import { Subject } from 'rxjs'; import { switchMap, take } from 'rxjs/operators'; import * as i0 from "@angular/core"; /** * @hidden */ const isEqual = (index) => (item) => item.index === index; /** * @hidden */ const isNotEqual = (index) => (item) => item.index !== index; /** * @hidden */ const isNewItem = (index) => index === -1 || index === undefined; /** * @hidden */ export class EditService { ngZone; changes = new EventEmitter(); changed; editedIndices = []; newItem; changedSource = new Subject(); constructor(ngZone) { this.ngZone = ngZone; this.changed = this.changedSource.asObservable().pipe(switchMap(() => this.ngZone.onStable.asObservable().pipe(take(1)))); } editItem(index, group = undefined) { this.editedIndices.push({ index, group }); this.onChanged(); } addItem(group) { this.newItem = { group }; this.onChanged(); } isEditing() { return this.editedIndices.length > 0; } get hasNewItem() { return isPresent(this.newItem); } get newDataItem() { if (this.hasNewItem) { return this.newItem.group.value; } return {}; } get newItemGroup() { if (this.hasNewItem) { return this.newItem.group; } return new FormGroup({}); } editGroup(index) { return this.context(index).group; } close(index) { if (isNewItem(index)) { this.newItem = undefined; return; } this.editedIndices = this.editedIndices.filter(isNotEqual(index)); this.onChanged(); } context(index) { if (isNewItem(index)) { return this.newItem; } return this.findByIndex(index); } isEdited(index) { if (isNewItem(index) && isPresent(this.newItem)) { return true; } return isPresent(this.findByIndex(index)); } hasEdited(index) { return isPresent(this.context(index)); } beginEdit(itemIndex) { this.changes.emit({ action: 'edit', itemIndex }); } beginAdd() { this.changes.emit({ action: 'add' }); } endEdit(itemIndex) { const { group: formGroup } = this.context(itemIndex); this.changes.emit({ action: 'cancel', itemIndex, formGroup, isNew: isNewItem(itemIndex) }); } save(itemIndex) { const { group: formGroup } = this.context(itemIndex); this.changes.emit({ action: 'save', itemIndex, formGroup, isNew: isNewItem(itemIndex) }); } remove(itemIndex) { this.changes.emit({ action: 'remove', itemIndex }); } findByIndex(index) { return this.editedIndices.find(isEqual(index)); } onChanged() { this.ngZone.runOutsideAngular(() => { this.changedSource.next(); }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditService, deps: [{ token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditService }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: EditService, decorators: [{ type: Injectable }], ctorParameters: function () { return [{ type: i0.NgZone }]; } });