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
import {Category, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
6
import {StakeholderService} from "../services/stakeholder.service";
7
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
8
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
9
import {Subscriber} from "rxjs";
10
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
11

    
12
declare var UIkit;
13

    
14
@Component({
15
  selector: 'topic',
16
  templateUrl: './topic.component.html',
17
})
18
export class TopicComponent implements OnInit, OnDestroy {
19
  public subscriptions: any[] = [];
20
  public properties: EnvProperties;
21
  public loading: boolean = true;
22
  public stakeholder: Stakeholder;
23
  /**
24
   * Current topic
25
   **/
26
  public topicIndex: number = 0;
27
  public topicFb: FormGroup;
28

    
29
  /**
30
   * categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
31
   */
32
  public categoryIndex: number = 0;
33
  public selectedCategoryIndex: number = 0;
34
  public categoryFb: FormGroup;
35
  /**
36
   * Current Subcategory to be edited
37
   */
38
  public subCategoryIndex: number = 0;
39
  public subcategoryFb: FormGroup;
40
  /**
41
   * Current drop element and index of topic, category or subcategory to be deleted.
42
   */
43
  public element: any;
44
  public index: number;
45
  /**
46
   * Check form validity
47
   */
48
  public toggle: boolean = false;
49

    
50
  @ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
51
  @ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal;
52
  @ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal;
53

    
54
  constructor(
55
    private route: ActivatedRoute,
56
    private router: Router,
57
    private title: Title,
58
    private fb: FormBuilder,
59
    private stakeholderService: StakeholderService) {
60
  }
61

    
62
  public ngOnInit() {
63
    this.route.data
64
      .subscribe((data: { envSpecific: EnvProperties }) => {
65
        this.properties = data.envSpecific;
66
        this.route.params.subscribe( params => {
67
          this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
68
            if (stakeholder) {
69
              this.stakeholder = HelperFunctions.copy(stakeholder);
70
              this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']);
71
              if(this.topicIndex === -1) {
72
                this.navigateToError();
73
              } else {
74
                this.title.setTitle(stakeholder.index_shortName + ' | ' + this.stakeholder.topics[this.topicIndex].name);
75
                this.toggle = true;
76
              }
77
            }
78
          }));
79
        });
80
      });
81
  }
82

    
83
  public ngOnDestroy() {
84
    this.subscriptions.forEach( value => {
85
      if(value instanceof Subscriber) {
86
        value.unsubscribe();
87
      }
88
    });
89
  }
90

    
91
  public hide(element) {
92
    UIkit.drop(element).hide();
93
  }
94

    
95
  public show(element) {
96
    UIkit.drop(element).show();
97
  }
98

    
99
  private buildTopic(topic: Topic) {
100
    this.topicFb = this.fb.group({
101
      _id: this.fb.control(topic._id),
102
      name: this.fb.control(topic.name, Validators.required),
103
      description: this.fb.control(topic.description),
104
      alias: this.fb.control(topic.alias),
105
      isActive: this.fb.control(topic.isActive),
106
      isPublic: this.fb.control(topic.isPublic),
107
      isDefault: this.fb.control(topic.isDefault),
108
      categories: this.fb.control(topic.categories)
109
    });
110
  }
111

    
112
  public editTopicOpen(element) {
113
    if(element.className.indexOf('uk-open') !== -1) {
114
      this.hide(element);
115
    } else {
116
      this.buildTopic(this.stakeholder.topics[this.topicIndex]);
117
      this.show(element);
118
    }
119
  }
120

    
121
  public saveTopic(element) {
122
    if(!this.topicFb.invalid) {
123
      if(!this.topicFb.value.alias) {
124
        this.topicFb.value.alias = this.topicFb.value.name.toLowerCase().trim();
125
      }
126
      let path = [this.stakeholder._id];
127
      let callback = (topic: Topic): void => {
128
          this.stakeholder.topics[this.topicIndex] = topic;
129
          this.stakeholderService.setStakeholder(this.stakeholder);
130
      };
131
      this.save('Topic has been successfully saved', element, path, this.topicFb.value, callback,true);
132
    }
133
  }
134

    
135
  public deleteTopicOpen(name: string, element) {
136
    this.deleteOpen(name,'topic', this.deleteTopicModal, element, this.topicIndex);
137
  }
138

    
139
  public deleteTopic() {
140
    let path: string[] = [
141
      this.stakeholder._id,
142
      this.stakeholder.topics[this.topicIndex]._id
143
    ];
144
    let callback = (): void => {
145
      this.stakeholder.topics.splice(this.topicIndex, 1);
146
      this.stakeholderService.setStakeholder(this.stakeholder);
147
    };
148
    this.delete('Topic has been successfully be deleted', path, callback, true);
149
  }
150

    
151
  public toggleCategory(index: number) {
152
   if(this.selectedCategoryIndex !== index) {
153
      this.selectedCategoryIndex = index;
154
      this.toggle = true;
155
    } else {
156
      this.toggle = !this.toggle;
157
    }
158
  }
159

    
160
  private buildCategory(category: Category) {
161
    this.categoryFb = this.fb.group({
162
      _id: this.fb.control(category._id),
163
      name: this.fb.control(category.name, Validators.required),
164
      alias: this.fb.control(category.alias),
165
      isActive: this.fb.control(category.isActive),
166
      isPublic: this.fb.control(category.isPublic),
167
      isDefault: this.fb.control(category.isDefault),
168
      subCategories: this.fb.control(category.subCategories)
169
    });
170
  }
171

    
172
  public editCategoryOpen(element, index:number = -1) {
173
    if(element.className.indexOf('uk-open') !== -1) {
174
      this.hide(element);
175
    } else {
176
      if(index === -1) {
177
        this.buildCategory(new Category(null, null, null, true, true));
178
      } else {
179
        this.buildCategory(this.stakeholder.topics[this.topicIndex].categories[index]);
180
      }
181
      this.show(element);
182
    }
183
  }
184

    
185
  public saveCategory(element, index = -1) {
186
    if(!this.categoryFb.invalid) {
187
      if(!this.categoryFb.value.alias) {
188
        this.categoryFb.value.alias = this.categoryFb.value.name.toLowerCase();
189
      }
190
      let path = [this.stakeholder._id, this.stakeholder.topics[this.topicIndex]._id];
191
      let callback = (category: Category): void => {
192
        if(index === -1) {
193
          this.stakeholder.topics[this.topicIndex].categories.push(category);
194
        } else {
195
          this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(category);
196
        }
197
        this.stakeholderService.setStakeholder(this.stakeholder);
198
      };
199
      if(index === -1) {
200
        this.save('Category has been successfully created', element, path, this.categoryFb.value, callback);
201
      } else {
202
        this.save('Category has been successfully saved', element, path, this.categoryFb.value, callback);
203
      }
204
    }
205
  }
206

    
207
  public deleteCategoryOpen(name: string, element, index) {
208
    this.deleteOpen(name,'category', this.deleteCategoryModal, element, index);
209
  }
210

    
211
  public deleteCategory() {
212
    let path: string[] = [
213
      this.stakeholder._id,
214
      this.stakeholder.topics[this.topicIndex]._id,
215
      this.stakeholder.topics[this.topicIndex].categories[this.index]._id
216
    ];
217
    let callback = (): void => {
218
      this.stakeholder.topics[this.topicIndex].categories.splice(this.index, 1);
219
      this.stakeholderService.setStakeholder(this.stakeholder);
220
    };
221
    this.delete('Category has been successfully be deleted', path, callback);
222
  }
223

    
224
  private buildSubcategory(subCategory: SubCategory) {
225
    this.subcategoryFb = this.fb.group({
226
      _id: this.fb.control(subCategory._id),
227
      name: this.fb.control(subCategory.name, Validators.required),
228
      alias: this.fb.control(subCategory.alias),
229
      isActive: this.fb.control(subCategory.isActive),
230
      isPublic: this.fb.control(subCategory.isPublic),
231
      isDefault: this.fb.control(subCategory.isDefault),
232
      charts: this.fb.control(subCategory.charts),
233
      numbers: this.fb.control(subCategory.numbers)
234
    });
235
  }
236

    
237
  public editSubCategoryOpen(element, index:number = -1) {
238
    if(element.className.indexOf('uk-open') !== -1) {
239
      this.hide(element);
240
    } else {
241
      if(index === -1) {
242
        this.buildSubcategory(new SubCategory(null, null, null, true, true));
243
      } else {
244
        this.buildSubcategory(this.stakeholder.topics[this.topicIndex].
245
          categories[this.selectedCategoryIndex].subCategories[index]);
246
      }
247
      this.show(element);
248
    }
249
  }
250

    
251
  public saveSubCategory(element, index = -1) {
252
    if(!this.subcategoryFb.invalid) {
253
      if(!this.subcategoryFb.value.alias) {
254
        this.subcategoryFb.value.alias = this.subcategoryFb.value.name.toLowerCase();
255
      }
256
      let path: string[] = [
257
        this.stakeholder._id,
258
        this.stakeholder.topics[this.topicIndex]._id,
259
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id,
260
      ];
261
      let callback = (subCategory: SubCategory): void => {
262
        if(index === -1) {
263
          this.stakeholder.topics[this.topicIndex].
264
            categories[this.selectedCategoryIndex].subCategories.push(subCategory);
265
        } else {
266
          this.stakeholder.topics[this.topicIndex].
267
            categories[this.selectedCategoryIndex].subCategories[index] = subCategory;
268
        }
269
        this.stakeholderService.setStakeholder(this.stakeholder);
270
      };
271
      if(index === -1) {
272
        this.save('Subcategory has been successfully created', element, path, this.subcategoryFb.value, callback);
273
      } else {
274
        this.save('Subcategory has been successfully saved', element, path, this.subcategoryFb.value, callback);
275
      }
276
      this.hide(element);
277
    }
278
  }
279

    
280
  public deleteSubcategoryOpen(name: string, element, index) {
281
    this.deleteOpen(name,'subcategory', this.deleteSubcategoryModal, element, index);
282
  }
283

    
284
  public deleteSubcategory() {
285
    let path: string[] = [
286
      this.stakeholder._id,
287
      this.stakeholder.topics[this.topicIndex]._id,
288
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id,
289
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]._id
290
    ];
291
    let callback = (): void => {
292
      this.stakeholder.topics[this.topicIndex].
293
        categories[this.selectedCategoryIndex].subCategories.splice(this.index, 1);
294
      this.stakeholderService.setStakeholder(this.stakeholder);
295
    };
296
    this.delete('Subcategory has been successfully be deleted', path, callback);
297
  }
298

    
299
  private navigateToError() {
300
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
301
  }
302

    
303
  private deleteOpen(name: string, type: string, modal: AlertModal, element, index) {
304
    this.element = element;
305
    this.index = index;
306
    modal.alertTitle = 'Delete ' + name;
307
    modal.cancelButtonText = 'No';
308
    modal.okButtonText = 'Yes';
309
    modal.message = 'This ' + type + ' will permanently be deleted. Are you sure you want to proceed?';
310
    modal.open();
311
  }
312

    
313
  private save(message: string, element, path: string[], saveElement: any, callback: Function, redirect = false) {
314
    this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, saveElement, path).subscribe(saveElement => {
315
      callback(saveElement);
316
      UIkit.notification(message, {
317
        status: 'success',
318
        timeout: 3000,
319
        pos: 'top-left'
320
      });
321
      if(redirect) {
322
        this.router.navigate(['../' + saveElement.alias], {
323
          relativeTo: this.route
324
        });
325
      }
326
      this.hide(element);
327
    }, error => {
328
      UIkit.notification(error.error.message, {
329
        status: 'danger',
330
        timeout: 3000,
331
        pos: 'top-left'
332
      });
333
      this.hide(element);
334
    });
335
  }
336

    
337
  private delete(message: string, path: string[], callback: Function, redirect = false) {
338
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
339
      callback();
340
      UIkit.notification(message, {
341
        status: 'success',
342
        timeout: 3000,
343
        pos: 'top-left'
344
      });
345
      if(redirect) {
346
        this.back();
347
      }
348
      this.hide(this.element);
349
    }, error => {
350
      UIkit.notification(error.error.message, {
351
        status: 'danger',
352
        timeout: 3000,
353
        pos: 'top-left'
354
      });
355
      this.hide(this.element);
356
    });
357
  }
358

    
359
  back() {
360
    this.router.navigate(['../'], {
361
      relativeTo: this.route
362
    });
363
  }
364

    
365
  chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
366
    this.categoryIndex = categoryIndex;
367
    this.subCategoryIndex = subcategoryIndex;
368
  }
369
}
(5-5/6)