]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: auto-coloring-badges-component
authorPedro Gonzalez Gomez <pegonzal@redhat.com>
Tue, 5 Jul 2022 09:40:01 +0000 (11:40 +0200)
committerPedro Gonzalez Gomez <pegonzal@redhat.com>
Mon, 29 Aug 2022 16:03:11 +0000 (18:03 +0200)
Signed-off-by: Pedro Gonzalez Gomez <pegonzal@redhat.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.html [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.scss [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/color-class-from-text.pipe.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/components/components.module.ts
src/pybind/mgr/dashboard/frontend/src/styles.scss
src/pybind/mgr/dashboard/frontend/src/styles/defaults/_bootstrap-defaults.scss

diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.html b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.html
new file mode 100644 (file)
index 0000000..41cfae7
--- /dev/null
@@ -0,0 +1,11 @@
+<span *ngIf="!key; else key_value"
+      class="badge badge-{{value}}"
+      ngClass="{{value | colorClassFromText}}">
+  {{ value }}
+</span>
+
+<ng-template #key_value>
+  <span class="badge badge-background-primary badge-{{key}}-{{value}}">
+        {{ key }}: {{ value }}
+  </span>
+</ng-template>
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.scss b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.scss
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.spec.ts
new file mode 100644 (file)
index 0000000..61472cd
--- /dev/null
@@ -0,0 +1,25 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { CdLabelComponent } from './cd-label.component';
+import { ColorClassFromTextPipe } from './color-class-from-text.pipe';
+
+describe('CdLabelComponent', () => {
+  let component: CdLabelComponent;
+  let fixture: ComponentFixture<CdLabelComponent>;
+
+  beforeEach(async () => {
+    await TestBed.configureTestingModule({
+      declarations: [CdLabelComponent, ColorClassFromTextPipe]
+    }).compileComponents();
+  });
+
+  beforeEach(() => {
+    fixture = TestBed.createComponent(CdLabelComponent);
+    component = fixture.componentInstance;
+    fixture.detectChanges();
+  });
+
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
+});
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/cd-label.component.ts
new file mode 100644 (file)
index 0000000..ae0471b
--- /dev/null
@@ -0,0 +1,11 @@
+import { Component, Input } from '@angular/core';
+
+@Component({
+  selector: 'cd-label',
+  templateUrl: './cd-label.component.html',
+  styleUrls: ['./cd-label.component.scss']
+})
+export class CdLabelComponent {
+  @Input() key?: string;
+  @Input() value?: string;
+}
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/color-class-from-text.pipe.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/cd-label/color-class-from-text.pipe.ts
new file mode 100644 (file)
index 0000000..628d743
--- /dev/null
@@ -0,0 +1,28 @@
+import { Pipe, PipeTransform } from '@angular/core';
+
+@Pipe({
+  name: 'colorClassFromText'
+})
+export class ColorClassFromTextPipe implements PipeTransform {
+  readonly cssClasses: string[] = [
+    'badge-cd-label-green',
+    'badge-cd-label-cyan',
+    'badge-cd-label-purple',
+    'badge-cd-label-light-blue',
+    'badge-cd-label-gold',
+    'badge-cd-label-light-green'
+  ];
+
+  transform(text: string): string {
+    let hash = 0;
+    let charCode = 0;
+    if (text) {
+      for (let i = 0; i < text.length; i++) {
+        charCode = text.charCodeAt(i);
+        // tslint:disable-next-line:no-bitwise
+        hash = Math.abs((hash << 5) - hash + charCode);
+      }
+    }
+    return this.cssClasses[hash % this.cssClasses.length];
+  }
+}
index a281bf8598e54d1a03f01be140cddf73a95b2442..5350e2bd50b00b0ddc5299ada572c2a00600a7be 100644 (file)
@@ -21,6 +21,8 @@ import { DirectivesModule } from '../directives/directives.module';
 import { PipesModule } from '../pipes/pipes.module';
 import { AlertPanelComponent } from './alert-panel/alert-panel.component';
 import { BackButtonComponent } from './back-button/back-button.component';
+import { CdLabelComponent } from './cd-label/cd-label.component';
+import { ColorClassFromTextPipe } from './cd-label/color-class-from-text.pipe';
 import { ConfigOptionComponent } from './config-option/config-option.component';
 import { ConfirmationModalComponent } from './confirmation-modal/confirmation-modal.component';
 import { Copy2ClipboardButtonComponent } from './copy2clipboard-button/copy2clipboard-button.component';
@@ -97,7 +99,9 @@ import { WizardComponent } from './wizard/wizard.component';
     FormButtonPanelComponent,
     MotdComponent,
     WizardComponent,
-    CustomLoginBannerComponent
+    CustomLoginBannerComponent,
+    CdLabelComponent,
+    ColorClassFromTextPipe
   ],
   providers: [],
   exports: [
@@ -126,7 +130,8 @@ import { WizardComponent } from './wizard/wizard.component';
     FormButtonPanelComponent,
     MotdComponent,
     WizardComponent,
-    CustomLoginBannerComponent
+    CustomLoginBannerComponent,
+    CdLabelComponent
   ]
 })
 export class ComponentsModule {}
index 099433d3148cc494089c1085700300078a2cfcf6..e27b012614715e338e234b8c1e9f5271800ad2d6 100644 (file)
@@ -105,6 +105,36 @@ $grid-breakpoints: (
   color: $gray-700;
 }
 
+.badge-cd-label-green {
+  background-color: $green-300;
+  color: $white;
+}
+
+.badge-cd-label-cyan {
+  background-color: $cyan-300;
+  color: $white;
+}
+
+.badge-cd-label-purple {
+  background-color: $purple-300;
+  color: $white;
+}
+
+.badge-cd-label-light-blue {
+  background-color: $light-blue-300;
+  color: $white;
+}
+
+.badge-cd-label-gold {
+  background-color: $gold-300;
+  color: $white;
+}
+
+.badge-cd-label-light-green {
+  background-color: $light-green-300;
+  color: $white;
+}
+
 // angular-tree-component
 tree-root {
   tree-viewport {
index d4a3d5f86bec7a491a5612b2a01dd911a07c9e45..27547c36b04351e89e89441b7b03988237d7bc62 100644 (file)
@@ -33,6 +33,14 @@ $danger: $red !default;
 $light: $gray-100 !default;
 $dark: #777 !default;
 
+//badges colors
+$green-300: #6ec664;
+$cyan-300: #009596;
+$purple-300: #a18fff;
+$light-blue-300: #35caed;
+$gold-300: #f4c145;
+$light-green-300: #ace12e;
+
 // Extra theme colors.
 $accent: $red !default;
 $warning-dark: $orange !default;