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

    
14
declare var UIkit;
15

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

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

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

    
46
  public ngOnInit() {
47
    this.route.data
48
      .subscribe((data: { envSpecific: EnvProperties }) => {
49
        this.properties = data.envSpecific;
50
        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
  }
62

    
63
  public show(element) {
64
    UIkit.drop(element).show();
65
  }
66

    
67
  public hide(element) {
68
    UIkit.drop(element).hide();
69
  }
70

    
71
  get open(): boolean {
72
    return this.sideBarService.open;
73
  }
74

    
75
  public toggleOpen(event = null) {
76
    if (!event) {
77
      this.sideBarService.setOpen(!this.open);
78
    } else if (event && event['value'] === true) {
79
      this.sideBarService.setOpen(false);
80
    }
81
  }
82

    
83
  public saveTopicOpen(element, index = -1) {
84
    if(index === -1) {
85
      this.copyTopic = new Topic(null, null, null,true, true);
86
    } else {
87
      this.copyTopic = HelperFunctions.copy(this.stakeholder.topics[index]);
88
    }
89
    this.show(element);
90
    this.valid = true;
91
  }
92

    
93
  public saveTopic(element, index = -1) {
94
    if(this.copyTopic.name && this.copyTopic.name !== ''
95
      && this.copyTopic.description && this.copyTopic.description !== '') {
96
      if(!this.copyTopic.alias) {
97
        this.copyTopic.alias = this.copyTopic.name.toLowerCase().trim();
98
      }
99
      if(index === -1) {
100
        this.stakeholder.topics.push(this.copyTopic);
101
        this.save('Topic has been successfully created', element);
102
      } else {
103
        this.stakeholder.topics[index] = HelperFunctions.copy(this.copyTopic);
104
        this.save('Topic has been successfully saved', element);
105
      }
106
    } else {
107
      this.valid = false;
108
    }
109
  }
110

    
111
  public deleteTopicOpen(element, index: number) {
112
    this.element = element;
113
    this.index = index;
114
    this.deleteTopicModal.alertHeader = true;
115
    this.deleteTopicModal.alertMessage = true;
116
    this.deleteTopicModal.cancelButton = true;
117
    this.deleteTopicModal.cancelButtonText = 'No';
118
    this.deleteTopicModal.okButtonText = 'Yes';
119
    this.deleteTopicModal.message = 'This topic will permanently be deleted. Are you sure you want to proceed?';
120
    this.deleteTopicModal.open();
121
  }
122

    
123
  public navigateTo(alias: string) {
124
    this.router.navigate(['./' + alias], {
125
      relativeTo: this.route
126
    });
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)