️ Unique name for edit modal + fixed bug empty content after closing modal one time + new event from parent to children instead of NavigationEnd + all component in one div (avoid moving two divs in the body at the same time) + check if modal is open for drag&drop events

timeline
Alix JEUDI--LEMOINE 1 week ago
parent 8c9b32a9ad
commit f3ea54f8af

@ -1,8 +1,8 @@
<!-- Modal toggle -->
<button
class="p-1 text-blue-500 rounded hover:bg-blue-100 focus:outline-none flex items-center"
aria-label="Edit"
(click)="openPinModal()"
class="p-1 text-blue-500 rounded hover:bg-blue-100 focus:outline-none flex items-center"
aria-label="Edit"
(click)="openPinModal()"
>
<svg
class="w-[1.125rem] h-[1.125rem] text-gray-800"
@ -23,171 +23,172 @@
</svg>
</button>
<!-- Fond assombri -->
<div
class="fixed inset-0 bg-gray-900 bg-opacity-50 w-full h-full transition-opacity duration-300 ease-in-out z-40"
[ngClass]="{
'opacity-0 pointer-events-none': !isPinModalOpen,
'opacity-100': isPinModalOpen
}"
(click)="closePinModal()"
id="pin-modal-background"
></div>
<div id="edit-pin-popup-{{pinId}}">
<!-- Fond assombri -->
<div
class="fixed inset-0 bg-gray-900 bg-opacity-50 w-full h-full transition-opacity duration-300 ease-in-out z-40"
[ngClass]="{
'opacity-0 pointer-events-none': !isPinModalOpen,
'opacity-100': isPinModalOpen
}"
(click)="closePinModal()"
></div>
<!-- Main modal -->
<div
id="pin-modal"
tabindex="-1"
aria-hidden="true"
[ngClass]="{
'opacity-0 scale-50 pointer-events-none': !isPinModalOpen,
'opacity-100 scale-100': isPinModalOpen
}"
class="fixed top-0 right-0 left-0 z-50 flex justify-center items-center w-full h-full transition-transform duration-300 ease-in-out overflow-y-auto"
>
<div class="relative p-4 w-full max-w-xl max-h-full">
<!-- Modal content -->
<div
class="relative bg-white rounded-lg shadow dark:bg-gray-700 transition-transform duration-300 ease-in-out my-8"
>
<!-- Modal header -->
<!-- Main modal -->
<div
tabindex="-1"
aria-hidden="true"
[ngClass]="{
'opacity-0 scale-50 pointer-events-none': !isPinModalOpen,
'opacity-100 scale-100': isPinModalOpen
}"
class="fixed top-0 right-0 left-0 z-50 flex justify-center items-center w-full h-full transition-transform duration-300 ease-in-out overflow-y-auto"
>
<div class="relative p-4 w-full max-w-xl max-h-full">
<!-- Modal content -->
<div
class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600"
class="relative bg-white rounded-lg shadow dark:bg-gray-700 transition-transform duration-300 ease-in-out my-8"
>
<h3 class="text-xl font-semibold text-gray-900 dark:text-white">
Modifie ton souvenir
</h3>
<button
type="button"
(click)="closePinModal()"
class="end-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white"
<!-- Modal header -->
<div
class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600"
>
<svg
class="w-3 h-3"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 14 14"
<h3 class="text-xl font-semibold text-gray-900 dark:text-white">
Modifie ton souvenir
</h3>
<button
type="button"
(click)="closePinModal()"
class="end-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white"
>
<path
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"
/>
</svg>
<span class="sr-only">Fermer la modal</span>
</button>
</div>
<!-- Modal body -->
<div class="p-4 md:p-5">
<form class="grid gap-6 mb-1 md:grid-cols-2" [formGroup]="form">
<div>
<label
for="title"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Titre</label
<svg
class="w-3 h-3"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 14 14"
>
<input
type="text"
id="title"
formControlName="title"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
placeholder="Mont Saint-Michel"
required
/>
</div>
<path
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"
/>
</svg>
<span class="sr-only">Fermer la modal</span>
</button>
</div>
<div>
<label
for="localisation"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Localisation</label
>
<input
type="text"
id="localisation"
formControlName="location"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
placeholder="Saisir la localisation"
(focus)="onFocus()"
(blur)="onBlur()"
/>
<ul
*ngIf="suggestions.length > 0 && inputFocused"
class="bg-white dark:bg-gray-700 border border-gray-300 mt-2 rounded shadow absolute z-10 mr-5 max-h-60 overflow-auto"
>
<li
*ngFor="let suggestion of suggestions"
(click)="selectSuggestion(suggestion)"
class="p-2 block mb-2 text-sm font-medium text-gray-900 dark:text-white hover:bg-gray-500 cursor-pointer"
<!-- Modal body -->
<div class="p-4 md:p-5">
<form class="grid gap-6 mb-1 md:grid-cols-2" [formGroup]="form">
<div>
<label
for="title"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Titre</label
>
{{ suggestion.display_name }}
</li>
</ul>
</div>
<input
type="text"
id="title"
formControlName="title"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
placeholder="Mont Saint-Michel"
required
/>
</div>
<div>
<label
for="files"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Images</label
>
<app-drag-drop
[initialFiles]="getFileNames()"
(filesSelected)="onFilesReceived($event)"
(fileRemoved)="removeFile($event)"
[errorMessage]="uploadError"
></app-drag-drop>
</div>
<div>
<label
for="localisation"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Localisation</label
>
<input
type="text"
id="localisation"
formControlName="location"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
placeholder="Saisir la localisation"
(focus)="onFocus()"
(blur)="onBlur()"
/>
<ul
*ngIf="suggestions.length > 0 && inputFocused"
class="bg-white dark:bg-gray-700 border border-gray-300 mt-2 rounded shadow absolute z-10 mr-5 max-h-60 overflow-auto"
>
<li
*ngFor="let suggestion of suggestions"
(click)="selectSuggestion(suggestion)"
class="p-2 block mb-2 text-sm font-medium text-gray-900 dark:text-white hover:bg-gray-500 cursor-pointer"
>
{{ suggestion.display_name }}
</li>
</ul>
</div>
<div>
<label
for="description"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Description</label
>
<textarea
id="description"
rows="4"
formControlName="description"
class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Décrit ton souvenir..."
></textarea>
</div>
<div>
<label
for="files"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Images</label
>
<app-drag-drop
*ngIf="isPinModalOpen"
[initialFiles]="getFileNames()"
(filesSelected)="onFilesReceived($event)"
(fileRemoved)="removeFile($event)"
[errorMessage]="uploadError"
></app-drag-drop>
</div>
<div>
<label
for="date"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Date (optionnel)</label
<div>
<label
for="description"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Description</label
>
<textarea
id="description"
rows="4"
formControlName="description"
class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Décrit ton souvenir..."
></textarea>
</div>
<div>
<label
for="date"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>Date (optionnel)</label
>
<input
type="date"
id="date"
formControlName="date"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
/>
</div>
</form>
<!-- Boutons alignés sous la grille -->
<div class="flex gap-4 mt-6">
<button
type="reset"
(click)="closePinModal()"
class="w-1/2 text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800"
>
Annuler
</button>
<button
type="submit"
(click)="submitForm()"
class="w-1/2 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
<input
type="date"
id="date"
formControlName="date"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
/>
Valider
</button>
</div>
</form>
<!-- Boutons alignés sous la grille -->
<div class="flex gap-4 mt-6">
<button
type="reset"
(click)="closePinModal()"
class="w-1/2 text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800"
>
Annuler
</button>
<button
type="submit"
(click)="submitForm()"
class="w-1/2 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
Valider
</button>
</div>
</div>
</div>

@ -1,7 +1,7 @@
import { CommonModule } from '@angular/common';
import {
AfterViewInit,
Component,
EventEmitter,
Input,
OnDestroy,
OnInit,
@ -38,22 +38,24 @@ import { DragDropComponent } from '../drag-drop/drag-drop.component';
imports: [ReactiveFormsModule, CommonModule, DragDropComponent],
templateUrl: './edit-pin-popup.component.html',
})
export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
export class EditPinPopupComponent implements OnInit, OnDestroy {
@Input() isHomePage: boolean = false;
@Input() pin!: Pin;
@Input() pinId!: string;
@Input() pinOpened!: EventEmitter<void>;
@ViewChild(DragDropComponent) dragDropComponent!: DragDropComponent;
private modalOpenSubscription!: Subscription;
form!: FormGroup;
suggestions: any[] = [];
inputFocused: boolean = false;
files: File[] = [];
isPinModalOpen: boolean = false;
@Input() modalId!: string;
uploadError: string = '';
private modalOpenSubscription!: Subscription;
private routerSubscription!: Subscription;
private locationSubscription!: Subscription;
modalId: string = '';
constructor(
private fb: FormBuilder,
@ -61,7 +63,6 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
private pinService: PinService,
private exifService: ExifService,
private modalService: ModalService,
private router: Router,
private mapReloadService: MapReloadService,
private imageService: ImageService
) {
@ -88,42 +89,19 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
}
ngOnInit(): void {
// Initialiser le formulaire avec les valeurs de base
this.form.patchValue({
title: this.pin?.title || '',
description: this.pin?.description || '',
location: this.pin?.complete_address || '',
complete_address: this.pin?.complete_address || '',
coordinates: this.pin?.location || [],
files: this.pin?.files || [],
date: this.pin?.date
? new Date(this.pin.date).toISOString().split('T')[0]
: '',
});
this.pin.files.forEach((file) => {
this.imageService.getImageMetadata(file).subscribe((metadata) => {
this.files.push(new File([], metadata.metadata.original_filename + "|" + file.toString(), { type: metadata.metadata.content_type }));
});
});
this.modalId = 'edit-pin-popup-' + this.pinId;
// S'abonner aux changements d'état du modal
this.modalOpenSubscription = this.modalService
.getModalState(this.modalId)
.subscribe((state) => {
this.isPinModalOpen = state;
if (state) {
setTimeout(() => this.moveModalToBody(), 0);
}
});
// S'abonner aux événements de navigation du router
this.routerSubscription = this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe(() => {
// Attendre que le DOM soit mis à jour après la navigation
setTimeout(() => this.moveModalToBody(), 0);
});
this.pinOpened.subscribe(() => {
this.moveModalToBody();
});
// Configuration de l'autocomplétion pour le champ d'adresse
this.form
@ -153,36 +131,19 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
});
}
ngAfterViewInit() {
// Appel initial pour déplacer le modal
this.moveModalToBody();
}
ngOnDestroy() {
// Nettoyage des abonnements pour éviter les fuites de mémoire
if (this.modalOpenSubscription) {
this.modalOpenSubscription.unsubscribe();
}
if (this.routerSubscription) {
this.routerSubscription.unsubscribe();
}
if (this.locationSubscription) {
this.locationSubscription.unsubscribe();
}
}
// Méthode dédiée pour déplacer le modal vers le body
private moveModalToBody(): void {
const modal = document.getElementById('pin-modal');
const modal = document.getElementById(this.modalId);
if (modal && modal.parentElement !== document.body) {
document.body.appendChild(modal);
}
const bg = document.getElementById('pin-modal-background');
if (bg && bg.parentElement !== document.body) {
document.body.appendChild(bg);
}
}
selectSuggestion(suggestion: any): void {
@ -257,7 +218,6 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
});
forkJoin(uploadObservables).subscribe((responses) => {
console.log(responses);
// Vérifier si toutes les réponses sont valides
if (responses.some(response => response === null)) {
return; // Ne pas continuer si une erreur s'est produite
@ -287,22 +247,35 @@ export class EditPinPopupComponent implements OnInit, AfterViewInit, OnDestroy {
}
openPinModal() {
// Initialiser le formulaire avec les valeurs de base
this.form.patchValue({
title: this.pin?.title || '',
description: this.pin?.description || '',
location: this.pin?.complete_address || '',
complete_address: this.pin?.complete_address || '',
coordinates: this.pin?.location || [],
files: this.pin?.files || [],
date: this.pin?.date
? new Date(this.pin.date).toISOString().split('T')[0]
: '',
});
this.pin.files.forEach((file) => {
this.imageService.getImageMetadata(file).subscribe((metadata) => {
this.files.push(new File([], metadata.metadata.original_filename + "|" + file.toString(), { type: metadata.metadata.content_type }));
});
});
this.modalService.openModal(this.modalId);
}
closePinModal() {
this.modalService.closeModal(this.modalId);
this.form.reset();
this.files = [];
this.uploadError = '';
if (this.dragDropComponent) {
this.dragDropComponent.updateFileNamesFromFileList(new DataTransfer().files);
this.dragDropComponent.errorMessage = '';
}
this.modalService.closeModal(this.modalId);
}
removeFile(fileName: string): void {
const index = this.files.findIndex((file) => file.name === fileName);
const index = this.files.findIndex((file) => file.name === fileName || file.name.split("|")[0] === fileName);
if (index > -1) {
this.files.splice(index, 1);
this.uploadError = ''; // Réinitialiser l'erreur lors de la suppression d'un fichier

Loading…
Cancel
Save