Project

General

Profile

1
import {Component, OnDestroy, OnInit} 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 {Tools} from "../utils/Tools";
9

    
10
declare var UIkit;
11

    
12
@Component({
13
  selector: 'topic',
14
  templateUrl: './topic.component.html',
15
})
16
export class TopicComponent implements OnInit, OnDestroy {
17
  public properties: EnvProperties;
18
  public loading: boolean = true;
19
  public stakeholder: Stakeholder;
20
  public topicIndex: number = -1;
21
  public topic: Topic = null;
22
  public categoryIndex: number = -1;
23
  public selectedCategoryIndex: number = -1;
24
  public copyCategory: Category = null;
25
  public subCategoryIndex: number = -1;
26
  public copySubCategory: SubCategory = null;
27
  public valid = true;
28
  public edit: boolean = false;
29
  public toggle: boolean = false;
30

    
31
  constructor(
32
    private route: ActivatedRoute,
33
    private router: Router,
34
    private title: Title,
35
    private sideBarService: SideBarService,
36
    private stakeholderService: StakeholderService) {
37
  }
38

    
39
  public ngOnInit() {
40
    this.route.data
41
      .subscribe((data: { envSpecific: EnvProperties }) => {
42
        this.properties = data.envSpecific;
43
        this.route.params.subscribe( params => {
44
          this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
45
            if (stakeholder) {
46
              this.sideBarService.setHasSidebar(true);
47
              this.stakeholder = Tools.copy(stakeholder);
48
              this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']);
49
              if(this.topicIndex === -1) {
50
                this.navigateToError();
51
              } else {
52
                this.title.setTitle(stakeholder.index_name);
53
                this.categoryIndex = 0;
54
                this.selectedCategoryIndex = 0;
55
                this.subCategoryIndex = 0;
56
                this.toggle = true;
57
              }
58
            }
59
          });
60
        });
61
      });
62
  }
63

    
64
  public ngOnDestroy() {
65
  }
66

    
67
  public hide(element) {
68
    this.edit = false;
69
    UIkit.drop(element).hide();
70
  }
71

    
72
  public show(element) {
73
    this.edit = true;
74
    UIkit.drop(element).show();
75
  }
76

    
77
  public toggleCategory(index: number) {
78
   if(this.selectedCategoryIndex !== index) {
79
      this.selectedCategoryIndex = index;
80
      this.toggle = true;
81
    } else {
82
      this.toggle = !this.toggle;
83
    }
84
  }
85

    
86
  public editCategoryOpen(index:number = -1, element = null) {
87
    if(index === -1) {
88
      this.copyCategory = new Category(null, null, null, true, true);
89
    } else {
90
      if(element.className.indexOf('uk-open') !== -1) {
91
        this.hide(element);
92
      } else {
93
        this.copyCategory = Tools.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
94
        this.show(element);
95
        this.valid = true;
96
      }
97
    }
98
  }
99

    
100
  public saveCategory(element, index = -1) {
101
    if(this.copyCategory.name && this.copyCategory.name !== '') {
102
      this.copyCategory.alias = this.copyCategory.name.toLowerCase();
103
      if(index === -1) {
104
        this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory);
105
      } else {
106
        this.stakeholder.topics[this.topicIndex].categories[index] = Tools.copy(this.copyCategory);
107
      }
108
      this.hide(element);
109
    } else {
110
      this.valid = false;
111
    }
112
  }
113

    
114
  public editSubCategoryOpen(index:number = -1, element = null) {
115
    if(index === -1) {
116
      this.copySubCategory = new SubCategory(null, null, null, true, true);
117
    } else {
118
      if(element.className.indexOf('uk-open') !== -1) {
119
        this.hide(element);
120
      } else {
121
        this.copySubCategory = Tools.copy(this.stakeholder.topics[this.topicIndex].
122
          categories[this.categoryIndex].subCategories[index]);
123
        this.show(element);
124
        this.valid = true;
125
      }
126
    }
127
  }
128

    
129
  public saveSubCategory(element, index = -1) {
130
    if(this.copySubCategory.name && this.copySubCategory.name !== '') {
131
      this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
132
      if(index === -1) {
133
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
134
        subCategories.push(this.copySubCategory);
135
      } else {
136
        this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
137
          subCategories[index] = Tools.copy(this.copySubCategory);
138
      }
139
      this.hide(element);
140
    } else {
141
      this.valid = false;
142
    }
143
  }
144

    
145
  public editTopicOpen(element) {
146
    if(element.className.indexOf('uk-open') !== -1) {
147
      this.hide(element);
148
    } else {
149
      this.topic = Tools.copy(this.stakeholder.topics[this.topicIndex]);
150
      this.valid = true;
151
      this.show(element);
152
    }
153
  }
154

    
155
  public saveTopic(element) {
156
    if(this.topic.name && this.topic.name !== '') {
157
      this.topic.alias = this.topic.name.toLowerCase();
158
      this.stakeholder.topics[this.topicIndex] = Tools.copy(this.topic);
159
      this.hide(element);
160
    } else {
161
      this.valid = false;
162
    }
163
  }
164

    
165
  public deleteTopic(element) {
166
    /*this.stakeholder.topics.splice(this.topicIndex, 1);
167
    this.hide(element);
168
    this.back();*/
169
  }
170

    
171
  private navigateToError() {
172
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
173
  }
174

    
175
  public getDefaultSubcategoryIndex(categoryIndex: number) {
176
    return this.stakeholder.topics[this.topicIndex].categories[categoryIndex].
177
    subCategories.findIndex(subcategory => subcategory.alias === null);
178
  }
179

    
180
  back() {
181
    this.router.navigate(['../'], {
182
      relativeTo: this.route
183
    });
184
  }
185

    
186
  chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
187
    /*let topic: Topic = this.stakeholder.topics[this.topicIndex];
188
    let category: Category = topic.categories[this.categoryIndex];
189
    let subCatetegory: SubCategory = category.subCategories[index];*/
190
    this.categoryIndex = categoryIndex;
191
    this.subCategoryIndex = subcategoryIndex;
192
  }
193
}
(5-5/6)