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
    UIkit.drop(element).hide();
94
  }
95

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

    
100
  public editTopicOpen(element) {
101
    if(element.className.indexOf('uk-open') !== -1) {
102
      this.hide(element);
103
    } else {
104
      this.topic = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex]);
105
      this.valid = true;
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
      this.stakeholder.topics[this.topicIndex] = HelperFunctions.copy(this.topic);
116
      this.save('Topic has been successfully saved', element, true);
117
    } else {
118
      this.valid = false;
119
    }
120
  }
121

    
122
  public deleteTopicOpen(element) {
123
    this.deleteOpen('topic', this.deleteTopicModal, element, this.topicIndex);
124
  }
125

    
126
  public deleteTopic() {
127
    let path: string[] = [
128
      this.stakeholder.alias,
129
      this.stakeholder.topics[this.topicIndex].alias
130
    ];
131
    this.delete('Topic has been successfully be deleted', path, true);
132
  }
133

    
134
  public toggleCategory(index: number) {
135
   if(this.selectedCategoryIndex !== index) {
136
      this.selectedCategoryIndex = index;
137
      this.toggle = true;
138
    } else {
139
      this.toggle = !this.toggle;
140
    }
141
  }
142

    
143
  public editCategoryOpen(index:number = -1, element = null) {
144
    if(index === -1) {
145
      this.copyCategory = new Category(null, null, null, true, true);
146
    } else {
147
      if(element.className.indexOf('uk-open') !== -1) {
148
        this.hide(element);
149
      } else {
150
        this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
151
        this.show(element);
152
        this.valid = true;
153
      }
154
    }
155
  }
156

    
157
  public saveCategory(element, index = -1) {
158
    if(this.copyCategory.name && this.copyCategory.name !== '') {
159
      if(!this.copyCategory.alias) {
160
        this.copyCategory.alias = this.copyCategory.name.toLowerCase();
161
      }
162
      if(index === -1) {
163
        this.copyCategory.subCategories.push(
164
          new SubCategory(this.copyCategory.name,
165
            this.copyCategory.description,
166
            this.copyCategory.alias,
167
            this.copyCategory.isPublic,
168
            this.copyCategory.isActive)
169
        );
170
        this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory);
171
        this.save('Category has been successfully created', element);
172
      } else {
173
        this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(this.copyCategory);
174
        this.save('Category has been successfully saved', element);
175
      }
176
    } else {
177
      this.valid = false;
178
    }
179
  }
180

    
181
  public deleteCategoryOpen(element, index) {
182
    this.deleteOpen('category', this.deleteCategoryModal, element, index);
183
  }
184

    
185
  public deleteCategory() {
186
    let path: string[] = [
187
      this.stakeholder.alias,
188
      this.stakeholder.topics[this.topicIndex].alias,
189
      this.stakeholder.topics[this.topicIndex].categories[this.index].alias
190
    ];
191
    this.delete('Category has been successfully be deleted', path);
192
  }
193

    
194
  public editSubCategoryOpen(index:number = -1, element = null) {
195
    if(index === -1) {
196
      this.copySubCategory = new SubCategory(null, null, null, true, true);
197
    } else {
198
      if(element.className.indexOf('uk-open') !== -1) {
199
        this.hide(element);
200
      } else {
201
        this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
202
          categories[this.categoryIndex].subCategories[index]);
203
        this.show(element);
204
        this.valid = true;
205
      }
206
    }
207
  }
208

    
209
  public saveSubCategory(element, index = -1) {
210
    if(this.copySubCategory.name && this.copySubCategory.name !== '') {
211
      if(!this.copySubCategory.alias) {
212
        this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
213
      }
214
      if(index === -1) {
215
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
216
        subCategories.push(this.copySubCategory);
217
        this.save('Subcategory has been successfully created', element);
218
      } else {
219
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
220
          subCategories[index] = HelperFunctions.copy(this.copySubCategory);
221
        this.save('Subcategory has been successfully saved', element);
222
      }
223
      this.hide(element);
224
    } else {
225
      this.valid = false;
226
    }
227
  }
228

    
229
  public deleteSubcategoryOpen(element, index) {
230
    this.deleteOpen('subcategory', this.deleteSubcategoryModal, element, index);
231
  }
232

    
233
  public deleteSubcategory() {
234
    let path: string[] = [
235
      this.stakeholder.alias,
236
      this.stakeholder.topics[this.topicIndex].alias,
237
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].alias,
238
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index].alias
239
    ];
240
    this.delete('Subcategory has been successfully be deleted', path);
241
  }
242

    
243
  private navigateToError() {
244
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
245
  }
246

    
247
  private deleteOpen(type: string, modal: AlertModal, element, index) {
248
    this.element = element;
249
    this.index = index;
250
    modal.alertHeader = true;
251
    modal.alertMessage = true;
252
    modal.cancelButton = true;
253
    modal.cancelButtonText = 'No';
254
    modal.okButtonText = 'Yes';
255
    modal.message = 'This ' + type + ' will permanently be deleted. Are you sure you want to proceed?';
256
    modal.open();
257
  }
258

    
259
  private save(message: string, element, redirect = false) {
260
    this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => {
261
      this.stakeholderService.setStakeholder(stakeholder);
262
      UIkit.notification(message, {
263
        status: 'success',
264
        timeout: 3000,
265
        pos: 'top-left'
266
      });
267
      if(redirect) {
268
        this.router.navigate(['../' + this.topic.alias], {
269
          relativeTo: this.route
270
        });
271
      }
272
      this.hide(element);
273
    }, error => {
274
      UIkit.notification(error.error.message, {
275
        status: 'danger',
276
        timeout: 3000,
277
        pos: 'top-left'
278
      });
279
      this.hide(element);
280
    });
281
  }
282

    
283
  private delete(message: string, path: string[], redirect = false) {
284
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(stakeholder => {
285
      this.stakeholderService.setStakeholder(stakeholder);
286
      UIkit.notification(message, {
287
        status: 'success',
288
        timeout: 3000,
289
        pos: 'top-left'
290
      });
291
      if(redirect) {
292
        this.back();
293
      }
294
      this.hide(this.element);
295
    }, error => {
296
      UIkit.notification(error.error.message, {
297
        status: 'danger',
298
        timeout: 3000,
299
        pos: 'top-left'
300
      });
301
      this.hide(this.element);
302
    });
303
  }
304

    
305
  back() {
306
    this.router.navigate(['../'], {
307
      relativeTo: this.route
308
    });
309
  }
310

    
311
  chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
312
    this.categoryIndex = categoryIndex;
313
    this.subCategoryIndex = subcategoryIndex;
314
  }
315
}
(5-5/6)