Utilisateur:Deep silence/ReferencesPopup.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.
/**
 * ReferencesPopup
 *
 * Shows the articles's references in a popup. 
 * Affiche les références des articles dans des popup.
 *
 * Add the followinf line in the monobook to activate the popups : 
 * Ajouter la ligne suivante au monobook pour activer les popups : 
 * importScript( "Utilisateur:Deep silence/ReferencesPopup.js" );
 * 
 * The following parameters can be cutomized : 
 * Les paramètres qui suivent peuvent être modifiés par l'utilistateur : 
 * referencesPopup_max_width   // Popup width (Percent of window : 1.0 == 100%)
 * referencesPopup_open_delay  // Open delay (Millisecs, set to zero to open immediately)
 * referencesPopup_hide_delay  // Close delay (Millisecs, set to zero to close immediately)
 * referencesPopup_style       // CSS style of the popup.
 *
 *
 * This script uses the 'Tooltips.js' by [[:commons:User:Lupo]].
 *
 * Auteurs : [[:fr:User:Deep silence]] (references), [[:commons:User:Lupo]] (popups)
 * License: GFDL
 * Date de dernière révision : 20/10/2008
 *
 */

// default values 
var referencesPopup_max_width_2 = 0.25
var referencesPopup_open_delay_2 = 800
var referencesPopup_hide_delay_2 = 500
var referencesPopup_style_2 = { border : "1px solid #C7DFFF"
			,background : "#F9F9F9"
			,padding : "0.5em"
			,opacity : "1"
                        ,cursor : "text"
			,filter : "alpha(Opacity=100)" // for IE
			,font: "x-small sans-serif"
		       }
// uses the users's values if exist
if (typeof(referencesPopup_max_width) != 'undefined'){
	referencesPopup_max_width_2 = referencesPopup_max_width;
}
if (typeof(referencesPopup_open_delay) != 'undefined'){
	referencesPopup_open_delay_2 = referencesPopup_open_delay;
}
if (typeof(referencesPopup_hide_delay) != 'undefined'){
	referencesPopup_hide_delay_2 = referencesPopup_hide_delay;
}
if (typeof(referencesPopup_style) != 'undefined'){
	referencesPopup_style_2 = referencesPopup_style;
}

mw.loader.load( 'https://commons.wikimedia.org/w/index.php?title=MediaWiki:Tooltips.js&action=raw&ctype=text/javascript' );

function referencesPopup($){
	// waits for the ToolTips load
	// (needed by IE...)
        var waitTooltips = function(){
                if (typeof Tooltip != "undefined"){
                        clearInterval( refPopupWaitTooltipsThread  );
			refPopupMain();		// LOAD ALL
                }
        }
        refPopupWaitTooltipsThread = window.setInterval(waitTooltips, '50');
	
}

/*
*	Main function
*/
function refPopupMain(){
	// some vars often used
	refPopupWpSectionNode = document.getElementsByName( "wpSection" );
	refPopupWpTextAreaNode = document.getElementById( "wpTextbox1" );

	
        // in case of edition of an article's section, hides the <\reference> used to show the refs when previewing
        if (refPopupWpSectionNode != null && refPopupWpSectionNode.length > 0 && refPopupWpSectionNode[0].value > 0 ){
                refPopupRemoveReferenceTag();
        }
	

        // lists all the ref calls and add to them a popup
	var refCallNode;
	var refLink;
	var refTargetNode;
	refPopupCreateRefCallList();	// get the call list
	for (var i=0; i<refPopup.divCallNodes.length; i++){
		refCallNode = refPopup.linkCallNodes[i];
		// gets the references marks
		refLink = refCallNode.getAttribute('href',2);
		refLink = refLink.substring(1, refLink.length);
		
		// get the target ref
		refTargetNode = document.getElementById(refLink);
		
		// get the content of the ref : !! IE needs the div node, not the link node!! 
		refPopupCreatePopup( refPopup.divCallNodes[i], refTargetNode, refLink );
	}


        // in case of edition of an article's section, adds the <\reference> in order to show the refs
        if (refPopupWpSectionNode != null && refPopupWpSectionNode.length > 0 && refPopupWpSectionNode[0].value > 0 ){		
		// adds the refs on preview
		var node = document.getElementById( "wpPreview" );
		EvtHandler.attach( node, "click", refPopupAddReferenceTag );
		
		// removes the refs on save
		node = document.getElementById( "wpSave" );
		EvtHandler.attach( node, "click", refPopupRemoveReferenceTag );
        }


        // removes the lupin's popups
        var killLupin = function(){
                if (typeof popupsReady == "undefined"){		// Lupin's popups are not used
                        clearInterval( refPopupkillLupinThread  );
                } else if ( popupsReady() ) {				// Lupin's popups are used and fully loaded
                        for (var i=0; i<refPopup.linkCallNodes.length; i++){
                                if (refPopup.linkCallNodes[i].onmouseover == mouseOverWikiLink) {
                                        refPopup.linkCallNodes[i].onmouseover = null;
                                }
                        }
                        clearInterval( refPopupkillLupinThread  );
                }
        }
        refPopupkillLupinThread = window.setInterval(killLupin, '500');
}
	
/*
* 	Theses functions are used to add and remove the '<references/>' tag needed to see the references
*/
var refPopupSectionTest = "<div style=\"visibility:hidden; height:0pt;\"><!-- added by \"ReferencesPopup\"--><references/></div>";
function refPopupAddReferenceTag( evt ){
	// add the ref tag only if there are some refs calls, and if the tag doesn't exist yet
	if ( (! refPopupWpTextAreaNode.value.match(/< *references.*\/ *>/i)) && refPopupWpTextAreaNode.value.match(/< *ref.*>/i)){	
		refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value + refPopupSectionTest;		
	}
}
function refPopupRemoveReferenceTag( evt ){
        // some browsers add a "\n" and we need to be sure the ref tag has been removed
	refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( "\r\n"+refPopupSectionTest + "\s*", "" );
	refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( "\n"+refPopupSectionTest + "\s*", "" );
	refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( refPopupSectionTest + "\s*", "" );
	refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( "\r\n"+refPopupSectionTest, "" );
	refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( "\n"+refPopupSectionTest, "" );
	refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( refPopupSectionTest, "" );
	//refPopupWpTextAreaNode.value = refPopupWpTextAreaNode.value.replace( /\s*$/g, "" );   // remove extra EOL
}


/*
*	Returns the list of references calls in a page
*/
function refPopupCreateRefCallList(){
	// some vars often used
	window.refPopup = {
		divCallNodes : [],
		linkCallNodes : []
	}

	// lists all the ref calls and add to the 'window.refPopup' list
	var refCall = document.getElementsByTagName("sup");
	var refCallLink;
	for (var i=0; i<refCall.length; i++){
		// gets the references marks
		if (refCall[i].getAttribute('class') == 'reference' || refCall[i].getAttribute('className') == 'reference'){
			refCallLink = refCall[i].firstChild;
			
			// gets the link to the ref
			if ( refCallLink != null && refCallLink.nodeName == 'A') {
			        refPopup.divCallNodes.push( refCall[i] ); // keeps it in memory
			        refPopup.linkCallNodes.push( refCallLink ); // keeps it in memory
			}
		}
	}
}


/*
*	Returns a pop up that contains the content of the parameter node.
*
*/
function refPopupCreatePopup( refCallNode, refNode, refLink ){
	var refId = "refPopup" + refLink;
	// refs can be used several times; it's useless to create a box for each different call.
	if ( document.getElementById( refId ) == null){
		// creates the popup
		var popup = document.createElement("div");
		popup.setAttribute("id", refId  );
		
		// fills it with the reference content (excludes the first node, which is the link to the ref. call)
		if (refNode == null){
			popup.appendChild( document.createTextNode( "Impossible de retrouver la source, elle a peut être été définie en dehors de cette section." ) );

		} else {
			var refText = refNode.childNodes;
			var refTextNode;
			var refLink;
			var refLinkTextNode;
			if (refText != null &&  refText.length > 1){
				for (var i=1; i<refText.length; i++){
					refTextNode = refText[i];
					// if links to a book, include the book description in the popup					
					if ( refTextNode != null && refTextNode.nodeName == 'A' && refTextNode.getAttribute('href',2).match(/^ *#.*/)) {
						refLink = refTextNode.getAttribute('href',2);
						refLink = refLink.substring(1, refLink.length);
						refLinkTextNode = document.getElementById(refLink);
						
						if (refLinkTextNode != null){
						        popup.appendChild( refLinkTextNode.cloneNode(true) ); 
							//popup.appendChild( document.createElement( "BR" ) );
						}
					// else (common case ), reproduce the node shown in the references section
					} else {
					        popup.appendChild( refTextNode.cloneNode(true) );
					}
				}
			}
		}
		
		// creates all the behaviour stuff, using MediaWiki:Tooltips.
		new Tooltip
		    (  refCallNode
		     , popup
		     , { fixed_offset : {x: -20, y: -20, dx : 1, dy: -1}
			,anchor : Tooltip.TOP_RIGHT
			,mode: Tooltip.FIXED
			,max_width : referencesPopup_max_width_2
			,open_delay : referencesPopup_open_delay_2
			,hide_delay : referencesPopup_hide_delay_2
		       }
		     , referencesPopup_style_2
		    );

	}
}

$(referencesPopup);