Utilisateur:Dr Brains/Statut.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.
//<syntaxhighlight lang=javascript>//<pre>//<nowiki>
if(typeof(Statut)==="undefined"){

var Statut = new Object();
Statut.Texts = new Object();
Statut.Texts.EditLinkText = "[m]";
Statut.Texts.EditLinkTitle = "Modifier mon statut";
Statut.Texts.OKInputText = "OK";
Statut.Texts.OKInputTitle = "Valider mon nouveau statut";
Statut.Texts.CancelInputText = "Annuler";
Statut.Texts.CancelInputTitle = "Annuler les changements de mon statut";

Statut.Texts.Comment = "Modification du statut : $1 ⇨ $2";

Statut.Variables = new Object();
Statut.Variables.InitPage = mw.config.get('wgUserName') + "/Statut";
Statut.Variables.CommonsURL = "//upload.wikimedia.org/wikipedia/commons/thumb/";
Statut.Variables.Statuts = new Object();
Statut.Variables.Statuts["online"] = "e/e5/Oxygen480-status-user-online.svg/16px-Oxygen480-status-user-online.svg.png";
Statut.Variables.Statuts["offline"] = "3/33/Oxygen480-status-user-offline.svg/16px-Oxygen480-status-user-offline.svg.png";
Statut.Variables.Statuts["away"] = "b/b9/Oxygen480-status-user-away.svg/16px-Oxygen480-status-user-away.svg.png";
Statut.Variables.Statuts["busy"] = "b/b6/Oxygen480-status-user-busy.svg/16px-Oxygen480-status-user-busy.svg.png";
Statut.Variables.Statuts["patrol"] = "5/5e/Nuvola_apps_agent.svg/16px-Nuvola_apps_agent.svg.png";
Statut.Variables.Statuts["redact"] = "2/2c/Writing.svg/16px-Writing.svg.png";
Statut.Variables.Statuts["invisible"] = "9/9d/Oxygen480-status-user-invisible.svg/16px-Oxygen480-status-user-invisible.svg.png";
Statut.Variables.Statuts["wikislow"] = "6/6c/Zeichen_274.1.svg/16px-Zeichen_274.1.svg.png";
Statut.Variables.Statuts["maintenance"] = "d/d1/Wikipedia_Administrator.svg/16px-Wikipedia_Administrator.svg.png";
Statut.Variables.statut = null;

Statut.Fonctions = new Object();

Statut.Fonctions.Init = function(){
  if(typeof(Statut.Fonctions.CustomParams)=="function") try{ Statut.Fonctions.CustomParams(); }catch(e){  }
  if(mw.config.get('wgNamespaceNumber')==2 && mw.config.get('wgTitle') == Statut.Variables.InitPage){
    //alert("Page = OK");
    var StatutDiv = document.getElementById('statut');
    if(!StatutDiv) return;
    var Table = StatutDiv.getElementsByTagName('table')[0];
    var InfosSpan = $.makeArray( $(StatutDiv).find('span.statutJS'))[0];
    if(!InfosSpan || !Table) return;
    Statut.Variables.statut = Statut.Fonctions.unHTMLize(InfosSpan.innerHTML);
    Statut.Fonctions.setCookie(Statut.Variables.statut);
    var EditLink = document.createElement('a');
    EditLink.id = "statut_link";
    EditLink.style.float = "left";
    EditLink.style.fontSize = "x-small";
    EditLink.style.marginLeft = "0.5em";
    EditLink.innerHTML = Statut.Texts.EditLinkText;
    EditLink.title = Statut.Texts.EditLinkTitle;
    EditLink.href = "javascript:Statut.Fonctions.editStatut(false);";
    StatutDiv.insertBefore(EditLink, StatutDiv.firstChild);
    Table.id = "statut_table";
    Table.style.marginTop = "-19px";
  }else{
    //alert("Page = NOK");
    Statut.Variables.statut = Statut.Fonctions.getCookie();
    if(!Statut.Variables.statut) Statut.Variables.statut = "invisible";
    var StatusLi = document.getElementById("pt-status");
    if(!StatusLi){
      var PersonalLink = document.getElementById('pt-userpage');
      if(!PersonalLink) return;
      StatusLi = document.createElement('li');
      StatusLi.id = "pt-status";
      var StatusImage = document.createElement('img');
      StatusImage.width = "16";
      StatusImage.src = Statut.Variables.CommonsURL+Statut.Variables.Statuts[Statut.Variables.statut];
      StatusImage.alt = Statut.Variables.statut;
      var StatusSpan = document.createElement("span");
      StatusSpan.innerHTML = Statut.Variables.statut;
      var Link = document.createElement('a');
      Link.appendChild(StatusImage);
      Link.appendChild(document.createTextNode(" "));
      Link.appendChild(StatusSpan);
      Link.title = Statut.Texts.EditLinkTitle;
      Link.href = "javascript:Statut.Fonctions.editStatut(true);";
      StatusLi.appendChild(Link);
      PersonalLink.parentNode.insertBefore(StatusLi, PersonalLink);
    }else{
      var StatusImage = StatusLi.getElementsByTagName('img')[0];
      if(StatusImage){
        StatusImage.src = Statut.Variables.CommonsURL+Statut.Variables.Statuts[Statut.Variables.statut];
        StatusImage.alt = Statut.Variables.statut;
      }
      var StatusSpan = StatusLi.getElementsByTagName('span')[0];
      if(StatusSpan) StatusSpan.innerHTML = Statut.Variables.statut;
      var OldStatutModif = document.getElementById('statut_edit');
      if(OldStatutModif) OldStatutModif.parentNode.removeChild(OldStatutModif);
      var StatusLink = StatusLi.getElementsByTagName('a')[0];
      if(StatusLink) StatusLink.style.display = "";      
    }
  }
}

Statut.Fonctions.editStatut = function(Distant){
  var Div, Table, EditLink, EditTable;
  if(!Distant){
    Div = document.getElementById('statut');
    Table = document.getElementById('statut_table');
    EditLink = document.getElementById('statut_link');
  }else{
    Div = document.getElementById("pt-status");
    Table = Div.getElementsByTagName("a")[0];
    EditLink = document.getElementById('statut_link');
  }
  EditTable = document.getElementById('statut_edit');
  if(EditTable){
    if(EditTable.style.display == "none"){
      EditTable.style.display = "";
      if(Table) Table.style.display = "none";
      if(EditLink) EditLink.style.display = "none";
    }else{
      EditTable.style.display = "none";
      if(Table) Table.style.display = "";
      if(EditLink) EditLink.style.display = "";
    }
    return;
  }
  var OKInput = document.createElement('input');
  OKInput.type = "button";
  OKInput.value = Statut.Texts.OKInputText;
  OKInput.title = Statut.Texts.OKInputTitle;
  OKInput.onclick = function(){ Statut.Fonctions.checkStatut(); };
  OKInput.onselect = function(){ Statut.Fonctions.checkStatut(); };
  OKInput.style.padding = "0";
  OKInput.style.fontSize = "80%";

  var CancelInput = document.createElement('input');
  CancelInput.type = "button";
  CancelInput.value = Statut.Texts.CancelInputText;
  CancelInput.title = Statut.Texts.CancelInputTitle;
  CancelInput.onclick = function(){ Statut.Fonctions.editStatut(Distant); };
  CancelInput.onselect = function(){ Statut.Fonctions.editStatut(Distant); };
  CancelInput.style.padding = "0";
  CancelInput.style.fontSize = "80%";

  if(!Distant){
    EditTable = document.createElement('table');
    EditTable.id = 'statut_edit';
    EditTable.width="249px";
    EditTable.height="64px";
    EditTable.style.textAlign = "center"
    var TR = document.createElement('tr');
    var TD = document.createElement('td');
    EditTable.appendChild(TR);
    TR.appendChild(TD);
    TD.appendChild(Statut.Fonctions.createSelect());
    TD.appendChild(OKInput);
    TD.appendChild(CancelInput);
    Div.appendChild(EditTable);
  }else{
    EditTable = document.createElement('span');
    EditTable.id = 'statut_edit';
    EditTable.appendChild(document.createTextNode(" "));
    EditTable.appendChild(Statut.Fonctions.createSelect());
    EditTable.appendChild(OKInput);
    EditTable.appendChild(CancelInput);
    Div.appendChild(EditTable);
  }
  if(Table) Table.style.display = "none";
  if(EditLink) EditLink.style.display = "none";
}

Statut.Fonctions.createSelect = function(){
  var Options = Statut.Variables.Statuts;
  var SelectedOption = Statut.Variables.statut;
  var Select = document.createElement('select');
  Select.id = 'statut_selector';
  Select.style.fontSize = "95%";
  Select.style.marginTop = "1px";
  for(var statut in Options){
    var Option = statut;
    var opt = document.createElement('option');
    if(Option == SelectedOption) opt.selected = "selected";
    opt.value = Option;
    opt.innerHTML = '<img src="'+Statut.Variables.CommonsURL+Options[statut]+'" width="12" /> ' + Option;
    Select.appendChild(opt);
  }
  return Select;
}

Statut.Fonctions.checkStatut = function(){
  var Select = document.getElementById('statut_selector');
  if(!Select) return;
  var OldStatut = Statut.Variables.statut;
  var NewStatut = Select.value;
  var Page = "User:" + Statut.Variables.InitPage;
  var URL = mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + encodeURIComponent(Page) + "&action=edit";
  Statut.Fonctions.Ajax({url:URL, 
                         onSuccess:Statut.Fonctions.updateStatut,
                         old:OldStatut,
                         new:NewStatut,
                         page:Page
  });
}

Statut.Fonctions.updateStatut = function(Req, data){
  var OldStatut = data.old;
  var NewStatut = data.new;
  var Page = data.page;
  var TreatmentDiv = document.createElement('div');
  TreatmentDiv.style.display = "none";
  TreatmentDiv.innerHTML = Req.responseText;
  document.body.insertBefore(TreatmentDiv, document.body.firstChild);
  var TextArea = document.getElementById('wpTextbox1');
  if(!TextArea){
    TreatmentDiv.parentNode.removeChild(TreatmentDiv);
    return;
  }
  var Text = TextArea.value;
  var TempReg = new RegExp("\\{\\{(_| )*(S|s)tatut(_| )*\\|[^\\}]*\\}\\}");
  var Matches = Text.match(TempReg);
  if(Matches == null){
    TreatmentDiv.parentNode.removeChild(TreatmentDiv);
    return;
  }
  var Template = Matches[0];
  Template = Template.replace(/( |_)*\|( |_)*/g, "|");
  Template = Template.replace(/( |_)*\}/g, "}");
  Template = Template.split("|"+OldStatut+"|").join("|"+NewStatut+"|");
  Template = Template.split("|"+OldStatut+"}").join("|"+NewStatut+"}");
  Text = Text.replace(Matches[0], Template);
  TextArea.value = Text;
  var Comment = Statut.Texts.Comment;
  Comment = Comment.split("$1").join(OldStatut);
  Comment = Comment.split("$2").join(NewStatut);
  var wpSummary = document.getElementById('wpSummary');
  if(wpSummary) wpSummary.value = Comment;
  var wpMinoredit = document.getElementById('wpMinoredit');
  if(wpMinoredit) wpMinoredit.checked = "checked";
  var wpWatchthis = document.getElementById('wpWatchthis');
  if(wpWatchthis) wpWatchthis.checked = "checked";
  if(mw.config.get('wgNamespaceNumber')==2 && mw.config.get('wgTitle') == Statut.Variables.InitPage){
    Statut.Fonctions.setCookie(NewStatut);
    document.editform.submit();
    return;
  }else{
    var URL = document.editform.action;
    var Params = new Array("wpTextbox1", "wpSection", "wpSummary", "wpStarttime", "wpEdittime", "wpEditToken", "wpMinoredit", "wpWatchthis");
    var URLParams = new Array();
    for(var a=0,l=Params.length;a<l;a++){
      var Element = document.getElementById(Params[a]);
      if(!Element) Element = document.getElementsByName(Params[a])[0];
      if(Element){
        URLParams.push(Params[a]+'='+ encodeURIComponent(Element.value));
      }
    }
    var headers = {"Content-Type": "application/x-www-form-urlencoded"};
    Statut.Fonctions.Ajax({url:URL, method: "POST", headers: headers, 
                           data:URLParams.join("&"),
                           onSuccess:Statut.Fonctions.saveStatut,
                           old:OldStatut,
                           new:NewStatut,
                           page:Page
    });
  }
}

Statut.Fonctions.saveStatut = function(Req, data){
  var NewStatut = data.new;
  Statut.Variables.statut = NewStatut;
  Statut.Fonctions.setCookie(NewStatut);
  Statut.Fonctions.Init();
}

Statut.Fonctions.setCookie = function(cookieValue){
    var now = new Date();
    var nextYear = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365 );
    document.cookie = escape("GadgetStatut") + '=' + escape(cookieValue) + '; EXPIRES=' + nextYear.toGMTString()  + "; PATH=/" ; 
}

Statut.Fonctions.getCookie = function(){
    var cookieName = "GadgetStatut";
    var cookieValue = null;
    var posName = document.cookie.indexOf(escape(cookieName) + '=' );
    if (posName != -1) {
        var posValue = posName + (escape(cookieName) + '=' ).length;
        var endPos = document.cookie.indexOf(';', posValue) ;
        if (endPos != -1) {
            cookieValue = unescape(document.cookie.substring(posValue, endPos));
        } else {
            cookieValue = unescape(document.cookie.substring(posValue));
        }
    }
    try{ cookieValue = decodeURIComponent(cookieValue); }catch(e){ };
    return cookieValue;
}
 
Statut.Fonctions.unHTMLize = function(string) {
  var entities = new Array('amp','lt','gt','quot');
  var chars = new Array('&','<','>','"');
  for (var i=0; i<chars.length; i++) {
    var regex = new RegExp('&'+entities[i]+';', "g");
    string = string.replace(regex, chars[i]);
  }
  return string;
}

Statut.Fonctions.Ajax = function(bundle){ 
  var xmlhttp; 
  try{ 
    xmlhttp = new XMLHttpRequest(); 
  }catch(e){ 
    try{ 
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){ 
      try{ 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      }catch(e){ 
        xmlhttp = false; 
      }
    }
  }; 
  if(xmlhttp){ 
    xmlhttp.onreadystatechange = function(){ 
      if (xmlhttp.readyState == 4){ 
        if(xmlhttp.status == 200 || xmlhttp.status == 302){ 
          if(bundle.onSuccess) bundle.onSuccess(xmlhttp,bundle); 
        }else if(bundle.onFailure){ 
          bundle.onFailure(xmlhttp,bundle); 
        }else{ } 
      }
    }; 
  xmlhttp.open(bundle.method ? bundle.method : "GET",bundle.url,bundle.async == false ? false : true); 
  if (bundle.headers) { 
    for (var field in bundle.headers){ 
      try{ xmlhttp.setRequestHeader(field,bundle.headers[field]); }catch(err){}
    }
  }; 
  xmlhttp.send(bundle.data ? bundle.data : null); };
  return xmlhttp;
};

$(Statut.Fonctions.Init);

}
//</nowiki></pre></syntaxhighlight>