Utilisateur:Zebulon84/TableauCoupeHighlight.js
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./***
* Coloration tableaux sportifs
*
* Auteur : [[:fr:User:Zebulon84|Zebulon84]]
*
*
*/
mw.loader.using( [ 'mediawiki.storage' ], function (require) {
'use strict';
const colorHover = 'Yellow';
const colorList = [ 'Pink', 'Cyan', 'PaleGreen', 'Bisque', 'LightSkyBlue', 'Orange', 'violet', 'Gold' ];
const storage = require( 'mediawiki.storage' ).session;
let colorIndex = 0;
let tableauCoupeNameIndexMax = 1;
function ChangeColor( event ) {
const cells = document.getElementsByClassName( event.data.class );
if ( event.type == 'click' ) {
let color = colorHover;
if ( !this.colorFixed ) {
color = colorList[ colorIndex ];
colorIndex = ( colorIndex + 1 ) %colorList.length;
}
for ( let cell of cells ) {
cell.colorFixed = !cell.colorFixed;
cell.style.backgroundColor = color;
}
} else if ( !this.colorFixed ) {
for ( let cell of cells ) {
cell.style.backgroundColor = ( event.type == 'mouseleave' ) ?
cell.oldColor : colorHover;
}
}
}
function setEvents( $content ) {
const listIndex = { };
function getIndex( cell ) {
let text = cell.innerText.replace( /\s+/g, '' ).replace( /\(.+/, '' );
let link = '';
for ( let a of cell.querySelectorAll( 'a:not(.image):not(.mw-file-description)' ) ) {
link += storage.get( a.title ) || a.title || a.text;
}
link = link || text;
if ( !link ) {
return;
}
let index = listIndex[ link ] || listIndex[ text ] || tableauCoupeNameIndexMax++;
listIndex[ link ] = index;
listIndex[ text ] = index;
return index;
}
function setEvent( cell, classe ) {
cell.classList.add( classe );
cell.oldColor = cell.style.backgroundColor;
$( cell ).on( 'mouseenter mouseleave click', { class :classe }, ChangeColor );
}
( $content || $('#content') )
.find( '.tableau_coupe_name' )
.each( function( i, cell ) {
let index = getIndex( cell );
if ( index ) {
let classe = 'TableauCoupeName' + index;
setEvent( cell, classe );
if ( cell.closest( '.tableau_coupe_tennis' ) ) {
setEvent( cell.nextElementSibling, classe );
}
}
} );
}
function init( $content ) {
const redirects = [];
( $content || $('#content') )
.find( '.tableau_coupe_name .mw-redirect:not(.image):not(.mw-file-description)' )
.each( function () {
if ( !storage.get( this.title ) ) {
redirects.push( this.title );
}
});
if ( redirects.length > 0 ) {
new mw.Api()
.get( { 'action' : 'query', 'redirects' : '1', 'titles' : redirects } )
.done( function ( data ) {
for ( let r of data.query.redirects ) {
storage.set(r.from, r.to);
}
setEvents( $content );
} )
.fail( setEvents );
} else {
setEvents( $content );
}
}
mw.hook( 'wikipage.content' ).add( init );
} );
// fonction pour annuler les classes et attributs ajoutées par ce code, pour débugger.
/*
function resetTCH() {
$content = $('#content');
for (i=0; i < 200; i++ ) {
$content.find( '.TableauCoupeName' + i )
.removeClass('TableauCoupeName' + i)
.off( 'mouseenter mouseleave click');
}
$content.find( '.tableau_coupe_name a' )
.removeAttr('data-redirect-to');
}
*/