From dacdb4ad3f5848eaa74133d1fee8e28ad0f30912 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Tue, 9 Jul 2024 14:18:43 +0200 Subject: [PATCH] feat: introduce new dropdown selector --- apps/datahub/src/app/app.module.ts | 3 + .../dropdown-multiselect.component.html | 6 +- .../filter-dropdown.component.html | 12 ++- .../mel-datahub-dropdown-range.component.html | 92 +++++++++++++++++ .../mel-datahub-dropdown-range.component.ts | 98 +++++++++++++++++++ 5 files changed, 206 insertions(+), 5 deletions(-) create mode 100644 apps/datahub/src/app/search/search-filters/mel-datahub-dropdown-range/mel-datahub-dropdown-range.component.html create mode 100644 apps/datahub/src/app/search/search-filters/mel-datahub-dropdown-range/mel-datahub-dropdown-range.component.ts diff --git a/apps/datahub/src/app/app.module.ts b/apps/datahub/src/app/app.module.ts index 63e1fa30..fb9ecf73 100644 --- a/apps/datahub/src/app/app.module.ts +++ b/apps/datahub/src/app/app.module.ts @@ -63,6 +63,7 @@ import { MelDataViewComponent } from './dataset/dataset-visualisation/data-view/ import { environment } from '../environments/environnment' import { MelModule, MelEmbeddedTranslateLoader } from '@mel-dataplatform/mel' import { MelFieldsService } from './search/service/fields.service' +import { MelDatahubDropdownRangeComponent } from './search/search-filters/mel-datahub-dropdown-range/mel-datahub-dropdown-range.component' @NgModule({ declarations: [ @@ -86,6 +87,8 @@ import { MelFieldsService } from './search/service/fields.service' DatasetVisualisationComponent, MelMapViewComponent, MelDataViewComponent, + MelDatahubDropdownRangeComponent, + MelDatahubDropdownRangeComponent, ], imports: [ MelModule, diff --git a/apps/datahub/src/app/search/search-filters/dropdown-multiselect/dropdown-multiselect.component.html b/apps/datahub/src/app/search/search-filters/dropdown-multiselect/dropdown-multiselect.component.html index a3494706..ffde1e08 100644 --- a/apps/datahub/src/app/search/search-filters/dropdown-multiselect/dropdown-multiselect.component.html +++ b/apps/datahub/src/app/search/search-filters/dropdown-multiselect/dropdown-multiselect.component.html @@ -1,16 +1,16 @@ + + @if(overlayOpen){ + expand_less + } @else { + expand_more + } + + + + +
+
+
+
+ mel.datahub.search.filters.range.from +
+ +
+
+
+ mel.datahub.search.filters.range.to +
+ +
+ +
+
+
diff --git a/apps/datahub/src/app/search/search-filters/mel-datahub-dropdown-range/mel-datahub-dropdown-range.component.ts b/apps/datahub/src/app/search/search-filters/mel-datahub-dropdown-range/mel-datahub-dropdown-range.component.ts new file mode 100644 index 00000000..dacd88d3 --- /dev/null +++ b/apps/datahub/src/app/search/search-filters/mel-datahub-dropdown-range/mel-datahub-dropdown-range.component.ts @@ -0,0 +1,98 @@ +import { + CdkOverlayOrigin, + ConnectedPosition, + ScrollStrategyOptions, +} from '@angular/cdk/overlay' +import { + ChangeDetectionStrategy, + Component, + ElementRef, + EventEmitter, + Input, + Output, + ViewChild, +} from '@angular/core' +import { Choice, propagateToDocumentOnly } from 'geonetwork-ui' + +@Component({ + selector: 'mel-datahub-dropdown-range', + templateUrl: './mel-datahub-dropdown-range.component.html', + styles: ``, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class MelDatahubDropdownRangeComponent { + lowValue = '' + highValue = '' + @Input() choices: Choice[] + @Input() title: string + @Input() selected: unknown[] = [] + @Output() selectValues = new EventEmitter() + @ViewChild('overlayOrigin') overlayOrigin: CdkOverlayOrigin + @ViewChild('overlayContainer', { read: ElementRef }) + overlayContainer: ElementRef + overlayPositions: ConnectedPosition[] = [ + { + originX: 'start', + originY: 'bottom', + overlayX: 'start', + overlayY: 'top', + offsetY: 8, + }, + { + originX: 'start', + originY: 'top', + overlayX: 'start', + overlayY: 'bottom', + offsetY: -8, + }, + ] + scrollStrategy = this.scrollStrategies.reposition() + overlayOpen = false + overlayWidth = 'auto' + overlayMaxHeight = 'none' + id = `dropdown-multiselect-${Math.floor(Math.random() * 10000)}` + + get hasSelectedChoices() { + return this.selected.length > 0 + } + + constructor(private scrollStrategies: ScrollStrategyOptions) {} + + openOverlay() { + this.overlayWidth = + this.overlayOrigin.elementRef.nativeElement.getBoundingClientRect() + .width + 'px' + this.overlayOpen = true + } + + closeOverlay() { + this.overlayOpen = false + } + + clearSelection(event: Event) { + this.selectValues.emit([]) + this.highValue = '' + this.lowValue = '' + this.selected = [] + propagateToDocumentOnly(event) + } + + onValidate() { + const lowValue = Number(this.lowValue) + const highValue = Number(this.highValue) + this.selected = this.choices + .filter((choice) => { + const choiceNb = Number(choice.value) + if (lowValue && highValue) { + return choiceNb >= lowValue && choiceNb <= highValue + } else if (lowValue) { + return choiceNb >= lowValue + } else if (highValue) { + return choiceNb <= highValue + } + return true + }) + .map((choice) => choice.value) + this.selectValues.emit(this.selected) + } +}