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.stakeholder.topics.push(this.copyTopic);
109
        this.save('Topic has been successfully created', element);
110
      } else {
111
        this.stakeholder.topics[index] = HelperFunctions.copy(this.copyTopic);
112
        this.save('Topic has been successfully saved', element);
113
      }
114
    } else {
115
      this.valid = false;
116
    }
117
  }
118

    
119
  public deleteTopicOpen(element, index: number) {
120
    this.element = element;
121
    this.index = index;
122
    this.deleteTopicModal.alertHeader = true;
123
    this.deleteTopicModal.alertMessage = true;
124
    this.deleteTopicModal.cancelButton = true;
125
    this.deleteTopicModal.cancelButtonText = 'No';
126
    this.deleteTopicModal.okButtonText = 'Yes';
127
    this.deleteTopicModal.message = 'This topic will permanently be deleted. Are you sure you want to proceed?';
128
    this.deleteTopicModal.open();
129
  }
130

    
131
  private save(message: string, element) {
132
    this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => {
133
      this.stakeholderService.setStakeholder(stakeholder);
134
      UIkit.notification(message, {
135
        status: 'success',
136
        timeout: 3000,
137
        pos: 'top-left'
138
      });
139
      this.hide(element);
140
    }, error => {
141
      UIkit.notification(error.error.message, {
142
        status: 'danger',
143
        timeout: 3000,
144
        pos: 'top-left'
145
      });
146
      this.hide(element);
147
    });
148
  }
149

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