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 {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
10
import {StakeholderService} from "../services/stakeholder.service";
11
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
12
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
13
import {Subscriber} from "rxjs";
14

    
15
declare var UIkit;
16

    
17
@Component({
18
  selector: 'home',
19
  templateUrl: './home.component.html',
20
})
21
export class HomeComponent implements OnInit, OnDestroy {
22
  public subscriptions: any[] = [];
23
  public loading: boolean = true;
24
  public errorCodes: ErrorCodes;
25
  public stakeholder: Stakeholder;
26
  public analysisOpen: boolean = true;
27
  private errorMessages: ErrorMessagesComponent;
28
  public copyTopic: Topic;
29
  public valid = true;
30
  public element: any;
31
  public index: number;
32
  properties: EnvProperties;
33

    
34
  @ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
35

    
36
  constructor(
37
    private route: ActivatedRoute,
38
    private router: Router,
39
    private title: Title,
40
    private sideBarService: SideBarService,
41
    private stakeholderService: StakeholderService) {
42
    this.errorCodes = new ErrorCodes();
43
    this.errorMessages = new ErrorMessagesComponent();
44
  }
45

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

    
60
  public ngOnDestroy() {
61
    this.subscriptions.forEach(value => {
62
      if (value instanceof Subscriber) {
63
        value.unsubscribe();
64
      }
65
    });
66
  }
67

    
68
  public show(element) {
69
    UIkit.drop(element).show();
70
  }
71

    
72
  public hide(element) {
73
    this.valid = true;
74
    UIkit.drop(element).hide();
75
  }
76

    
77
  get open(): boolean {
78
    return this.sideBarService.open;
79
  }
80

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

    
89
  public saveTopicOpen(element, index = -1) {
90
    if (element.className.indexOf('uk-open') !== -1) {
91
      this.hide(element);
92
    } else {
93
      if (index === -1) {
94
        this.copyTopic = new Topic(null, null, null, true, true);
95
      } else {
96
        this.copyTopic = HelperFunctions.copy(this.stakeholder.topics[index]);
97
      }
98
      this.show(element);
99
    }
100
  }
101

    
102
  public saveTopic(element, index = -1) {
103
    if (this.copyTopic.name && this.copyTopic.name !== '') {
104
      if (!this.copyTopic.alias) {
105
        this.copyTopic.alias = this.copyTopic.name.toLowerCase().trim();
106
      }
107
      if (index === -1) {
108
        this.save('Topic has been successfully created', element);
109
      } else {
110
        this.save('Topic has been successfully saved', element, index);
111
      }
112
    } else {
113
      this.valid = false;
114
    }
115
  }
116

    
117
  public deleteTopicOpen(name: string, element, index: number) {
118
    this.element = element;
119
    this.index = index;
120
    this.deleteTopicModal.alertTitle = 'Delete ' + name;
121
    this.deleteTopicModal.cancelButtonText = 'No';
122
    this.deleteTopicModal.okButtonText = 'Yes';
123
    this.deleteTopicModal.message = 'This topic will permanently be deleted. Are you sure you want to proceed?';
124
    this.deleteTopicModal.open();
125
  }
126

    
127
  private save(message: string, element, index: number = -1) {
128
    let path = [this.stakeholder._id];
129
    this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.copyTopic, path).subscribe(topic => {
130
      if (index === -1) {
131
        this.stakeholder.topics.push(topic);
132
      } else {
133
        this.stakeholder.topics[index] = topic;
134
      }
135
      this.stakeholderService.setStakeholder(this.stakeholder);
136
      UIkit.notification(message, {
137
        status: 'success',
138
        timeout: 3000,
139
        pos: 'top-left'
140
      });
141
      this.hide(element);
142
    }, error => {
143
      UIkit.notification(error.error.message, {
144
        status: 'danger',
145
        timeout: 3000,
146
        pos: 'top-left'
147
      });
148
      this.hide(element);
149
    });
150
  }
151

    
152
  deleteTopic() {
153
    let path = [
154
      this.stakeholder._id,
155
      this.stakeholder.topics[this.index]._id
156
    ];
157
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
158
      this.stakeholder.topics.splice(this.index, 1);
159
      this.stakeholderService.setStakeholder(this.stakeholder);
160
      UIkit.notification('Topic has been successfully deleted', {
161
        status: 'success',
162
        timeout: 3000,
163
        pos: 'top-left'
164
      });
165
      this.hide(this.element);
166
    }, error => {
167
      UIkit.notification(error.error.message, {
168
        status: 'danger',
169
        timeout: 3000,
170
        pos: 'top-left'
171
      });
172
      this.hide(this.element);
173
    });
174
  }
175
}
(3-3/4)