From 346cfa3969d752cbcc55ba6f6aec903a8bdf5fb1 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Fri, 1 Mar 2019 10:27:13 +0100 Subject: [PATCH] mgr/dashboard: Introduce pipe to convert bool to text Convert boolean values to 'Yes' or 'No' or any other value. Signed-off-by: Volker Theile --- .../configuration-details.component.html | 2 +- .../configuration-details.component.spec.ts | 3 +- .../rgw-bucket-details.component.html | 2 +- .../rgw-bucket-details.component.spec.ts | 5 +- .../rgw-user-details.component.html | 6 +-- .../shared/pipes/boolean-text.pipe.spec.ts | 47 +++++++++++++++++++ .../src/app/shared/pipes/boolean-text.pipe.ts | 18 +++++++ .../src/app/shared/pipes/pipes.module.ts | 4 ++ .../frontend/src/locale/messages.xlf | 14 ++++++ 9 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/boolean-text.pipe.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/boolean-text.pipe.ts diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/configuration/configuration-details/configuration-details.component.html b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/configuration/configuration-details/configuration-details.component.html index b8d6037ca00..b5776a7e067 100755 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/configuration/configuration-details/configuration-details.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/configuration/configuration-details/configuration-details.component.html @@ -85,7 +85,7 @@ Can be updated at runtime (editable) - {{ selectedItem.can_update_at_runtime }} + {{ selectedItem.can_update_at_runtime | booleanText }} { @@ -12,7 +13,7 @@ describe('ConfigurationDetailsComponent', () => { configureTestBed({ declarations: [ConfigurationDetailsComponent], - imports: [DataTableModule, TabsModule.forRoot()], + imports: [DataTableModule, SharedModule, TabsModule.forRoot()], providers: [i18nProviders] }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-details/rgw-bucket-details.component.html b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-details/rgw-bucket-details.component.html index 3ccb9cb684f..f1ef63cb2af 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-details/rgw-bucket-details.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-bucket-details/rgw-bucket-details.component.html @@ -69,7 +69,7 @@ Enabled - {{ bucket.bucket_quota.enabled ? "Yes" : "No" }} + {{ bucket.bucket_quota.enabled | booleanText }} { configureTestBed({ declarations: [RgwBucketDetailsComponent], - imports: [SharedModule, TabsModule.forRoot()] + imports: [SharedModule, TabsModule.forRoot()], + providers: [i18nProviders] }); beforeEach(() => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-details/rgw-user-details.component.html b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-details/rgw-user-details.component.html index 0e23d16b44e..4a90eb45a25 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-details/rgw-user-details.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-details/rgw-user-details.component.html @@ -22,7 +22,7 @@ Suspended - {{ user.suspended ? "Yes" : "No" }} + {{ user.suspended | booleanText }} Enabled - {{ user.user_quota.enabled ? "Yes" : "No" }} + {{ user.user_quota.enabled | booleanText }} Enabled - {{ user.bucket_quota.enabled ? "Yes" : "No" }} + {{ user.bucket_quota.enabled | booleanText }} { + let pipe: BooleanTextPipe; + + configureTestBed({ + providers: [i18nProviders] + }); + + beforeEach(() => { + const i18n = TestBed.get(I18n); + pipe = new BooleanTextPipe(i18n); + }); + + it('create an instance', () => { + expect(pipe).toBeTruthy(); + }); + + it('transforms true', () => { + expect(pipe.transform(true)).toEqual('Yes'); + }); + + it('transforms true, alternative text', () => { + expect(pipe.transform(true, 'foo')).toEqual('foo'); + }); + + it('transforms 1', () => { + expect(pipe.transform(1)).toEqual('Yes'); + }); + + it('transforms false', () => { + expect(pipe.transform(false)).toEqual('No'); + }); + + it('transforms false, alternative text', () => { + expect(pipe.transform(false, 'foo', 'bar')).toEqual('bar'); + }); + + it('transforms 0', () => { + expect(pipe.transform(0)).toEqual('No'); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/boolean-text.pipe.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/boolean-text.pipe.ts new file mode 100644 index 00000000000..dba4e8bc010 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/boolean-text.pipe.ts @@ -0,0 +1,18 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +import { I18n } from '@ngx-translate/i18n-polyfill'; + +@Pipe({ + name: 'booleanText' +}) +export class BooleanTextPipe implements PipeTransform { + constructor(private i18n: I18n) {} + + transform( + value: any, + truthyText: string = this.i18n('Yes'), + falsyText: string = this.i18n('No') + ): string { + return Boolean(value) ? truthyText : falsyText; + } +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/pipes.module.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/pipes.module.ts index 0e53746f9d6..85db7a7c96f 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/pipes.module.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/pipes.module.ts @@ -1,6 +1,7 @@ import { CommonModule, DatePipe } from '@angular/common'; import { NgModule } from '@angular/core'; +import { BooleanTextPipe } from './boolean-text.pipe'; import { CdDatePipe } from './cd-date.pipe'; import { CephReleaseNamePipe } from './ceph-release-name.pipe'; import { CephShortVersionPipe } from './ceph-short-version.pipe'; @@ -23,6 +24,7 @@ import { UpperFirstPipe } from './upper-first.pipe'; @NgModule({ imports: [CommonModule], declarations: [ + BooleanTextPipe, DimlessBinaryPipe, DimlessBinaryPerSecondPipe, HealthColorPipe, @@ -43,6 +45,7 @@ import { UpperFirstPipe } from './upper-first.pipe'; UpperFirstPipe ], exports: [ + BooleanTextPipe, DimlessBinaryPipe, DimlessBinaryPerSecondPipe, HealthColorPipe, @@ -63,6 +66,7 @@ import { UpperFirstPipe } from './upper-first.pipe'; UpperFirstPipe ], providers: [ + BooleanTextPipe, DatePipe, CephShortVersionPipe, CephReleaseNamePipe, diff --git a/src/pybind/mgr/dashboard/frontend/src/locale/messages.xlf b/src/pybind/mgr/dashboard/frontend/src/locale/messages.xlf index f8f072ddaf4..8b88098120f 100644 --- a/src/pybind/mgr/dashboard/frontend/src/locale/messages.xlf +++ b/src/pybind/mgr/dashboard/frontend/src/locale/messages.xlf @@ -6228,6 +6228,20 @@ 1 + + Yes + + src/app/shared/pipes/boolean-text.pipe.ts + 1 + + + + No + + src/app/shared/pipes/boolean-text.pipe.ts + 1 + + Quality of Service