Revision 57927
Added by Konstantinos Triantafyllou over 4 years ago
topic.component.ts | ||
---|---|---|
9 | 9 |
import {Subscriber, Subscription} from "rxjs"; |
10 | 10 |
import {FormBuilder, FormGroup, Validators} from "@angular/forms"; |
11 | 11 |
import {StakeholderUtils} from "../utils/indicator-utils"; |
12 |
import {StringUtils} from "../openaireLibrary/utils/string-utils.class"; |
|
12 | 13 |
|
13 | 14 |
declare var UIkit; |
14 | 15 |
|
15 | 16 |
@Component({ |
16 |
selector: 'topic',
|
|
17 |
templateUrl: './topic.component.html',
|
|
17 |
selector: 'topic', |
|
18 |
templateUrl: './topic.component.html', |
|
18 | 19 |
}) |
19 | 20 |
export class TopicComponent implements OnInit, OnDestroy { |
20 |
public subscriptions: any[] = []; |
|
21 |
public properties: EnvProperties; |
|
22 |
public stakeholderUtils: StakeholderUtils = new StakeholderUtils(); |
|
23 |
public loading: boolean = true; |
|
24 |
public stakeholder: Stakeholder; |
|
25 |
/** |
|
26 |
* Current topic |
|
27 |
**/ |
|
28 |
public topicIndex: number = 0; |
|
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 |
/** |
|
35 |
* Current Subcategory to be edited |
|
36 |
*/ |
|
37 |
public subCategoryIndex: number = 0; |
|
38 |
/** |
|
39 |
* Current element and index of topic, category or subcategory to be deleted. |
|
40 |
*/ |
|
41 |
public form: FormGroup; |
|
42 |
public element: Topic | Category | SubCategory; |
|
43 |
public type: 'topic' | 'category' | 'subcategory' = "topic"; |
|
44 |
public index: number = -1; |
|
45 |
/** |
|
46 |
* Check form validity |
|
47 |
*/ |
|
48 |
public toggle: boolean = false; |
|
49 |
|
|
50 |
@ViewChild('deleteModal') deleteModal: AlertModal; |
|
51 |
@ViewChild('editModal') editModal: AlertModal; |
|
52 |
|
|
53 |
constructor( |
|
54 |
private route: ActivatedRoute, |
|
55 |
private router: Router, |
|
56 |
private title: Title, |
|
57 |
private fb: FormBuilder, |
|
58 |
private stakeholderService: StakeholderService) { |
|
59 |
} |
|
60 |
|
|
61 |
public ngOnInit() { |
|
62 |
this.route.data |
|
63 |
.subscribe((data: { envSpecific: EnvProperties }) => { |
|
64 |
this.properties = data.envSpecific; |
|
65 |
let subscription: Subscription; |
|
66 |
this.route.params.subscribe(params => { |
|
67 |
if(subscription) { |
|
68 |
subscription.unsubscribe(); |
|
69 |
} |
|
70 |
subscription = this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => { |
|
71 |
if (stakeholder) { |
|
72 |
this.stakeholder = HelperFunctions.copy(stakeholder); |
|
73 |
this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']); |
|
74 |
if (this.topicIndex === -1) { |
|
75 |
this.navigateToError(); |
|
76 |
} else { |
|
77 |
this.title.setTitle(stakeholder.index_shortName + ' | ' + this.stakeholder.topics[this.topicIndex].name); |
|
78 |
this.toggle = true; |
|
79 |
} |
|
80 |
} |
|
81 |
}); |
|
82 |
this.subscriptions.push(subscription); |
|
83 |
}); |
|
84 |
}); |
|
85 |
} |
|
86 |
|
|
87 |
public ngOnDestroy() { |
|
88 |
this.subscriptions.forEach(value => { |
|
89 |
if (value instanceof Subscriber) { |
|
90 |
value.unsubscribe(); |
|
21 |
public subscriptions: any[] = []; |
|
22 |
public properties: EnvProperties; |
|
23 |
public stakeholderUtils: StakeholderUtils = new StakeholderUtils(); |
|
24 |
public loading: boolean = true; |
|
25 |
public stakeholder: Stakeholder; |
|
26 |
/** |
|
27 |
* Current topic |
|
28 |
**/ |
|
29 |
public topicIndex: number = 0; |
|
30 |
/** |
|
31 |
* categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened) |
|
32 |
*/ |
|
33 |
public categoryIndex: number = 0; |
|
34 |
public selectedCategoryIndex: number = 0; |
|
35 |
/** |
|
36 |
* Current Subcategory to be edited |
|
37 |
*/ |
|
38 |
public subCategoryIndex: number = 0; |
|
39 |
/** |
|
40 |
* Current element and index of topic, category or subcategory to be deleted. |
|
41 |
*/ |
|
42 |
public form: FormGroup; |
|
43 |
public element: Topic | Category | SubCategory; |
|
44 |
public type: 'topic' | 'category' | 'subcategory' = "topic"; |
|
45 |
public index: number = -1; |
|
46 |
/** |
|
47 |
* Check form validity |
|
48 |
*/ |
|
49 |
public toggle: boolean = false; |
|
50 |
|
|
51 |
@ViewChild('deleteModal') deleteModal: AlertModal; |
|
52 |
@ViewChild('editModal') editModal: AlertModal; |
|
53 |
|
|
54 |
constructor( |
|
55 |
private route: ActivatedRoute, |
|
56 |
private router: Router, |
|
57 |
private title: Title, |
|
58 |
private fb: FormBuilder, |
|
59 |
private stakeholderService: StakeholderService) { |
|
60 |
} |
|
61 |
|
|
62 |
public ngOnInit() { |
|
63 |
this.route.data |
|
64 |
.subscribe((data: { envSpecific: EnvProperties }) => { |
|
65 |
this.properties = data.envSpecific; |
|
66 |
let subscription: Subscription; |
|
67 |
this.route.params.subscribe(params => { |
|
68 |
if (subscription) { |
|
69 |
subscription.unsubscribe(); |
|
70 |
} |
|
71 |
subscription = this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => { |
|
72 |
if (stakeholder) { |
|
73 |
this.stakeholder = HelperFunctions.copy(stakeholder); |
|
74 |
this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']); |
|
75 |
if (this.topicIndex === -1) { |
|
76 |
this.navigateToError(); |
|
77 |
} else { |
|
78 |
this.title.setTitle(stakeholder.index_shortName + ' | ' + this.stakeholder.topics[this.topicIndex].name); |
|
79 |
this.toggle = true; |
|
80 |
} |
|
91 | 81 |
} |
82 |
}); |
|
83 |
this.subscriptions.push(subscription); |
|
92 | 84 |
}); |
85 |
}); |
|
86 |
} |
|
87 |
|
|
88 |
public ngOnDestroy() { |
|
89 |
this.subscriptions.forEach(value => { |
|
90 |
if (value instanceof Subscriber) { |
|
91 |
value.unsubscribe(); |
|
92 |
} |
|
93 |
}); |
|
94 |
} |
|
95 |
|
|
96 |
public saveElement() { |
|
97 |
if (this.type === "topic") { |
|
98 |
this.saveTopic(); |
|
99 |
} else if (this.type === "category") { |
|
100 |
this.saveCategory(); |
|
101 |
} else { |
|
102 |
this.saveSubCategory(); |
|
93 | 103 |
} |
94 |
|
|
95 |
public saveElement() { |
|
96 |
if(this.type === "topic") {
|
|
97 |
this.saveTopic();
|
|
98 |
} else if(this.type === "category") {
|
|
99 |
this.saveCategory();
|
|
100 |
} else {
|
|
101 |
this.saveSubCategory();
|
|
102 |
}
|
|
104 |
} |
|
105 |
|
|
106 |
public deleteElement() {
|
|
107 |
if (this.type === "topic") {
|
|
108 |
this.deleteTopic();
|
|
109 |
} else if (this.type === "category") {
|
|
110 |
this.deleteCategory();
|
|
111 |
} else {
|
|
112 |
this.deleteSubcategory();
|
|
103 | 113 |
} |
104 |
|
|
105 |
public deleteElement() { |
|
106 |
if(this.type === "topic") { |
|
107 |
this.deleteTopic(); |
|
108 |
} else if(this.type === "category") { |
|
109 |
this.deleteCategory(); |
|
110 |
} else { |
|
111 |
this.deleteSubcategory(); |
|
112 |
} |
|
114 |
} |
|
115 |
|
|
116 |
private buildTopic(topic: Topic) { |
|
117 |
let topics = this.stakeholder.topics.filter(element => element._id !== topic._id); |
|
118 |
this.form = this.fb.group({ |
|
119 |
_id: this.fb.control(topic._id), |
|
120 |
name: this.fb.control(topic.name, Validators.required), |
|
121 |
description: this.fb.control(topic.description), |
|
122 |
alias: this.fb.control(topic.alias, [ |
|
123 |
Validators.required, |
|
124 |
this.stakeholderUtils.aliasValidator(topics) |
|
125 |
] |
|
126 |
), |
|
127 |
isActive: this.fb.control(topic.isActive), |
|
128 |
isPublic: this.fb.control(topic.isPublic), |
|
129 |
isDefault: this.fb.control(topic.isDefault), |
|
130 |
categories: this.fb.control(topic.categories) |
|
131 |
}); |
|
132 |
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => { |
|
133 |
let i = 1; |
|
134 |
value = this.stakeholderUtils.generateAlias(value); |
|
135 |
this.form.controls['alias'].setValue(value); |
|
136 |
while (this.form.get('alias').invalid) { |
|
137 |
this.form.controls['alias'].setValue(value + i); |
|
138 |
i++; |
|
139 |
} |
|
140 |
})); |
|
141 |
} |
|
142 |
|
|
143 |
public editTopicOpen() { |
|
144 |
this.index = this.topicIndex; |
|
145 |
this.type = 'topic'; |
|
146 |
this.buildTopic(this.stakeholder.topics[this.index]); |
|
147 |
this.editOpen(); |
|
148 |
} |
|
149 |
|
|
150 |
public saveTopic() { |
|
151 |
if (!this.form.invalid) { |
|
152 |
let path = [this.stakeholder._id]; |
|
153 |
let callback = (topic: Topic): void => { |
|
154 |
this.stakeholder.topics[this.index] = topic; |
|
155 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
156 |
}; |
|
157 |
this.save('Topic has been successfully saved', path, this.form.value, callback, true); |
|
113 | 158 |
} |
114 |
|
|
115 |
private buildTopic(topic: Topic) { |
|
116 |
let topics = this.stakeholder.topics.filter(element => element._id !== topic._id); |
|
117 |
this.form = this.fb.group({ |
|
118 |
_id: this.fb.control(topic._id), |
|
119 |
name: this.fb.control(topic.name, Validators.required), |
|
120 |
description: this.fb.control(topic.description), |
|
121 |
alias: this.fb.control(topic.alias, [ |
|
122 |
Validators.required, |
|
123 |
this.stakeholderUtils.aliasValidator(topics) |
|
124 |
] |
|
125 |
), |
|
126 |
isActive: this.fb.control(topic.isActive), |
|
127 |
isPublic: this.fb.control(topic.isPublic), |
|
128 |
isDefault: this.fb.control(topic.isDefault), |
|
129 |
categories: this.fb.control(topic.categories) |
|
130 |
}); |
|
131 |
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => { |
|
132 |
let i = 1; |
|
133 |
value = this.stakeholderUtils.generateAlias(value); |
|
134 |
this.form.controls['alias'].setValue(value); |
|
135 |
while (this.form.get('alias').invalid) { |
|
136 |
this.form.controls['alias'].setValue(value + i); |
|
137 |
i++; |
|
138 |
} |
|
139 |
})); |
|
159 |
} |
|
160 |
|
|
161 |
public toggleTopicStatus() { |
|
162 |
this.index = this.topicIndex; |
|
163 |
this.type = 'topic'; |
|
164 |
let path = [ |
|
165 |
this.stakeholder._id, |
|
166 |
this.stakeholder.topics[this.topicIndex]._id |
|
167 |
]; |
|
168 |
this.toggleStatus(this.stakeholder.topics[this.topicIndex], path); |
|
169 |
} |
|
170 |
|
|
171 |
public toggleTopicAccess() { |
|
172 |
this.index = this.topicIndex; |
|
173 |
this.type = 'topic'; |
|
174 |
let path = [ |
|
175 |
this.stakeholder._id, |
|
176 |
this.stakeholder.topics[this.topicIndex]._id |
|
177 |
]; |
|
178 |
this.toggleAccess(this.stakeholder.topics[this.topicIndex], path); |
|
179 |
} |
|
180 |
|
|
181 |
public deleteTopicOpen() { |
|
182 |
this.type = 'topic'; |
|
183 |
this.index = this.topicIndex; |
|
184 |
this.element = this.stakeholder.topics[this.index]; |
|
185 |
this.deleteOpen(); |
|
186 |
} |
|
187 |
|
|
188 |
public deleteTopic() { |
|
189 |
let path: string[] = [ |
|
190 |
this.stakeholder._id, |
|
191 |
this.stakeholder.topics[this.index]._id |
|
192 |
]; |
|
193 |
let callback = (): void => { |
|
194 |
this.stakeholder.topics.splice(this.index, 1); |
|
195 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
196 |
}; |
|
197 |
this.delete('Topic has been successfully be deleted', path, callback, true); |
|
198 |
} |
|
199 |
|
|
200 |
public toggleCategory(index: number) { |
|
201 |
if (this.selectedCategoryIndex !== index) { |
|
202 |
this.selectedCategoryIndex = index; |
|
203 |
this.toggle = true; |
|
204 |
} else { |
|
205 |
this.toggle = !this.toggle; |
|
140 | 206 |
} |
141 |
|
|
142 |
public editTopicOpen() { |
|
143 |
this.index = this.topicIndex; |
|
144 |
this.type = 'topic'; |
|
145 |
this.buildTopic(this.stakeholder.topics[this.index]); |
|
146 |
this.editOpen(); |
|
207 |
} |
|
208 |
|
|
209 |
private buildCategory(category: Category) { |
|
210 |
let categories = this.stakeholder.topics[this.topicIndex].categories.filter(element => element._id !== category._id); |
|
211 |
this.form = this.fb.group({ |
|
212 |
_id: this.fb.control(category._id), |
|
213 |
name: this.fb.control(category.name, Validators.required), |
|
214 |
description: this.fb.control(category.description), |
|
215 |
alias: this.fb.control(category.alias, [ |
|
216 |
Validators.required, |
|
217 |
this.stakeholderUtils.aliasValidator(categories) |
|
218 |
] |
|
219 |
), |
|
220 |
isActive: this.fb.control(category.isActive), |
|
221 |
isPublic: this.fb.control(category.isPublic), |
|
222 |
isDefault: this.fb.control(category.isDefault), |
|
223 |
subCategories: this.fb.control(category.subCategories) |
|
224 |
}); |
|
225 |
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => { |
|
226 |
let i = 1; |
|
227 |
value = this.stakeholderUtils.generateAlias(value); |
|
228 |
this.form.controls['alias'].setValue(value); |
|
229 |
while (this.form.get('alias').invalid) { |
|
230 |
this.form.controls['alias'].setValue(value + i); |
|
231 |
i++; |
|
232 |
} |
|
233 |
})); |
|
234 |
} |
|
235 |
|
|
236 |
public editCategoryOpen(index: number = -1) { |
|
237 |
this.index = index; |
|
238 |
this.type = 'category'; |
|
239 |
if (index === -1) { |
|
240 |
this.buildCategory(new Category(null, null, null, true, true)); |
|
241 |
} else { |
|
242 |
this.buildCategory(this.stakeholder.topics[this.topicIndex].categories[index]); |
|
147 | 243 |
} |
148 |
|
|
149 |
public saveTopic() { |
|
150 |
if (!this.form.invalid) { |
|
151 |
let path = [this.stakeholder._id]; |
|
152 |
let callback = (topic: Topic): void => { |
|
153 |
this.stakeholder.topics[this.index] = topic; |
|
154 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
155 |
}; |
|
156 |
this.save('Topic has been successfully saved', path, this.form.value, callback, true); |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
public deleteTopicOpen() { |
|
161 |
this.type = 'topic'; |
|
162 |
this.index = this.topicIndex; |
|
163 |
this.element = this.stakeholder.topics[this.index]; |
|
164 |
this.deleteOpen(); |
|
165 |
} |
|
166 |
|
|
167 |
public deleteTopic() { |
|
168 |
let path: string[] = [ |
|
169 |
this.stakeholder._id, |
|
170 |
this.stakeholder.topics[this.index]._id |
|
171 |
]; |
|
172 |
let callback = (): void => { |
|
173 |
this.stakeholder.topics.splice(this.index, 1); |
|
174 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
175 |
}; |
|
176 |
this.delete('Topic has been successfully be deleted', path, callback, true); |
|
177 |
} |
|
178 |
|
|
179 |
public toggleCategory(index: number) { |
|
180 |
if (this.selectedCategoryIndex !== index) { |
|
181 |
this.selectedCategoryIndex = index; |
|
182 |
this.toggle = true; |
|
244 |
this.editOpen(); |
|
245 |
} |
|
246 |
|
|
247 |
public saveCategory() { |
|
248 |
if (!this.form.invalid) { |
|
249 |
let path = [this.stakeholder._id, this.stakeholder.topics[this.topicIndex]._id]; |
|
250 |
let callback = (category: Category): void => { |
|
251 |
if (this.index === -1) { |
|
252 |
this.stakeholder.topics[this.topicIndex].categories.push(category); |
|
183 | 253 |
} else { |
184 |
this.toggle = !this.toggle;
|
|
254 |
this.stakeholder.topics[this.topicIndex].categories[this.index] = HelperFunctions.copy(category);
|
|
185 | 255 |
} |
256 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
257 |
}; |
|
258 |
if (this.index === -1) { |
|
259 |
this.save('Category has been successfully created', path, this.form.value, callback); |
|
260 |
} else { |
|
261 |
this.save('Category has been successfully saved', path, this.form.value, callback); |
|
262 |
} |
|
186 | 263 |
} |
187 |
|
|
188 |
private buildCategory(category: Category) { |
|
189 |
let categories = this.stakeholder.topics[this.topicIndex].categories.filter(element => element._id !== category._id); |
|
190 |
this.form = this.fb.group({ |
|
191 |
_id: this.fb.control(category._id), |
|
192 |
name: this.fb.control(category.name, Validators.required), |
|
193 |
description: this.fb.control(category.description), |
|
194 |
alias: this.fb.control(category.alias, [ |
|
195 |
Validators.required, |
|
196 |
this.stakeholderUtils.aliasValidator(categories) |
|
197 |
] |
|
198 |
), |
|
199 |
isActive: this.fb.control(category.isActive), |
|
200 |
isPublic: this.fb.control(category.isPublic), |
|
201 |
isDefault: this.fb.control(category.isDefault), |
|
202 |
subCategories: this.fb.control(category.subCategories) |
|
203 |
}); |
|
204 |
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => { |
|
205 |
let i = 1; |
|
206 |
value = this.stakeholderUtils.generateAlias(value); |
|
207 |
this.form.controls['alias'].setValue(value); |
|
208 |
while (this.form.get('alias').invalid) { |
|
209 |
this.form.controls['alias'].setValue(value + i); |
|
210 |
i++; |
|
211 |
} |
|
212 |
})); |
|
264 |
} |
|
265 |
|
|
266 |
public toggleCategoryStatus(index: number) { |
|
267 |
this.index = index; |
|
268 |
this.type = 'category'; |
|
269 |
let path = [ |
|
270 |
this.stakeholder._id, |
|
271 |
this.stakeholder.topics[this.topicIndex]._id, |
|
272 |
this.stakeholder.topics[this.topicIndex].categories[this.index]._id |
|
273 |
]; |
|
274 |
this.toggleStatus(this.stakeholder.topics[this.topicIndex].categories[this.index], path); |
|
275 |
} |
|
276 |
|
|
277 |
public toggleCategoryAccess(index: number) { |
|
278 |
this.index = index; |
|
279 |
this.type = 'category'; |
|
280 |
let path = [ |
|
281 |
this.stakeholder._id, |
|
282 |
this.stakeholder.topics[this.topicIndex]._id, |
|
283 |
this.stakeholder.topics[this.topicIndex].categories[this.index]._id |
|
284 |
]; |
|
285 |
this.toggleAccess(this.stakeholder.topics[this.topicIndex].categories[this.index], path); |
|
286 |
} |
|
287 |
|
|
288 |
public deleteCategoryOpen(index: number) { |
|
289 |
this.type = 'category'; |
|
290 |
this.index = index; |
|
291 |
this.element = this.stakeholder.topics[this.topicIndex].categories[this.index]; |
|
292 |
this.deleteOpen(); |
|
293 |
} |
|
294 |
|
|
295 |
public deleteCategory() { |
|
296 |
let path: string[] = [ |
|
297 |
this.stakeholder._id, |
|
298 |
this.stakeholder.topics[this.topicIndex]._id, |
|
299 |
this.stakeholder.topics[this.topicIndex].categories[this.index]._id |
|
300 |
]; |
|
301 |
let callback = (): void => { |
|
302 |
this.stakeholder.topics[this.topicIndex].categories.splice(this.index, 1); |
|
303 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
304 |
}; |
|
305 |
this.delete('Category has been successfully be deleted', path, callback); |
|
306 |
} |
|
307 |
|
|
308 |
private buildSubcategory(subCategory: SubCategory) { |
|
309 |
let subCategories = this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.filter(element => element._id !== subCategory._id); |
|
310 |
this.form = this.fb.group({ |
|
311 |
_id: this.fb.control(subCategory._id), |
|
312 |
name: this.fb.control(subCategory.name, Validators.required), |
|
313 |
description: this.fb.control(subCategory.description), |
|
314 |
alias: this.fb.control(subCategory.alias, [ |
|
315 |
Validators.required, |
|
316 |
this.stakeholderUtils.aliasValidator(subCategories) |
|
317 |
] |
|
318 |
), |
|
319 |
isActive: this.fb.control(subCategory.isActive), |
|
320 |
isPublic: this.fb.control(subCategory.isPublic), |
|
321 |
isDefault: this.fb.control(subCategory.isDefault), |
|
322 |
charts: this.fb.control(subCategory.charts), |
|
323 |
numbers: this.fb.control(subCategory.numbers) |
|
324 |
}); |
|
325 |
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => { |
|
326 |
let i = 1; |
|
327 |
value = this.stakeholderUtils.generateAlias(value); |
|
328 |
this.form.controls['alias'].setValue(value); |
|
329 |
while (this.form.get('alias').invalid) { |
|
330 |
this.form.controls['alias'].setValue(value + i); |
|
331 |
i++; |
|
332 |
} |
|
333 |
})); |
|
334 |
} |
|
335 |
|
|
336 |
public editSubCategoryOpen(index: number = -1) { |
|
337 |
this.index = index; |
|
338 |
this.type = 'subcategory'; |
|
339 |
if (index === -1) { |
|
340 |
this.buildSubcategory(new SubCategory(null, null, null, true, true)); |
|
341 |
} else { |
|
342 |
this.buildSubcategory(this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[index]); |
|
213 | 343 |
} |
214 |
|
|
215 |
public editCategoryOpen(index: number = -1) { |
|
216 |
this.index = index; |
|
217 |
this.type = 'category'; |
|
218 |
if (index === -1) { |
|
219 |
this.buildCategory(new Category(null, null, null, true, true)); |
|
344 |
this.editOpen(); |
|
345 |
} |
|
346 |
|
|
347 |
public saveSubCategory() { |
|
348 |
if (!this.form.invalid) { |
|
349 |
let path: string[] = [ |
|
350 |
this.stakeholder._id, |
|
351 |
this.stakeholder.topics[this.topicIndex]._id, |
|
352 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id, |
|
353 |
]; |
|
354 |
let callback = (subCategory: SubCategory): void => { |
|
355 |
if (this.index === -1) { |
|
356 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.push(subCategory); |
|
220 | 357 |
} else { |
221 |
this.buildCategory(this.stakeholder.topics[this.topicIndex].categories[index]);
|
|
358 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index] = subCategory;
|
|
222 | 359 |
} |
223 |
this.editOpen(); |
|
360 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
361 |
}; |
|
362 |
if (this.index === -1) { |
|
363 |
this.save('Subcategory has been successfully created', path, this.form.value, callback); |
|
364 |
} else { |
|
365 |
this.save('Subcategory has been successfully saved', path, this.form.value, callback); |
|
366 |
} |
|
224 | 367 |
} |
225 |
|
|
226 |
public saveCategory() { |
|
227 |
if (!this.form.invalid) { |
|
228 |
let path = [this.stakeholder._id, this.stakeholder.topics[this.topicIndex]._id]; |
|
229 |
let callback = (category: Category): void => { |
|
230 |
if (this.index === -1) { |
|
231 |
this.stakeholder.topics[this.topicIndex].categories.push(category); |
|
232 |
} else { |
|
233 |
this.stakeholder.topics[this.topicIndex].categories[this.index] = HelperFunctions.copy(category); |
|
234 |
} |
|
235 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
236 |
}; |
|
237 |
if (this.index === -1) { |
|
238 |
this.save('Category has been successfully created', path, this.form.value, callback); |
|
239 |
} else { |
|
240 |
this.save('Category has been successfully saved', path, this.form.value, callback); |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
public deleteCategoryOpen(index: number) { |
|
246 |
this.type = 'category'; |
|
368 |
} |
|
369 |
|
|
370 |
public toggleSubcategoryStatus(index: number) { |
|
247 | 371 |
this.index = index; |
248 |
this.element = this.stakeholder.topics[this.topicIndex].categories[this.index]; |
|
249 |
this.deleteOpen(); |
|
250 |
} |
|
251 |
|
|
252 |
public deleteCategory() { |
|
253 |
let path: string[] = [ |
|
372 |
this.type = 'subcategory'; |
|
373 |
let path = [ |
|
254 | 374 |
this.stakeholder._id, |
255 | 375 |
this.stakeholder.topics[this.topicIndex]._id, |
256 |
this.stakeholder.topics[this.topicIndex].categories[this.index]._id |
|
376 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id, |
|
377 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]._id |
|
257 | 378 |
]; |
258 |
let callback = (): void => { |
|
259 |
this.stakeholder.topics[this.topicIndex].categories.splice(this.index, 1); |
|
260 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
261 |
}; |
|
262 |
this.delete('Category has been successfully be deleted', path, callback); |
|
379 |
this.toggleStatus(this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index], path); |
|
263 | 380 |
} |
264 |
|
|
265 |
private buildSubcategory(subCategory: SubCategory) { |
|
266 |
let subCategories = this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.filter(element => element._id !== subCategory._id); |
|
267 |
this.form = this.fb.group({ |
|
268 |
_id: this.fb.control(subCategory._id), |
|
269 |
name: this.fb.control(subCategory.name, Validators.required), |
|
270 |
description: this.fb.control(subCategory.description), |
|
271 |
alias: this.fb.control(subCategory.alias, [ |
|
272 |
Validators.required, |
|
273 |
this.stakeholderUtils.aliasValidator(subCategories) |
|
274 |
] |
|
275 |
), |
|
276 |
isActive: this.fb.control(subCategory.isActive), |
|
277 |
isPublic: this.fb.control(subCategory.isPublic), |
|
278 |
isDefault: this.fb.control(subCategory.isDefault), |
|
279 |
charts: this.fb.control(subCategory.charts), |
|
280 |
numbers: this.fb.control(subCategory.numbers) |
|
281 |
}); |
|
282 |
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => { |
|
283 |
let i = 1; |
|
284 |
value = this.stakeholderUtils.generateAlias(value); |
|
285 |
this.form.controls['alias'].setValue(value); |
|
286 |
while (this.form.get('alias').invalid) { |
|
287 |
this.form.controls['alias'].setValue(value + i); |
|
288 |
i++; |
|
289 |
} |
|
290 |
})); |
|
291 |
} |
|
292 |
|
|
293 |
public editSubCategoryOpen(index: number = -1) { |
|
381 |
|
|
382 |
public toggleSubcategoryAccess(index: number) { |
|
294 | 383 |
this.index = index; |
295 | 384 |
this.type = 'subcategory'; |
296 |
if (index === -1) { |
|
297 |
this.buildSubcategory(new SubCategory(null, null, null, true, true)); |
|
298 |
} else { |
|
299 |
this.buildSubcategory(this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[index]); |
|
300 |
} |
|
301 |
this.editOpen(); |
|
302 |
} |
|
303 |
|
|
304 |
public saveSubCategory() { |
|
305 |
if (!this.form.invalid) { |
|
306 |
let path: string[] = [ |
|
307 |
this.stakeholder._id, |
|
308 |
this.stakeholder.topics[this.topicIndex]._id, |
|
309 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id, |
|
310 |
]; |
|
311 |
let callback = (subCategory: SubCategory): void => { |
|
312 |
if (this.index === -1) { |
|
313 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.push(subCategory); |
|
314 |
} else { |
|
315 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index] = subCategory; |
|
316 |
} |
|
317 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
318 |
}; |
|
319 |
if (this.index === -1) { |
|
320 |
this.save('Subcategory has been successfully created', path, this.form.value, callback); |
|
321 |
} else { |
|
322 |
this.save('Subcategory has been successfully saved', path, this.form.value, callback); |
|
323 |
} |
|
324 |
} |
|
325 |
} |
|
326 |
|
|
327 |
public deleteSubcategoryOpen(index) { |
|
328 |
this.type = 'subcategory'; |
|
329 |
this.index = index; |
|
330 |
this.element = this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]; |
|
331 |
this.deleteOpen(); |
|
332 |
} |
|
333 |
|
|
334 |
public deleteSubcategory() { |
|
335 |
let path: string[] = [ |
|
385 |
let path = [ |
|
336 | 386 |
this.stakeholder._id, |
337 | 387 |
this.stakeholder.topics[this.topicIndex]._id, |
338 | 388 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id, |
339 | 389 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]._id |
340 | 390 |
]; |
341 |
let callback = (): void => { |
|
342 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.splice(this.index, 1); |
|
343 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
344 |
}; |
|
345 |
this.delete('Subcategory has been successfully be deleted', path, callback); |
|
391 |
this.toggleAccess(this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index], path); |
|
346 | 392 |
} |
347 |
|
|
348 |
private navigateToError() { |
|
349 |
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}}); |
|
393 |
|
|
394 |
public deleteSubcategoryOpen(index) { |
|
395 |
this.type = 'subcategory'; |
|
396 |
this.index = index; |
|
397 |
this.element = this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]; |
|
398 |
this.deleteOpen(); |
|
399 |
} |
|
400 |
|
|
401 |
public deleteSubcategory() { |
|
402 |
let path: string[] = [ |
|
403 |
this.stakeholder._id, |
|
404 |
this.stakeholder.topics[this.topicIndex]._id, |
|
405 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]._id, |
|
406 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index]._id |
|
407 |
]; |
|
408 |
let callback = (): void => { |
|
409 |
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories.splice(this.index, 1); |
|
410 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
411 |
}; |
|
412 |
this.delete('Subcategory has been successfully be deleted', path, callback); |
|
413 |
} |
|
414 |
|
|
415 |
private navigateToError() { |
|
416 |
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}}); |
|
417 |
} |
|
418 |
|
|
419 |
private editOpen() { |
|
420 |
this.editModal.cancelButtonText = 'Cancel'; |
|
421 |
this.editModal.okButtonLeft = false; |
|
422 |
this.editModal.alertMessage = false; |
|
423 |
if (this.index === -1) { |
|
424 |
this.editModal.alertTitle = 'Create a new ' + this.type; |
|
425 |
this.editModal.okButtonText = 'Create'; |
|
426 |
} else { |
|
427 |
this.editModal.alertTitle = 'Edit ' + this.type + '\'s information '; |
|
428 |
this.editModal.okButtonText = 'Save'; |
|
350 | 429 |
} |
351 |
|
|
352 |
private editOpen() { |
|
353 |
this.editModal.cancelButtonText = 'Cancel'; |
|
354 |
this.editModal.okButtonLeft = false; |
|
355 |
this.editModal.alertMessage = false; |
|
356 |
if (this.index === -1) { |
|
357 |
this.editModal.alertTitle = 'Create a new ' + this.type; |
|
358 |
this.editModal.okButtonText = 'Create'; |
|
359 |
} else { |
|
360 |
this.editModal.alertTitle = 'Edit ' + this.type + '\'s information '; |
|
361 |
this.editModal.okButtonText = 'Save'; |
|
362 |
} |
|
363 |
this.editModal.open(); |
|
364 |
} |
|
365 |
|
|
366 |
private deleteOpen() { |
|
367 |
this.deleteModal.alertTitle = 'Delete ' + this.type; |
|
368 |
this.deleteModal.cancelButtonText = 'No'; |
|
369 |
this.deleteModal.okButtonText = 'Yes'; |
|
370 |
this.deleteModal.open(); |
|
371 |
} |
|
372 |
|
|
373 |
private save(message: string, path: string[], saveElement: any, callback: Function, redirect = false) { |
|
374 |
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, saveElement, path).subscribe(saveElement => { |
|
375 |
callback(saveElement); |
|
376 |
UIkit.notification(message, { |
|
377 |
status: 'success', |
|
378 |
timeout: 3000, |
|
379 |
pos: 'top-left' |
|
380 |
}); |
|
381 |
if (redirect) { |
|
382 |
this.router.navigate(['../' + saveElement.alias], { |
|
383 |
relativeTo: this.route |
|
384 |
}); |
|
385 |
} |
|
386 |
}, error => { |
|
387 |
UIkit.notification(error.error.message, { |
|
388 |
status: 'danger', |
|
389 |
timeout: 3000, |
|
390 |
pos: 'top-left' |
|
391 |
}); |
|
430 |
this.editModal.open(); |
|
431 |
} |
|
432 |
|
|
433 |
private deleteOpen() { |
|
434 |
this.deleteModal.alertTitle = 'Delete ' + this.type; |
|
435 |
this.deleteModal.cancelButtonText = 'No'; |
|
436 |
this.deleteModal.okButtonText = 'Yes'; |
|
437 |
this.deleteModal.open(); |
|
438 |
} |
|
439 |
|
|
440 |
private save(message: string, path: string[], saveElement: any, callback: Function, redirect = false) { |
|
441 |
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, saveElement, path).subscribe(saveElement => { |
|
442 |
callback(saveElement); |
|
443 |
UIkit.notification(message, { |
|
444 |
status: 'success', |
|
445 |
timeout: 3000, |
|
446 |
pos: 'top-left' |
|
447 |
}); |
|
448 |
if (redirect) { |
|
449 |
this.router.navigate(['../' + saveElement.alias], { |
|
450 |
relativeTo: this.route |
|
392 | 451 |
}); |
393 |
} |
|
394 |
|
|
395 |
private delete(message: string, path: string[], callback: Function, redirect = false) { |
|
396 |
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => { |
|
397 |
callback(); |
|
398 |
UIkit.notification(message, { |
|
399 |
status: 'success', |
|
400 |
timeout: 3000, |
|
401 |
pos: 'top-left' |
|
402 |
}); |
|
403 |
if (redirect) { |
|
404 |
this.back(); |
|
405 |
} |
|
406 |
}, error => { |
|
407 |
UIkit.notification(error.error.message, { |
|
408 |
status: 'danger', |
|
409 |
timeout: 3000, |
|
410 |
pos: 'top-left' |
|
411 |
}); |
|
412 |
}); |
|
413 |
} |
|
414 |
|
|
415 |
back() { |
|
416 |
this.router.navigate(['../'], { |
|
417 |
relativeTo: this.route |
|
418 |
}); |
|
419 |
} |
|
420 |
|
|
421 |
chooseSubcategory(categoryIndex: number, subcategoryIndex: number) { |
|
422 |
this.categoryIndex = categoryIndex; |
|
423 |
this.subCategoryIndex = subcategoryIndex; |
|
424 |
} |
|
452 |
} |
|
453 |
}, error => { |
|
454 |
UIkit.notification(error.error.message, { |
|
455 |
status: 'danger', |
|
456 |
timeout: 3000, |
|
457 |
pos: 'top-left' |
|
458 |
}); |
|
459 |
}); |
|
460 |
} |
|
461 |
|
|
462 |
private delete(message: string, path: string[], callback: Function, redirect = false) { |
|
463 |
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => { |
|
464 |
callback(); |
|
465 |
UIkit.notification(message, { |
|
466 |
status: 'success', |
|
467 |
timeout: 3000, |
|
468 |
pos: 'top-left' |
|
469 |
}); |
|
470 |
if (redirect) { |
|
471 |
this.back(); |
|
472 |
} |
|
473 |
}, error => { |
|
474 |
UIkit.notification(error.error.message, { |
|
475 |
status: 'danger', |
|
476 |
timeout: 3000, |
|
477 |
pos: 'top-left' |
|
478 |
}); |
|
479 |
}); |
|
480 |
} |
|
481 |
|
|
482 |
private toggleStatus(element: Topic | Category | SubCategory, path: string[]) { |
|
483 |
this.stakeholderService.toggleStatus(this.properties.monitorServiceAPIURL, path).subscribe(isActive => { |
|
484 |
element.isActive = isActive; |
|
485 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
486 |
UIkit.notification(StringUtils.capitalize(this.type) + ' has been successfully ' + (isActive ? 'activated' : 'deactivated'), { |
|
487 |
status: 'success', |
|
488 |
timeout: 3000, |
|
489 |
pos: 'top-left' |
|
490 |
}); |
|
491 |
}, error => { |
|
492 |
UIkit.notification(error.error.message, { |
|
493 |
status: 'danger', |
|
494 |
timeout: 3000, |
|
495 |
pos: 'top-left' |
|
496 |
}); |
|
497 |
}); |
|
498 |
} |
|
499 |
|
|
500 |
private toggleAccess(element: Topic | Category | SubCategory, path: string[]) { |
|
501 |
this.stakeholderService.toggleAccess(this.properties.monitorServiceAPIURL, path).subscribe(isPublic => { |
|
502 |
element.isPublic = isPublic; |
|
503 |
this.stakeholderService.setStakeholder(this.stakeholder); |
|
504 |
UIkit.notification(StringUtils.capitalize(this.type) + ' has been successfully changed to ' + (isPublic ? 'public' : 'private'), { |
|
505 |
status: 'success', |
|
506 |
timeout: 3000, |
|
507 |
pos: 'top-left' |
|
508 |
}); |
|
509 |
}, error => { |
|
510 |
UIkit.notification(error.error.message, { |
|
511 |
status: 'danger', |
|
512 |
timeout: 3000, |
|
513 |
pos: 'top-left' |
|
514 |
}); |
|
515 |
}); |
|
516 |
} |
|
517 |
|
|
518 |
back() { |
|
519 |
this.router.navigate(['../'], { |
|
520 |
relativeTo: this.route |
|
521 |
}); |
|
522 |
} |
|
523 |
|
|
524 |
chooseSubcategory(categoryIndex: number, subcategoryIndex: number) { |
|
525 |
this.categoryIndex = categoryIndex; |
|
526 |
this.subCategoryIndex = subcategoryIndex; |
|
527 |
} |
|
425 | 528 |
} |
Also available in: Unified diff
[Monitor Dashboard | Trunk]: Add toggle options on all levels. Change main.css for width of sidebar in medium screens