From: Ricardo Marques Date: Mon, 18 Jun 2018 15:24:31 +0000 (+0100) Subject: mgr/dashboard: Add cd-select-badges component X-Git-Tag: v14.0.1~771^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=10e83a03b63a35234d827e27cde2fe5f755a025a;p=ceph.git mgr/dashboard: Add cd-select-badges component Signed-off-by: Ricardo Marques --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/components.module.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/components.module.ts index e4fb9b2b29c3..ae30c292ecaf 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/components.module.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/components.module.ts @@ -13,6 +13,7 @@ import { HelperComponent } from './helper/helper.component'; 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'; @@ -36,6 +37,7 @@ import { WarningPanelComponent } from './warning-panel/warning-panel.component'; ViewCacheComponent, SparklineComponent, HelperComponent, + SelectBadgesComponent, SubmitButtonComponent, UsageBarComponent, ErrorPanelComponent, @@ -51,6 +53,7 @@ import { WarningPanelComponent } from './warning-panel/warning-panel.component'; ViewCacheComponent, SparklineComponent, HelperComponent, + SelectBadgesComponent, SubmitButtonComponent, ErrorPanelComponent, LoadingPanelComponent, diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges-option.model.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges-option.model.ts new file mode 100644 index 000000000000..0c28f1cc532c --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges-option.model.ts @@ -0,0 +1,5 @@ +interface SelectBadgesOption { + selected: boolean; + name: string; + description: string; +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.html b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.html new file mode 100644 index 000000000000..2fa9d1b52477 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.html @@ -0,0 +1,40 @@ + +
+
+ +   +
+
+ {{ option.name }} +
+ + {{ option.description }}  + +
+
+
+ + + + + + {{ emptyMessage }} + + + + {{ dataItem }} + + + + + diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.scss b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.scss new file mode 100644 index 000000000000..bd43177b64cf --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.scss @@ -0,0 +1,25 @@ +@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; +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.spec.ts new file mode 100644 index 000000000000..52c3de2161cd --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.spec.ts @@ -0,0 +1,58 @@ +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; + + 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']); + }); + +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.ts new file mode 100644 index 000000000000..5ca1275e4714 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/select-badges/select-badges.component.ts @@ -0,0 +1,48 @@ +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 = []; + @Input() options: Array = []; + @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(); + } +}