]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
5ca1275e47148988cbc15ddab8750ec855b1ce9f
[ceph.git] /
1 import { Component, OnChanges } from '@angular/core';
2 import { Input } from '@angular/core';
3
4 @Component({
5   selector: 'cd-select-badges',
6   templateUrl: './select-badges.component.html',
7   styleUrls: ['./select-badges.component.scss']
8 })
9 export class SelectBadgesComponent implements OnChanges {
10   @Input() data: Array<string> = [];
11   @Input() options: Array<SelectBadgesOption> = [];
12   @Input() emptyMessage = 'There are no items.';
13
14   constructor() {}
15
16   ngOnChanges() {
17     if (!this.options || !this.data || this.data.length === 0) {
18       return;
19     }
20     this.options.forEach((option) => {
21       if (this.data.indexOf(option.name) !== -1) {
22         option.selected = true;
23       }
24     });
25   }
26
27   private updateOptions() {
28     this.data.splice(0, this.data.length);
29     this.options.forEach((option: SelectBadgesOption) => {
30       if (option.selected) {
31         this.data.push(option.name);
32       }
33     });
34   }
35
36   selectOption(option: SelectBadgesOption) {
37     option.selected = !option.selected;
38     this.updateOptions();
39   }
40
41   removeItem(item: string) {
42     const optionToRemove = this.options.find((option: SelectBadgesOption) => {
43       return option.name === item;
44     });
45     optionToRemove.selected = false;
46     this.updateOptions();
47   }
48 }