Project

General

Profile

1 57591 k.triantaf
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
2 57506 k.triantaf
import {ActivatedRoute, Router} from '@angular/router';
3
import {Title} from '@angular/platform-browser';
4
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
5 57514 k.triantaf
import {Category, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
6 57506 k.triantaf
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
7
import {StakeholderService} from "../services/stakeholder.service";
8 57574 k.triantaf
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
9 57591 k.triantaf
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
10 57506 k.triantaf
11
declare var UIkit;
12
13
@Component({
14
  selector: 'topic',
15
  templateUrl: './topic.component.html',
16
})
17
export class TopicComponent implements OnInit, OnDestroy {
18 57555 k.triantaf
  public properties: EnvProperties;
19 57506 k.triantaf
  public loading: boolean = true;
20
  public stakeholder: Stakeholder;
21 57594 k.triantaf
  /**
22
   * Current topic
23
   **/
24 57514 k.triantaf
  public topicIndex: number = -1;
25
  public topic: Topic = null;
26 57594 k.triantaf
27
  /**
28
   * categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
29
   */
30 57514 k.triantaf
  public categoryIndex: number = -1;
31 57555 k.triantaf
  public selectedCategoryIndex: number = -1;
32 57514 k.triantaf
  public copyCategory: Category = null;
33 57594 k.triantaf
  /**
34
   * Current Subcategory to be edited
35
   */
36 57514 k.triantaf
  public subCategoryIndex: number = -1;
37
  public copySubCategory: SubCategory = null;
38 57594 k.triantaf
  /**
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 57506 k.triantaf
  public valid = true;
47 57514 k.triantaf
  public toggle: boolean = false;
48 57506 k.triantaf
49 57591 k.triantaf
  @ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
50
  @ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal;
51
  @ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal;
52
53 57506 k.triantaf
  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 57574 k.triantaf
              this.stakeholder = HelperFunctions.copy(stakeholder);
69 57514 k.triantaf
              this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']);
70
              if(this.topicIndex === -1) {
71 57506 k.triantaf
                this.navigateToError();
72 57514 k.triantaf
              } else {
73
                this.title.setTitle(stakeholder.index_name);
74 57555 k.triantaf
                this.categoryIndex = 0;
75
                this.selectedCategoryIndex = 0;
76
                this.subCategoryIndex = 0;
77
                this.toggle = true;
78 57506 k.triantaf
              }
79
            }
80
          });
81
        });
82
      });
83
  }
84
85
  public ngOnDestroy() {
86
  }
87
88 57514 k.triantaf
  public hide(element) {
89
    UIkit.drop(element).hide();
90 57506 k.triantaf
  }
91
92 57514 k.triantaf
  public show(element) {
93
    UIkit.drop(element).show();
94 57506 k.triantaf
  }
95
96 57594 k.triantaf
  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 57514 k.triantaf
  public toggleCategory(index: number) {
132 57555 k.triantaf
   if(this.selectedCategoryIndex !== index) {
133
      this.selectedCategoryIndex = index;
134 57514 k.triantaf
      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 57574 k.triantaf
        this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
148 57514 k.triantaf
        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 57591 k.triantaf
      if(!this.copyCategory.alias) {
157
        this.copyCategory.alias = this.copyCategory.name.toLowerCase();
158
      }
159 57514 k.triantaf
      if(index === -1) {
160
        this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory);
161 57591 k.triantaf
        this.save('Category has been successfully created', element);
162 57514 k.triantaf
      } else {
163 57574 k.triantaf
        this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(this.copyCategory);
164 57591 k.triantaf
        this.save('Category has been successfully saved', element);
165 57514 k.triantaf
      }
166
    } else {
167
      this.valid = false;
168
    }
169
  }
170
171 57591 k.triantaf
  public deleteCategoryOpen(element, index) {
172
    this.deleteOpen('category', this.deleteCategoryModal, element, index);
173
  }
174
175
  public deleteCategory() {
176 57594 k.triantaf
    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 57591 k.triantaf
  }
183
184 57514 k.triantaf
  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 57574 k.triantaf
        this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
192 57514 k.triantaf
          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 57591 k.triantaf
      if(!this.copySubCategory.alias) {
202
        this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
203
      }
204 57514 k.triantaf
      if(index === -1) {
205 57555 k.triantaf
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
206 57514 k.triantaf
        subCategories.push(this.copySubCategory);
207 57591 k.triantaf
        this.save('Subcategory has been successfully created', element);
208 57514 k.triantaf
      } else {
209 57555 k.triantaf
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
210 57574 k.triantaf
          subCategories[index] = HelperFunctions.copy(this.copySubCategory);
211 57591 k.triantaf
        this.save('Subcategory has been successfully saved', element);
212 57514 k.triantaf
      }
213
      this.hide(element);
214
    } else {
215
      this.valid = false;
216
    }
217
  }
218
219 57591 k.triantaf
  public deleteSubcategoryOpen(element, index) {
220
    this.deleteOpen('subcategory', this.deleteSubcategoryModal, element, index);
221
  }
222
223
  public deleteSubcategory() {
224 57594 k.triantaf
    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 57591 k.triantaf
  }
232
233 57506 k.triantaf
  private navigateToError() {
234
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
235
  }
236 57514 k.triantaf
237 57591 k.triantaf
  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 57555 k.triantaf
  }
248
249 57591 k.triantaf
  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 57594 k.triantaf
  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 57514 k.triantaf
  back() {
296
    this.router.navigate(['../'], {
297
      relativeTo: this.route
298
    });
299
  }
300 57555 k.triantaf
301
  chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
302
    this.categoryIndex = categoryIndex;
303
    this.subCategoryIndex = subcategoryIndex;
304
  }
305 57506 k.triantaf
}