Project

General

Profile

1 57697 k.triantaf
import {AfterViewInit, Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from "@angular/core";
2 57644 k.triantaf
import {Indicator, IndicatorPath, Stakeholder} from "../utils/entities/stakeholder";
3 57697 k.triantaf
import {IndicatorUtils, Option} from "../utils/indicator-utils";
4 57673 k.triantaf
import {FormArray, FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
5 57639 k.triantaf
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
6
import {StatisticsService} from "../utils/services/statistics.service";
7
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
8 57644 k.triantaf
import {DomSanitizer} from "@angular/platform-browser";
9 57673 k.triantaf
import {StakeholderService} from "../services/stakeholder.service";
10
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
11 57697 k.triantaf
import {Subscriber} from "rxjs";
12 57706 k.triantaf
import {LayoutService} from "../library/sharedComponents/sidebar/layout.service";
13 57555 k.triantaf
14 57673 k.triantaf
declare var UIkit;
15
16 57555 k.triantaf
@Component({
17
  selector: 'indicators',
18
  templateUrl: './indicators.component.html'
19
})
20 57697 k.triantaf
export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterViewInit {
21 57555 k.triantaf
22
  @Input()
23 57673 k.triantaf
  public properties: EnvProperties = null;
24
  @Input()
25 57555 k.triantaf
  public topicIndex: number = 0;
26
  @Input()
27
  public categoryIndex: number = 0;
28
  @Input()
29
  public subcategoryIndex: number = 0;
30
  @Input()
31
  public stakeholder: Stakeholder = null;
32
  public indicatorUtils: IndicatorUtils = new IndicatorUtils();
33 57639 k.triantaf
  public indicatorFb: FormGroup;
34 57594 k.triantaf
  /**
35 57639 k.triantaf
   * Editable indicator
36
   */
37
  public indicator: Indicator;
38 57673 k.triantaf
  public index: number;
39 57639 k.triantaf
  /**
40 57594 k.triantaf
   * Displayed chart and numbers base on Top filters
41
   */
42 57555 k.triantaf
  public displayCharts: Indicator[] = [];
43
  public displayNumbers: Indicator[] = [];
44 57594 k.triantaf
  /**
45
   * Top filters
46
   */
47 57697 k.triantaf
  public filters: FormGroup;
48
  public all: Option = {
49
    value: 'all',
50
    label: 'All'
51
  };
52 57594 k.triantaf
  /**
53
   * Grid or List View
54
   */
55 57555 k.triantaf
  public grid: boolean = true;
56 57697 k.triantaf
  private subscriptions: any[] = [];
57 57639 k.triantaf
  @ViewChild('editIndicatorModal') editIndicatorModal: AlertModal;
58 57697 k.triantaf
  @ViewChild('deleteIndicatorModal') deleteIndicatorModal: AlertModal;
59 57555 k.triantaf
60 57706 k.triantaf
  constructor(private layoutService: LayoutService,
61 57673 k.triantaf
              private stakeholderService: StakeholderService,
62 57639 k.triantaf
              private statisticsService: StatisticsService,
63 57644 k.triantaf
              private fb: FormBuilder,
64
              private sanitizer: DomSanitizer) {
65 57639 k.triantaf
  }
66
67 57555 k.triantaf
  ngOnInit(): void {
68 57697 k.triantaf
    this.buildFilters();
69
  }
70
71
  ngOnDestroy(): void {
72
    this.subscriptions.forEach(value => {
73
      if (value instanceof Subscriber) {
74
        value.unsubscribe();
75
      } else if(value instanceof Function){
76
        value();
77
      }
78
    });
79
  }
80
81
  ngAfterViewInit(): void {
82 57673 k.triantaf
    if (document !== undefined) {
83
      let callback = (list): void => {
84
        let items: HTMLCollection = list.current.children;
85
        let reordered = [];
86
        for (let i = 0; i < items.length; i++) {
87
          if (items.item(i).id) {
88 57697 k.triantaf
            reordered.push(items.item(i).id);
89 57673 k.triantaf
          }
90
        }
91 57697 k.triantaf
        this.reorderIndicators(list.current.id , reordered);
92 57673 k.triantaf
      };
93 57697 k.triantaf
      this.subscriptions.push(UIkit.util.on(document, 'moved', '#chart', callback));
94
      this.subscriptions.push(UIkit.util.on(document, 'moved', '#number', callback));
95 57673 k.triantaf
    }
96 57555 k.triantaf
  }
97
98
  ngOnChanges(changes: SimpleChanges): void {
99 57639 k.triantaf
    if (this.canEdit) {
100 57719 k.triantaf
      if(changes.topicIndex || changes.categoryIndex || changes.subcategoryIndex) {
101
        this.buildFilters();
102
      }
103 57673 k.triantaf
      this.filterCharts();
104
      this.filterNumbers();
105 57555 k.triantaf
    }
106
  }
107
108 57639 k.triantaf
  public toggleOpen(event = null) {
109
    if (!event) {
110 57706 k.triantaf
      this.layoutService.setOpen(!this.open);
111 57639 k.triantaf
    } else if (event && event['value'] === true) {
112 57706 k.triantaf
      this.layoutService.setOpen(false);
113 57639 k.triantaf
    }
114
  }
115
116
  public changeGrid(value) {
117
    this.grid = value;
118
  }
119
120 57697 k.triantaf
  private buildFilters() {
121
    this.filters = this.fb.group({
122
      chartType: this.fb.control('all'),
123
      privacy: this.fb.control('all'),
124
      status: this.fb.control('all'),
125
      keyword: this.fb.control('')
126
    });
127
    this.subscriptions.push(this.filters.get('chartType').valueChanges.subscribe( value => {
128
      this.onChartTypeChange(value);
129
    }));
130
    this.subscriptions.push(this.filters.get('privacy').valueChanges.subscribe( value => {
131
      this.onPrivacyChange(value);
132
    }));
133
    this.subscriptions.push(this.filters.get('status').valueChanges.subscribe( value => {
134
      this.onStatusChange(value);
135
    }));
136
    this.subscriptions.push(this.filters.get('keyword').valueChanges.subscribe( value => {
137
      this.onKeywordChange(value);
138
    }));
139
  }
140
141 57673 k.triantaf
  filterCharts() {
142
    this.displayCharts = this.filterChartType(this.filterPrivacy(
143 57697 k.triantaf
      this.filterStatus(this.filterByKeyword(this.charts, this.filters.value.keyword),
144
        this.filters.value.status),
145
      this.filters.value.privacy),
146
      this.filters.value.chartType
147 57673 k.triantaf
    );
148
  }
149
150
  filterNumbers() {
151
    this.displayNumbers = this.filterPrivacy(this.filterStatus(
152 57697 k.triantaf
      this.filterByKeyword(this.numbers, this.filters.value.keyword),
153
      this.filters.value.status),
154
      this.filters.value.privacy);
155 57673 k.triantaf
  }
156
157 57555 k.triantaf
  onChartTypeChange(value) {
158
    this.displayCharts = this.filterChartType(this.charts, value);
159
  }
160
161
  onPrivacyChange(value) {
162
    this.displayCharts = this.filterPrivacy(this.charts, value);
163
    this.displayNumbers = this.filterPrivacy(this.numbers, value);
164
  }
165
166
  onStatusChange(value) {
167
    this.displayCharts = this.filterStatus(this.charts, value);
168
    this.displayNumbers = this.filterStatus(this.numbers, value);
169
  }
170
171
  onKeywordChange(value) {
172
    this.displayCharts = this.filterByKeyword(this.charts, value);
173
    this.displayNumbers = this.filterByKeyword(this.numbers, value);
174
  }
175
176 57639 k.triantaf
  private filterChartType(indicators: Indicator[], value): Indicator[] {
177 57555 k.triantaf
    if (value === 'all') {
178
      return indicators;
179
    } else {
180
      return indicators.filter(indicator =>
181
        indicator.indicatorPaths.filter(indicatorPath => indicatorPath.type === value).length > 0);
182
    }
183
  }
184
185 57639 k.triantaf
  private filterPrivacy(indicators: Indicator[], value): Indicator[] {
186 57555 k.triantaf
    if (value === 'all') {
187
      return indicators;
188
    } else {
189 57697 k.triantaf
      return indicators.filter(indicator => indicator.isPublic === value);
190 57555 k.triantaf
    }
191
  }
192
193 57639 k.triantaf
  private filterStatus(indicators: Indicator[], value): Indicator[] {
194 57555 k.triantaf
    if (value === 'all') {
195
      return indicators;
196
    } else {
197 57697 k.triantaf
      return indicators.filter(indicator => indicator.isActive === value);
198 57555 k.triantaf
    }
199
  }
200
201 57639 k.triantaf
  private filterByKeyword(indicators: Indicator[], value): Indicator[] {
202 57555 k.triantaf
    if (value === null || value === '') {
203
      return indicators;
204
    } else {
205 57697 k.triantaf
      return indicators.filter(indicator => (indicator.name && indicator.name.toLowerCase().includes(value.toLowerCase()))
206
        || (indicator.description && indicator.description.toLowerCase().includes(value.toLowerCase())));
207 57555 k.triantaf
    }
208
  }
209
210 57673 k.triantaf
  get charts(): Indicator[] {
211
    return this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts;
212
  }
213
214
  set charts(indicators: Indicator[]) {
215
    this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts = indicators;
216
  }
217
218
  get numbers(): Indicator[] {
219
    return this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers;
220
  }
221
222
  set numbers(indicators: Indicator[]) {
223
    this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers = indicators;
224
  }
225
226 57555 k.triantaf
  get open(): boolean {
227 57706 k.triantaf
    return this.layoutService.open;
228 57555 k.triantaf
  }
229
230 57673 k.triantaf
  get canNumbersReorder(): boolean {
231
    return this.displayNumbers.length === this.numbers.length;
232
  }
233
234
  get canChartsReorder(): boolean {
235
    return this.displayCharts.length === this.charts.length;
236
  }
237
238 57594 k.triantaf
  get canEdit() {
239
    return this.stakeholder &&
240
      this.stakeholder.topics[this.topicIndex] &&
241
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
242
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex];
243
  }
244
245 57639 k.triantaf
  public get urls(): FormArray {
246
    return this.indicatorFb.get('urls') as FormArray;
247 57555 k.triantaf
  }
248
249 57673 k.triantaf
  public get indicatorPaths(): FormArray {
250 57706 k.triantaf
    return this.indicatorFb.get('indicatorPaths') as FormArray;
251 57673 k.triantaf
  }
252
253
  public getParameters(index: number): FormArray {
254
    return this.indicatorPaths.at(index).get('parameters') as FormArray;
255
  }
256
257
  public getParameter(index: number, key: string): FormControl {
258
    return this.getParameters(index).controls.filter(control => control.value.key === key)[0] as FormControl;
259
  }
260
261 57644 k.triantaf
  private getUrlByStakeHolder(indicatorPath: IndicatorPath) {
262
    return this.sanitizer.bypassSecurityTrustResourceUrl(
263
      this.statisticsService.getChartUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(indicatorPath)));
264
  }
265
266 57639 k.triantaf
  public addUrl() {
267
    this.urls.push(this.fb.control('', [Validators.required,
268
      Validators.pattern('https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.' +
269
        '[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.' +
270
        '[a-zA-Z0-9]+\.[^\s]{2,}')])
271
    );
272 57555 k.triantaf
  }
273 57639 k.triantaf
274
  public createIndicatorOpen() {
275 57706 k.triantaf
    this.indicator = null;
276 57639 k.triantaf
    this.indicatorFb = this.fb.group({
277 57706 k.triantaf
      id: this.fb.control(null),
278 57639 k.triantaf
      name: this.fb.control('', Validators.required),
279
      description: this.fb.control(''),
280 57706 k.triantaf
      isPublic: this.fb.control(false),
281
      isActive: this.fb.control(false),
282
      urls: this.fb.array([]),
283 57639 k.triantaf
      width: this.fb.control('small', Validators.required),
284
    });
285
    this.addUrl();
286 57706 k.triantaf
    this.editIndicatorModal.alertTitle = 'Create a new chart Indicator';
287
    this.editIndicatorModal.cancelButtonText = 'Cancel';
288
    this.editIndicatorModal.okButtonText = 'Create';
289
    this.editIndicatorModal.okButtonLeft = false;
290
    this.editIndicatorModal.alertMessage = false;
291
    this.editIndicatorModal.stayOpen = true;
292
    this.editIndicatorModal.open();
293 57639 k.triantaf
  }
294
295
  public createIndicator() {
296
    this.indicator = new Indicator(
297
      this.indicatorFb.value.name,
298
      this.indicatorFb.value.description,
299
      'chart',
300
      this.indicatorFb.value.width,
301 57706 k.triantaf
      this.indicatorFb.value.isActive,
302
      this.indicatorFb.value.isPublic,
303 57639 k.triantaf
      []
304
    );
305
    this.indicatorFb.value.urls.forEach(url => {
306
      this.indicator.indicatorPaths.push(
307
        this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(url), url));
308
    });
309 57673 k.triantaf
    this.editIndicatorOpen();
310 57639 k.triantaf
  }
311
312 57719 k.triantaf
  public editIndicatorOpen(id = null) {
313
    this.index = this.charts.findIndex(value => value._id === id);
314 57673 k.triantaf
    if (this.index !== -1) {
315 57719 k.triantaf
      this.indicator = HelperFunctions.copy(this.charts[this.index]);
316 57673 k.triantaf
    }
317 57644 k.triantaf
    let indicatorPaths = this.fb.array([]);
318
    this.indicator.indicatorPaths.forEach(indicatorPath => {
319
      let parameters = this.fb.array([]);
320 57673 k.triantaf
      if (indicatorPath.parameters) {
321
        Object.keys(indicatorPath.parameters).forEach(key => {
322
          if (this.indicatorUtils.ignoredParameters.indexOf(key) === -1) {
323
            if (this.indicatorUtils.parametersValidators.has(key)) {
324
              parameters.push(this.fb.group({
325
                key: this.fb.control(key),
326
                value: this.fb.control(indicatorPath.parameters[key], this.indicatorUtils.parametersValidators.get(key))
327
              }));
328
            } else {
329
              parameters.push(this.fb.group({
330
                key: this.fb.control(key),
331
                value: this.fb.control(indicatorPath.parameters[key], Validators.required)
332
              }));
333
            }
334
          }
335
        });
336
      }
337 57639 k.triantaf
      indicatorPaths.push(this.fb.group({
338 57644 k.triantaf
        parameters: parameters
339 57639 k.triantaf
      }));
340
    });
341 57706 k.triantaf
    this.indicatorFb = this.fb.group({
342 57673 k.triantaf
      id: this.fb.control(this.indicator._id),
343 57639 k.triantaf
      name: this.fb.control(this.indicator.name, Validators.required),
344
      description: this.fb.control(this.indicator.description),
345
      isPublic: this.fb.control(this.indicator.isPublic),
346
      isActive: this.fb.control(this.indicator.isActive),
347
      indicatorPaths: indicatorPaths,
348
      width: this.fb.control(this.indicator.width, Validators.required),
349
    });
350 57719 k.triantaf
    if(this.index === -1) {
351 57706 k.triantaf
      this.editIndicatorModal.okButtonText = 'Save';
352
      this.editIndicatorModal.stayOpen = false;
353
    } else {
354
      this.editIndicatorModal.cancelButtonText = 'Cancel';
355
      this.editIndicatorModal.okButtonText = 'Save Changes';
356
      this.editIndicatorModal.okButtonLeft = false;
357
      this.editIndicatorModal.alertMessage = false;
358
      this.editIndicatorModal.open();
359
    }
360
    this.indicator.indicatorPaths.forEach(indicatorPath => {
361
      indicatorPath.safeResourceUrl = this.getUrlByStakeHolder(indicatorPath)
362
    });
363 57639 k.triantaf
  }
364
365
  saveIndicator() {
366 57706 k.triantaf
    if(this.indicator) {
367
      this.indicator = this.indicatorUtils.generateIndicatorByForm(this.indicatorFb.value, this.indicator.indicatorPaths);
368
      let path = [
369
        this.stakeholder._id,
370
        this.stakeholder.topics[this.topicIndex]._id,
371
        this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
372
        this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id
373
      ];
374
      this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.indicator, path).subscribe(indicator => {
375
        if (this.index !== -1) {
376
          this.charts[this.index] = indicator;
377
        } else {
378
          this.charts.push(indicator);
379
        }
380
        this.filterCharts();
381
        this.stakeholderService.setStakeholder(this.stakeholder);
382
        this.indicatorFb = null;
383
      }, error => {
384
        this.indicatorFb = null;
385
      });
386
    } else {
387
      this.createIndicator();
388
    }
389 57673 k.triantaf
  }
390 57639 k.triantaf
391 57697 k.triantaf
  reorderIndicators(type: string, indicatorIds: string[]) {
392 57673 k.triantaf
    let path = [
393
      this.stakeholder._id,
394
      this.stakeholder.topics[this.topicIndex]._id,
395
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
396
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id
397
    ];
398 57697 k.triantaf
    this.stakeholderService.reorderIndicators(this.properties.monitorServiceAPIURL, path, indicatorIds, type).subscribe(indicators => {
399
      if (type === 'chart') {
400 57673 k.triantaf
        this.charts = indicators;
401
        this.filterCharts();
402
      } else {
403
        this.numbers = indicators;
404
        this.filterNumbers();
405
      }
406
      this.stakeholderService.setStakeholder(this.stakeholder);
407
    });
408 57639 k.triantaf
  }
409 57644 k.triantaf
410 57673 k.triantaf
  refreshIndicator(index: number) {
411 57706 k.triantaf
    this.indicator = this.indicatorUtils.generateIndicatorByForm(this.indicatorFb.value, this.indicator.indicatorPaths);
412 57673 k.triantaf
    this.indicator.indicatorPaths.forEach(indicatorPath => {
413
      indicatorPath.safeResourceUrl = this.getUrlByStakeHolder(indicatorPath)
414
    });
415
    this.indicatorPaths.at(index).markAsPristine({onlySelf: true});
416 57644 k.triantaf
  }
417 57697 k.triantaf
418 57719 k.triantaf
  deleteIndicatorOpen(id: string, type: string = 'chart') {
419 57697 k.triantaf
    if(type === 'chart') {
420 57719 k.triantaf
      this.indicator = this.charts.find(value => value._id == id);
421 57697 k.triantaf
    } else {
422 57719 k.triantaf
      this.indicator = this.numbers.find(value => value._id == id);
423 57697 k.triantaf
    }
424
    this.deleteIndicatorModal.alertTitle = 'Delete ' + this.indicator.name;
425
    this.deleteIndicatorModal.cancelButtonText = 'No';
426
    this.deleteIndicatorModal.okButtonText = 'Yes';
427
    this.deleteIndicatorModal.message = 'This indicator will permanently be deleted. Are you sure you want to proceed?';
428
    this.deleteIndicatorModal.open();
429
  }
430
431
  deleteIndicator() {
432
    let path = [
433
      this.stakeholder._id,
434
      this.stakeholder.topics[this.topicIndex]._id,
435
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
436
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id,
437
      this.indicator._id
438
    ];
439
    this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe( () => {
440
      if(this.indicator.type === 'chart') {
441
        this.charts.splice(this.index, 1);
442
      } else {
443
        this.numbers.splice(this.index, 1);
444
      }
445
      this.stakeholderService.setStakeholder(this.stakeholder);
446
    });
447
  }
448 57555 k.triantaf
}