import { InfoPanelComponent } from './info-panel/info-panel.component';
import { LoadingPanelComponent } from './loading-panel/loading-panel.component';
import { ModalComponent } from './modal/modal.component';
+import { SelectBadgesComponent } from './select-badges/select-badges.component';
import { SparklineComponent } from './sparkline/sparkline.component';
import { SubmitButtonComponent } from './submit-button/submit-button.component';
import { UsageBarComponent } from './usage-bar/usage-bar.component';
ViewCacheComponent,
SparklineComponent,
HelperComponent,
+ SelectBadgesComponent,
SubmitButtonComponent,
UsageBarComponent,
ErrorPanelComponent,
ViewCacheComponent,
SparklineComponent,
HelperComponent,
+ SelectBadgesComponent,
SubmitButtonComponent,
ErrorPanelComponent,
LoadingPanelComponent,
--- /dev/null
+interface SelectBadgesOption {
+ selected: boolean;
+ name: string;
+ description: string;
+}
--- /dev/null
+<ng-template #popTemplate>
+ <div *ngFor="let option of options"
+ class="select-menu-item"
+ (click)="selectOption(option)">
+ <div class="select-menu-item-icon">
+ <i class="fa fa-check" aria-hidden="true"
+ *ngIf="option.selected"></i>
+
+ </div>
+ <div class="select-menu-item-content">
+ {{ option.name }}
+ <br>
+ <small class="text-muted">
+ {{ option.description }}
+ </small>
+ </div>
+ </div>
+</ng-template>
+
+<a class="margin-right-sm select-menu-edit"
+ [popover]="popTemplate"
+ placement="bottom"
+ container="body"
+ outsideClick="true">
+ <i class="fa fa-fw fa-pencil"></i>
+</a>
+<span class="text-muted"
+ *ngIf="data.length === 0"
+ i18n>
+ {{ emptyMessage }}
+</span>
+<span *ngFor="let dataItem of data">
+ <span class="badge badge-pill badge-primary margin-right-sm">
+ <span class="margin-right-sm">{{ dataItem }}</span>
+ <a class="badge-remove"
+ (click)="removeItem(dataItem)">
+ <i class="fa fa-times" aria-hidden="true"></i>
+ </a>
+ </span>
+</span>
--- /dev/null
+@import '../../../../defaults';
+
+.select-menu-edit {
+ margin-left: -20px
+}
+.select-menu-item {
+ display: block;
+ cursor: pointer;
+ border-bottom: 1px solid $color-transparent;
+ font-size: 12px;
+ &:hover {
+ background-color: $color-off-white;
+ }
+}
+.select-menu-item-icon {
+ float: left;
+ padding: 8px 8px 8px 8px;
+ width: 30px;
+}
+.select-menu-item-content {
+ padding: 8px 8px 8px 8px;
+}
+.badge-remove {
+ color: $color-solid-white;
+}
--- /dev/null
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { PopoverModule } from 'ngx-bootstrap';
+
+import { configureTestBed } from '../../../../testing/unit-test-helper';
+import { SelectBadgesComponent } from './select-badges.component';
+
+describe('SelectBadgesComponent', () => {
+ let component: SelectBadgesComponent;
+ let fixture: ComponentFixture<SelectBadgesComponent>;
+
+ configureTestBed({
+ declarations: [SelectBadgesComponent],
+ imports: [PopoverModule.forRoot()]
+ });
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(SelectBadgesComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+
+ it('should add item', () => {
+ component.options = [
+ {name: 'option1', description: '', selected: false},
+ {name: 'option2', description: '', selected: false}
+ ];
+ component.data = [];
+ component.selectOption(component.options[1]);
+ expect(component.data).toEqual(['option2']);
+ });
+
+ it('should update selected', () => {
+ component.options = [
+ {name: 'option1', description: '', selected: false},
+ {name: 'option2', description: '', selected: false}
+ ];
+ component.data = ['option2'];
+ component.ngOnChanges();
+ expect(component.options[0].selected).toBe(false);
+ expect(component.options[1].selected).toBe(true);
+ });
+
+ it('should remove item', () => {
+ component.options = [
+ {name: 'option1', description: '', selected: true},
+ {name: 'option2', description: '', selected: true}
+ ];
+ component.data = ['option1', 'option2'];
+ component.removeItem('option1');
+ expect(component.data).toEqual(['option2']);
+ });
+
+});
--- /dev/null
+import { Component, OnChanges } from '@angular/core';
+import { Input } from '@angular/core';
+
+@Component({
+ selector: 'cd-select-badges',
+ templateUrl: './select-badges.component.html',
+ styleUrls: ['./select-badges.component.scss']
+})
+export class SelectBadgesComponent implements OnChanges {
+ @Input() data: Array<string> = [];
+ @Input() options: Array<SelectBadgesOption> = [];
+ @Input() emptyMessage = 'There are no items.';
+
+ constructor() {}
+
+ ngOnChanges() {
+ if (!this.options || !this.data || this.data.length === 0) {
+ return;
+ }
+ this.options.forEach((option) => {
+ if (this.data.indexOf(option.name) !== -1) {
+ option.selected = true;
+ }
+ });
+ }
+
+ private updateOptions() {
+ this.data.splice(0, this.data.length);
+ this.options.forEach((option: SelectBadgesOption) => {
+ if (option.selected) {
+ this.data.push(option.name);
+ }
+ });
+ }
+
+ selectOption(option: SelectBadgesOption) {
+ option.selected = !option.selected;
+ this.updateOptions();
+ }
+
+ removeItem(item: string) {
+ const optionToRemove = this.options.find((option: SelectBadgesOption) => {
+ return option.name === item;
+ });
+ optionToRemove.selected = false;
+ this.updateOptions();
+ }
+}