import { Component, Inject } from '@angular/core'; import { WPT_DIALOG_DATA, WptDialogRef } from '@arrow/warpaint/dialog'; export interface MoqDialogData { isAutoRound: boolean; items: any; totalParts: number; } @Component({ selector: 'bom-moq-modal', templateUrl: 'moq-modal.component.html', styleUrls: ['moq-modal.component.scss'] }) export class MOQModalComponent { public columnHeaders = [ 'select', 'partNumber', 'qtyRequested', 'minMultiple', 'proposedChange' ]; public entryData; public checkAll: boolean = true; public totalSelected: number; public rowsDataSource; public totalItems: number; public paginatorTotalPages: number; public paginatorItemsPerPage = 5; public paginatorCurrentPage = 1; public bomTotalParts: number; public isAutoRound: boolean; constructor( @Inject(WPT_DIALOG_DATA) public data: MoqDialogData, public dialogRef: WptDialogRef ) { this.isAutoRound = this.data.isAutoRound; this.rowsDataSource = [this.data.items]; this.entryData = this.data.items.map(this.addSelectedProperty); this.bomTotalParts = this._getTotalPartSources(); this.rowsDataSource = this.entryData.slice(0, 5); this.totalItems = this.entryData.length; this.totalSelected = this.totalItems; this.paginatorTotalPages = Math.ceil(this.entryData.length / 5); } _getTotalPartSources():number{ let total = this.data.totalParts; const evalRowIds: String[] = []; this.entryData.forEach(element => { if(!this._contains(evalRowIds, element.rowId)){ const count = this.entryData.filter(ed=>ed.rowId === element.rowId).length; if(count > 1){ total += (count - 1); } evalRowIds.push(element.rowId); } }); return total; } //temporary while we support ES2017 for build:lib _contains(arr: any, id: any):boolean{ return arr.includes(id); } changeAllSelectState(checkAll: boolean) { checkAll ? this.changeStates(false) : this.changeStates(true); } changeStates(state: boolean) { state ? (this.totalSelected = this.totalItems) : (this.totalSelected = 0); this.entryData.forEach(element => { element.isSelected = state; }); } changeUniqueSelectState(element) { element.isSelected ? this.totalSelected-- : this.totalSelected++; this.checkAll = this.totalSelected > 0; } updatePage(page: any) { this.rowsDataSource = this.entryData.slice( page.pageIndex * 5, (page.pageIndex + 1) * 5 ); this.paginatorCurrentPage = page.pageIndex + 1; } addSelectedProperty(value) { return { ...value, isSelected: true }; } save(): void { if (this.isAutoRound) { this.dialogRef.close( this.entryData.filter(el => el.isSelected).map(el => { return {rowId: el.rowId, sourcePartId: el.sourcePartId} }) ); } else { this.dialogRef.close( this.entryData.map(el =>{return {rowId: el.rowId, sourcePartId: el.sourcePartId, isSelected: el.isSelected}}) ); } } cancel(): void { this.dialogRef.close(); } }