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(name: string, element, index: number) {
120
    this.element = element;
121
    this.index = index;
122
    this.deleteTopicModal.alertTitle = 'Delete ' + name;
123
    this.deleteTopicModal.cancelButtonText = 'No';
124
    this.deleteTopicModal.okButtonText = 'Yes';
125
    this.deleteTopicModal.message = 'This topic will permanently be deleted. Are you sure you want to proceed?';
126
    this.deleteTopicModal.open();
127
  }
128

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

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