Utilisateur:Dr Brains/MunicipalitesColombiennes.js/Request.js

Une page de Wikipédia, l'encyclopédie libre.
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.
var MunicipalitiesPage = "Utilisateur:Dr Brains/MunicipalitesColombiennes.js";

if(typeof(municipalites)=="undefined") 
importScript(MunicipalitiesPage);

if(mw.config.get('wgAction')=="edit" || mw.config.get('wgAction')=="submit") 
addOnloadHook(MunCol_AddLink);

var Departements = new Array();

Departements[0]  = new Array("91", "CO-AMA", "Amazonas (Colombie)");
Departements[1]  = new Array("05", "CO-ANT", "Antioquia");
Departements[2]  = new Array("81", "CO-ARA", "Arauca");
Departements[3]  = new Array("08", "CO-ATL", "Atlántico");
Departements[4]  = new Array("11", "CO-DC", "Bogota DC");
Departements[5]  = new Array("13", "CO-BOL", "Bolívar (Colombie)");
Departements[6]  = new Array("15", "CO-BOY", "Boyacá");
Departements[7]  = new Array("17", "CO-CAL", "Caldas");
Departements[8]  = new Array("18", "CO-CAQ", "Caquetá");
Departements[9]  = new Array("85", "CO-CAS", "Casanare");
Departements[10] = new Array("19", "CO-CAU", "Cauca");
Departements[11] = new Array("20", "CO-CES", "Cesar");
Departements[12] = new Array("27", "CO-CHO", "Chocó");
Departements[14] = new Array("23", "CO-COR", "Córdoba");
Departements[13] = new Array("25", "CO-CUN", "Cundinamarca");
Departements[15] = new Array("94", "CO-GUA", "Guainía");
Departements[16] = new Array("95", "CO-GUV", "Guaviare");
Departements[17] = new Array("41", "CO-HUI", "Huila (Colombie)");
Departements[18] = new Array("44", "CO-LAG", "La Guajira");
Departements[19] = new Array("47", "CO-MAG", "Magdalena");
Departements[20] = new Array("50", "CO-MET", "Meta");
Departements[21] = new Array("52", "CO-NAR", "Nariño");
Departements[22] = new Array("54", "CO-NSA", "Norte de Santander");
Departements[23] = new Array("86", "CO-PUT", "Putumayo");
Departements[24] = new Array("63", "CO-QUI", "Quindío");
Departements[25] = new Array("66", "CO-RIS", "Risaralda");
Departements[26] = new Array("88", "CO-SAP", "San Andrés et Providencia");
Departements[27] = new Array("68", "CO-SAN", "Santander");
Departements[28] = new Array("70", "CO-SUC", "Sucre (Colombie)");
Departements[29] = new Array("73", "CO-TOL", "Tolima");
Departements[30] = new Array("76", "CO-VAC", "Valle del Cauca");
Departements[31] = new Array("97", "CO-VAU", "Vaupés");
Departements[32] = new Array("99", "CO-VID", "Vichada");

var GetDataIndex = 0;
function MunCol_AddLink(){
    for(var a=0,l=municipalites.length;a<l;a++){
        if(mw.config.get('wgTitle') = municipalites[a]["article"]) return MunCol_GetDataSelect(a);
    } 
    MunCol_GetDataSelect(0);
}

function MunCol_GetDataSelect(index){
     GetDataIndex = index;
     var OngletsCactions = document.getElementById('p-cactions');
     if(OngletsCactions){
          var CactionsUl = OngletsCactions.getElementsByTagName('ul')[0];
          CactionsUl.innerHTML += '<li>'
                                + '<a href="javascript:MunCol_ToggleSelect('+index+');">Mun. Col.</a>'
                                + '</li>';
          $(OngletsCactions).removeClass("emptyPortlet");
     }
}

function MunCol_ToggleSelect(index){
     if(!index) index = 0;
     var SelectDiv = document.getElementById("MunCol_Div");
     if(SelectDiv){
          if(SelectDiv.style.display == "none"){
               SelectDiv.style.display = "block";
          }else{
               SelectDiv.style.display = "none";
          }
          return;
     }
     SelectDiv = document.createElement('div');
     
     SelectDiv.id = "MunCol_Div";
     var Select = document.createElement('select');
     Select.id = "MunCol_Select";
     var AutoLaunch = false;
     for(var a=0,l=municipalites.length;a<l;a++){
          var Line = municipalites[a];
          var Article = Line["article"];
          var Opt = document.createElement('option');
          Opt.innerHTML = Article;
          Opt.text = Article;
          Opt.value = a;
          if(a==index || Article == mw.config.get('wgPageName').replace(/_/g, ' ')){
               Opt.selected = "selected";
               AutoLaunch = true;
          }
          Select.appendChild(Opt);
     }
     SelectDiv.appendChild(Select);
     var AddDatasInput = document.createElement('input');
     AddDatasInput.type = "button";
     AddDatasInput.value = "Mettre à jour";
     AddDatasInput.onclick = function(){ MunCol_AddDatas();};
     SelectDiv.appendChild(AddDatasInput);
     var TextArea = document.createElement('textarea');
     TextArea.id = "MunCol_TextArea";
     TextArea.rows = "10";
     SelectDiv.appendChild(TextArea);
     var Target = document.getElementById("wpTextbox1");
     Target.parentNode.insertBefore(SelectDiv, Target);
     Select.onchange = function(){ MunCol_GetThisData(); };
     if(AutoLaunch) MunCol_GetThisData();
}

function MunCol_GetThisData(){
     var Select = document.getElementById("MunCol_Select");
     MunCol_GetData(Select.value);
}

function MunCol_GetData(index){
    var Line = municipalites[index];
    if(!Line) return;
    var Text = "";
    for(var thing in Line){
        if(Line[thing]) Text += "| " + thing + " = " + Line[thing] + "\n";
    }
    var TextArea = document.getElementById("MunCol_TextArea");
    if(TextArea) TextArea.value = Text;
}

function MunCol_AddDatas(index){
    var Select = document.getElementById("MunCol_Select");
    index = Select.value;
    var TextArea = document.getElementById("wpTextbox1");
    var Line = municipalites[index];
    if(!TextArea || !Line) return;
    var TextAreaText = TextArea.value;
    var TextLines = TextAreaText.split("\n");
    var code = municipalites[index]["code"];
    if(code){
        var ParamDep; var ParamGeoloc;
        code = code.substring(0, 2);
        for(var a=0,l=Departements.length;a<l;a++){
            var prefix = Departements[a][0];
            if(prefix==code){
                ParamDep = "{{"+Departements[a][1]+"}}";
                ParamGeoloc = "Colombie/"+Departements[a][2];
            }
        }
    }
    var InfoboxLines = new Array();
    var NewInfoboxLines = new Array();
    var IsInfobox = false;
    var GotInfobox = false;
    var InfoboxRegExp = new RegExp("\\{\\{(I|i)nfobox (Municipalité|municipalité|Ville) de Colombie", "g");
    for(var a=0,l=TextLines.length;a<l;a++){
        if(GotInfobox) continue;
        var ThisLine = TextLines[a];
        if(ThisLine.match(InfoboxRegExp)!=null){
            IsInfobox = true
            continue;
        }
        if(!IsInfobox) continue;
        if(ThisLine=="}}" || ThisLine.indexOf("}}")==0) GotInfobox = true;
        if(GotInfobox) continue;
        InfoboxLines.push(ThisLine);
    }
    for(var a=0,l=InfoboxLines.length;a<l;a++){
        var ThisLine = InfoboxLines[a];
        var ParamRegExp = new RegExp("\\|[ ]*[^=]+[ ]*=");
        if(ThisLine.match(ParamRegExp)==null) continue;
        var ThisParamName = ThisLine.match(ParamRegExp)[0].replace(/[ ]*\|[ ]*/g, "").replace(/[ ]*=[ ]*/g, "");
        var ThisParamValue = ThisLine.replace(ParamRegExp, "").replace(/^[ ]*/g, "");
        if(ThisParamName=="géolocalisation" && code){
            NewInfoboxLines[a] = ThisLine.replace(/=.*/g, "= ") + ParamGeoloc;
            continue;
        }
        if(ThisParamName=="dep" && code){
            NewInfoboxLines[a] = ThisLine.replace(/=.*/g, "= ") + ParamDep;
            continue;
        }
        if(ThisParamValue!="" || (ThisParamName==ThisParamValue)) continue;
        for(var thing in Line){
            if(Line[thing] && (ThisParamName == thing)){
               var NewValue = Line[thing];
               if(thing == "site"){
                    var Domaine = NewValue.replace(/http:\/\//g, "").replace(/\.gov\.co.*/g, ".gov.co").replace(/^\[/, "");
                    NewValue = "[http://" + Domaine + " " + Domaine + "]";
               }
               if(thing=="altitude") NewValue = NewValue.split(".").join("");
               if(thing=="supkm2") NewValue = NewValue.split(".").join("").split(",").join(".");
               NewInfoboxLines[a] = ThisLine.replace(/=.*/g, "= ") + NewValue;
            }
        }
    }
    
    for(var a=0,l=TextLines.length;a<l;a++){
        var index = InfoboxLines.indexOf(TextLines[a]);
        if(index!=-1 && NewInfoboxLines[index]) TextLines[a] = NewInfoboxLines[index];
    }
    var NewText = TextLines.join("\n");
    NewText = NewText.replace(/\{\{(C|c)oord[^\}]*\}\}\n/, "");
    NewText = NewText.replace(/\{\{(I|i)nfobox (Municipalité|municipalité|Ville) de Colombie/g, "{{Infobox Municipalité de Colombie");
    TextArea.value = NewText;
    document.getElementById('wpSummary').value = "Mise à jour des paramètres de l'infobox";
    document.getElementById('wpMinoredit').checked = false;
}