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

Un livre de Wikilivres.
Contenu supprimé Contenu ajouté
Bayo (discussion | contributions)
autre solution pour mettre un href
Bayo (discussion | contributions)
jout d'un span englobant pour être cohérent avec editsection
Ligne 15 : Ligne 15 :
description : "Obtenir une URL vers cette section",
description : "Obtenir une URL vers cette section",
descinterne : "Obtenir un [[Lien#interne]]",
descinterne : "Obtenir un [[Lien#interne]]",
linkcolor : "blue",
linkcolor : "inherite",
fontSize : "50%",
fontSize : "xx-small",
fontWeight : "normal",
afficheE : true,
afficheE : true,
afficheI : true,
afficheI : true,
Ligne 32 : Ligne 33 :
var i = 0;
var i = 0;
var titre_courant;
var titre_courant;
if (!AncreTitres.afficheI && !AncreTitres.afficheE) return;
for (i = 0 ; i < titres.length ; i++){
for (i = 0 ; i < titres.length ; i++){
var titre_courant = titres[i].parentNode;
var titre_courant = titres[i].parentNode;
Ligne 44 : Ligne 46 :
var small;
var small;
var link;
var link;

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

if (AncreTitres.afficheE) {
if (AncreTitres.afficheE) {
small = document.createElement('small'); with(small) {
small = document.createElement('small'); with(small) {
innerHTML=AncreTitres.nom_ancre;
innerHTML=AncreTitres.nom_ancre;
title=AncreTitres.description;
title=AncreTitres.description;
style.color=AncreTitres.linkcolor;
style.fontSize=AncreTitres.fontSize;
}
}
link = document.createElement('a');
link = document.createElement('a');
Ligne 55 : Ligne 63 :
link.setAttribute("onclick", "window.prompt('Lien : ', this.getAttribute('ancre'));");
link.setAttribute("onclick", "window.prompt('Lien : ', this.getAttribute('ancre'));");
link.setAttribute("href", "javascript:;"); link.appendChild(small);
link.setAttribute("href", "javascript:;"); link.appendChild(small);
titre_courant.appendChild(link);
span.appendChild(link);
}
}
if (AncreTitres.afficheI) {
if (AncreTitres.afficheI) {
Ligne 61 : Ligne 69 :
innerHTML=AncreTitres.nom_lien_interne;
innerHTML=AncreTitres.nom_lien_interne;
title=AncreTitres.descinterne;
title=AncreTitres.descinterne;
style.color=AncreTitres.linkcolor;
style.fontSize=AncreTitres.fontSize;
}
}
link = document.createElement('a');
link = document.createElement('a');
Ligne 69 : Ligne 75 :
link.setAttribute("href", "javascript:;");
link.setAttribute("href", "javascript:;");
link.appendChild(small);
link.appendChild(small);
titre_courant.appendChild(link);
span.appendChild(link);
}
}
titre_courant.appendChild(span);
}
}
}
}

Version du 8 mai 2008 à 18:04

/**
 * 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 : "inherite",
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 ) {
			style.color=AncreTitres.linkcolor;
			style.fontSize=AncreTitres.fontSize;
			style.fontWeight=AncreTitres.fontWeight;
		}

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