Project

General

Profile

1
import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
2
import {FormBuilder, FormGroup} from "@angular/forms";
3
import {Filter} from "./searchHelperClasses.class";
4
import {EnvProperties} from "../../utils/properties/env-properties";
5
import {ConfigurationService} from "../../utils/configuration/configuration.service";
6

    
7
@Component({
8
  selector: 'quick-selections',
9
  template: `
10
    <form *ngIf="resultTypes || quickFilter" [formGroup]="control" class="uk-text-small uk-margin-small-bottom">
11
      <div  *ngIf="quickFilter" class="uk-margin-small-top uk-margin-small-left">
12
        <span class="uk-text-bold">{{quickFilter.value}}</span>
13
        <mat-slide-toggle
14
                          class="uk-margin-small-left" formControlName="QFselected" (change)="quickFilterChanged()">
15
        </mat-slide-toggle>
16
      </div>
17
      <div *ngIf="resultTypes && showEntities" class="uk-margin-small-top uk-margin-small-left">
18
        <span class="uk-text-muted">Include: </span>
19
      <span>
20
        <span *ngIf="showPublications" class="uk-margin-small-left"> <input type="checkbox" id="publ" name="Publications" formControlName="publication" (change)="changed()"> Publications </span>
21
        <span *ngIf="showDatasets" class="uk-margin-small-left"> <input type="checkbox" formControlName="dataset" (change)="changed()"> Research Data </span>
22
        <span *ngIf="showSoftware" class="uk-margin-small-left"> <input type="checkbox" formControlName="software" (change)="changed()"> Software </span>
23
        <span *ngIf="showOrp" class="uk-margin-small-left"> <input type="checkbox" formControlName="other" (change)="changed()"> Other Research outcomes </span>
24
      </span>
25
      </div>
26
    </form>
27
  `
28
})
29

    
30
export class QuickSelectionsComponent implements OnChanges {
31
  @Input() resultTypes;
32
  @Output() typeChange = new EventEmitter();
33
  @Input() isDisabled;
34
  @Input() quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string };
35
  @Input() QFselected: boolean;
36
  control: FormGroup;
37
  initialized = false;
38
  @Input() properties: EnvProperties;
39
  showPublications:boolean= false;
40
  showDatasets:boolean= false;
41
  showSoftware: boolean = false;
42
  showOrp: boolean = false;
43
  showEntities = false;
44
  constructor(private _fb: FormBuilder, private config: ConfigurationService) {
45

    
46
    this.control = this._fb.group({
47
      publication: true,
48
      dataset: true,
49
      software: true,
50
      other: true,
51
      QFselected: true
52
    });
53

    
54

    
55
  }
56

    
57
  changed(typeChanged: boolean = true) {
58
    if (!this.initialized && this.isDisabled) {
59
      this.initialized = true;
60
      return;
61
    }
62
    let value = this.control.getRawValue();
63
    this.resultTypes.publication = value.publication;
64
    this.resultTypes.dataset = value.dataset;
65
    this.resultTypes.software = value.software;
66
    this.resultTypes.other = value.other;
67
    // this.resultTypes.open = value.open;
68
    if (typeChanged && this.resultTypes && !this.resultTypes.publication && !this.resultTypes.dataset && !this.resultTypes.software && !this.resultTypes.other) {
69
      this.resultTypes.publication = true;
70
      this.resultTypes.dataset = true;
71
      this.resultTypes.software = true;
72
      this.resultTypes.other = true;
73
      this.setFormValues();
74
    }
75
    this.typeChange.emit({});
76
  }
77

    
78
  quickFilterChanged() {
79
    let value = this.control.getRawValue();
80
    this.quickFilter.selected = value.QFselected;
81
    if(this.quickFilter.filter) {
82
      if (value.QFselected) {
83
        for (let filterValue of this.quickFilter.filter.values) {
84
          filterValue.selected = (filterValue.name == this.quickFilter.value)
85
        }
86
        this.quickFilter.filter.countSelectedValues = 1;
87
      } else {
88
        for (let filterValue of this.quickFilter.filter.values) {
89
          filterValue.selected = false;
90
        }
91
        this.quickFilter.filter.countSelectedValues = 0;
92
      }
93
    }
94
    this.typeChange.emit({});
95
  }
96

    
97
  ngOnInit() {
98
    if (this.resultTypes) {
99
      this.setFormValues();
100
    }
101
    if(this.properties) {
102
      this.config.getCommunityInformation(this.properties, this.properties.adminToolsCommunity).subscribe(data => {
103
        var showEntity = {};
104
        for (var i = 0; i < data['entities'].length; i++) {
105
          showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
106
        }
107
        this.showPublications = showEntity["publication"];
108
        this.showDatasets = showEntity["dataset"];
109
        this.showSoftware = showEntity["software"];
110
        this.showOrp = showEntity["orp"];
111
        this.showEntities = this.showPublications || this.showDatasets || this.showSoftware || this.showOrp;
112
      });
113
    }
114
  }
115

    
116
  ngOnChanges(changes: SimpleChanges): void {
117
    if (changes.isDisabled) {
118
      if (changes.isDisabled.currentValue == true) {
119
        this.control.disable();
120
      } else if (changes.isDisabled.currentValue == false) {
121
        this.control.enable();
122
      }
123
    }
124

    
125
    if (changes.QFselected) {
126
      let value = this.control.getRawValue();
127
      if (changes.QFselected.currentValue != value.QFselected) {
128
        this.setFormValues();
129
      }
130

    
131
    }
132
  }
133

    
134
  setFormValues() {
135
    this.control.setValue({
136
      publication: (this.resultTypes && this.resultTypes.publication)?this.resultTypes.publication:null,
137
      dataset: (this.resultTypes && this.resultTypes.dataset)?this.resultTypes.dataset:null,
138
      software: (this.resultTypes && this.resultTypes.software)?this.resultTypes.software:null,
139
      other: (this.resultTypes && this.resultTypes.other)?this.resultTypes.other:null,
140
      QFselected: this.QFselected
141
    });
142
  }
143

    
144

    
145
}
(23-23/54)