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

    
11
declare var UIkit;
12

    
13
@Component({
14
  selector: 'topic',
15
  templateUrl: './topic.component.html',
16
})
17
export class TopicComponent implements OnInit, OnDestroy {
18
  public properties: EnvProperties;
19
  public loading: boolean = true;
20
  public stakeholder: Stakeholder;
21
  /**
22
   * Current topic
23
   **/
24
  public topicIndex: number = -1;
25
  public topic: Topic = null;
26

    
27
  /**
28
   * categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
29
   */
30
  public categoryIndex: number = -1;
31
  public selectedCategoryIndex: number = -1;
32
  public copyCategory: Category = null;
33
  /**
34
   * Current Subcategory to be edited
35
   */
36
  public subCategoryIndex: number = -1;
37
  public copySubCategory: SubCategory = null;
38
  /**
39
   * Current drop element and index of topic, category or subcategory to be deleted.
40
   */
41
  public element: any;
42
  public index: number;
43
  /**
44
   * Check form validity
45
   */
46
  public valid = true;
47
  public toggle: boolean = false;
48

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

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

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

    
85
  public ngOnDestroy() {
86
  }
87

    
88
  public hide(element) {
89
    UIkit.drop(element).hide();
90
  }
91

    
92
  public show(element) {
93
    UIkit.drop(element).show();
94
  }
95

    
96
  public editTopicOpen(element) {
97
    if(element.className.indexOf('uk-open') !== -1) {
98
      this.hide(element);
99
    } else {
100
      this.topic = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex]);
101
      this.valid = true;
102
      this.show(element);
103
    }
104
  }
105

    
106
  public saveTopic(element) {
107
    if(this.topic.name && this.topic.name !== ''
108
      && this.topic.description && this.topic.description !== '') {
109
      if(!this.topic.alias) {
110
        this.topic.alias = this.topic.name.toLowerCase().trim();
111
      }
112
      this.stakeholder.topics[this.topicIndex] = HelperFunctions.copy(this.topic);
113
      this.save('Topic has been successfully saved', element, true);
114
    } else {
115
      this.valid = false;
116
    }
117
  }
118

    
119
  public deleteTopicOpen(element) {
120
    this.deleteOpen('topic', this.deleteTopicModal, element, this.topicIndex);
121
  }
122

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

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

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

    
154
  public saveCategory(element, index = -1) {
155
    if(this.copyCategory.name && this.copyCategory.name !== '') {
156
      if(!this.copyCategory.alias) {
157
        this.copyCategory.alias = this.copyCategory.name.toLowerCase();
158
      }
159
      if(index === -1) {
160
        this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory);
161
        this.save('Category has been successfully created', element);
162
      } else {
163
        this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(this.copyCategory);
164
        this.save('Category has been successfully saved', element);
165
      }
166
    } else {
167
      this.valid = false;
168
    }
169
  }
170

    
171
  public deleteCategoryOpen(element, index) {
172
    this.deleteOpen('category', this.deleteCategoryModal, element, index);
173
  }
174

    
175
  public deleteCategory() {
176
    let path: string[] = [
177
      this.stakeholder.alias,
178
      this.stakeholder.topics[this.topicIndex].alias,
179
      this.stakeholder.topics[this.topicIndex].categories[this.index].alias
180
    ];
181
    this.delete('Category has been successfully be deleted', path);
182
  }
183

    
184
  public editSubCategoryOpen(index:number = -1, element = null) {
185
    if(index === -1) {
186
      this.copySubCategory = new SubCategory(null, null, null, true, true);
187
    } else {
188
      if(element.className.indexOf('uk-open') !== -1) {
189
        this.hide(element);
190
      } else {
191
        this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
192
          categories[this.categoryIndex].subCategories[index]);
193
        this.show(element);
194
        this.valid = true;
195
      }
196
    }
197
  }
198

    
199
  public saveSubCategory(element, index = -1) {
200
    if(this.copySubCategory.name && this.copySubCategory.name !== '') {
201
      if(!this.copySubCategory.alias) {
202
        this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
203
      }
204
      if(index === -1) {
205
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
206
        subCategories.push(this.copySubCategory);
207
        this.save('Subcategory has been successfully created', element);
208
      } else {
209
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
210
          subCategories[index] = HelperFunctions.copy(this.copySubCategory);
211
        this.save('Subcategory has been successfully saved', element);
212
      }
213
      this.hide(element);
214
    } else {
215
      this.valid = false;
216
    }
217
  }
218

    
219
  public deleteSubcategoryOpen(element, index) {
220
    this.deleteOpen('subcategory', this.deleteSubcategoryModal, element, index);
221
  }
222

    
223
  public deleteSubcategory() {
224
    let path: string[] = [
225
      this.stakeholder.alias,
226
      this.stakeholder.topics[this.topicIndex].alias,
227
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].alias,
228
      this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index].alias
229
    ];
230
    this.delete('Subcategory has been successfully be deleted', path);
231
  }
232

    
233
  private navigateToError() {
234
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
235
  }
236

    
237
  private deleteOpen(type: string, modal: AlertModal, element, index) {
238
    this.element = element;
239
    this.index = index;
240
    modal.alertHeader = true;
241
    modal.alertMessage = true;
242
    modal.cancelButton = true;
243
    modal.cancelButtonText = 'No';
244
    modal.okButtonText = 'Yes';
245
    modal.message = 'This ' + type + ' will permanently be deleted. Are you sure you want to proceed?';
246
    modal.open();
247
  }
248

    
249
  private save(message: string, element, redirect = false) {
250
    this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => {
251
      this.stakeholderService.setStakeholder(stakeholder);
252
      UIkit.notification(message, {
253
        status: 'success',
254
        timeout: 3000,
255
        pos: 'top-left'
256
      });
257
      if(redirect) {
258
        this.router.navigate(['../' + this.topic.alias], {
259
          relativeTo: this.route
260
        });
261
      }
262
      this.hide(element);
263
    }, error => {
264
      UIkit.notification(error.error.message, {
265
        status: 'danger',
266
        timeout: 3000,
267
        pos: 'top-left'
268
      });
269
      this.hide(element);
270
    });
271
  }
272

    
273
  private delete(message: string, path: string[], redirect = false) {
274
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(stakeholder => {
275
      this.stakeholderService.setStakeholder(stakeholder);
276
      UIkit.notification(message, {
277
        status: 'success',
278
        timeout: 3000,
279
        pos: 'top-left'
280
      });
281
      if(redirect) {
282
        this.back();
283
      }
284
      this.hide(this.element);
285
    }, error => {
286
      UIkit.notification(error.error.message, {
287
        status: 'danger',
288
        timeout: 3000,
289
        pos: 'top-left'
290
      });
291
      this.hide(this.element);
292
    });
293
  }
294

    
295
  back() {
296
    this.router.navigate(['../'], {
297
      relativeTo: this.route
298
    });
299
  }
300

    
301
  chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
302
    this.categoryIndex = categoryIndex;
303
    this.subCategoryIndex = subcategoryIndex;
304
  }
305
}
(5-5/6)