| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { Component, OnDestroy, OnInit } from '@angular/core'; |
| import { Router } from '@angular/router'; |
| import { GRID_FAILURE_COLDEF } from '@grid-failure-information-app/pages/grid-failure/grid-failure-list/grid-failure-list-column-definition'; |
| import { GRID_FAILURE_FOR_CONDENSATION_COLDEF } from '@grid-failure-information-app/app/pages/grid-failure/grid-failure-list/grid-failure-list-for-condensation-column-definition'; |
| import { GridFailureSandbox } from '@grid-failure-information-app/pages/grid-failure/grid-failure-list/grid-failure.sandbox'; |
| import { BaseList } from '@grid-failure-information-app/shared/components/base-components/base.list'; |
| import { HeaderCellRendererComponent } from '@grid-failure-information-app/shared/components/cell-renderer/header-cell-renderer/header-cell-renderer.component'; |
| import { ModeEnum, EventTypeEnum, StateEnum } from '@grid-failure-information-app/shared/constants/enums'; |
| import { SetFilterComponent } from '@grid-failure-information-app/shared/filters/ag-grid/set-filter/set-filter.component'; |
| import { UtilService } from '@grid-failure-information-app/shared/utility'; |
| import { GridApi, GridOptions, IFilterComp, IFilterParams } from 'ag-grid-community'; |
| import { Subject } from 'rxjs'; |
| import { takeUntil, take } from 'rxjs/operators'; |
| import { Globals } from '@grid-failure-information-app/shared/constants/globals'; |
| import { RolesEnum } from '@grid-failure-information-app/shared/constants/enums'; |
| import { GridFailure } from '@grid-failure-information-app/shared/models'; |
| import { MapOptions } from '@openk-libs/grid-failure-information-map/shared/models/map-options.model'; |
| import * as store from '@grid-failure-information-app/shared/store'; |
| import { Store } from '@ngrx/store'; |
| |
| @Component({ |
| selector: 'app-grid-failure-list', |
| templateUrl: './grid-failure-list.component.html', |
| styleUrls: ['./grid-failure-list.component.scss'], |
| }) |
| export class GridFailureListComponent extends BaseList implements OnInit, OnDestroy { |
| public Globals = Globals; |
| public RolesEnum = RolesEnum; |
| public overviewColumnDefinition: any = GRID_FAILURE_COLDEF; |
| public condensationColumnDefinition: any = GRID_FAILURE_FOR_CONDENSATION_COLDEF; |
| public mapOptions: MapOptions = new MapOptions(); |
| public frameworkComponents: any; |
| public view: any = 'list'; |
| public showCondensationTable: boolean = false; |
| public enableSelectionMode: boolean = false; |
| public condensationEvents$: Subject<any> = new Subject(); |
| public gridOptionsCondensation: GridOptions = { |
| context: { |
| eventSubject: this.condensationEvents$, |
| }, |
| suppressLoadingOverlay: true, |
| localeText: Globals.LOCALE_TEXT, |
| }; |
| |
| private _gridApi: GridApi; |
| private _modeEnum = ModeEnum; |
| private _eventTypeEnum = EventTypeEnum; |
| |
| constructor(public sandbox: GridFailureSandbox, private appState$: Store<store.State>, private _router: Router, private _utilService: UtilService) { |
| super(); |
| this.frameworkComponents = { setFilterComponent: SetFilterComponent, headerCellRendererComponent: HeaderCellRendererComponent }; |
| } |
| |
| ngOnInit(): void { |
| this._setInitialGridOptions(); |
| this.appState$.select(store.getPreConfiguration).subscribe(mapConfig => { |
| this.mapOptions = new MapOptions(mapConfig); |
| }); |
| } |
| |
| ngOnDestroy(): void { |
| this._endSubscriptions$.next(true); |
| this.sandbox.endSubscriptions(); |
| this.clearGridFailureCondensation(); |
| } |
| |
| public initGridFilter(params): void { |
| this._gridApi = params.api; |
| if (this.sandbox.publisherFilterIsActive) { |
| this.changeFilter(true, RolesEnum.PUBLISHER); |
| } else if (this.sandbox.qualifierFilterIsActive) { |
| this.changeFilter(true, RolesEnum.QUALIFIER); |
| } else { |
| this.sandbox.gridFailureListSuccess$ && |
| this.sandbox.gridFailureListSuccess$.pipe(take(1), takeUntil(this._endSubscriptions$)).subscribe(() => { |
| setTimeout(() => { |
| this._gridApi.setFilterModel(this.sandbox.filterOptions); |
| }, 100); |
| }); |
| } |
| } |
| |
| public changeFilter(checked: boolean, userRole: string): void { |
| this.overviewColumnDefinition = this.overviewColumnDefinition.map(item => { |
| const fieldValueOfColDef: string = item['field']; |
| if (!!fieldValueOfColDef && fieldValueOfColDef === Globals.STATUS_INTERN_FIELD) { |
| if (userRole === RolesEnum.PUBLISHER) { |
| this.sandbox.publisherFilterIsActive = checked; |
| } else if (userRole === RolesEnum.QUALIFIER) { |
| this.sandbox.qualifierFilterIsActive = checked; |
| } |
| const filterInstance = this._gridApi.getFilterInstance(fieldValueOfColDef); |
| const filterModel = filterInstance.getModel(); |
| const filterModelAll = this.sandbox.filterOptions ? this.sandbox.filterOptions : {}; |
| if (checked) { |
| this._getFilterModelForQuickFilter(filterModel, userRole); |
| this._setFilterModelForQuickFilter(filterModelAll, fieldValueOfColDef, filterModel, 1500); |
| } else { |
| this._resetFilterModelForQuickFilter(filterModel); |
| this._setFilterModelForQuickFilter(filterModelAll, fieldValueOfColDef, filterModel); |
| } |
| return { ...item, suppressMenu: checked }; |
| } |
| return item; |
| }); |
| } |
| |
| private _setFilterModelForQuickFilter(filterModelAll: any, filterName: string, filterModel: any, msDelayToSetFilterModel: number = 0) { |
| filterModelAll[filterName] = filterModel; |
| this.sandbox.filterOptions = filterModelAll; |
| setTimeout(() => { |
| this._gridApi.setFilterModel(filterModelAll); |
| }, msDelayToSetFilterModel); |
| this._gridApi.onFilterChanged(); |
| } |
| |
| private _resetFilterModelForQuickFilter(filterModel: any) { |
| const filterValues = filterModel['values']; |
| for (const valueKey in filterValues) { |
| filterValues[valueKey]['checked'] = true; |
| } |
| } |
| |
| private _getFilterModelForQuickFilter(filterModel: any, userRole: string) { |
| const filterValues = filterModel['values']; |
| for (const valueKey in filterValues) { |
| switch (userRole) { |
| case RolesEnum.QUALIFIER: |
| filterValues[valueKey]['checked'] = valueKey === StateEnum.CREATED || valueKey === StateEnum.PLANNED || valueKey === StateEnum.UPDATED; |
| break; |
| case RolesEnum.PUBLISHER: |
| filterValues[valueKey]['checked'] = valueKey === StateEnum.QUALIFIED || valueKey === StateEnum.PUBLISHED; |
| break; |
| } |
| } |
| } |
| |
| public clearGridFailureCondensation(): void { |
| this.sandbox.clearGridFailureCondensation(); |
| this.showCondensationTable = false; |
| this.enableSelectionMode = false; |
| this._changeMode(); |
| } |
| |
| public condenseChoosedGridFailureInformations(): void { |
| if (this.sandbox.condensationList.length != 0) { |
| this.sandbox.condenseCondensationList(); |
| this.clearGridFailureCondensation(); |
| } else { |
| this._utilService.displayNotification('EmptyListWarning', 'alert'); |
| } |
| } |
| |
| public addCompleteTable(): void { |
| let isNotificationAlreadyShown: boolean = false; |
| this._gridApi.forEachNodeAfterFilter(node => { |
| if ((this.sandbox.condensationList.length == 0 || node.data.branch === this.sandbox.condensationList[0].branch) && !node.data.condensed) { |
| this.sandbox.addItemToCondensationList(node.data); |
| } else if (node.data.condensed) { |
| return; |
| } else { |
| if (!isNotificationAlreadyShown) this._utilService.displayNotification('DifferentBranchesWarning', 'alert'); |
| isNotificationAlreadyShown = true; |
| } |
| }); |
| } |
| |
| public changeToSelectionMode(): void { |
| this.showCondensationTable = true; |
| this.enableSelectionMode = true; |
| this._changeMode(); |
| } |
| |
| public loadCondensedGridFailures(id: string): void { |
| this.sandbox.loadCondensedGridFailures(id); |
| this.showCondensationTable = true; |
| this.enableSelectionMode = false; |
| this._changeMode(); |
| } |
| public navigateToDetails(id: string) { |
| this._router.navigate(['/grid-failures', id]); |
| } |
| |
| public setMapFilter() { |
| // set filtered list |
| const filteredGridFailureMapList: GridFailure[] = []; |
| this._gridApi.forEachNodeAfterFilter(node => { |
| filteredGridFailureMapList.push(new GridFailure(node.data)); |
| }); |
| this.sandbox.setFilteredGridFailureMapList(filteredGridFailureMapList); |
| this.sandbox.filterOptions = this._gridApi.getFilterModel(); |
| } |
| |
| private _setInitialGridOptions(): void { |
| this.gridOptions.context = { ...this.gridOptions.context, icons: { readonly: this.sandbox.permissions.reader } }; |
| this.gridOptions = { |
| ...this.gridOptions, |
| onFilterChanged: this.setMapFilter.bind(this), |
| }; |
| this.gridOptions.context.eventSubject.pipe(takeUntil(this._endSubscriptions$)).subscribe(event => { |
| switch (event.type) { |
| case this._eventTypeEnum.Edit: |
| case this._eventTypeEnum.Readonly: |
| this._router.navigate(['/grid-failures', event.data.id]); |
| break; |
| case this._eventTypeEnum.Add: |
| if ((this.sandbox.condensationList.length == 0 || event.data.branch === this.sandbox.condensationList[0].branch) && !event.data.condensed) { |
| this.sandbox.addItemToCondensationList(event.data); |
| } else if (event.data.condensed) { |
| break; |
| } else { |
| this._utilService.displayNotification('DifferentBranchesWarning', 'alert'); |
| } |
| break; |
| case this._eventTypeEnum.AddAllItems: |
| this.addCompleteTable(); |
| break; |
| case this._eventTypeEnum.InitialLoad: |
| this._changeMode(); |
| break; |
| case this._eventTypeEnum.LoadCondensedItems: |
| this.loadCondensedGridFailures(event.data.id); |
| break; |
| |
| default: |
| break; |
| } |
| }); |
| |
| this.gridOptionsCondensation.context = { ...this.gridOptionsCondensation.context, icons: { readonly: this.sandbox.permissions.reader } }; |
| this.gridOptionsCondensation.context.eventSubject.pipe(takeUntil(this._endSubscriptions$)).subscribe(event => { |
| switch (event.type) { |
| case this._eventTypeEnum.Remove: |
| this.sandbox.removeItemFromCondensationList(event.data); |
| break; |
| case this._eventTypeEnum.Edit: |
| case this._eventTypeEnum.Readonly: |
| this._router.navigate(['/grid-failures', event.data.id]); |
| break; |
| case this._eventTypeEnum.InitialLoad: |
| this._changeMode(); |
| break; |
| default: |
| break; |
| } |
| }); |
| } |
| |
| private _changeMode(): void { |
| this.enableSelectionMode |
| ? this.condensationEvents$.next({ eventType: this._modeEnum.CondensationTableSelectionMode }) |
| : this.condensationEvents$.next({ eventType: this._modeEnum.InitialMode }); |
| this.enableSelectionMode |
| ? this.events$.next({ eventType: this._modeEnum.OverviewTableSelectionMode }) |
| : this.events$.next({ eventType: this._modeEnum.InitialMode }); |
| } |
| } |