|
| 1 | +import Alpine from 'alpinejs'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Store global pour gérer les alertes de sortie avec saisie en cours. |
| 5 | + * |
| 6 | + * Usage depuis n'importe quel composant : |
| 7 | + * |
| 8 | + * // Signaler une saisie en cours |
| 9 | + * this.$store.onLeaveAlert.setDirty(true); |
| 10 | + * |
| 11 | + * // Signaler la fin de la saisie (après submit) |
| 12 | + * this.$store.onLeaveAlert.setDirty(false); |
| 13 | + * |
| 14 | + * Le store intercepte automatiquement : |
| 15 | + * - La fermeture de l'onglet/navigateur (beforeunload) |
| 16 | + * - Les clics sur les liens de navigation |
| 17 | + */ |
| 18 | +Alpine.store('onLeaveAlert', { |
| 19 | + isDirty: false, |
| 20 | + isOpen: false, |
| 21 | + pendingNavigation: null, |
| 22 | + |
| 23 | + init() { |
| 24 | + this.setupBeforeUnload(); |
| 25 | + this.setupNavigationInterception(); |
| 26 | + }, |
| 27 | + |
| 28 | + /** |
| 29 | + * Définit l'état de saisie en cours |
| 30 | + */ |
| 31 | + setDirty(value) { |
| 32 | + this.isDirty = value; |
| 33 | + }, |
| 34 | + |
| 35 | + /** |
| 36 | + * Configure l'événement beforeunload |
| 37 | + */ |
| 38 | + setupBeforeUnload() { |
| 39 | + window.addEventListener('beforeunload', (event) => { |
| 40 | + if (this.isDirty) { |
| 41 | + event.preventDefault(); |
| 42 | + } |
| 43 | + }); |
| 44 | + }, |
| 45 | + |
| 46 | + /** |
| 47 | + * Intercepte les clics sur les liens |
| 48 | + */ |
| 49 | + setupNavigationInterception() { |
| 50 | + document.addEventListener( |
| 51 | + 'click', |
| 52 | + (event) => { |
| 53 | + if (!this.isDirty) return; |
| 54 | + |
| 55 | + const link = event.target.closest('a[href]'); |
| 56 | + if (!link) return; |
| 57 | + |
| 58 | + // Ignorer certains liens |
| 59 | + if (link.matches('[data-no-leave-alert]')) return; |
| 60 | + if (link.target === '_blank') return; |
| 61 | + if (link.hasAttribute('aria-controls')) return; |
| 62 | + |
| 63 | + const href = link.getAttribute('href'); |
| 64 | + const normalizedHref = href ? href.trim().toLowerCase() : ''; |
| 65 | + if ( |
| 66 | + !href || |
| 67 | + normalizedHref.startsWith('#') || |
| 68 | + normalizedHref.startsWith('javascript:') || |
| 69 | + normalizedHref.startsWith('data:') || |
| 70 | + normalizedHref.startsWith('vbscript:') |
| 71 | + ) |
| 72 | + return; |
| 73 | + |
| 74 | + event.preventDefault(); |
| 75 | + event.stopPropagation(); |
| 76 | + this.pendingNavigation = href; |
| 77 | + this.isOpen = true; |
| 78 | + }, |
| 79 | + true |
| 80 | + ); |
| 81 | + }, |
| 82 | + |
| 83 | + /** |
| 84 | + * Confirme la sortie |
| 85 | + */ |
| 86 | + confirmLeave() { |
| 87 | + const href = this.pendingNavigation; |
| 88 | + this.isDirty = false; |
| 89 | + this.isOpen = false; |
| 90 | + this.pendingNavigation = null; |
| 91 | + |
| 92 | + if (href) { |
| 93 | + window.location.href = href; |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * Annule la sortie |
| 99 | + */ |
| 100 | + cancelLeave() { |
| 101 | + this.isOpen = false; |
| 102 | + this.pendingNavigation = null; |
| 103 | + }, |
| 104 | +}); |
| 105 | + |
| 106 | +export default Alpine.store('onLeaveAlert'); |
0 commit comments