MediaWiki:Gadget-BandeauxCategories.js
Apparence
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) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.//{{Catégorisation JS|BandeauxCategories}}
//<source lang=javascript>
//<pre><nowiki>
if((wgNamespaceNumber==14)&&((wgAction=="view")||(wgAction=="purge"))){
BandeauxCategories_GetPageWikitext(true);
addOnloadHook(BandeauxCategories_GetTables);
}else if((wgNamespaceNumber==14)&&(wgAction=="edit")){
addOnloadHook(BandeauxCategories_Edit);
}
var BandeauxCategories_PageWikitext = false;
function BandeauxCategories_GetPageWikitext(Mode){
if(BandeauxCategories_PageWikitext != false) return;
var NouvelleRequete = sajax_init_object();
var URL = wgServer + wgScript + '?title=' + wgPageName.replace(/&/g, "26") + '&action=raw&oldid=' + wgCurRevisionId;
if(Mode){
NouvelleRequete.open("GET", URL, true);
NouvelleRequete.onreadystatechange = function() {
if(NouvelleRequete.readyState != 4 ) return;
BandeauxCategories_PageWikitext = BandeauxCategories_Clean(NouvelleRequete.responseText);
return BandeauxCategories_PageWikitext;
}
NouvelleRequete.send(null);
}else{
NouvelleRequete.open("GET", URL, false);
NouvelleRequete.send(null);
if (NouvelleRequete.readyState == 4){
BandeauxCategories_PageWikitext = BandeauxCategories_Clean(NouvelleRequete.responseText);
return BandeauxCategories_PageWikitext;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
function BandeauxCategories_GetTables(){
var bodyC = document.getElementById("bodyContent"); // Monobook, Chick, Simple, Myskin, Vector
if(!bodyC) bodyC = document.getElementById("article"); // Cologneblue, Nostalgia, Standard
if(!bodyC) bodyC = document.getElementById("mw_contentholder"); // Modern
if(!bodyC) return;
var TableArray = new Array();
var Tables = document.getElementsByTagName("table");
for(var a=0;a<Tables.length;a++){
if(hasClass(Tables[a], "bandeau-categorie")) TableArray.push(Tables[a]);
}
if(!TableArray[0]) TableArray[0] = bodyC;
if(TableArray) BandeauxCategories_ModifyTables(TableArray);
}
function BandeauxCategories_ModifyTables(TableArray){
var HasCat = false;
var indexTable = 0;
var indexTd = 0;
for(var a=0;a<TableArray.length;a++){
indexTd = 0;
var Cadres = TableArray[a].getElementsByTagName("div");
for(var b=0;b<Cadres.length;b++){
if(hasClass(Cadres[b], "cadre_portail")){
HasCat = true;
Cadres[b].id = "cadre_categorie_"+indexTable+"_"+indexTd;
var Cadre = Cadres[b].getElementsByTagName("tr")[0];
var CadreLiens = document.createElement('td');
CadreLiens.width = "100px";
CadreLiens.id = "TDLiens_"+indexTable+"_"+indexTd;
CadreLiens.valign = "top";
CadreLiens.innerHTML = '<span style="white-space:nowrap;">'
+ '<a href="javascript:BandeauxCategories_SupprOne('+indexTable+','+indexTd+');" '
+ 'title="Supprimer ce bandeau">(-)</a>'
+ '<a href="javascript:BandeauxCategories_ModifyOne('+indexTable+','+indexTd+');" '
+ 'title="Modifier ce bandeau">(±)</a>'
+ '<a href="javascript:BandeauxCategories_AddOne('+indexTable+','+indexTd+');" '
+ 'title="Ajouter un bandeau">(+)</a>'
+ '</span>';
Cadre.appendChild(CadreLiens);
indexTd++;
}
}
indexTable++;
}
if(!HasCat){
var bodyC = document.getElementById("bodyContent"); // Monobook, Chick, Simple, Myskin, Vector
if(!bodyC) bodyC = document.getElementById("article"); // Cologneblue, Nostalgia, Standard
if(!bodyC) bodyC = document.getElementById("mw_contentholder"); // Modern
if(!bodyC) return;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
function BandeauxCategories_Clean(text){
text = text.replace(/\{\{multi bandeau\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Multibandeau\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Multi-bandeau\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Multib\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Multi bandeaux\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Duo ébauche\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Trio ébauche\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Duo portail\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{Trio portail\|/ig, "\{\{Multi bandeau\|");
text = text.replace(/\{\{catégorie\|/ig, "\{\{Catégorie\|");
text = text.replace(/\|catégorie/ig, "\|Catégorie");
text = text.replace(/\{\{Catégorie /ig, "\{\{Multi bandeau\|Catégorie ");
return text;
}
function BandeauxCategories_GetTemplateName(indexTable, indexTd){
if(BandeauxCategories_PageWikitext == false) BandeauxCategories_PageWikitext = BandeauxCategories_GetPageWikitext(false);
var AllTables = new RegExp("\\{\\{Multi bandeau\\|"+ "[^\\}]*" + "\\}\\}", "g");
var MatchTables = BandeauxCategories_PageWikitext.match(AllTables);
if(MatchTables!=null){
for(var a=0;a<MatchTables.length;a++){
if(a==indexTable){
var Template = MatchTables[a];
Template = Template.replace(/\n/g, "");
}
}
}
if(Template){
//alert(Template);
Template = Template.replace(/.*Multi bandeau\|/g, "");
Template = Template.replace(/\}\}.*/g, "");
Template = Template.replace(/Catégorie /g, "");
Templates = Template.split("|");
for(var a=0;a<Templates.length;a++){
if(a==indexTd){
var TheTemplate = Templates[a];
}
}
}
if(TheTemplate) return TheTemplate;
return false;
}
function BandeauxCategories_SupprOne(indexTable, indexTd){
var Cat = BandeauxCategories_GetTemplateName(indexTable, indexTd);
if(!Cat) return;
var Text = "Voulez-vous vraiment supprimer le bandeau « Catégorie "+Cat+" » ?";
var Confirmation = confirm(Text);
if(!Confirmation) return;
var EditURL = wgServer+wgScript+'?title='+encodeURIComponent(wgPageName)+'&action=edit';
var Params = '&SupprCat='+encodeURIComponent(Cat);
document.location = EditURL+Params;
}
function BandeauxCategories_ModifyOne(indexTable, indexTd){
var Cat = BandeauxCategories_GetTemplateName(indexTable, indexTd);
if(!Cat) return;
BandeauxCategories_CreateForm(indexTable, indexTd, Cat, "Modif");
}
function BandeauxCategories_AddOne(indexTable, indexTd){
BandeauxCategories_CreateForm(indexTable, indexTd, "", "Add");
}
var BandeauxCategories_suggestion_delay = 200;
var BandeauxCategories_text_InputOK = "OK";
var BandeauxCategories_text_InputCancel = "Annuler"
function BandeauxCategories_CreateForm(indexTable, indexTd, Cat, Mode){
var TheTd = document.getElementById("TDLiens_"+indexTable+"_"+indexTd);
TheTd.getElementsByTagName("span")[0].style.display = "none";
var Form = document.createElement("form");
Form.id = "CatFormZZ"+indexTable+"_"+indexTd;
Form.className = Mode;
Form.method = "post" ;
Form.onsubmit = function () {
BandeauxCategories_SubmitForm(BandeauxCategories_GetIndex(this));
return false;
} ;
Form.style.display = "inline" ;
var text = document.createElement ( "input" ) ;
text.size = 40 ;
text.id = "CatInputZZ" + indexTable+"_"+indexTd ;
text.type = "text" ;
text.value = Cat ;
text.onkeyup = function () {
window.setTimeout("BandeauxCategories_text_changed('"+BandeauxCategories_GetIndex(this)+"');", BandeauxCategories_suggestion_delay );
} ;
var list = document.createElement ( "select" ) ;
list.id = "CatSelectZZ" + indexTable+"_"+indexTd ;
list.style.display = "none" ;
list.onclick = function () {
BandeauxCategories_text_replace(BandeauxCategories_GetIndex(this));
} ;
var OK = document.createElement ( "input" ) ;
OK.id = "CatOKZZ" + indexTable+"_"+indexTd ;
OK.type = "button" ;
OK.value = BandeauxCategories_text_InputOK ;
OK.onclick = function(){
BandeauxCategories_SubmitForm(BandeauxCategories_GetIndex(this)) ;
}
var cancel = document.createElement ( "input" ) ;
cancel.id = "CatCancelZZ" + indexTable+"_"+indexTd ;
cancel.type = "button" ;
cancel.value = BandeauxCategories_text_InputCancel ;
cancel.onclick = function(){
BandeauxCategories_CancelForm(BandeauxCategories_GetIndex(this)) ;
}
Form.appendChild ( text ) ;
Form.appendChild ( list ) ;
Form.appendChild ( OK ) ;
Form.appendChild ( cancel ) ;
TheTd.appendChild ( Form ) ;
text.focus () ;
}
function BandeauxCategories_CancelForm(index){
var TheTd = document.getElementById("TDLiens_"+index);
TheTd.getElementsByTagName("span")[0].style.display = "";
var Form = TheTd.getElementsByTagName("form")[0];
Form.parentNode.removeChild(Form);
}
function BandeauxCategories_SubmitForm(index){
var NewTemplate = document.getElementById("CatInputZZ"+index).value ;
if(NewTemplate == "") return;
var OldTemplate = "";
var Form = document.getElementById("CatFormZZ"+index) ;
var Mode = Form.className;
var EditURL = wgServer+wgScript+'?title='+encodeURIComponent(wgPageName)+'&action=edit';
var Params = '&AddCat='+encodeURIComponent(NewTemplate);
if(Mode=="Modif"){
var indexTable = parseInt(index.replace(/_.*/g, ""));
var indexTd = parseInt(index.replace(/.*_/g, ""))
OldTemplate = BandeauxCategories_GetTemplateName(indexTable, indexTd)
Params += "&SupprCat="+encodeURIComponent(OldTemplate);
}
if(NewTemplate==OldTemplate){
BandeauxCategories_CancelForm(index);
return;
}
document.location = EditURL+Params;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var BandeauxCategories_list_size = 5;
var BandeauxCategories_list_items = 10;
var BandeauxCategories_list_down = false;
var BandeauxCategories_suggest_running = false;
function BandeauxCategories_text_changed(index){
if ( BandeauxCategories_suggest_running ) return ;
var APILimit = ( ((wgUserGroups.indexOf("sysop")!=-1)||(wgUserGroups.indexOf("bot")!=-1)) ? 4999 : 499 );
if(BandeauxCategories_list_items>APILimit) BandeauxCategories_list_items = APILimit; // API max
var Input = document.getElementById("CatInputZZ"+index);
var Text = Input.value;
if(Text !=""){
var URL = "/api.php?format=xml&action=query&list=allpages&apnamespace=10&apprefix=Catégorie "+Text+"&aplimit="+hotcat_list_items;
BandeauxCategories_suggest_running = true;
if ( typeof ( BandeauxCategories_xmlhttp ) != "undefined" ) BandeauxCategories_xmlhttp.abort() ;
BandeauxCategories_xmlhttp = new sajax_init_object() ;
BandeauxCategories_xmlhttp.open('GET', wgServer + wgScriptPath + URL, true);
BandeauxCategories_xmlhttp.onreadystatechange = function () {
if ( typeof BandeauxCategories_xmlhttp == "undefined" ) return ;
if (BandeauxCategories_xmlhttp.readyState == 4) {
var xml = BandeauxCategories_xmlhttp.responseXML ;
if ( xml == null ) return ;
var pages = xml.getElementsByTagName( "p" ) ;
var titles = new Array () ;
for ( var i = 0 ; i < pages.length ; i++ ) {
var s = pages[i].getAttribute("title");
if(s.indexOf(wgFormattedNamespaces[10]+":")!=-1){
s = s.split(wgFormattedNamespaces[10]+":").join('');
s = s.replace(/Catégorie /g, "");
titles.push ( s ) ;
}
}
BandeauxCategories_show_suggestions ( titles, index ) ;
}
};
BandeauxCategories_xmlhttp.send(null);
} else {
var titles = new Array () ;
BandeauxCategories_show_suggestions ( titles, index) ;
}
BandeauxCategories_suggest_running = false ;
}
function BandeauxCategories_show_suggestions (titles, index){
var text = document.getElementById("CatInputZZ"+index) ;
var list = document.getElementById("CatSelectZZ"+index) ;
if(titles.length==0){
list.style.display = "none" ;
return ;
}
var listh = BandeauxCategories_list_size * 20 ;
if (titles.length < BandeauxCategories_list_size) {
listh = titles.length * 20 ;
}
var nl = parseInt ( text.offsetLeft ) - 1 ;
var nt = parseInt(text.offsetTop) - listh ;
if (typeof BandeauxCategories_list_down != "undefined" && BandeauxCategories_list_down) {
nt = text.offsetTop + text.offsetHeight;
}
list.size = BandeauxCategories_list_size ;
if (titles.length < BandeauxCategories_list_size) {
list.size = titles.length ;
}
list.style.align = "left" ;
list.style.zIndex = 5 ;
list.style.width = text.offsetWidth + "px" ;
list.style.height = listh + "px" ;
list.style.position = "relative" ;
while ( list.firstChild ) list.removeChild ( list.firstChild ) ;
for ( var i = 0 ; i < titles.length ; i++ ) {
var opt = document.createElement ( "option" ) ;
var ot = document.createTextNode ( titles[i] ) ;
opt.appendChild ( ot ) ;
opt.value = titles[i];
list.appendChild ( opt ) ;
}
/*
if(ListDown){
list.style.top = parseInt(text.offsetHeight) + "px";
list.style.marginBottom = "-" + ((TailleListe * 20) + parseInt(text.offsetHeight)) + "px" ;
}else{
*/
list.style.marginTop = "-" + listh + "px" ;
// }
list.style.marginLeft = "-" + text.offsetWidth + "px" ;
list.onkeyup = BandeauxCategories_KeypressedOnList;
list.style.display = "inline" ;
var first_title = titles.shift () ;
var last_value = text.value;
if( first_title == last_value ) return ;
var suggestion = first_title;
if (text.createTextRange) {
var ra = text.createTextRange();
ra.moveStart("character", last_value.length);
ra.moveEnd("character", suggestion.length);
ra.select();
} else if( text.setSelectionRange ) {
text.setSelectionRange( last_value.length, suggestion.length );
} else {
text.selectionStart = last_value.length ;
text.selectionEnd = suggestion.length ;
}
}
function BandeauxCategories_KeypressedOnList(){
if (!e) var e = window.event;
if (e.keyCode != 13) return;
BandeauxCategories_text_replace(BandeauxCategories_GetIndex(this));
}
function BandeauxCategories_text_replace(index){
var Text = document.getElementById("CatInputZZ"+index) ;
var List = document.getElementById("CatSelectZZ"+index) ;
var TextValue = Text.value;
var Options = List.getElementsByTagName('option');
for(var a=0;a<Options.length;a++){
if(Options[a].selected){
var ListValue = Options[a].value;
Text.value = ListValue;
BandeauxCategories_text_changed(index) ;
Text.focus();
return;
}
}
}
function BandeauxCategories_GetIndex(element){
var ID = element.id;
ID = ID.replace(/.*ZZ/ig, "");
return ID;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
function BandeauxCategories_Edit(){
var SupprCat = false;
var AddCat = false;
var URL = decodeURIComponent(document.URL);
var Params = URL.split("&");
for(var a=0;a<Params.length;a++){
if(Params[a].indexOf("SupprCat=")!=-1){
SupprCat = Params[a].split("SupprCat=").join("");
}
if(Params[a].indexOf("AddCat=")!=-1){
AddCat = Params[a].split("AddCat=").join("");
}
}
if((!SupprCat)&&(!AddCat)) return;
//alert("* SupprCat : "+ SupprCat + "\n* AddCat : "+ AddCat);
var EditForm = document.editform;
EditForm.style.display = "none";
var AutoCommit = true;
var SommaireFinal = "";
var OldText = document.getElementById("wpTextbox1").value;
OldText = BandeauxCategories_Clean(OldText);
if((SupprCat!=false)&&(AddCat!=false)){
//alert("Modification");
var OldCatRegExp = new RegExp("\\|[ ]*(C|c)atégorie "+SupprCat+"[ ]*(\\||\\})", "ig");
var OldCatMatches = OldText.match(OldCatRegExp);
var NewCatRegExp = new RegExp("\\|[ ]*(C|c)atégorie "+AddCat+"[ ]*(\\||\\})", "ig");
var NewCatMatches = OldText.match(NewCatRegExp);
if(OldCatMatches==null){
alert("Erreur : Le bandeau « Catégorie "+SupprCat+" » introuvable.");
AutoCommit = false;
}else if(NewCatMatches!=null){
alert("Erreur : Le bandeau « Catégorie "+AddCat+" » déjà présent.");
AutoCommit = false;
}else{
for(var a=0,l=OldCatMatches.length;a<l;a++){
var EndMatch = OldCatMatches[a].substring((OldCatMatches[a].length-1), OldCatMatches[a].length);
if(a==0) OldText = OldText.replace(OldCatMatches[a], "|Catégorie "+AddCat+EndMatch);
else OldText = OldText.replace(OldCatMatches[a], EndMatch);
}
SommaireFinal = "Modification de bandeau de catégorie : "+SupprCat+" -> "+AddCat+" ;";
}
}else if((SupprCat!=false)&&(AddCat==false)){
//alert("Retrait");
var OldCatRegExp = new RegExp("\\|[ ]*(C|c)atégorie "+SupprCat+"[ ]*(\\||\\})", "ig");
var OldCatMatches = OldText.match(OldCatRegExp);
if(OldCatMatches==null){
alert("Erreur : Le bandeau « Catégorie "+SupprCat+" » introuvable.");
AutoCommit = false;
}else{
for(var a=0,l=OldCatMatches.length;a<l;a++){
var EndMatch = OldCatMatches[a].substring((OldCatMatches[a].length-1), OldCatMatches[a].length);
OldText = OldText.replace(OldCatMatches[a], EndMatch);
}
if(OldText.indexOf("{{Multi bandeau}}")!=-1){
OldText = OldText.split("{{Multi bandeau}}\n").join("");
OldText = OldText.split("{{Multi bandeau}}").join("");
}
SommaireFinal = "Retrait de bandeau de catégorie : "+SupprCat+" ;";
}
}else if((AddCat!=false)&&(SupprCat==false)){
//alert("Ajout");
var NewCatRegExp = new RegExp("\\|[ ]*(C|c)atégorie "+AddCat+"[ ]*(\\||\\})", "ig");
var NewCatMatches = OldText.match(NewCatRegExp);
if(NewCatMatches!=null){
alert("Erreur : Le bandeau « Catégorie "+AddCat+" » déjà présent.\n"+NewCatRegExp+"\n\n"+NewCatMatches.join("\n"));
AutoCommit = false;
}else{
var AddedTemplate = false;
var AllTables = new RegExp("\\{\\{Multi bandeau\\|"+ "[^\\}]*" + "\\}\\}", "g");
var MatchTables = OldText.match(AllTables);
if(MatchTables!=null){
for(var a=0;a<MatchTables.length;a++){
var Template = MatchTables[a];
var Params = Template.replace(/.*Multi bandeau\|/g, "");
Params = Params.replace(/\}\}.*/g, "");
Params = Params.split("|");
var ParamLength = Params.length;
if((ParamLength<9)&&(!AddedTemplate)){
var OldParams = Params.join("|");
var NewParams = OldParams+"|Catégorie "+AddCat;
OldText = OldText.replace(OldParams, NewParams);
//alert(OldParams+"\n"+NewParams);
SommaireFinal = "Ajout de bandeau de catégorie : "+AddCat+" ;";
AddedTemplate = true;
}
}
if(!AddedTemplate){
OldText = OldText.replace(/\{\{Multi bandeau/, "\{\{Multi bandeau\|Catégorie "+AddCat+"\}\}\n\{\{Multi bandeau");
SommaireFinal = "Ajout de bandeau de catégorie : "+AddCat+" ;";
AddedTemplate = true;
}
if(!AddedTemplate){
alert("Erreur : Le bandeau « Catégorie "+AddCat+" » n'a pas pû être ajouté.");
AutoCommit = false;
}
}
}
}
document.getElementById("wpTextbox1").value = OldText;
document.getElementById("wpSummary").value = SommaireFinal;
document.getElementById("wpMinoredit").checked = "checked";
AutoCommit = false;
if(AutoCommit){
//alert("Publication");
EditForm.submit();
}else{
EditForm.style.display = "block";
}
}
//</nowiki></pre></source>