Project

General

Profile

1 58683 k.triantaf
import {
2
  AfterViewInit,
3
  ChangeDetectorRef,
4
  Component,
5
  Input,
6
  OnChanges,
7
  OnDestroy,
8
  OnInit,
9
  SimpleChanges,
10
  ViewChild
11
} from "@angular/core";
12 59675 argiro.kok
import {
13
  Indicator,
14 60004 k.triantaf
  IndicatorPath, IndicatorSize,
15 59675 argiro.kok
  IndicatorType,
16
  Section,
17
  Stakeholder,
18
  Visibility
19
} from "../openaireLibrary/monitor/entities/stakeholder";
20
import {IndicatorUtils, StakeholderUtils} from "../utils/indicator-utils";
21 57673 k.triantaf
import {FormArray, FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
22 57639 k.triantaf
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
23
import {StatisticsService} from "../utils/services/statistics.service";
24
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
25 58694 k.triantaf
import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser";
26 60104 k.triantaf
import {Reorder, StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
27 57673 k.triantaf
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
28 57697 k.triantaf
import {Subscriber} from "rxjs";
29 57936 argiro.kok
import {LayoutService} from "../openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
30 57969 k.triantaf
import {Router} from "@angular/router";
31 58870 k.triantaf
import {Session} from "../openaireLibrary/login/utils/helper.class";
32
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
33 60161 k.triantaf
import {StringUtils} from "../openaireLibrary/utils/string-utils.class";
34 57555 k.triantaf
35 57673 k.triantaf
declare var UIkit;
36
37 57555 k.triantaf
@Component({
38 57888 k.triantaf
  selector: 'indicators',
39 59736 k.triantaf
  templateUrl: './indicators.component.html',
40
  styleUrls: ['indicators.component.css']
41 57555 k.triantaf
})
42 57697 k.triantaf
export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterViewInit {
43 57888 k.triantaf
44
  @Input()
45
  public properties: EnvProperties = null;
46
  @Input()
47
  public topicIndex: number = 0;
48
  @Input()
49
  public categoryIndex: number = 0;
50
  @Input()
51
  public subcategoryIndex: number = 0;
52
  public stakeholder: Stakeholder = null;
53 58870 k.triantaf
  public user = null;
54 57969 k.triantaf
  public preview: string;
55 57888 k.triantaf
  public indicatorUtils: IndicatorUtils = new IndicatorUtils();
56 59675 argiro.kok
  public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
57 58870 k.triantaf
  public numberIndicatorFb: FormGroup;
58
  public chartIndicatorFb: FormGroup;
59 57978 k.triantaf
  public chartSections: FormArray;
60
  public numberSections: FormArray;
61 57888 k.triantaf
  /**
62
   * Editable indicator
63
   */
64 57959 k.triantaf
  public section: Section;
65 57888 k.triantaf
  public indicator: Indicator;
66 57922 k.triantaf
  public index: number = -1;
67 57888 k.triantaf
  /**
68
   * Displayed chart and numbers base on Top filters
69
   */
70 57959 k.triantaf
  public displayCharts: Section[] = [];
71
  public displayNumbers: Section[] = [];
72 57888 k.triantaf
  /**
73
   * Top filters
74
   */
75 59736 k.triantaf
  @Input()
76 57888 k.triantaf
  public filters: FormGroup;
77 58952 k.triantaf
  public editing: boolean = false;
78 58694 k.triantaf
  /** Safe Urls*/
79
  public safeUrls: Map<string, SafeResourceUrl> = new Map<string, SafeResourceUrl>([]);
80 59770 k.triantaf
  public numberResults: Map<string, number> = new Map<string, number>();
81 57888 k.triantaf
  private subscriptions: any[] = [];
82
  private urlSubscriptions: any[] = [];
83 58870 k.triantaf
  @ViewChild('editChartModal') editChartModal: AlertModal;
84
  @ViewChild('editNumberModal') editNumberModal: AlertModal;
85 57922 k.triantaf
  @ViewChild('deleteModal') deleteModal: AlertModal;
86 58980 konstantin
  //@ViewChild('deleteAllModal') deleteAllModal: AlertModal;
87
  //@ViewChild('deleteAndDisconnectModal') deleteAndDisconnectModal: AlertModal;
88
  //@ViewChild('deleteChartSectionModal') deleteChartSectionModal: AlertModal;
89
  //@ViewChild('deleteNumberSectionModal') deleteNumberSectionModal: AlertModal;
90
  @ViewChild('deleteSectionModal') deleteSectionModal: AlertModal;
91
  public sectionTypeToDelete: string;
92
  public sectionChildrenActionOnDelete: string;
93
  public indicatorChildrenActionOnDelete: string;
94 60245 k.triantaf
  private firstLoad: boolean = true;
95 58870 k.triantaf
96 59811 k.triantaf
  urlParameterizedMessage = null;
97 59775 k.triantaf
98
  constructor(private layoutService: LayoutService,
99 57888 k.triantaf
              private stakeholderService: StakeholderService,
100
              private statisticsService: StatisticsService,
101 58870 k.triantaf
              private userManagementService: UserManagementService,
102 57888 k.triantaf
              private fb: FormBuilder,
103 57969 k.triantaf
              private router: Router,
104 58683 k.triantaf
              private cdr: ChangeDetectorRef,
105 57888 k.triantaf
              private sanitizer: DomSanitizer) {
106
  }
107
108
  ngOnInit(): void {
109 59847 argiro.kok
    this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
110 58870 k.triantaf
      this.user = user;
111 59847 argiro.kok
    }));
112
    this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
113 58985 k.triantaf
      this.stakeholder = stakeholder;
114 59775 k.triantaf
      if (this.stakeholder) {
115 58985 k.triantaf
        this.buildFilters();
116
        this.buildSections();
117 60245 k.triantaf
        if(this.firstLoad) {
118
          this.filterCharts();
119
          this.filterNumbers();
120
          this.firstLoad = false;
121
        }
122 58998 argiro.kok
        this.setPreview();
123 58985 k.triantaf
      }
124 59847 argiro.kok
    }));
125 57888 k.triantaf
  }
126
127
  ngOnDestroy(): void {
128
    this.subscriptions.forEach(value => {
129
      if (value instanceof Subscriber) {
130
        value.unsubscribe();
131
      } else if (value instanceof Function) {
132
        value();
133
      }
134
    });
135 59847 argiro.kok
    this.urlSubscriptions.forEach(value => {
136
      if (value instanceof Subscriber) {
137
        value.unsubscribe();
138
      }
139
    });
140 57888 k.triantaf
  }
141
142
  ngAfterViewInit(): void {
143 58685 k.triantaf
    this.initReorder();
144
  }
145
146
  ngOnChanges(changes: SimpleChanges): void {
147
    if (this.canEdit) {
148
      if (changes.topicIndex || changes.categoryIndex || changes.subcategoryIndex) {
149
        this.buildFilters();
150 58952 k.triantaf
        this.initReorder();
151 58685 k.triantaf
      }
152
      this.filterCharts();
153
      this.filterNumbers();
154
    }
155 58985 k.triantaf
    this.setPreview();
156
  }
157
158 59770 k.triantaf
  setNumberIndicators() {
159
    this.numberResults.clear();
160
    let urls: Map<string, [number, number][]> = new Map<string, [number, number][]>();
161
    this.numbers.forEach((section, i) => {
162
      section.indicators.forEach((number, j) => {
163 59775 k.triantaf
        let url = this.indicatorUtils.getFullUrlWithFilters(this.stakeholder, number.indicatorPaths[0]);
164 59770 k.triantaf
        const pair = JSON.stringify([number.indicatorPaths[0].source, url]);
165
        const indexes = urls.get(pair) ? urls.get(pair) : [];
166
        indexes.push([i, j]);
167
        urls.set(pair, indexes);
168
      });
169
    });
170
    urls.forEach((indexes, pair) => {
171
      pair = JSON.parse(pair);
172 59847 argiro.kok
      this.subscriptions.push(this.statisticsService.getNumbers(this.statisticsService.getSourceType(pair[0]), pair[1]).subscribe(response => {
173 59770 k.triantaf
        indexes.forEach(([i, j]) => {
174
          let result = JSON.parse(JSON.stringify(response));
175
          this.numbers[i].indicators[j].indicatorPaths[0].jsonPath.forEach(jsonPath => {
176
            if (result) {
177
              result = result[jsonPath];
178
            }
179
          });
180 60161 k.triantaf
          if (typeof result === 'string' || typeof result === 'number') {
181
            result = Number(result);
182
            if (result === Number.NaN) {
183
              result = 0;
184
            }
185
          } else {
186
            result = 0;
187
          }
188 59811 k.triantaf
          this.numberResults.set(i + '-' + j, result);
189 59770 k.triantaf
        });
190 59847 argiro.kok
      }));
191 59770 k.triantaf
    });
192
  }
193
194 58985 k.triantaf
  setPreview() {
195 59775 k.triantaf
    if (this.stakeholder) {
196 58998 argiro.kok
      this.preview = '/' + this.stakeholder.alias;
197 59775 k.triantaf
      if (this.stakeholder.topics[this.topicIndex]) {
198 58998 argiro.kok
        this.preview = '/' + this.stakeholder.alias + '/' + this.stakeholder.topics[this.topicIndex].alias;
199
        if (this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]) {
200
          this.preview += '/' + this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].alias;
201
          if (this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]) {
202
            this.preview += '/' + this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].alias;
203
          }
204 58985 k.triantaf
        }
205 58908 k.triantaf
      }
206
    }
207 58685 k.triantaf
  }
208
209
  initReorder() {
210
    this.subscriptions.forEach(value => {
211
      if (value instanceof Function) {
212
        value();
213
      }
214
    });
215 57888 k.triantaf
    if (document !== undefined) {
216 60104 k.triantaf
      let callback = (list, type: IndicatorType, action: 'moved' | 'added' | 'removed'): void => {
217 57888 k.triantaf
        let items: HTMLCollection = list.current.children;
218
        let reordered = [];
219
        for (let i = 0; i < items.length; i++) {
220
          if (items.item(i).id) {
221
            reordered.push(items.item(i).id);
222
          }
223
        }
224 60104 k.triantaf
        let reorder: Reorder = {
225
          action: action,
226
          target: list.detail[1].id,
227
          ids: reordered
228
        }
229
        this.reorderIndicators(list.current.id.toString().split('-')[1], type, reorder);
230 57888 k.triantaf
      };
231 57959 k.triantaf
      this.numbers.forEach((section) => {
232 58918 k.triantaf
        this.subscriptions.push(UIkit.util.on(document, 'moved', '#number-' + section._id, (list): void => {
233 60104 k.triantaf
          callback(list, "number", 'moved');
234 57959 k.triantaf
        }));
235 58918 k.triantaf
        this.subscriptions.push(UIkit.util.on(document, 'added', '#number-' + section._id, (list): void => {
236 60104 k.triantaf
          callback(list, "number", 'added');
237 57959 k.triantaf
        }));
238 58918 k.triantaf
        this.subscriptions.push(UIkit.util.on(document, 'removed', '#number-' + section._id, (list): void => {
239 60104 k.triantaf
          callback(list, "number", 'removed');
240 57959 k.triantaf
        }));
241
      });
242
      this.charts.forEach((section) => {
243
        this.subscriptions.push(UIkit.util.on(document, 'moved', '#chart-' + section._id, (list): void => {
244 60104 k.triantaf
          callback(list, "chart", 'moved');
245 57959 k.triantaf
        }));
246
        this.subscriptions.push(UIkit.util.on(document, 'added', '#chart-' + section._id, (list): void => {
247 60104 k.triantaf
          callback(list, "chart", 'added');
248 57959 k.triantaf
        }));
249
        this.subscriptions.push(UIkit.util.on(document, 'removed', '#chart-' + section._id, (list): void => {
250 60104 k.triantaf
          callback(list, "chart", 'removed');
251 57959 k.triantaf
        }));
252
      });
253 57883 k.triantaf
    }
254 57888 k.triantaf
  }
255
256 57969 k.triantaf
  hide(element: any) {
257
    UIkit.dropdown(element).hide();
258
  }
259 59775 k.triantaf
260 57888 k.triantaf
  private buildFilters() {
261
    this.subscriptions.push(this.filters.get('chartType').valueChanges.subscribe(value => {
262
      this.onChartTypeChange(value);
263
    }));
264 59775 k.triantaf
265 57888 k.triantaf
    this.subscriptions.push(this.filters.get('status').valueChanges.subscribe(value => {
266
      this.onStatusChange(value);
267
    }));
268
    this.subscriptions.push(this.filters.get('keyword').valueChanges.subscribe(value => {
269
      this.onKeywordChange(value);
270
    }));
271
  }
272
273 57978 k.triantaf
  private buildSections() {
274
    this.numberSections = this.fb.array([]);
275
    this.numbers.forEach(section => {
276
      this.numberSections.push(this.fb.group({
277
        _id: this.fb.control(section._id),
278
        title: this.fb.control(section.title),
279 59891 k.triantaf
        creationDate: this.fb.control(section.creationDate),
280 57978 k.triantaf
        stakeholderAlias: this.fb.control(section.stakeholderAlias),
281
        defaultId: this.fb.control(section.defaultId),
282
        type: this.fb.control(section.type),
283
        indicators: this.fb.control(section.indicators)
284
      }));
285
    });
286
    this.chartSections = this.fb.array([]);
287
    this.charts.forEach(section => {
288
      this.chartSections.push(this.fb.group({
289
        _id: this.fb.control(section._id),
290
        title: this.fb.control(section.title),
291 59891 k.triantaf
        creationDate: this.fb.control(section.creationDate),
292 57978 k.triantaf
        stakeholderAlias: this.fb.control(section.stakeholderAlias),
293
        defaultId: this.fb.control(section.defaultId),
294
        type: this.fb.control(section.type),
295
        indicators: this.fb.control(section.indicators)
296
      }));
297
    });
298
  }
299
300 57888 k.triantaf
  filterCharts() {
301 59675 argiro.kok
    this.displayCharts = this.filterChartType(
302 57969 k.triantaf
      this.filterStatus(this.filterByKeyword(HelperFunctions.copy(this.charts), this.filters.value.keyword),
303 57888 k.triantaf
        this.filters.value.status),
304
      this.filters.value.chartType
305
    );
306 58683 k.triantaf
    this.displayCharts.forEach(section => {
307
      section.indicators.forEach(indicator => {
308
        indicator.indicatorPaths.forEach(indicatorPath => {
309 58823 argiro.kok
          let url = this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath);
310 58694 k.triantaf
          if (!this.safeUrls.get('url')) {
311
            indicatorPath.safeResourceUrl = this.getSecureUrlByStakeHolder(indicatorPath);
312
            this.safeUrls.set(url, indicatorPath.safeResourceUrl);
313
          }
314 58683 k.triantaf
        });
315
      })
316
    });
317 58685 k.triantaf
    this.buildSections();
318 57888 k.triantaf
  }
319
320
  filterNumbers() {
321 59675 argiro.kok
    this.displayNumbers = this.filterStatus(
322 57969 k.triantaf
      this.filterByKeyword(HelperFunctions.copy(this.numbers), this.filters.value.keyword),
323 59675 argiro.kok
      this.filters.value.status);
324 58685 k.triantaf
    this.buildSections();
325 59770 k.triantaf
    this.setNumberIndicators();
326 57888 k.triantaf
  }
327
328
  onChartTypeChange(value) {
329 57969 k.triantaf
    this.displayCharts = this.filterChartType(HelperFunctions.copy(this.charts), value);
330 57888 k.triantaf
  }
331
332 59775 k.triantaf
333 57888 k.triantaf
  onStatusChange(value) {
334 57969 k.triantaf
    this.displayCharts = this.filterStatus(HelperFunctions.copy(this.charts), value);
335
    this.displayNumbers = this.filterStatus(HelperFunctions.copy(this.numbers), value);
336 57888 k.triantaf
  }
337
338
  onKeywordChange(value) {
339 57969 k.triantaf
    this.displayCharts = this.filterByKeyword(HelperFunctions.copy(this.charts), value);
340
    this.displayNumbers = this.filterByKeyword(HelperFunctions.copy(this.numbers), value);
341 57888 k.triantaf
  }
342
343 57959 k.triantaf
  private filterChartType(sections: Section[], value): Section[] {
344 57969 k.triantaf
    if (value !== 'all') {
345
      sections.forEach(section =>
346 57959 k.triantaf
        section.indicators = section.indicators.filter(indicator =>
347
          indicator.indicatorPaths.filter(indicatorPath => indicatorPath.type === value).length > 0));
348 57555 k.triantaf
    }
349 57969 k.triantaf
    return sections;
350 57888 k.triantaf
  }
351
352
353 57959 k.triantaf
  private filterStatus(sections: Section[], value): Section[] {
354 57969 k.triantaf
    if (value !== 'all') {
355
      sections.forEach(section =>
356 59675 argiro.kok
        section.indicators = section.indicators.filter(indicator => indicator.visibility === value));
357 57883 k.triantaf
    }
358 57969 k.triantaf
    return sections;
359 57888 k.triantaf
  }
360
361 57959 k.triantaf
  private filterByKeyword(sections: Section[], value): Section[] {
362 57969 k.triantaf
    if (value !== null && value !== '') {
363
      sections.forEach(section =>
364 57959 k.triantaf
        section.indicators = section.indicators.filter(indicator => (indicator.name && indicator.name.toLowerCase().includes(value.toLowerCase()))
365 58870 k.triantaf
          || (indicator.description && indicator.description.toLowerCase().includes(value.toLowerCase()))
366 59775 k.triantaf
          || (indicator.additionalDescription && indicator.additionalDescription.toLowerCase().includes(value.toLowerCase()))
367 58870 k.triantaf
          || indicator.indicatorPaths.filter(indicatorPath => (indicatorPath.parameters && indicatorPath.parameters.title &&
368
            indicatorPath.parameters.title.includes(value.toLowerCase()))).length > 0));
369 57883 k.triantaf
    }
370 57969 k.triantaf
    return sections;
371 57888 k.triantaf
  }
372
373 57959 k.triantaf
  get charts(): Section[] {
374 58691 k.triantaf
    if (this.stakeholder.topics[this.topicIndex] &&
375
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
376
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]) {
377 58689 k.triantaf
      return this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts;
378
    } else {
379
      return [];
380
    }
381 57888 k.triantaf
  }
382
383 57959 k.triantaf
  set charts(sections: Section[]) {
384
    this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts = sections;
385 57888 k.triantaf
  }
386
387 57959 k.triantaf
  get numbers(): Section[] {
388 58691 k.triantaf
    if (this.stakeholder.topics[this.topicIndex] &&
389
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
390
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]) {
391 58689 k.triantaf
      return this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers;
392
    } else {
393
      return [];
394
    }
395 57888 k.triantaf
  }
396
397 57959 k.triantaf
  set numbers(sections: Section[]) {
398
    this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers = sections;
399 57888 k.triantaf
  }
400
401
  get open(): boolean {
402
    return this.layoutService.open;
403
  }
404
405 57959 k.triantaf
  get canReorder(): boolean {
406 59675 argiro.kok
    return this.filters.value.chartType === 'all' &&
407
      this.filters.value.status === 'all' && this.filters.value.keyword === '' && !this.editing;
408 57888 k.triantaf
  }
409
410
  get canEdit() {
411
    return this.stakeholder &&
412
      this.stakeholder.topics[this.topicIndex] &&
413
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
414
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex];
415
  }
416
417 58870 k.triantaf
  public get numberIndicatorPaths(): FormArray {
418
    return this.numberIndicatorFb.get('indicatorPaths') as FormArray;
419 57888 k.triantaf
  }
420
421 58870 k.triantaf
  public get chartIndicatorPaths(): FormArray {
422
    return this.chartIndicatorFb.get('indicatorPaths') as FormArray;
423
  }
424
425 60004 k.triantaf
  public getNumberClassBySize(size: IndicatorSize) {
426 60104 k.triantaf
    if (size === 'small') {
427 60004 k.triantaf
      return 'uk-width-1-4@xl uk-width-1-3@l uk-width-1-2@m uk-width-1-1';
428 60104 k.triantaf
    } else if (size === 'medium') {
429 60004 k.triantaf
      return 'uk-width-1-3@l uk-width-1-2@m uk-width-1-1';
430
    } else {
431
      return 'uk-width-1-2@l uk-width-1-1@m uk-width-1-1';
432
    }
433
  }
434
435
  public getChartClassBySize(size: IndicatorSize) {
436 60104 k.triantaf
    if (size === 'small') {
437 60004 k.triantaf
      return 'uk-width-1-3@xl uk-width-1-2@m uk-width-1-1';
438 60104 k.triantaf
    } else if (size === 'medium') {
439 60004 k.triantaf
      return 'uk-width-1-2@l uk-width-1-1';
440
    } else {
441
      return 'uk-width-1-1';
442
    }
443
  }
444
445 58870 k.triantaf
  public addJsonPath(index: number) {
446 59775 k.triantaf
    if (index == 0 && this.getJsonPath(index).getRawValue()[index].indexOf(",") != -1) {
447 60161 k.triantaf
      //if in the first path there are more than one paths comma separated, split them and autogenerate the forms
448 59224 argiro.kok
      let paths = this.getJsonPath(index).getRawValue()[index].split(",");
449 59775 k.triantaf
      for (let i = 0; i < paths.length; i++) {
450
        if (i != 0) {
451 59224 argiro.kok
          this.getJsonPath(index).push(this.fb.control('', Validators.required));
452
        }
453
      }
454
      this.getJsonPath(index).setValue(paths)
455 60161 k.triantaf
    } else {
456
      this.getJsonPath(index).push(this.fb.control('', Validators.required));
457 59224 argiro.kok
    }
458 58870 k.triantaf
  }
459
460 59775 k.triantaf
  public removeJsonPath(i: number, j: number) {
461 60234 k.triantaf
    if (this.getJsonPath(i).enabled) {
462 60161 k.triantaf
      this.getJsonPath(i).removeAt(j);
463 59775 k.triantaf
    }
464 58870 k.triantaf
  }
465
466 60161 k.triantaf
  public validateJsonPath(index: number, dirty: boolean = false) {
467 59775 k.triantaf
    let indicatorPath: FormGroup = <FormGroup>this.numberIndicatorPaths.at(index);
468 60104 k.triantaf
    if (this.indicator.defaultId === null) {
469 59891 k.triantaf
      this.getJsonPath(index).disable();
470
    }
471 59811 k.triantaf
    indicatorPath.get('result').setErrors({validating: true});
472 59847 argiro.kok
    this.subscriptions.push(this.statisticsService.getNumbers(null, indicatorPath.get('url').value).subscribe(response => {
473 59775 k.triantaf
      let result = JSON.parse(JSON.stringify(response));
474
      this.getJsonPath(index).controls.forEach(jsonPath => {
475
        if (result) {
476
          result = result[jsonPath.value];
477
        }
478
      });
479 59811 k.triantaf
      setTimeout(() => {
480 60104 k.triantaf
        if (this.indicator.defaultId === null) {
481 59891 k.triantaf
          this.getJsonPath(index).enable();
482 60234 k.triantaf
          if (dirty) {
483 60161 k.triantaf
            this.getJsonPath(index).markAsDirty();
484
          }
485 59891 k.triantaf
        }
486 59811 k.triantaf
        indicatorPath.get('result').setErrors(null);
487 60104 k.triantaf
        if (typeof result === 'string' || typeof result === 'number') {
488 59811 k.triantaf
          result = Number(result);
489
          if (result !== Number.NaN) {
490
            indicatorPath.get('result').setValue(result);
491
          } else {
492 60161 k.triantaf
            indicatorPath.get('result').setValue(0);
493 59811 k.triantaf
          }
494 59775 k.triantaf
        } else {
495 60161 k.triantaf
          indicatorPath.get('result').setValue(0);
496 59775 k.triantaf
        }
497 59811 k.triantaf
      }, 500);
498 60161 k.triantaf
    }, error => {
499
      setTimeout(() => {
500
        if (this.indicator.defaultId === null) {
501
          this.getJsonPath(index).enable();
502
          if (dirty) {
503
            this.getJsonPath(index).markAsDirty();
504
          }
505
        }
506
        indicatorPath.get('result').setErrors(null);
507
        indicatorPath.get('result').setValue(0);
508
      }, 500);
509 59847 argiro.kok
    }));
510 59775 k.triantaf
  }
511
512 58870 k.triantaf
  public getJsonPath(index: number): FormArray {
513
    return this.numberIndicatorPaths.at(index).get('jsonPath') as FormArray;
514
  }
515
516 59775 k.triantaf
  public getCurrentJsonPath(index: number): string[] {
517
    return this.section.indicators[this.index].indicatorPaths[index].jsonPath;
518
  }
519
520 57888 k.triantaf
  public getParameters(index: number): FormArray {
521 58870 k.triantaf
    return this.chartIndicatorPaths.at(index).get('parameters') as FormArray;
522 57888 k.triantaf
  }
523
524
  public getParameter(index: number, key: string): FormControl {
525
    return this.getParameters(index).controls.filter(control => control.value.key === key)[0] as FormControl;
526
  }
527
528
  private getSecureUrlByStakeHolder(indicatorPath: IndicatorPath) {
529
    return this.sanitizer.bypassSecurityTrustResourceUrl(
530 58823 argiro.kok
      this.statisticsService.getChartUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath)));
531 57888 k.triantaf
  }
532
533
  private getUrlByStakeHolder(indicatorPath: IndicatorPath) {
534 58823 argiro.kok
    return this.statisticsService.getChartUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath));
535 57888 k.triantaf
  }
536
537 59920 argiro.kok
  public addNumberIndicatorPath(url: string = '', parameters: FormArray = new FormArray([]), source: string = 'stats-tool', jsonPath: FormArray = new FormArray([])) {
538 59775 k.triantaf
    if (jsonPath.length === 0) {
539 58870 k.triantaf
      jsonPath.push(this.fb.control('', Validators.required));
540
    }
541
    this.numberIndicatorPaths.push(this.fb.group({
542 60161 k.triantaf
        url: this.fb.control(url, [Validators.required, StringUtils.urlValidator()]),
543 58870 k.triantaf
        jsonPath: jsonPath,
544 59811 k.triantaf
        result: this.fb.control(0, Validators.required),
545 59056 argiro.kok
        // parameters: parameters,
546 58870 k.triantaf
        source: this.fb.control(source, Validators.required)
547
      }
548
    ));
549 60234 k.triantaf
    let index = this.numberIndicatorPaths.length - 1;
550
    if (this.numberIndicatorPaths.at(index).get('url').valid) {
551
      this.validateJsonPath(index);
552
    }
553
    if (this.indicator.defaultId === null) {
554
      this.subscriptions.push(this.numberIndicatorPaths.at(index).get('url').valueChanges.subscribe(value => {
555
          this.numberIndicatorPaths.at(index).get('result').setValue(null);
556
          if (this.numberIndicatorPaths.at(index).get('url').valid) {
557
            let indicatorPath: IndicatorPath = this.indicatorUtils.generateIndicatorByNumberUrl(this.statisticsService.getNumberSource(value), value, this.stakeholder, this.numberIndicatorPaths.at(index).get('jsonPath').value, this.statisticsService.numberSources.get(this.statisticsService.getNumberSource(value)));
558
            if ((indicatorPath.chartObject && Object.keys(indicatorPath.parameters).indexOf("index_id") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_name") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_shortName") == -1)
559
              || (!indicatorPath.chartObject && indicatorPath.url.indexOf("index_id") == -1 && indicatorPath.url.indexOf("index_name") == -1 && (indicatorPath.url).indexOf("index_shortName") == -1)) {
560
              // default profile
561
              if (this.stakeholder.defaultId == null) {
562
                this.urlParameterizedMessage = "This indicator couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly."
563 59775 k.triantaf
              } else {
564 60234 k.triantaf
                this.urlParameterizedMessage = "This indicator couldn't be generated properly. Please make sure chart data is for the current stakeholder."
565 59224 argiro.kok
              }
566 59775 k.triantaf
            } else {
567 59811 k.triantaf
              this.urlParameterizedMessage = null;
568 59224 argiro.kok
            }
569 59811 k.triantaf
            if (this.indicator.indicatorPaths[index]) {
570 60234 k.triantaf
              this.indicator.indicatorPaths[index] = indicatorPath;
571
            } else {
572
              this.indicator.indicatorPaths.push(indicatorPath);
573 59811 k.triantaf
            }
574 60234 k.triantaf
            if (indicatorPath.source) {
575
              this.numberIndicatorPaths.at(index).get('source').setValue(indicatorPath.source);
576 59811 k.triantaf
            }
577 60234 k.triantaf
            if (indicatorPath.jsonPath.length > 1 && this.getJsonPath(index).length == 1) {
578
              let paths = indicatorPath.jsonPath;
579
              for (let i = 0; i < paths.length; i++) {
580
                if (i == this.getJsonPath(index).length) {
581
                  this.getJsonPath(index).push(this.fb.control('', Validators.required));
582
                }
583
              }
584
              this.getJsonPath(index).setValue(paths)
585
            }
586
          } else {
587
            this.urlParameterizedMessage = null;
588
          }
589
        })
590
      );
591
592
      this.subscriptions.push(this.numberIndicatorPaths.at(index).get('jsonPath').valueChanges.subscribe(value => {
593
          if (this.indicator.indicatorPaths[index]) {
594
            this.indicator.indicatorPaths[index].jsonPath = value;
595
          }
596
          this.numberIndicatorPaths.at(index).get('result').setValue(null);
597
        })
598
      );
599
      this.subscriptions.push(this.numberIndicatorPaths.at(index).get('source').valueChanges.subscribe(value => {
600
          if (this.indicator.indicatorPaths[index]) {
601
            this.indicator.indicatorPaths[index].source = value;
602
          }
603
        })
604
      );
605
    } else {
606
      this.numberIndicatorPaths.at(index).get('url').disable();
607
      this.numberIndicatorPaths.at(index).get('jsonPath').disable();
608
      this.numberIndicatorPaths.at(index).get('source').disable();
609 58924 argiro.kok
    }
610 58870 k.triantaf
  }
611
612 59775 k.triantaf
  public addChartIndicatorPath(value: string = '', parameters: FormArray = new FormArray([]), disableUrl: boolean = false, type: string = null) {
613 58870 k.triantaf
    this.chartIndicatorPaths.push(this.fb.group({
614 60161 k.triantaf
        url: this.fb.control(value, [Validators.required, StringUtils.urlValidator()]),
615 58824 argiro.kok
        parameters: parameters,
616
        type: this.fb.control(type)
617 57888 k.triantaf
      }
618
    ));
619 60234 k.triantaf
    let index = this.chartIndicatorPaths.length - 1;
620 59775 k.triantaf
    if (disableUrl) {
621 60234 k.triantaf
      this.chartIndicatorPaths.at(index).get('url').disable();
622 58707 k.triantaf
    } else {
623 60234 k.triantaf
      this.urlSubscriptions.push(this.chartIndicatorPaths.at(index).get('url').valueChanges.subscribe(value => {
624
        if (this.chartIndicatorPaths.at(index).get('url').valid) {
625
          let indicatorPath: IndicatorPath = this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(value), value, this.chartIndicatorPaths.at(index).get('type').value, this.stakeholder);
626
          if (indicatorPath.chartObject && Object.keys(indicatorPath.parameters).indexOf("index_id") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_name") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_shortName") == -1) {
627
            // default profile
628
            if (this.stakeholder.defaultId == null) {
629
              this.urlParameterizedMessage = "This chart couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly."
630 59811 k.triantaf
            } else {
631 60234 k.triantaf
              this.urlParameterizedMessage = "This chart couldn't be generated properly. Please make sure chart data is for the current stakeholder."
632 58707 k.triantaf
            }
633 60234 k.triantaf
          } else {
634
            this.urlParameterizedMessage = null;
635
          }
636
          (this.chartIndicatorPaths.at(index) as FormGroup).get('type').setValue(indicatorPath.type);
637
          let parameters = this.getParametersAsFormArray(indicatorPath);
638
          (this.chartIndicatorPaths.at(index) as FormGroup).setControl('parameters', parameters);
639
          if (!this.indicator.indicatorPaths[index]) {
640
            this.indicator.indicatorPaths[index] = indicatorPath;
641
            this.indicator.indicatorPaths[index].safeResourceUrl = this.getSecureUrlByStakeHolder(indicatorPath);
642
          } else {
643
            indicatorPath.safeResourceUrl = this.indicator.indicatorPaths[index].safeResourceUrl;
644
            this.indicator.indicatorPaths[index] = indicatorPath;
645
          }
646
        } else {
647
          this.urlParameterizedMessage = null;
648
        }
649
      }));
650 58707 k.triantaf
    }
651 57888 k.triantaf
  }
652
653 58870 k.triantaf
  private getJsonPathAsFormArray(indicatorPath: IndicatorPath): FormArray {
654
    let jsonPath = this.fb.array([]);
655
    if (indicatorPath.jsonPath) {
656
      indicatorPath.jsonPath.forEach(path => {
657
        jsonPath.push(this.fb.control(path, Validators.required));
658
      });
659
    }
660
    return jsonPath;
661
  }
662
663 57888 k.triantaf
  private getParametersAsFormArray(indicatorPath: IndicatorPath): FormArray {
664
    let parameters = this.fb.array([]);
665
    if (indicatorPath.parameters) {
666
      Object.keys(indicatorPath.parameters).forEach(key => {
667
        if (this.indicatorUtils.ignoredParameters.indexOf(key) === -1) {
668
          if (this.indicatorUtils.parametersValidators.has(key)) {
669
            parameters.push(this.fb.group({
670
              key: this.fb.control(key),
671
              value: this.fb.control(indicatorPath.parameters[key], this.indicatorUtils.parametersValidators.get(key))
672
            }));
673
          } else {
674
            parameters.push(this.fb.group({
675
              key: this.fb.control(key),
676
              value: this.fb.control(indicatorPath.parameters[key])
677
            }));
678
          }
679 57883 k.triantaf
        }
680 57888 k.triantaf
      });
681 57555 k.triantaf
    }
682 57888 k.triantaf
    return parameters;
683
  }
684
685 58870 k.triantaf
  public editNumberIndicatorOpen(section: Section, id = null) {
686 59811 k.triantaf
    this.urlParameterizedMessage = null;
687 58870 k.triantaf
    this.section = section;
688
    this.index = (id) ? section.indicators.findIndex(value => value._id === id) : -1;
689
    if (this.index !== -1) {
690
      this.indicator = HelperFunctions.copy(this.section.indicators[this.index]);
691
      this.numberIndicatorFb = this.fb.group({
692
        _id: this.fb.control(this.indicator._id),
693 58985 k.triantaf
        name: this.fb.control(this.indicator.name, Validators.required),
694 58870 k.triantaf
        description: this.fb.control(this.indicator.description),
695 59891 k.triantaf
        creationDate: this.fb.control(this.indicator.creationDate),
696 59713 argiro.kok
        additionalDescription: this.fb.control(this.indicator.additionalDescription),
697 59675 argiro.kok
        visibility: this.fb.control(this.indicator.visibility),
698 58870 k.triantaf
        indicatorPaths: this.fb.array([], Validators.required),
699
        type: this.fb.control(this.indicator.type),
700
        width: this.fb.control(this.indicator.width),
701 59980 argiro.kok
        height: this.fb.control(this.indicator.height),
702 58870 k.triantaf
        defaultId: this.fb.control(this.indicator.defaultId)
703
      });
704
      this.indicator.indicatorPaths.forEach(indicatorPath => {
705 59775 k.triantaf
        this.addNumberIndicatorPath(this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath)), indicatorPath.parameters, indicatorPath.source, this.getJsonPathAsFormArray(indicatorPath));
706 58870 k.triantaf
      });
707
    } else {
708 59980 argiro.kok
      this.indicator = new Indicator('', '', '', 'number', 'small', 'small', "PUBLIC", []);
709 58870 k.triantaf
      this.numberIndicatorFb = this.fb.group({
710
        _id: this.fb.control(this.indicator._id),
711 58985 k.triantaf
        name: this.fb.control(this.indicator.name, Validators.required),
712 58870 k.triantaf
        description: this.fb.control(this.indicator.description),
713 59713 argiro.kok
        additionalDescription: this.fb.control(this.indicator.additionalDescription),
714 59675 argiro.kok
        visibility: this.fb.control(this.indicator.visibility),
715 58870 k.triantaf
        indicatorPaths: this.fb.array([], Validators.required),
716
        type: this.fb.control(this.indicator.type),
717
        width: this.fb.control(this.indicator.width),
718 59980 argiro.kok
        height: this.fb.control(this.indicator.height),
719 58870 k.triantaf
        defaultId: this.fb.control(this.indicator.defaultId)
720
      });
721
      this.addNumberIndicatorPath();
722
    }
723 59811 k.triantaf
    if (this.indicator.defaultId) {
724 59776 k.triantaf
      setTimeout(() => {
725 59926 argiro.kok
        this.numberIndicatorFb.get('description').disable();
726 59776 k.triantaf
      }, 0);
727
    }
728 58870 k.triantaf
    this.editNumberModal.cancelButtonText = 'Cancel';
729
    this.editNumberModal.okButtonLeft = false;
730
    this.editNumberModal.alertMessage = false;
731
    if (this.index === -1) {
732
      this.editNumberModal.alertTitle = 'Create a new number indicator';
733
      this.editNumberModal.okButtonText = 'Save';
734
    } else {
735
      this.editNumberModal.alertTitle = 'Edit number indicator\'s information';
736
      this.editNumberModal.okButtonText = 'Save Changes';
737
    }
738
    this.editNumberModal.open();
739
  }
740
741 57959 k.triantaf
  public editChartIndicatorOpen(section: Section, id = null) {
742 59811 k.triantaf
    this.urlParameterizedMessage = null;
743 57888 k.triantaf
    this.urlSubscriptions.forEach(value => {
744
      if (value instanceof Subscriber) {
745
        value.unsubscribe();
746
      }
747
    });
748 57959 k.triantaf
    this.section = section;
749
    this.index = (id) ? section.indicators.findIndex(value => value._id === id) : -1;
750 57888 k.triantaf
    if (this.index !== -1) {
751 57959 k.triantaf
      this.indicator = HelperFunctions.copy(this.section.indicators[this.index]);
752 58870 k.triantaf
      this.chartIndicatorFb = this.fb.group({
753
        _id: this.fb.control(this.indicator._id),
754
        name: this.fb.control(this.indicator.name),
755 59891 k.triantaf
        creationDate: this.fb.control(this.indicator.creationDate),
756 57888 k.triantaf
        description: this.fb.control(this.indicator.description),
757 59713 argiro.kok
        additionalDescription: this.fb.control(this.indicator.additionalDescription),
758 59675 argiro.kok
        visibility: this.fb.control(this.indicator.visibility),
759 57888 k.triantaf
        indicatorPaths: this.fb.array([]),
760
        width: this.fb.control(this.indicator.width),
761 59980 argiro.kok
        height: this.fb.control(this.indicator.height),
762 58707 k.triantaf
        defaultId: this.fb.control(this.indicator.defaultId)
763 57888 k.triantaf
      });
764
      this.indicator.indicatorPaths.forEach(indicatorPath => {
765 58870 k.triantaf
        this.addChartIndicatorPath(this.getUrlByStakeHolder(indicatorPath),
766 58824 argiro.kok
          this.getParametersAsFormArray(indicatorPath), this.indicator.defaultId !== null, indicatorPath.type);
767 58683 k.triantaf
        indicatorPath.safeResourceUrl = this.getSecureUrlByStakeHolder(indicatorPath);
768 57888 k.triantaf
      });
769
    } else {
770 59980 argiro.kok
      this.indicator = new Indicator('', '', '', 'chart', 'medium', 'medium', "PUBLIC", []);
771 58870 k.triantaf
      this.chartIndicatorFb = this.fb.group({
772 58876 k.triantaf
        _id: this.fb.control(this.indicator._id),
773
        name: this.fb.control(this.indicator.name),
774
        description: this.fb.control(this.indicator.description),
775 59713 argiro.kok
        additionalDescription: this.fb.control(this.indicator.additionalDescription),
776 59675 argiro.kok
        visibility: this.fb.control(this.indicator.visibility),
777 57888 k.triantaf
        indicatorPaths: this.fb.array([]),
778 58876 k.triantaf
        width: this.fb.control(this.indicator.width, Validators.required),
779 59980 argiro.kok
        height: this.fb.control(this.indicator.height, Validators.required),
780 58876 k.triantaf
        defaultId: this.fb.control(this.indicator.defaultId)
781 57888 k.triantaf
      });
782 58870 k.triantaf
      this.addChartIndicatorPath();
783 57555 k.triantaf
    }
784 59811 k.triantaf
    if (this.indicator.defaultId) {
785 59776 k.triantaf
      setTimeout(() => {
786 59926 argiro.kok
        this.chartIndicatorFb.get('description').disable();
787 59776 k.triantaf
      }, 0);
788
    }
789 58870 k.triantaf
    this.editChartModal.cancelButtonText = 'Cancel';
790
    this.editChartModal.okButtonLeft = false;
791
    this.editChartModal.alertMessage = false;
792 57888 k.triantaf
    if (this.index === -1) {
793 58870 k.triantaf
      this.editChartModal.alertTitle = 'Create a new chart indicator';
794
      this.editChartModal.okButtonText = 'Save';
795 57888 k.triantaf
    } else {
796 58870 k.triantaf
      this.editChartModal.alertTitle = 'Edit chart indicator\'s information';
797
      this.editChartModal.okButtonText = 'Save Changes';
798 57555 k.triantaf
    }
799 58870 k.triantaf
    this.editChartModal.open();
800 57888 k.triantaf
  }
801
802
  saveIndicator() {
803 58933 k.triantaf
    this.editing = true;
804 57888 k.triantaf
    if (this.indicator.type === 'chart') {
805 59926 argiro.kok
      this.chartIndicatorFb.get('description').enable();
806 59056 argiro.kok
      this.indicator = this.indicatorUtils.generateIndicatorByForm(this.chartIndicatorFb.value, this.indicator.indicatorPaths, this.indicator.type, true);
807 58683 k.triantaf
      this.section = this.charts.find(section => section._id === this.section._id);
808 58870 k.triantaf
    } else {
809 59926 argiro.kok
      this.numberIndicatorFb.get('description').enable();
810 59056 argiro.kok
      this.indicator = this.indicatorUtils.generateIndicatorByForm(this.numberIndicatorFb.value, this.indicator.indicatorPaths, this.indicator.type, false);
811 58985 k.triantaf
      this.section = this.numbers.find(section => section._id === this.section._id);
812 57883 k.triantaf
    }
813 57888 k.triantaf
    let path = [
814
      this.stakeholder._id,
815
      this.stakeholder.topics[this.topicIndex]._id,
816
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
817 57959 k.triantaf
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id,
818
      this.section._id
819 57888 k.triantaf
    ];
820 59847 argiro.kok
    this.subscriptions.push(this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.indicator, path).subscribe(indicator => {
821 57888 k.triantaf
      if (this.index !== -1) {
822 57959 k.triantaf
        this.section.indicators[this.index] = indicator;
823 57888 k.triantaf
      } else {
824 57959 k.triantaf
        this.section.indicators.push(indicator);
825 57888 k.triantaf
      }
826 59775 k.triantaf
      if (this.indicator.type === "chart") {
827 58985 k.triantaf
        this.filterCharts();
828
        this.chartIndicatorFb = null;
829
      } else {
830
        this.filterNumbers();
831
        this.numberIndicatorFb = null;
832
      }
833 60004 k.triantaf
      UIkit.notification('Indicator has been <b>successfully saved</b>', {
834 58694 k.triantaf
        status: 'success',
835 60004 k.triantaf
        timeout: 6000,
836
        pos: 'bottom-right'
837 58694 k.triantaf
      });
838 58933 k.triantaf
      this.editing = false;
839 57888 k.triantaf
    }, error => {
840 58870 k.triantaf
      this.chartIndicatorFb = null;
841 58694 k.triantaf
      UIkit.notification(error.error.message, {
842
        status: 'danger',
843 60004 k.triantaf
        timeout: 6000,
844
        pos: 'bottom-right'
845 58694 k.triantaf
      });
846 58933 k.triantaf
      this.editing = false;
847 59847 argiro.kok
    }));
848 57888 k.triantaf
  }
849
850 60104 k.triantaf
  reorderIndicators(sectionId: string, type: IndicatorType, reorder: Reorder) {
851 58933 k.triantaf
    this.editing = true;
852 57888 k.triantaf
    let path = [
853
      this.stakeholder._id,
854
      this.stakeholder.topics[this.topicIndex]._id,
855
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
856 57959 k.triantaf
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id,
857
      sectionId
858 57888 k.triantaf
    ];
859 60104 k.triantaf
    this.subscriptions.push(this.stakeholderService.reorderIndicators(this.properties.monitorServiceAPIURL, path, reorder, type).subscribe(indicators => {
860 57888 k.triantaf
      if (type === 'chart') {
861 57959 k.triantaf
        this.charts.find(section => section._id === sectionId).indicators = indicators;
862 57888 k.triantaf
        this.filterCharts();
863
      } else {
864 57959 k.triantaf
        this.numbers.find(section => section._id === sectionId).indicators = indicators;
865 57888 k.triantaf
        this.filterNumbers();
866
      }
867 58933 k.triantaf
      this.editing = false;
868 59847 argiro.kok
    }));
869 57888 k.triantaf
  }
870
871
  hasDifference(index: number): boolean {
872 57889 k.triantaf
    let hasDifference = false;
873 58870 k.triantaf
    this.chartIndicatorPaths.at(index).value.parameters.forEach((parameter) => {
874 57959 k.triantaf
      if (parameter.value !== this.indicator.indicatorPaths[index].parameters[parameter.key]) {
875 57889 k.triantaf
        hasDifference = true;
876
        return;
877
      }
878
    });
879
    return hasDifference || this.indicator.indicatorPaths[index].safeResourceUrl.toString() !==
880 57888 k.triantaf
      this.getSecureUrlByStakeHolder(this.indicator.indicatorPaths[index]).toString();
881
  }
882
883 58870 k.triantaf
  public get isAdministrator(): boolean {
884 59920 argiro.kok
    return Session.isPortalAdministrator(this.user);
885 58870 k.triantaf
  }
886 60104 k.triantaf
887 59920 argiro.kok
  public get isCurator(): boolean {
888
    return this.isAdministrator || Session.isMonitorCurator(this.user);
889
  }
890 60104 k.triantaf
891 57888 k.triantaf
  refreshIndicator() {
892 59775 k.triantaf
    this.indicator = this.indicatorUtils.generateIndicatorByForm(this.chartIndicatorFb.value, this.indicator.indicatorPaths, 'chart', true);
893 57888 k.triantaf
    this.indicator.indicatorPaths.forEach(indicatorPath => {
894
      indicatorPath.safeResourceUrl = this.getSecureUrlByStakeHolder(indicatorPath);
895
    });
896
  }
897
898 58980 konstantin
  deleteIndicatorOpen(section: Section, indicatorId: string, type: string, childrenAction: string = null) {
899
    this.indicatorChildrenActionOnDelete = null;
900 59775 k.triantaf
    if (childrenAction == "delete") {
901 58980 konstantin
      this.indicatorChildrenActionOnDelete = childrenAction;
902 59775 k.triantaf
    } else if (childrenAction == "disconnect") {
903 58980 konstantin
      this.indicatorChildrenActionOnDelete = childrenAction;
904
    }
905 59775 k.triantaf
906 57959 k.triantaf
    this.section = section;
907 58689 k.triantaf
    if (type === 'chart') {
908 58683 k.triantaf
      this.index = this.charts.find(value => value._id == section._id).indicators.findIndex(value => value._id == indicatorId);
909
    } else {
910
      this.index = this.numbers.find(value => value._id == section._id).indicators.findIndex(value => value._id == indicatorId);
911
    }
912 57959 k.triantaf
    this.indicator = section.indicators.find(value => value._id == indicatorId);
913 57922 k.triantaf
    this.deleteModal.alertTitle = 'Delete indicator';
914
    this.deleteModal.cancelButtonText = 'No';
915
    this.deleteModal.okButtonText = 'Yes';
916
    this.deleteModal.open();
917 57888 k.triantaf
  }
918
919
  deleteIndicator() {
920 58933 k.triantaf
    this.editing = true;
921 57888 k.triantaf
    let path = [
922
      this.stakeholder._id,
923
      this.stakeholder.topics[this.topicIndex]._id,
924
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
925
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id,
926 57959 k.triantaf
      this.section._id,
927 57888 k.triantaf
      this.indicator._id
928
    ];
929 59847 argiro.kok
    this.subscriptions.push(this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path, this.indicatorChildrenActionOnDelete).subscribe(() => {
930 57888 k.triantaf
      if (this.indicator.type === 'chart') {
931 57959 k.triantaf
        this.charts.find(section => section._id === this.section._id).indicators.splice(this.index, 1);
932 58683 k.triantaf
        this.filterCharts();
933 57888 k.triantaf
      } else {
934 57959 k.triantaf
        this.numbers.find(section => section._id === this.section._id).indicators.splice(this.index, 1);
935 58683 k.triantaf
        this.filterNumbers();
936 57888 k.triantaf
      }
937 60004 k.triantaf
      UIkit.notification('Indicator has been <b>successfully deleted</b>', {
938 58694 k.triantaf
        status: 'success',
939 60004 k.triantaf
        timeout: 6000,
940
        pos: 'bottom-right'
941 58694 k.triantaf
      });
942 58933 k.triantaf
      this.editing = false;
943 58694 k.triantaf
    }, error => {
944
      UIkit.notification(error.error.message, {
945
        status: 'danger',
946 60004 k.triantaf
        timeout: 6000,
947
        pos: 'bottom-right'
948 58694 k.triantaf
      });
949 58933 k.triantaf
      this.editing = false;
950 59847 argiro.kok
    }));
951 57888 k.triantaf
  }
952
953 59775 k.triantaf
  changeIndicatorStatus(sectionId: string, indicator: Indicator, visibility: Visibility) {
954 58933 k.triantaf
    this.editing = true;
955 57888 k.triantaf
    let path = [
956
      this.stakeholder._id,
957
      this.stakeholder.topics[this.topicIndex]._id,
958
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
959
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id,
960 57959 k.triantaf
      sectionId,
961 57888 k.triantaf
      indicator._id
962
    ];
963 59847 argiro.kok
    this.subscriptions.push(this.stakeholderService.changeVisibility(this.properties.monitorServiceAPIURL, path, visibility).subscribe(visibility => {
964 59675 argiro.kok
      indicator.visibility = visibility;
965 60004 k.triantaf
      UIkit.notification('Indicator has been <b>successfully changed</b> to ' + indicator.visibility.toLowerCase(), {
966 58694 k.triantaf
        status: 'success',
967 60004 k.triantaf
        timeout: 6000,
968
        pos: 'bottom-right'
969 58694 k.triantaf
      });
970 58933 k.triantaf
      this.editing = false;
971 58694 k.triantaf
    }, error => {
972 59749 k.triantaf
      UIkit.notification('An error has been occurred. Try again later', {
973 58694 k.triantaf
        status: 'danger',
974 60004 k.triantaf
        timeout: 6000,
975
        pos: 'bottom-right'
976 58694 k.triantaf
      });
977 58933 k.triantaf
      this.editing = false;
978 59847 argiro.kok
    }));
979 57888 k.triantaf
  }
980
981 58689 k.triantaf
  saveSection(section: Section, index: number, type: IndicatorType = "chart") {
982 58933 k.triantaf
    this.editing = true;
983 58685 k.triantaf
    let path = [
984
      this.stakeholder._id,
985
      this.stakeholder.topics[this.topicIndex]._id,
986
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
987
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id
988
    ];
989 59847 argiro.kok
    this.subscriptions.push(this.stakeholderService.saveSection(this.properties.monitorServiceAPIURL, section, path, index).subscribe(section => {
990 58689 k.triantaf
      if (type === 'chart') {
991 58685 k.triantaf
        this.charts[index] = section;
992
        this.filterCharts();
993
      } else {
994
        this.numbers[index] = section;
995
        this.filterNumbers();
996
      }
997
      this.initReorder();
998 60004 k.triantaf
      UIkit.notification('Section has been <b>successfully saved</b>', {
999 58685 k.triantaf
        status: 'success',
1000 60004 k.triantaf
        timeout: 6000,
1001
        pos: 'bottom-right'
1002 58685 k.triantaf
      });
1003 58933 k.triantaf
      this.editing = false;
1004 58685 k.triantaf
    }, error => {
1005
      UIkit.notification(error.error.message, {
1006
        status: 'danger',
1007 60004 k.triantaf
        timeout: 6000,
1008
        pos: 'bottom-right'
1009 58685 k.triantaf
      });
1010 58933 k.triantaf
      this.editing = false;
1011 59847 argiro.kok
    }));
1012 58685 k.triantaf
  }
1013 57978 k.triantaf
1014 58685 k.triantaf
  createSection(index = -1, type: IndicatorType = 'chart') {
1015 58933 k.triantaf
    this.editing = true;
1016 58685 k.triantaf
    this.section = new Section(type, null, null, this.stakeholder.alias);
1017
    let path = [
1018
      this.stakeholder._id,
1019
      this.stakeholder.topics[this.topicIndex]._id,
1020
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
1021
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id
1022
    ];
1023 59847 argiro.kok
    this.subscriptions.push(this.stakeholderService.saveSection(this.properties.monitorServiceAPIURL, this.section, path, index).subscribe(section => {
1024 58689 k.triantaf
      if (type === 'chart') {
1025
        if (index !== -1) {
1026 58685 k.triantaf
          this.charts.splice(index, 0, section);
1027
        } else {
1028
          this.charts.push(section);
1029
        }
1030
        this.filterCharts();
1031
      } else {
1032 58689 k.triantaf
        if (index !== -1) {
1033 58685 k.triantaf
          this.numbers.splice(index, 0, section);
1034
        } else {
1035
          this.numbers.push(section);
1036
        }
1037
        this.filterNumbers();
1038
      }
1039
      this.initReorder();
1040 60004 k.triantaf
      UIkit.notification('Section has been <b>successfully created</b>', {
1041 58685 k.triantaf
        status: 'success',
1042 60004 k.triantaf
        timeout: 6000,
1043
        pos: 'bottom-right'
1044 58685 k.triantaf
      });
1045 58933 k.triantaf
      this.editing = false;
1046 58685 k.triantaf
    }, error => {
1047
      UIkit.notification(error.error.message, {
1048
        status: 'danger',
1049 60004 k.triantaf
        timeout: 6000,
1050
        pos: 'bottom-right'
1051 58685 k.triantaf
      });
1052 58933 k.triantaf
      this.editing = false;
1053 59847 argiro.kok
    }));
1054 57978 k.triantaf
  }
1055 58685 k.triantaf
1056 58980 konstantin
  // deleteNumberSectionOpen(section: Section, index: number) {
1057
  //   this.section = section;
1058
  //   this.index = index;
1059
  //   this.deleteNumberSectionModal.alertTitle = 'Delete Section';
1060
  //   this.deleteNumberSectionModal.cancelButtonText = 'No';
1061
  //   this.deleteNumberSectionModal.okButtonText = 'Yes';
1062
  //   this.deleteNumberSectionModal.open();
1063
  // }
1064
  //
1065
  // deleteChartSectionOpen(section: Section, index: number) {
1066
  //   this.section = section;
1067
  //   this.index = index;
1068
  //   this.deleteChartSectionModal.alertTitle = 'Delete Section';
1069
  //   this.deleteChartSectionModal.cancelButtonText = 'No';
1070
  //   this.deleteChartSectionModal.okButtonText = 'Yes';
1071
  //   this.deleteChartSectionModal.open();
1072
  // }
1073 59775 k.triantaf
1074 58980 konstantin
  deleteSectionOpen(section: Section, index: number, type: IndicatorType, childrenAction: string = null) {
1075 59775 k.triantaf
    if (!this.editing && !section.defaultId) {
1076 59736 k.triantaf
      this.sectionTypeToDelete = type;
1077
      this.sectionChildrenActionOnDelete = null;
1078
      if (childrenAction == "delete") {
1079
        this.sectionChildrenActionOnDelete = childrenAction;
1080
      } else if (childrenAction == "disconnect") {
1081
        this.sectionChildrenActionOnDelete = childrenAction;
1082
      }
1083 59775 k.triantaf
1084 59736 k.triantaf
      this.section = section;
1085
      this.index = index;
1086
      this.deleteSectionModal.alertTitle = 'Delete Section';
1087
      this.deleteSectionModal.cancelButtonText = 'No';
1088
      this.deleteSectionModal.okButtonText = 'Yes';
1089
      this.deleteSectionModal.open();
1090 58980 konstantin
    }
1091 58870 k.triantaf
  }
1092
1093 58980 konstantin
  deleteSection() {
1094 58933 k.triantaf
    this.editing = true;
1095 58685 k.triantaf
    let path = [
1096
      this.stakeholder._id,
1097
      this.stakeholder.topics[this.topicIndex]._id,
1098
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
1099
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id,
1100
      this.section._id
1101
    ];
1102 59847 argiro.kok
    this.subscriptions.push(this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path, this.sectionChildrenActionOnDelete).subscribe(() => {
1103 58980 konstantin
      if (this.sectionTypeToDelete === "chart") {
1104 58685 k.triantaf
        this.charts.splice(this.index, 1);
1105
        this.filterCharts();
1106
      } else {
1107
        this.numbers.splice(this.index, 1);
1108
        this.filterNumbers();
1109
      }
1110
      this.initReorder();
1111 60004 k.triantaf
      UIkit.notification('Section has been <b>successfully deleted</b>', {
1112 58685 k.triantaf
        status: 'success',
1113 60004 k.triantaf
        timeout: 6000,
1114
        pos: 'bottom-right'
1115 58685 k.triantaf
      });
1116 58933 k.triantaf
      this.editing = false;
1117 58685 k.triantaf
    }, error => {
1118
      UIkit.notification(error.error.message, {
1119
        status: 'danger',
1120 60004 k.triantaf
        timeout: 6000,
1121
        pos: 'bottom-right'
1122 58685 k.triantaf
      });
1123 58933 k.triantaf
      this.editing = false;
1124 59847 argiro.kok
    }));
1125 58685 k.triantaf
  }
1126 57555 k.triantaf
}