Project

General

Profile

1
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Title} from '@angular/platform-browser';
4
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
5

    
6
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
7
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
8
import {Stakeholder, Topic} from "../utils/entities/stakeholder";
9
import {StakeholderService} from "../services/stakeholder.service";
10
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
11
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
12
import {Subscriber} from "rxjs";
13
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
14
import {LayoutService} from "../library/sharedComponents/sidebar/layout.service";
15
import {IndicatorUtils} from "../utils/indicator-utils";
16

    
17
declare var UIkit;
18

    
19
@Component({
20
  selector: 'stakeholder',
21
  templateUrl: './stakeholder.component.html',
22
})
23
export class StakeholderComponent implements OnInit, OnDestroy {
24
  public subscriptions: any[] = [];
25
  public loading: boolean = true;
26
  public errorCodes: ErrorCodes;
27
  public stakeholder: Stakeholder;
28
  public analysisOpen: boolean = true;
29
  private errorMessages: ErrorMessagesComponent;
30
  public topicFb: FormGroup;
31
  public indicatorUtils: IndicatorUtils = new IndicatorUtils();
32
  public element: any;
33
  public index: number;
34
  properties: EnvProperties;
35

    
36
  @ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
37

    
38
  constructor(
39
    private route: ActivatedRoute,
40
    private router: Router,
41
    private title: Title,
42
    private layoutService: LayoutService,
43
    private fb: FormBuilder,
44
    private stakeholderService: StakeholderService) {
45
    this.errorCodes = new ErrorCodes();
46
    this.errorMessages = new ErrorMessagesComponent();
47
  }
48

    
49
  public ngOnInit() {
50
    this.route.data
51
      .subscribe((data: { envSpecific: EnvProperties }) => {
52
        this.properties = data.envSpecific;
53
        this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
54
          if (stakeholder) {
55
            this.stakeholder = HelperFunctions.copy(stakeholder);
56
            this.topicFb = null;
57
            this.title.setTitle(stakeholder.index_name);
58
          }
59
        }));
60
      });
61
  }
62

    
63
  public ngOnDestroy() {
64
    this.subscriptions.forEach(value => {
65
      if (value instanceof Subscriber) {
66
        value.unsubscribe();
67
      }
68
    });
69
  }
70

    
71
  public show(element) {
72
    UIkit.drop(element).show();
73
  }
74

    
75
  public hide(element) {
76
    UIkit.drop(element).hide();
77
  }
78

    
79
  get open(): boolean {
80
    return this.layoutService.open;
81
  }
82

    
83
  public toggleOpen(event = null) {
84
    if (!event) {
85
      this.layoutService.setOpen(!this.open);
86
    } else if (event && event['value'] === true) {
87
      this.layoutService.setOpen(false);
88
    }
89
  }
90

    
91
  private buildTopic(topic: Topic) {
92
    this.topicFb = this.fb.group({
93
      _id: this.fb.control(topic._id),
94
      name: this.fb.control(topic.name, Validators.required),
95
      description: this.fb.control(topic.description),
96
      alias: this.fb.control(topic.alias),
97
      isActive: this.fb.control(topic.isActive),
98
      isPublic: this.fb.control(topic.isPublic),
99
      isDefault: this.fb.control(topic.isDefault),
100
      categories: this.fb.control(topic.categories)
101
    });
102
  }
103

    
104
  public saveTopicOpen(element, index = -1) {
105
    if (element.className.indexOf('uk-open') !== -1) {
106
      this.hide(element);
107
    } else {
108
      if (index === -1) {
109
        this.buildTopic(new Topic(null, null, null, true, true, false));
110
      } else {
111
        this.buildTopic(this.stakeholder.topics[index]);
112
      }
113
      this.show(element);
114
    }
115
  }
116

    
117
  public saveTopic(element, index = -1) {
118
    if (!this.topicFb.invalid) {
119
      if (!this.topicFb.value.alias) {
120
        this.topicFb.value.alias = this.topicFb.value.name.toLowerCase().trim();
121
      }
122
      if (index === -1) {
123
        this.save('Topic has been successfully created', element);
124
      } else {
125
        this.save('Topic has been successfully saved', element, index);
126
      }
127
    }
128
  }
129

    
130
  public deleteTopicOpen(name: string, element, index: number) {
131
    this.element = element;
132
    this.index = index;
133
    this.deleteTopicModal.alertTitle = 'Delete ' + name;
134
    this.deleteTopicModal.cancelButtonText = 'No';
135
    this.deleteTopicModal.okButtonText = 'Yes';
136
    this.deleteTopicModal.message = 'This topic will permanently be deleted. Are you sure you want to proceed?';
137
    this.deleteTopicModal.open();
138
  }
139

    
140
  private save(message: string, element, index: number = -1) {
141
    let path = [this.stakeholder._id];
142
    this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.topicFb.value, path).subscribe(topic => {
143
      if (index === -1) {
144
        this.stakeholder.topics.push(topic);
145
      } else {
146
        this.stakeholder.topics[index] = topic;
147
      }
148
      this.stakeholderService.setStakeholder(this.stakeholder);
149
      UIkit.notification(message, {
150
        status: 'success',
151
        timeout: 3000000,
152
        pos: 'top-left'
153
      });
154
      this.hide(element);
155
    }, error => {
156
      UIkit.notification(error.error.message, {
157
        status: 'danger',
158
        timeout: 3000,
159
        pos: 'top-left'
160
      });
161
      this.hide(element);
162
    });
163
  }
164

    
165
  deleteTopic() {
166
    let path = [
167
      this.stakeholder._id,
168
      this.stakeholder.topics[this.index]._id
169
    ];
170
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
171
      this.stakeholder.topics.splice(this.index, 1);
172
      this.stakeholderService.setStakeholder(this.stakeholder);
173
      UIkit.notification('Topic has been successfully deleted', {
174
        status: 'success',
175
        timeout: 3000,
176
        pos: 'top-left'
177
      });
178
      this.hide(this.element);
179
    }, error => {
180
      UIkit.notification(error.error.message, {
181
        status: 'danger',
182
        timeout: 3000,
183
        pos: 'top-left'
184
      });
185
      this.hide(this.element);
186
    });
187
  }
188
}
(3-3/4)