11import { Component , OnInit } from '@angular/core' ;
22import { ymlService } from '../../service/yaml-parser/yaml-parser.service' ;
33import { MatTableDataSource } from '@angular/material/table' ;
4+ import { COMMA , ENTER } from '@angular/cdk/keycodes' ;
5+ import { ElementRef , ViewChild } from '@angular/core' ;
6+ import { FormControl } from '@angular/forms' ;
7+ import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete' ;
8+ import { MatChipInputEvent } from '@angular/material/chips' ;
9+ import { Observable } from 'rxjs' ;
10+ import { map , startWith } from 'rxjs/operators' ;
411
512export interface MappingElement {
613 dimension : string ;
@@ -17,14 +24,31 @@ export interface MappingElement {
1724 styleUrls : [ './mapping.component.css' ]
1825} )
1926export class MappingComponent implements OnInit {
20- MAPPING_DATA :MappingElement [ ] = [ ]
21- dataSource :any = new MatTableDataSource < MappingElement > ( this . MAPPING_DATA ) ;
27+ allMappingData :MappingElement [ ] = [ ]
28+ plannedMappingData :MappingElement [ ] = [ ]
29+ performedMappingData :MappingElement [ ] = [ ]
30+
31+ dataSource :any = new MatTableDataSource < MappingElement > ( this . allMappingData ) ;
2232 YamlObject :any ;
2333
2434 displayedColumns : string [ ] = [ 'dimension' , 'subDimension' , 'taskName' , 'samm2' , 'ISO' ] ;
2535 allDimensionNames :string [ ] = [ ] ;
2636 temporaryMappingElement :any
27- constructor ( private yaml :ymlService ) { }
37+
38+ separatorKeysCodes : number [ ] = [ ENTER , COMMA ] ;
39+ chipCtrl = new FormControl ( '' ) ;
40+ filteredChips : Observable < string [ ] > ;
41+ currentChip : string [ ] = [ 'Performed Activities' , 'Planned Activities' ] ;
42+ allChips : string [ ] = [ 'Performed Activities' , 'Planned Activities' ] ;
43+
44+ @ViewChild ( 'chipInput' ) chipInput ! : ElementRef < HTMLInputElement > ;
45+
46+ constructor ( private yaml :ymlService ) {
47+ this . filteredChips = this . chipCtrl . valueChanges . pipe (
48+ startWith ( null ) ,
49+ map ( ( x : string | null ) => ( x ? this . _filter ( x ) : this . allChips . slice ( ) ) ) ,
50+ ) ;
51+ }
2852
2953 ngOnInit ( ) : void {
3054 //gets value from generated folder
@@ -44,6 +68,7 @@ export class MappingComponent implements OnInit {
4468 }
4569 // weird fix to render DOM for table viewing in angular material
4670 this . dataSource . _data . next ( this . dataSource . data ) ;
71+ this . allMappingData = this . dataSource . data
4772 } )
4873
4974 //this.dataSource=new MatTableDataSource([...this.dataSource]);
@@ -57,7 +82,46 @@ export class MappingComponent implements OnInit {
5782 this . temporaryMappingElement = { "dimension" :dim , "subDimension" :subDim , "taskName" :task , "ISO" :ISOArray , "samm2" :SAMMArray }
5883 //console.log(this.temp)
5984 this . dataSource . data . push ( this . temporaryMappingElement )
85+ if ( this . YamlObject [ dim ] [ subDim ] [ task ] [ 'isImplemented' ] ) {
86+ this . performedMappingData . push ( this . temporaryMappingElement )
87+ }
88+ else {
89+ this . plannedMappingData . push ( this . temporaryMappingElement )
90+ }
6091
6192 }
6293
94+ remove ( chip : string ) : void {
95+ const index = this . currentChip . indexOf ( chip ) ;
96+ //console.log(fruit)
97+ if ( index >= 0 ) {
98+ this . currentChip . splice ( index , 1 ) ;
99+ }
100+ }
101+
102+ selected ( event : MatAutocompleteSelectedEvent ) : void {
103+ this . currentChip . push ( event . option . viewValue ) ;
104+ if ( ( this . currentChip . length > 1 ) || ( this . currentChip . length == 0 ) ) { // both planned and performed actvities are selected
105+ this . dataSource . _data . next ( this . allMappingData ) ;
106+ }
107+ else {
108+ if ( this . currentChip [ 0 ] == 'Planned Activities' ) { // planned actvities shows planned data
109+ this . dataSource . _data . next ( this . plannedMappingData ) ;
110+ console . log ( this . plannedMappingData )
111+ }
112+ else {
113+ this . dataSource . _data . next ( this . performedMappingData ) ; // performed actvities shows performed data
114+ console . log ( this . performedMappingData )
115+ }
116+ }
117+
118+ this . chipInput . nativeElement . value = '' ;
119+ this . chipCtrl . setValue ( null ) ;
120+ }
121+
122+ private _filter ( value : string ) : string [ ] {
123+ const filterValue = value . toLowerCase ( ) ;
124+
125+ return this . allChips . filter ( chip => chip . toLowerCase ( ) . includes ( filterValue ) ) ;
126+ }
63127}
0 commit comments