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

    
16
declare var UIkit;
17

    
18
@Component({
19
  selector: 'home',
20
  templateUrl: './home.component.html',
21
})
22
export class HomeComponent implements OnInit, OnDestroy {
23
  public subscriptions: any[] = [];
24
  public loading: boolean = true;
25
  public errorCodes: ErrorCodes;
26
  public stakeholder: Stakeholder;
27
  public analysisOpen: boolean = true;
28
  private errorMessages: ErrorMessagesComponent;
29
  public topicFb: FormGroup;
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 layoutService: LayoutService,
41
    private fb: FormBuilder,
42
    private stakeholderService: StakeholderService) {
43
    this.errorCodes = new ErrorCodes();
44
    this.errorMessages = new ErrorMessagesComponent();
45
  }
46

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

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

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

    
73
  public hide(element) {
74
    UIkit.drop(element).hide();
75
  }
76

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

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

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

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

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

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

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

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