« MediaWiki:Gadget-AncreTitres.js » : différence entre les versions

Un livre de Wikilivres.
Contenu supprimé Contenu ajouté
Bayo (discussion | contributions)
retouche pour retirer le tag small
Bayo (discussion | contributions)
m coquille
Ligne 66 : Ligne 66 :
);
);
setAttribute("href", "javascript:;"); }
setAttribute("href", "javascript:;"); }
span.appendChild(document.createText(' '));
span.appendChild(document.createTextNode(' '));
span.appendChild(link);
span.appendChild(link);
}
}
Ligne 80 : Ligne 80 :
setAttribute("href", "javascript:;");
setAttribute("href", "javascript:;");
}
}
span.appendChild(document.createText(' '));
span.appendChild(document.createTextNode(' '));
span.appendChild(link);
span.appendChild(link);
}
}

Version du 8 mai 2008 à 19:21

/**
 * AncreTitres
 *
 * Cette fonction fournit un lien vers une section de page en cliquant
 * sur le lien [ancre] à droite du titre de section.
 *
 * Auteurs : Pabix, Phe
 * Date de dernière révision : 15 août 2007
 * [[Catégorie:MediaWiki:Fonction Monobook en JavaScript]]
 */
 
var AncreTitres={
nom_ancre : "[URL]",
nom_lien_interne : "[[lien]]",
description : "Obtenir une URL vers cette section",
descinterne : "Obtenir un [[Lien#interne]]",
linkcolor : "",
fontSize : "xx-small",
fontWeight : "normal",
afficheE : true,
afficheI : true,
noeud_precedent : function(noeud) {
        while (noeud.previousSibling.nodeType!=1){
    	    noeud = noeud.previousSibling;
	    if (noeud==null) return undefined;
        }
        return noeud.previousSibling;
}
}
function ancrer(){
	if (!document.getElementById('content')) return;
	var titres = document.getElementsByTagName("span");
	var i = 0;
	var titre_courant;
        if (!AncreTitres.afficheI && !AncreTitres.afficheE) return;
	for (i = 0 ; i < titres.length ; i++){
		var titre_courant = titres[i].parentNode;	
		if (!(titres[i].className.match(/mw-headline/gi))) continue;
		if (!AncreTitres.noeud_precedent(titre_courant)) continue;
		var ancre_nom;
		if (AncreTitres.noeud_precedent(titre_courant).lastChild != null){
			ancre_nom = AncreTitres.noeud_precedent(titre_courant).lastChild.name;
		} else {
			ancre_nom = AncreTitres.noeud_precedent(titre_courant).name;
		}
		var small;
		var link;

		span = document.createElement('span');
		with(span ) {
			if (AncreTitres.linkcolor != "") style.color=AncreTitres.linkcolor;
			style.fontSize=AncreTitres.fontSize;
			style.fontWeight=AncreTitres.fontWeight;
		}

		if (AncreTitres.afficheE) {
			link = document.createElement('a');
			with(link) {
				innerHTML=AncreTitres.nom_ancre;
				title=AncreTitres.description;
				setAttribute("ancre", wgServer + "/w/index.php?title="
					+ wgPageName + "#" + ancre_nom
				);
				setAttribute("onclick", "window.prompt("
					+ "'Lien : ', this.getAttribute('ancre'));"
				);
                        	setAttribute("href", "javascript:;");										}
			span.appendChild(document.createTextNode(' '));
			span.appendChild(link);
		}
		if (AncreTitres.afficheI) {
			link = document.createElement('a');
			with(link) {
				innerHTML=AncreTitres.nom_lien_interne;
				title=AncreTitres.descinterne;
				setAttribute("ancre", "[[" + wgPageName + "#" + ancre_nom + "]]");
				setAttribute("onclick", "window.prompt("
					+ "'Lien : ', this.getAttribute('ancre'));"
				);
                        	setAttribute("href", "javascript:;");
			}
			span.appendChild(document.createTextNode(' '));
			span.appendChild(link);
		}
		titre_courant.appendChild(span);
	}
}
addOnloadHook(ancrer);