]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
38dc5cbae878259ed1e4197c03f531a141f90504
[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()
11   data: Array<string> = [];
12   @Input()
13   options: Array<SelectBadgesOption> = [];
14   @Input()
15   emptyMessage = 'There are no items.';
16
17   constructor() {}
18
19   ngOnChanges() {
20     if (!this.options || !this.data || this.data.length === 0) {
21       return;
22     }
23     this.options.forEach((option) => {
24       if (this.data.indexOf(option.name) !== -1) {
25         option.selected = true;
26       }
27     });
28   }
29
30   private updateOptions() {
31     this.data.splice(0, this.data.length);
32     this.options.forEach((option: SelectBadgesOption) => {
33       if (option.selected) {
34         this.data.push(option.name);
35       }
36     });
37   }
38
39   selectOption(option: SelectBadgesOption) {
40     option.selected = !option.selected;
41     this.updateOptions();
42   }
43
44   removeItem(item: string) {
45     const optionToRemove = this.options.find((option: SelectBadgesOption) => {
46       return option.name === item;
47     });
48     optionToRemove.selected = false;
49     this.updateOptions();
50   }
51 }