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 {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
7
import {StakeholderService} from "../services/stakeholder.service";
8
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
9
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
10
import {Subscriber} from "rxjs";
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 topic: Topic = null;
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 copyCategory: Category = null;
35
  /**
36
   * Current Subcategory to be edited
37
   */
38
  public subCategoryIndex: number = 0;
39
  public copySubCategory: SubCategory = null;
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 valid = true;
49
  public toggle: boolean = false;
50

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

    
55
  constructor(
56
    private route: ActivatedRoute,
57
    private router: Router,
58
    private title: Title,
59
    private sideBarService: SideBarService,
60
    private stakeholderService: StakeholderService) {
61
  }
62

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

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

    
92
  public hide(element) {
93
    this.valid = true;
94
    UIkit.drop(element).hide();
95
  }
96

    
97
  public show(element) {
98
    UIkit.drop(element).show();
99
  }
100

    
101
  public editTopicOpen(element) {
102
    if(element.className.indexOf('uk-open') !== -1) {
103
      this.hide(element);
104
    } else {
105
      this.topic = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex]);
106
      this.show(element);
107
    }
108
  }
109

    
110
  public saveTopic(element) {
111
    if(this.topic.name && this.topic.name !== '') {
112
      if(!this.topic.alias) {
113
        this.topic.alias = this.topic.name.toLowerCase().trim();
114
      }
115
      let path = [this.stakeholder._id];
116
      let callback = (topic: Topic): void => {
117
          this.stakeholder.topics[this.topicIndex] = topic;
118
          this.stakeholderService.setStakeholder(this.stakeholder);
119
      };
120
      this.save('Topic has been successfully saved', element, path, this.topic, callback,true);
121
    } else {
122
      this.valid = false;
123
    }
124
  }
125

    
126
  public deleteTopicOpen(name: string, element) {
127
    this.deleteOpen(name,'topic', this.deleteTopicModal, element, this.topicIndex);
128
  }
129

    
130
  public deleteTopic() {
131
    let path: string[] = [
132
      this.stakeholder._id,
133
      this.stakeholder.topics[this.topicIndex]._id
134
    ];
135
    let callback = (): void => {
136
      this.stakeholder.topics.splice(this.topicIndex, 1);
137
      this.stakeholderService.setStakeholder(this.stakeholder);
138
    };
139
    this.delete('Topic has been successfully be deleted', path, callback, true);
140
  }
141

    
142
  public toggleCategory(index: number) {
143
   if(this.selectedCategoryIndex !== index) {
144
      this.selectedCategoryIndex = index;
145
      this.toggle = true;
146
    } else {
147
      this.toggle = !this.toggle;
148
    }
149
  }
150

    
151
  public editCategoryOpen(element, index:number = -1) {
152
    if(element.className.indexOf('uk-open') !== -1) {
153
      this.hide(element);
154
    } else {
155
      if(index === -1) {
156
        this.copyCategory = new Category(null, null, null, true, true);
157
      } else {
158
        this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
159
      }
160
      this.show(element);
161
    }
162
  }
163

    
164
  public saveCategory(element, index = -1) {
165
    if(this.copyCategory.name && this.copyCategory.name !== '') {
166
      if(!this.copyCategory.alias) {
167
        this.copyCategory.alias = this.copyCategory.name.toLowerCase();
168
      }
169
      let path = [this.stakeholder._id, this.stakeholder.topics[this.topicIndex]._id];
170
      let callback = (category: Category): void => {
171
        if(index === -1) {
172
          this.stakeholder.topics[this.topicIndex].categories.push(category);
173
        } else {
174
          this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(category);
175
        }
176
        this.stakeholderService.setStakeholder(this.stakeholder);
177
      };
178
      if(index === -1) {
179
        this.save('Category has been successfully created', element, path, this.copyCategory, callback);
180
      } else {
181
        this.save('Category has been successfully saved', element, path, this.copyCategory, callback);
182
      }
183
    } else {
184
      this.valid = false;
185
    }
186
  }
187

    
188
  public deleteCategoryOpen(name: string, element, index) {
189
    this.deleteOpen(name,'category', this.deleteCategoryModal, element, index);
190
  }
191

    
192
  public deleteCategory() {
193
    let path: string[] = [
194
      this.stakeholder._id,
195
      this.stakeholder.topics[this.topicIndex]._id,
196
      this.stakeholder.topics[this.topicIndex].categories[this.index]._id
197
    ];
198
    let callback = (): void => {
199
      this.stakeholder.topics[this.topicIndex].categories.splice(this.index, 1);
200
      this.stakeholderService.setStakeholder(this.stakeholder);
201
    };
202
    this.delete('Category has been successfully be deleted', path, callback);
203
  }
204

    
205
  public editSubCategoryOpen(element, index:number = -1) {
206
    if(element.className.indexOf('uk-open') !== -1) {
207
      this.hide(element);
208
    } else {
209
      if(index === -1) {
210
        this.copySubCategory = new SubCategory(null, null, null, true, true);
211
      } else {
212
        this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
213
          categories[this.categoryIndex].subCategories[index]);
214
      }
215
      this.show(element);
216
    }
217
  }
218

    
219
  public saveSubCategory(element, index = -1) {
220
    if(this.copySubCategory.name && this.copySubCategory.name !== '') {
221
      if(!this.copySubCategory.alias) {
222
        this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
223
      }
224
      let path: string[] = [
225
        this.stakeholder._id,
226
        this.stakeholder.topics[this.topicIndex]._id,
227
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id,
228
      ];
229
      let callback = (subCategory: SubCategory): void => {
230
        if(index === -1) {
231
          this.stakeholder.topics[this.topicIndex].
232
            categories[this.selectedCategoryIndex].subCategories.push(subCategory);
233
        } else {
234
          this.stakeholder.topics[this.topicIndex].
235
            categories[this.selectedCategoryIndex].subCategories[index] = subCategory;
236
        }
237
        this.stakeholderService.setStakeholder(this.stakeholder);
238
      };
239
      if(index === -1) {
240
        this.save('Subcategory has been successfully created', element, path, this.copySubCategory, callback);
241
      } else {
242
        this.save('Subcategory has been successfully saved', element, path, this.copySubCategory, callback);
243
      }
244
      this.hide(element);
245
    } else {
246
      this.valid = false;
247
    }
248
  }
249

    
250
  public deleteSubcategoryOpen(name: string, element, index) {
251
    this.deleteOpen(name,'subcategory', this.deleteSubcategoryModal, element, index);
252
  }
253

    
254
  public deleteSubcategory() {
255
    let path: string[] = [
256
      this.stakeholder._id,
257
      this.stakeholder.topics[this.topicIndex]._id,
258
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id,
259
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]._id
260
    ];
261
    let callback = (): void => {
262
      this.stakeholder.topics[this.topicIndex].
263
        categories[this.selectedCategoryIndex].subCategories.splice(this.index, 1);
264
      this.stakeholderService.setStakeholder(this.stakeholder);
265
    };
266
    this.delete('Subcategory has been successfully be deleted', path, callback);
267
  }
268

    
269
  private navigateToError() {
270
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
271
  }
272

    
273
  private deleteOpen(name: string, type: string, modal: AlertModal, element, index) {
274
    this.element = element;
275
    this.index = index;
276
    modal.alertTitle = 'Delete ' + name;
277
    modal.cancelButtonText = 'No';
278
    modal.okButtonText = 'Yes';
279
    modal.message = 'This ' + type + ' will permanently be deleted. Are you sure you want to proceed?';
280
    modal.open();
281
  }
282

    
283
  private save(message: string, element, path: string[], saveElement: any, callback: Function, redirect = false) {
284
    this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, saveElement, path).subscribe(saveElement => {
285
      callback(saveElement);
286
      UIkit.notification(message, {
287
        status: 'success',
288
        timeout: 3000,
289
        pos: 'top-left'
290
      });
291
      if(redirect) {
292
        this.router.navigate(['../' + saveElement.alias], {
293
          relativeTo: this.route
294
        });
295
      }
296
      this.hide(element);
297
    }, error => {
298
      UIkit.notification(error.error.message, {
299
        status: 'danger',
300
        timeout: 3000,
301
        pos: 'top-left'
302
      });
303
      this.hide(element);
304
    });
305
  }
306

    
307
  private delete(message: string, path: string[], callback: Function, redirect = false) {
308
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
309
      callback();
310
      UIkit.notification(message, {
311
        status: 'success',
312
        timeout: 3000,
313
        pos: 'top-left'
314
      });
315
      if(redirect) {
316
        this.back();
317
      }
318
      this.hide(this.element);
319
    }, error => {
320
      UIkit.notification(error.error.message, {
321
        status: 'danger',
322
        timeout: 3000,
323
        pos: 'top-left'
324
      });
325
      this.hide(this.element);
326
    });
327
  }
328

    
329
  back() {
330
    this.router.navigate(['../'], {
331
      relativeTo: this.route
332
    });
333
  }
334

    
335
  chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
336
    this.categoryIndex = categoryIndex;
337
    this.subCategoryIndex = subcategoryIndex;
338
  }
339
}
(5-5/6)