Projet:Herpétologie/gadgets/herpbar.js

Une page de Wikipédia, l'encyclopédie libre.

//

//<source lang=javascript>//

/*
 
Barre d'outils pour le projet herpéto
 
la structure du code et la plupart du code de ce gadget vient du gadget [[MediaWiki:Gadget-OuvrageArticle.js]] (brillamment) developpe par [[Utilisateur:Dr Brains]]
 
*/
var listofref = ["ReptileDB","ReptileDB espèce","UICN","IUCN","Amphibiaweb","Amphibiaweb espèce","aweb","ASW","ASW espèce","TFTSG"];
var listofbiorefespece = ["ReptileDB espèce","ReptileDB espèce","UICN","UICN","Amphibiaweb espèce","Amphibiaweb espèce","Amphibiaweb espèce","ASW espèce","ASW espèce","TFTSG"];
var listofbiorefgenre = ["ReptileDB genre","ReptileDB genre","UICN Liste","UICN Liste","Amphibiaweb genre","Amphibiaweb genre","Amphibiaweb genre","ASW genre","ASW genre","TFTSG"];
var listofbiorefsousfamille = ["ReptileDB sous-famille","ReptileDB sous-famille","UICN Liste","UICN Liste","Amphibiaweb sous-famille","Amphibiaweb sous-famille","Amphibiaweb sous-famille","ASW sous-famille","ASW sous-famille","TFTSG"];
var listofbioreffamille = ["ReptileDB famille","ReptileDB famille","UICN Liste","UICN Liste","Amphibiaweb famille","Amphibiaweb famille","Amphibiaweb famille","ASW famille","ASW famille","TFTSG"];

// ------------------------------------------------------ Lancement
if( (mw.config.get('wgNamespaceNumber')>-1)&&( (mw.config.get('wgAction')=="edit")||(mw.config.get('wgAction')=="submit")) ) addOnloadHook(Reference_AddButton);
 
// ------------------------------------------------------ Ajout du bouton
function Reference_AddButton(){
	if(!document.editform) return;
	var Toolbar = document.getElementById('toolbar');
	if(!Toolbar){
			Toolbar = document.createElement('div');
			Toolbar.id = 'toolbar';
			document.editform.insertBefore(Toolbar, document.editform.firstChild);
	}

	var buttonser = '<img src="http://upload.wikimedia.org/wikipedia/commons/1/13/Farm-Fresh_key_s.png" heigth="23" width="23" alt="Reference" />';
	var NewButtonser = document.createElement('a')
	NewButtonser.title = "Utiliser le modèle {{ser.}}";
	NewButtonser.href="javascript:insertserie();";
	NewButtonser.innerHTML = buttonser;
	Toolbar.appendChild(NewButtonser);
 
	var buttonvol = '<img src="http://upload.wikimedia.org/wikipedia/commons/5/5c/Farm-Fresh_key_v.png" heigth="23" width="23" alt="Reference" />';
	var NewButtonvol = document.createElement('a')
	NewButtonvol.title = "Utiliser le modèle {{vol.}}";
	NewButtonvol.href="javascript:insertvolume();";
	NewButtonvol.innerHTML = buttonvol;
	Toolbar.appendChild(NewButtonvol);
 
	var buttonnum = '<img src="http://upload.wikimedia.org/wikipedia/commons/9/95/Farm-Fresh_key_n.png" heigth="23" width="23" alt="Reference" />';
	var NewButtonnum = document.createElement('a')
	NewButtonnum.title = "Utiliser le modèle {{n°}}";
	NewButtonnum.href="javascript:insertnumero();";
	NewButtonnum.innerHTML = buttonnum;
	Toolbar.appendChild(NewButtonnum);
 
	var buttonpage = '<img src="http://upload.wikimedia.org/wikipedia/commons/c/c7/Farm-Fresh_key_p.png" heigth="23" width="23" alt="Reference" />';
	var NewButtonpage = document.createElement('a')
	NewButtonpage.title = "Utiliser le modèle {{p.}}";
	NewButtonpage.href="javascript:insertpage();";
	NewButtonpage.innerHTML = buttonpage;
	Toolbar.appendChild(NewButtonpage);

	var buttonftsg = '<img src="http://upload.wikimedia.org/wikipedia/commons/7/70/Kturtle_top_view.svg" heigth="23" width="23" alt="Reference" />';
	var NewButtonftsg = document.createElement('a')
	NewButtonftsg.title = "TFTSG";
	NewButtonftsg.href="javascript:inserttftsg();";
	NewButtonftsg.innerHTML = buttonftsg;
	Toolbar.appendChild(NewButtonftsg);

	var buttonasw = '<img src="http://upload.wikimedia.org/wikipedia/commons/2/23/Azureus.png" heigth="23" width="23" alt="Reference" />';
	var NewButtonasw = document.createElement('a')
	NewButtonasw.title = "ASW";
	NewButtonasw.href="javascript:insertasw();";
	NewButtonasw.innerHTML = buttonasw;
	Toolbar.appendChild(NewButtonasw);

	var buttonrdb = '<img src="http://upload.wikimedia.org/wikipedia/commons/d/d4/Chamwiki.png" heigth="23" width="23" alt="Reference" />';
	var NewButtonrdb = document.createElement('a')
	NewButtonrdb.title = "ReptileDB";
	NewButtonrdb.href="javascript:insertrdb();";
	NewButtonrdb.innerHTML = buttonrdb;
	Toolbar.appendChild(NewButtonrdb);

	var buttonuicn = '<img src="http://upload.wikimedia.org/wikipedia/commons/1/16/CR_IUCN_3_1.svg" heigth="23" width="23" alt="Reference" />';
	var NewButtonuicn = document.createElement('a')
	NewButtonuicn.title = "UICN";
	NewButtonuicn.href="javascript:insertuicn();";
	NewButtonuicn.innerHTML = buttonuicn;
	Toolbar.appendChild(NewButtonuicn);

	var buttonref = '<img src="http://upload.wikimedia.org/wikipedia/commons/f/fb/Template_icon.svg" heigth="23" width="23" alt="Reference" />';
	var NewButtonref = document.createElement('a')
	NewButtonref.title = "Notes et Références";
	NewButtonref.href="javascript:insertref();";
	NewButtonref.innerHTML = buttonref;
	Toolbar.appendChild(NewButtonref);

	var buttoncomma = '<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Comma.png/180px-Comma.png" heigth="23" width="23" alt="Reference" />';
	var NewButtoncomma = document.createElement('a')
	NewButtoncomma.title = "{{,}}";
	NewButtoncomma.href="javascript:insertcomma();";
	NewButtoncomma.innerHTML = buttoncomma;
	Toolbar.appendChild(NewButtoncomma);

	var buttonmigref = '<img src="http://upload.wikimedia.org/wikipedia/commons/8/8d/Farm-Fresh_pencil_go.png" heigth="23" width="23" alt="Migreference" />';
	var NewButtonmigref = document.createElement('a')
	NewButtonmigref.title = "Migration des références vers Bioref";
	NewButtonmigref.href="javascript:migref();";
	NewButtonmigref.innerHTML = buttonmigref;
	Toolbar.appendChild(NewButtonmigref);

}

function insertserie() {
	insertTags1('{{ser.|', '}}', '', 'ser. ');
}

function insertvolume() {
	insertTags1('{{vol.|', '}}', '', 'vol. ');
}

function insertnumero() {
	insertTags1('{{n°|', '}}', '', 'n. ');
}

function insertpage() {
	insertTags1('{{p.|', '}}', '', 'p. ');
}

function inserttftsg() {
	replaceTags('{{Bioref|', '}}', 'TFTSG|afficher=ref');
}

function insertasw() {
	inserttemplate(7);
}

function insertrdb() {
	inserttemplate(0);
}

function insertuicn() {
	inserttemplate(2);
}

function insertcomma() {
	replaceTags('{{', '}}', ',');
}

function insertref() {
	insertreferences();
}

function migref() {
	migreferences();
}

function inserttemplate(sequence) {
    var txtarea = document.editform.wpTextbox1;
	var EmptyParamRegExpEsp = new RegExp("{{"+listofbiorefespece[sequence]+"\\|", "ig")
	var Resultat = txtarea.value.replace(EmptyParamRegExpEsp, "a");
	if (Resultat!=txtarea.value){
		replaceTags('{{Bioref|', '}}', listofbiorefespece[sequence]+'|afficher=ref');
	}else{
		var EmptyParamRegExpGenre = new RegExp("{{"+listofbiorefgenre[sequence]+"\\|", "ig")
		Resultat = txtarea.value.replace(EmptyParamRegExpGenre, "a");
		if (Resultat!=txtarea.value){
			replaceTags('{{Bioref|', '}}', listofbiorefgenre[sequence]+'|afficher=ref');
		}else{
			var EmptyParamRegExpSousFam = new RegExp("{{"+listofbiorefsousfamille[sequence]+"\\|", "ig")
			Resultat = txtarea.value.replace(EmptyParamRegExpSousFam, "a");
			if (Resultat!=txtarea.value){
				replaceTags('{{Bioref|', '}}', listofbiorefsousfamille[sequence]+'|afficher=ref');
			}else{
				var EmptyParamRegExpFam = new RegExp("{{"+listofbioreffamille[sequence]+"\\|", "ig")
				Resultat = txtarea.value.replace(EmptyParamRegExpFam, "a");
				if (Resultat!=txtarea.value){
					replaceTags('{{Bioref|', '}}', listofbioreffamille[sequence]+'|afficher=ref');
				}
			}
		}
	}
}

function migreferences() {
    var txtarea = document.editform.wpTextbox1;
	
	//save textarea scroll position
	var textScroll = txtarea.scrollTop;

	for (var i = 0; i < listofref.length; i++) {
		var EmptyParamRegExp1 = new RegExp("\\<ref name=\"*"+listofref[i]+"\"* *\\/\\>", "ig");
		var EmptyParamRegExp2 = new RegExp("\\<ref name=\"*"+listofref[i]+"\"*(.*)\\<\\/ref\\>", "ig");
		var EmptyParamRegExpEsp = new RegExp("{{"+listofbiorefespece[i]+"\\|", "ig")
		var Resultat = txtarea.value.replace(EmptyParamRegExpEsp, "a");
		if (Resultat!=txtarea.value){
			txtarea.value = txtarea.value.replace(EmptyParamRegExp1, "{{Bioref|"+listofbiorefespece[i]+"|afficher=ref}}");
			txtarea.value = txtarea.value.replace(EmptyParamRegExp2, "{{Bioref|"+listofbiorefespece[i]+"|afficher=ref}}");
		}else{
			var EmptyParamRegExpGenre = new RegExp("{{"+listofbiorefgenre[i]+"\\|", "ig")
			Resultat = txtarea.value.replace(EmptyParamRegExpGenre, "a");
			if (Resultat!=txtarea.value){
				txtarea.value = txtarea.value.replace(EmptyParamRegExp1, "{{Bioref|"+listofbiorefgenre[i]+"|afficher=ref}}");
				txtarea.value = txtarea.value.replace(EmptyParamRegExp2, "{{Bioref|"+listofbiorefgenre[i]+"|afficher=ref}}");
			}else{
				var EmptyParamRegExpSousFam = new RegExp("{{"+listofbiorefsousfamille[i]+"\\|", "ig")
				Resultat = txtarea.value.replace(EmptyParamRegExpSousFam, "a");
				if (Resultat!=txtarea.value){
					txtarea.value = txtarea.value.replace(EmptyParamRegExp1, "{{Bioref|"+listofbiorefsousfamille[i]+"|afficher=ref}}");
					txtarea.value = txtarea.value.replace(EmptyParamRegExp2, "{{Bioref|"+listofbiorefsousfamille[i]+"|afficher=ref}}");
				}else{
					var EmptyParamRegExpFam = new RegExp("{{"+listofbioreffamille[i]+"\\|", "ig")
					Resultat = txtarea.value.replace(EmptyParamRegExpFam, "a");
					if (Resultat!=txtarea.value){
						txtarea.value = txtarea.value.replace(EmptyParamRegExp1, "{{Bioref|"+listofbioreffamille[i]+"|afficher=ref}}");
						txtarea.value = txtarea.value.replace(EmptyParamRegExp2, "{{Bioref|"+listofbioreffamille[i]+"|afficher=ref}}");
					}
				}
			}
		}
	}
	//get current selection
	txtarea.focus();
	
    //restore textarea scroll position
	txtarea.scrollTop = textScroll;
}

function insertreferences() {
	var txtarea = document.editform.wpTextbox1;
	var sampleText = "== Notes et références ==\n{{Références}}\n\n";

	//save textarea scroll position
	var textScroll = txtarea.scrollTop;
	//get current selection
	txtarea.focus();
	txtarea.value = txtarea.value.replace(new RegExp(sampleText, 'gi'), "");
	var startPos = txtarea.value.indexOf("{{Portail|");
	var startText = txtarea.value.substring(0, startPos);
	var stopText = txtarea.value.substring(startPos);
	//insert tags
	txtarea.value = startText + sampleText + stopText;
	//restore textarea scroll position
	txtarea.scrollTop = textScroll;
}

function replaceTags(tagOpen, tagClose, sampleText) {
    var txtarea = document.editform.wpTextbox1;
    var selText, isSample = false;

	//save textarea scroll position
	var textScroll = txtarea.scrollTop;
	//get current selection
	txtarea.focus();
	var startPos = txtarea.selectionStart;
	var endPos = txtarea.selectionEnd;
	selText = txtarea.value.substring(startPos, endPos);
	//insert tags
	txtarea.value = txtarea.value.substring(0, startPos) + tagOpen + sampleText + tagClose + txtarea.value.substring(endPos, txtarea.value.length);
	//set new selection
	txtarea.selectionStart = startPos + tagOpen.length + sampleText.length + tagClose.length;
	txtarea.selectionEnd = txtarea.selectionStart;
	//restore textarea scroll position
	txtarea.scrollTop = textScroll;

}

function insertTags1(tagOpen, tagClose, sampleText, replaceText) {
    var txtarea = document.editform.wpTextbox1;
    var selText, isSample = false;
    var reg = new RegExp(replaceText,"g");
    
	//save textarea scroll position
	var textScroll = txtarea.scrollTop;
	//get current selection
	txtarea.focus();
	var startPos = txtarea.selectionStart;
	var endPos = txtarea.selectionEnd;
	selText = txtarea.value.substring(startPos, endPos);
	//insert tags
	checkSelectedText();
	txtarea.value = txtarea.value.substring(0, startPos) + tagOpen + selText.replace(reg, '') + tagClose + txtarea.value.substring(endPos, txtarea.value.length);
	//set new selection
	if (isSample) {
		txtarea.selectionStart = startPos + tagOpen.length;
		txtarea.selectionEnd = startPos + tagOpen.length + selText.replace(reg, '').length;
	} else {
		txtarea.selectionStart = startPos + tagOpen.length + selText.replace(reg, '').length + tagClose.length;
		txtarea.selectionEnd = txtarea.selectionStart;
	}
	//restore textarea scroll position
	txtarea.scrollTop = textScroll;
 
    function checkSelectedText(){
        if (!selText) {
            selText = sampleText;
            isSample = true;
        } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
            selText = selText.substring(0, selText.length - 1);
            tagClose += ' '
        }
    }
}


// ------------------------------------------------------ 
//