highlight.component.ts 2.29 KB
Newer Older
Laura Morel's avatar
Laura Morel committed
1
2
3
4
5
import { Component, OnInit, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { IDropdownSettings } from 'ng-multiselect-dropdown';

Laura Morel's avatar
Laura Morel committed
6
import { DataService } from '../../data.service';
Laura Morel's avatar
Laura Morel committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

@Component({
  selector: 'app-highlight',
  templateUrl: './highlight.component.html',
  styleUrls: ['./highlight.component.css']
})
export class HighlightComponent implements OnInit {
  @Input() formGroup: FormGroup;
  @Input() METADATA: Object;
  @Input() color: string;

  public fields: string[];
  public values: string[];
  private selectedValues: string[] = [];

  public fieldDropdownSettings: IDropdownSettings = {};
  public valuesDropdownSettings: IDropdownSettings = {};

  constructor(private dataService: DataService) { }

  ngOnInit(): void {
    this.fields = Object.keys(this.METADATA);
    console.log(this.fields)
    this.fieldDropdownSettings = {
      singleSelection: true,
      allowSearchFilter: true,
      closeDropDownOnSelection: true
    };
    this.valuesDropdownSettings = {
      singleSelection: false,
37
      allowSearchFilter: true,
Laura Morel's avatar
Laura Morel committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    }
  }

  getValues(e): void {
    let selected = e;
    console.log("Selected ", selected)
    this.formGroup.controls['field'].setValue(selected);
    this.dataService.get_fields(this.formGroup.value)
                    .subscribe(meta => {this.METADATA = meta});
    this.values = this.METADATA[selected]
  }

  addSelected(e): void {
    this.selectedValues.push(e);
  }

  removeSelected(e): void {
    let i=this.selectedValues.indexOf(e);
    console.log(e, " found at ", i, " in ", this.selectedValues)
    this.selectedValues.splice(i, 1);
    console.log(this.selectedValues)
  }

  onSelectValue(e): void {
    let selected = this.addSelected(e);
    console.log("Selected value ", e);
    this.formGroup.controls['values'].setValue(this.selectedValues);
    console.log("Currently selected ", this.formGroup.controls['values'])
  }

  onChangeColor(e): void {
    this.color = e;
    this.formGroup.controls['color'].setValue(e.substr(1));
    console.log("Changing color to ", e);
  }

  onDeSelectValue(e): void {
    let selected = this.removeSelected(e);
    console.log("Deselected value ", e);
    this.formGroup.controls['values'].setValue(this.selectedValues);
    console.log("Currently selected ", this.formGroup.controls['values'])
  }
}