Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

« MediaWiki:Common.js » : différence entre les versions

Page de l’interface de MediaWiki
Hiob (discussion | contributions)
minicard
Hiob (discussion | contributions)
Aucun résumé des modifications
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 255 : Ligne 255 :
/* === fin Modèle:Règle === */
/* === fin Modèle:Règle === */


/* MiniCard Grid - Ajustement dynamique des colonnes */
/* =============================================================================
( function () {
  EFFET D'APPARITION EN CASCADE POUR LES MINICARDS (Intersection Observer)
     'use strict';
  ============================================================================= */
mw.hook('wikipage.content').add(function ($content) {
     // On cherche toutes les minicards de la page qui n'ont pas encore été animées
    const cards = $content[0].querySelectorAll('.minicard:not(.is-loaded)');
    if (!cards.length) return;


     var MIN_CARD_WIDTH = 140;
     // Vérification de la compatibilité du navigateur
    var GAP = 12;
    if ('IntersectionObserver' in window) {
        let delay = 0;
        let resetDelayTimeout;


    function calcBestCols( total, containerWidth ) {
        const observer = new IntersectionObserver((entries, obs) => {
        var maxColsByWidth = Math.floor(
            entries.forEach(entry => {
            ( containerWidth + GAP ) / ( MIN_CARD_WIDTH + GAP )
                if (entry.isIntersecting) {
        );
                    // Si la carte entre dans l'écran, on applique un délai pour l'effet "cascade"
        maxColsByWidth = Math.max( 1, Math.min( maxColsByWidth, total ) );
                    setTimeout(() => {
                        entry.target.classList.add('is-loaded');
                    }, delay);
                   
                    delay += 75; // 75ms de décalage entre chaque carte
                   
                    // On réinitialise le délai si on a fini de traiter un groupe de cartes
                    clearTimeout(resetDelayTimeout);
                    resetDelayTimeout = setTimeout(() => { delay = 0; }, 100);
                   
                    // On arrête d'observer cette carte pour ne l'animer qu'une seule fois
                    obs.unobserve(entry.target);
                }
            });
        }, {
            rootMargin: '0px 0px -30px 0px', // Déclenche l'animation 30px AVANT que la carte n'arrive en bas de l'écran
            threshold: 0.1
        });


         /* Cherche le diviseur exact le plus grand <= maxColsByWidth */
         // On observe chaque carte trouvée
         var bestCols = 1;
        cards.forEach(card => observer.observe(card));
         for ( var n = 1; n <= maxColsByWidth; n++ ) {
    } else {
             if ( total % n === 0 ) {
        // Fallback pour les très vieux navigateurs : on affiche tout direct
                 bestCols = n;
        cards.forEach(card => card.classList.add('is-loaded'));
    }
});
 
/* ============================================================
* Nefald Page Search — Recherche dans le contenu de la page
* ============================================================ */
 
/* --- Interception globale de Ctrl+F / Cmd+F --- */
$( document ).on( 'keydown.nefaldPageSearch', function ( e ) {
    if ( ( e.ctrlKey || e.metaKey ) && e.key.toLowerCase() === 'f' ) {
        var $searchInput = $( '.nefald-page-search__input' );
       
        if ( $searchInput.length > 0 ) {
            e.preventDefault();
           
            /* Scrolle vers la barre si elle n'est pas sticky ou visible */
            $searchInput[0].scrollIntoView( { behavior: 'smooth', block: 'center' } );
           
            /* Donne le focus et sélectionne le texte pour une frappe rapide */
            $searchInput.focus().select();
        }
    }
} );
 
mw.hook( 'wikipage.content' ).add( function ( $content ) {
    $content.find( '.nefald-page-search-container' ).each( function () {
        var $container = $( this );
        if ( $container.children().length > 0 ) { return; }
 
        /* Mise à jour du placeholder par défaut pour indiquer le raccourci */
        var placeholder = $container.data( 'placeholder' ) || 'Rechercher dans la page (Ctrl+F)…';
 
        /* --- Injection du HTML --- */
        var html =
            '<div class="nefald-page-search">' +
                '<span class="nefald-page-search__icon">&#x2315;</span>' +
                '<input class="nefald-page-search__input" type="search"' +
                    ' placeholder="' + placeholder + '"' +
                    ' aria-label="Rechercher dans la page"' +
                    ' autocomplete="off" spellcheck="false" />' +
                '<span class="nefald-page-search__counter"></span>' +
                '<button class="nefald-page-search__prev" title="Résultat précédent">&#x2191;</button>' +
                '<button class="nefald-page-search__next" title="Résultat suivant">&#x2193;</button>' +
                '<button class="nefald-page-search__clear" title="Effacer">&#x2715;</button>' +
            '</div>';
        $container.append( html );
 
        /* --- Références DOM --- */
         var $input  = $container.find( '.nefald-page-search__input' );
        var $counter = $container.find( '.nefald-page-search__counter' );
        var $prev    = $container.find( '.nefald-page-search__prev' );
        var $next    = $container.find( '.nefald-page-search__next' );
        var $clear  = $container.find( '.nefald-page-search__clear' );
 
        /* Zone dans laquelle on cherche : le contenu principal */
         var $scope = $( '#mw-content-text .mw-parser-output' );
 
        var matches = [];
        var currentIndex = -1;
        var timer = null;
        var HIGHLIGHT_CLASS = 'nefald-search-highlight';
        var ACTIVE_CLASS    = 'nefald-search-highlight--active';
 
        /* --- Fonctions utilitaires --- */
 
        /**
        * Parcourt récursivement les nœuds texte d'un élément
        * en ignorant les scripts, styles et le widget lui-même.
        */
        function getTextNodes( root ) {
             var nodes = [];
            var walker = document.createTreeWalker(
                root,
                NodeFilter.SHOW_TEXT,
                {
                    acceptNode: function ( node ) {
                        var parent = node.parentNode;
                        if ( !parent ) { return NodeFilter.FILTER_REJECT; }
                        var tag = parent.nodeName.toLowerCase();
                        if ( tag === 'script' || tag === 'style' || tag === 'textarea' || tag === 'noscript' ) {
                            return NodeFilter.FILTER_REJECT;
                        }
                        /* Ignorer le widget de recherche lui-même */
                        if ( $( parent ).closest( '.nefald-page-search-container' ).length ) {
                            return NodeFilter.FILTER_REJECT;
                        }
                        /* Ignorer les nœuds vides */
                        if ( node.nodeValue.trim() === '' ) {
                            return NodeFilter.FILTER_REJECT;
                        }
                        return NodeFilter.FILTER_ACCEPT;
                    }
                }
            );
            while ( walker.nextNode() ) {
                 nodes.push( walker.currentNode );
             }
             }
            return nodes;
        }
        /**
        * Supprime tous les surlignages existants en restaurant le texte d'origine.
        */
        function clearHighlights() {
            $scope.find( '.' + HIGHLIGHT_CLASS ).each( function () {
                var parent = this.parentNode;
                parent.replaceChild( document.createTextNode( this.textContent ), this );
                parent.normalize();
            } );
            matches = [];
            currentIndex = -1;
            $counter.text( '' );
         }
         }


         /* Pas de diviseur exact : on prend maxColsByWidth */
         /**
         if ( bestCols === 1 && maxColsByWidth > 1 ) {
        * Cherche `query` dans tous les nœuds texte et entoure
             bestCols = maxColsByWidth;
        * chaque occurrence d'un <mark>.
        */
         function doSearch( query ) {
            clearHighlights();
            if ( !query || query.length < 2 ) { return; }
 
            /* Échapper les caractères spéciaux pour la RegExp */
            var escaped = query.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' );
            var regex = new RegExp( '(' + escaped + ')', 'gi' );
 
            var textNodes = getTextNodes( $scope[ 0 ] );
 
            textNodes.forEach( function ( node ) {
                var text = node.nodeValue;
                if ( !regex.test( text ) ) { return; }
                regex.lastIndex = 0; /* reset après test */
 
                var frag = document.createDocumentFragment();
                var lastIndex = 0;
                var match;
 
                while ( ( match = regex.exec( text ) ) !== null ) {
                    /* Texte avant le match */
                    if ( match.index > lastIndex ) {
                        frag.appendChild( document.createTextNode( text.slice( lastIndex, match.index ) ) );
                    }
                    /* Le match lui-même dans un <mark> */
                    var mark = document.createElement( 'mark' );
                    mark.className = HIGHLIGHT_CLASS;
                    mark.textContent = match[ 1 ];
                    frag.appendChild( mark );
                    lastIndex = regex.lastIndex;
                }
 
                /* Texte restant après le dernier match */
                if ( lastIndex < text.length ) {
                    frag.appendChild( document.createTextNode( text.slice( lastIndex ) ) );
                }
 
                node.parentNode.replaceChild( frag, node );
            } );
 
            matches = $scope.find( '.' + HIGHLIGHT_CLASS ).toArray();
 
             if ( matches.length > 0 ) {
                currentIndex = 0;
                goTo( 0 );
            } else {
                $counter.text( '0 résultat' );
            }
         }
         }


         return bestCols;
         /**
    }
        * Navigue vers le match d'index donné.
        */
        function goTo( index ) {
            if ( matches.length === 0 ) { return; }
 
            /* Retirer la classe active du précédent */
            $( matches ).removeClass( ACTIVE_CLASS );
 
            /* Boucler */
            if ( index < 0 ) { index = matches.length - 1; }
            if ( index >= matches.length ) { index = 0; }
            currentIndex = index;


    function adjustGrid( grid ) {
            var el = matches[ currentIndex ];
        var cards = grid.querySelectorAll( '.minicard' );
            $( el ).addClass( ACTIVE_CLASS );
        var total = cards.length;
        if ( total === 0 ) return;


        var containerWidth = grid.offsetWidth;
            /* Scroller vers l'élément actif */
            el.scrollIntoView( { behavior: 'smooth', block: 'center' } );


        /* Si le conteneur n'est pas encore rendu (width = 0), on réessaie */
             $counter.text( ( currentIndex + 1 ) + ' / ' + matches.length );
        if ( containerWidth === 0 ) {
             requestAnimationFrame( function () { adjustGrid( grid ); } );
            return;
         }
         }


         var bestCols = calcBestCols( total, containerWidth );
         /* --- Événements --- */
        grid.style.gridTemplateColumns = 'repeat(' + bestCols + ', 1fr)';


         /* Hauteur adaptative */
         $input.on( 'input', function () {
        var cardWidth = ( containerWidth - ( bestCols - 1 ) * GAP ) / bestCols;
            clearTimeout( timer );
        var rowHeight = cardWidth < 120 ? '48px' : cardWidth < 160 ? '52px' : '60px';
            var query = $input.val();
        grid.style.gridAutoRows = rowHeight;
            /* Debounce de 250 ms pour ne pas spammer sur chaque frappe */
        cards.forEach( function ( c ) {
            timer = setTimeout( function () {
             c.style.height = rowHeight;
                doSearch( query );
             }, 250 );
         } );
         } );


         /* Révèle la grille une fois calculée */
         $next.on( 'click', function () {
        grid.classList.add( 'is-ready' );
            goTo( currentIndex + 1 );
    }
        } );


    function adjustAllGrids() {
        $prev.on( 'click', function () {
        document.querySelectorAll( '.minicard-grid' ).forEach( adjustGrid );
            goTo( currentIndex - 1 );
    }
        } );


    /* Exécution immédiate si le DOM est prêt, sinon au plus tôt */
        $clear.on( 'click', function () {
    if ( document.readyState === 'loading' ) {
            $input.val( '' ).trigger( 'focus' );
        document.addEventListener( 'DOMContentLoaded', adjustAllGrids );
            clearHighlights();
    } else {
        } );
        adjustAllGrids();
    }


    /* Recalcul au resize avec debounce */
        /* Raccourcis clavier dans l'input */
    var resizeTimer;
        $input.on( 'keydown', function ( e ) {
    window.addEventListener( 'resize', function () {
            if ( e.key === 'Enter' ) {
        clearTimeout( resizeTimer );
                e.preventDefault();
        resizeTimer = setTimeout( adjustAllGrids, 100 );
                if ( e.shiftKey ) {
                    goTo( currentIndex - 1 );
                } else {
                    goTo( currentIndex + 1 );
                }
            }
            if ( e.key === 'Escape' ) {
                $input.val( '' );
                clearHighlights();
                $input.trigger( 'blur' );
            }
        } );
     } );
     } );
 
} );
}() );
Les témoins (''cookies'') nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de témoins.