import { Component, Input, OnInit, OnDestroy } from '@angular/core'; import { PermissionsService, Entity2textPipe, BomColumnId, BomCustomColum, BomService, Utils } from '@arrow/bom/core'; import { Subject } from 'rxjs'; import { take, takeUntil } from 'rxjs/operators'; const NUMBERS_ONLY = '^([0-9]\\d*)?(\\.\\d+)?$'; @Component({ selector: 'bom-custom-column', templateUrl: 'custom-column.component.html', styleUrls: ['custom-column.component.scss'], providers: [Entity2textPipe] }) export class CustomColumnComponent implements OnInit, OnDestroy { @Input() column: BomCustomColum; @Input() rowId: number; @Input() isLoading: boolean; @Input() partName; columnName: string; placeholder: string; toPartText: string; permissions: any = {}; disableUpdateBtn: boolean; private unsubscribe$ = new Subject(); constructor( private bomService: BomService, private permissionService: PermissionsService, private utils: Utils, private e2t: Entity2textPipe ) {} ngOnInit(): void { if (this.column) { this.columnName = this.column.name; this.placeholder = this.utils.translateByKeyWithFallback("add-" + this.column.nameId, this.columnName); this.toPartText = this.utils.translateByKeyWithFallback("to-part-text"); this.permissionService .getPermissions() .pipe(takeUntil(this.unsubscribe$)) .subscribe((permissions: any) => (this.permissions = permissions)); } } ngOnDestroy() { this.unsubscribe$.next(); this.unsubscribe$.complete(); } validateTargetPrice(targetPrice: string, columnId: number) { this.disableUpdateBtn = !!( columnId === BomColumnId.TARGET_PRICE && !targetPrice.match(NUMBERS_ONLY) ); } /** * check column comment and previous value * if those are diff then replace column value * @param comment * @param column * @param rowId */ updateComment(comment: string, column: any, rowId: number) { if (typeof comment !== 'string' || column.value === comment) return; column.value = comment; // send new comment to the server this.bomService .setColumnComment(rowId, column.mapId, comment) .pipe(take(1)) .subscribe(); } // function to set dynamic aria-label to warpaint edit buttons setInputAriaLabel(columnId) { switch (columnId) { case BomColumnId.TARGET_PRICE: return this.utils.translateByKeyWithFallback("add-target-price-btn") + this.partName; case BomColumnId.NOTE: return this.utils.translateByKeyWithFallback("add-notes-btn") + this.partName; case BomColumnId.REFERENCE_DESIGNATOR: return this.utils.translateByKeyWithFallback("add-reference-btn") + this.partName; default: return this.placeholder +'to part'+ this.partName; } } // function to set dynamic aria-label to warpaint edit buttons setBtnAriaLabel(columnId) { switch (columnId) { case BomColumnId.TARGET_PRICE: return this.utils.translateByKeyWithFallback("edit-target-price-btn") + this.partName; case BomColumnId.NOTE: return this.utils.translateByKeyWithFallback("edit-notes-btn") + this.partName; case BomColumnId.REFERENCE_DESIGNATOR: return this.utils.translateByKeyWithFallback("edit-reference-btn") + this.partName; default: return this.placeholder +'to part'+ this.partName; } } }