-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact: move badge to a separate component
- Loading branch information
Showing
7 changed files
with
114 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
frontend/src/app/GN2CommonModule/others/badge/badge.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<span | ||
class="badge badge-secondary" | ||
[style]="symbologyAsCSS" | ||
matTooltip="{{ tooltip }}" | ||
> | ||
{{ text }} | ||
</span> |
15 changes: 15 additions & 0 deletions
15
frontend/src/app/GN2CommonModule/others/badge/badge.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.badge { | ||
--bgColor: #ffffff; // Default value | ||
--textColor: #444; // Default value | ||
|
||
display: flex; | ||
flex-flow: row nowrap; | ||
white-space: nowrap; | ||
gap: 0.25rem; | ||
text-transform: uppercase; | ||
font-weight: bold; | ||
|
||
background-color: var(--bgColor); | ||
border: 2px solid color-mix(in srgb, var(--bgColor) 80%, black); | ||
color: var(--textColor); | ||
} |
68 changes: 68 additions & 0 deletions
68
frontend/src/app/GN2CommonModule/others/badge/badge.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { GN2CommonModule } from '@geonature_common/GN2Common.module'; | ||
|
||
// //////////////////////////////////////////////////////////////////////////// | ||
// helper method | ||
// //////////////////////////////////////////////////////////////////////////// | ||
|
||
function isHexadecimalColor(color: string) { | ||
return /^#[0-9A-F]{6}$/i.test(color); | ||
} | ||
|
||
function computeContrastColor(backgroundColor: string) { | ||
// Convertir la couleur en un format RGB | ||
const r = parseInt(backgroundColor.slice(1, 3), 16); | ||
const g = parseInt(backgroundColor.slice(3, 5), 16); | ||
const b = parseInt(backgroundColor.slice(5, 7), 16); | ||
|
||
// Calculer la luminosité | ||
const luminance = 0.299 * r + 0.587 * g + 0.114 * b; | ||
|
||
// Retourner une couleur claire ou foncée selon la luminosité | ||
return luminance < 128 ? '#ffffff' : '#444'; | ||
} | ||
|
||
function colorToCSS(color: string) { | ||
return `--bgColor: ${color}; --textColor: ${computeContrastColor(color)};`; | ||
} | ||
|
||
// //////////////////////////////////////////////////////////////////////////// | ||
// Badge parameters | ||
// //////////////////////////////////////////////////////////////////////////// | ||
|
||
export interface BadgeSymbology { | ||
color?: string; | ||
} | ||
|
||
// //////////////////////////////////////////////////////////////////////////// | ||
// helper method | ||
// //////////////////////////////////////////////////////////////////////////// | ||
|
||
@Component({ | ||
selector: 'gn-badge', | ||
templateUrl: 'badge.component.html', | ||
styleUrls: ['badge.component.scss'], | ||
}) | ||
export class BadgeComponent { | ||
@Input() | ||
text: string; | ||
|
||
@Input() | ||
tooltip: string; | ||
|
||
symbologyAsCSS: string; | ||
|
||
@Input() | ||
set symbology(symbology: BadgeSymbology | null) { | ||
this.symbologyAsCSS = ''; | ||
if (!symbology) { | ||
return; | ||
} | ||
if (!isHexadecimalColor(symbology.color)) { | ||
console.warn(`[badge] ${symbology.color} is not a valid hexadecimal color`); | ||
return; | ||
} | ||
this.symbologyAsCSS = colorToCSS(symbology.color); | ||
} | ||
} |
14 changes: 6 additions & 8 deletions
14
frontend/src/app/syntheseModule/taxon-sheet/infos/status/status.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
<div class="Status"> | ||
<div class="Status__header">Statuts</div> | ||
<div class="Status__badges"> | ||
<span | ||
class="badge badge-secondary" | ||
*ngFor="let status of status; let isLast = last" | ||
[style]="status.symbologyAsCSS" | ||
matTooltip="{{ status.tooltip }}" | ||
> | ||
{{ status.badge }} | ||
</span> | ||
<gn-badge | ||
*ngFor="let status of status" | ||
[tooltip]="status.tooltip" | ||
[text]="status.badge" | ||
[symbology]="status.symbology" | ||
/> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters