Wikisource:Monobook.js/Collegamenti personali.js

Da Wikisource.

/*

Interfaccia grafica per il setup dei collegamenti personali.
Autore: Jalo.

  • /

//

<nowiki>
var myLinksNew;
var tblBody;

// Grazie a [[:en:User:Topaz]]
function msXmlhttpreq() {
  if (window.XMLHttpRequest) {
    xmlhttpobj = new XMLHttpRequest()
  } else {
    try {
      xmlhttpobj = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttpobj = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttpobj = null;
      }
    }
  }
  return xmlhttpobj;
}

// Grazie a [[:en:User:Topaz]]
function msDoReq(url,postdata,headers) {
  var req = msXmlhttpreq();
  req.open(postdata?"POST":"GET", url, false);
  for(header in headers)
    req.setRequestHeader(header,headers[header]);
  req.send(postdata||null);
  return req;
}

// Grazie a [[:en:User:Topaz]]
function msSetPageContent(pagename,content,summary,watch) {
  var prelimresp = msDoReq("/w/index.php?title="+pagename+"&action=submit");
  var edittime = prelimresp.responseText.match(/\<input type='hidden' value="(\d+)" name="wpEdittime" \/\>/)[1];
  var edittoken = prelimresp.responseText.match(/\<input type='hidden' value="([0-9a-f]+\\?)" name="wpEditToken" \/\>/)[1];
  msDoReq("/w/index.php?title="+pagename+"&action=submit",
      "wpEdittime="+edittime+
      "&wpEditToken="+edittoken+
      "&wpTextbox1="+encodeURIComponent(content)+
      "&wpSummary="+encodeURIComponent(summary)+
      (watch?"&wpWatchthis=on":"")+
      "&wpSection=&wpSave=Save+page",
      {"Content-Type":"application/x-www-form-urlencoded"});
}

// Grazie a [[:en:User:Topaz]]
function msGetPageContent(pagename) {
  var req = msDoReq("/w/index.php?title="+pagename+"&action=edit");
  var content = req.responseText.match(/\<textarea[\w\W]*?\>([\w\W]*?)\<\/textarea\>/)[1];
  content = content.replace(/\&lt\;/gi, "<");
  content = content.replace(/\&gt\;/gi, ">");
  content = content.replace(/\&quot\;/gi, "\"");
  content = content.replace(/\&amp\;/gi, "&");
  return content;
}

// Crea una nuova stringa da aggiungere al monobook
function msGenerateNewConfig(){
  var res = '';
  
  // tolgo i caratteri speciali
//  for(i = 0; i < myButtonsNew.length; i++)
//    for(j = 0; j < myButtonsNew[0].length; j++)
//      if (myButtonsNew[i][j] == "·")
//        myButtonsNew[i][j] = "";

  // creo la stringa da salvare
  for(i = 0; i < myLinksNew.length; i++)
  {
    //  trasformo   '   in   \\\'
    myLinksNew[i][0] = myLinksNew[i][0].replace(/\'/gi, "\\\'");
    myLinksNew[i][1] = myLinksNew[i][1].replace(/\'/gi, "\\\'");
  
    res += '[\'' + myLinksNew[i][0] + '\',\'' + myLinksNew[i][1] + '\'],';
  }
    
  // tolgo l'ultima virgola
  res = res.substr(0,res.length-1);

  return res;
}

// Controlla se il monobook e' tra tag -PRE-
function checkPre(newMonobook)
{
    if (newMonobook.indexOf("//<pre>") == -1)
        newMonobook = "//<pre>\n" + newMonobook;

    if (newMonobook.indexOf("//

") == -1) newMonobook = newMonobook + "\n//";

   return newMonobook;

}

// Salva la tabella corrente function msSave(){

 var oldMonobook = msGetPageContent('Utente:' + wgUserName + '/' + msConfigFile);
 
 // Se c'erano gia' i pulsanti
 if (oldMonobook.match(/var myLinks[ ]*=[ ]*new Array.*;/) != null)
   var newMonobook = oldMonobook.replace(/var myLinks[ ]*=[ ]*new Array.*;/, "var myLinks = new Array(" + msGenerateNewConfig() + ");");
 else
   var newMonobook = oldMonobook.replace(/var toLoad/, "var myLinks = new Array(" + msGenerateNewConfig() + ");\n\nvar toLoad");
 // controlla se e' il caso di racchiudere il monobook tra tag -PRE-
 tempMonobook = checkPre(newMonobook);
 msSetPageContent('Utente:' + wgUserName + '/' + msConfigFile, tempMonobook, "Collegamenti personali Setup");
 if (newMonobook != oldMonobook)
   alert("Configurazione salvata. Ricordati di premere ctrl+r per pulire la cache per attivare i cambiamenti.");
 else
   alert("Nessuna modifica effettuata.");
 RemoveNode('SetupCollegamenti');

}

// Crea una riga nella tabella function creaRiga(numero, nome, link) {

   var row = document.createElement("tr");
   row.id = 'Riga'+numero;
   
   // Crea pulsante "rimuovi questo collegamento"
   var cell = document.createElement("td");
   var rimuovimi = document.createElement('input');
   rimuovimi.value = 'Rimuovi';
   rimuovimi.id = numero;
   rimuovimi.type = 'button';
   
   rimuovimi.onclick = function() {rimuoviCollegamento(this.id);};
       
   cell.appendChild(rimuovimi);
   row.appendChild(cell);
   // Creo cella per nome
   var cell = document.createElement("td");
   cell.innerHTML += nome;
   row.appendChild(cell);
   // Creo cella per link
   var cell = document.createElement("td");
   cell.innerHTML += link;
   row.appendChild(cell);
   // aggiunge la riga alla fine della tabella
   tblBody.appendChild(row);

}

// Crea un collegamento function msCreate() {

   msWndSetup=document.getElementById("SetupCollegamenti");
   var nome, link;
   
   // Recupera i valori da salvare
   nome = document.getElementById("nomeCollegamento").value;
   link = document.getElementById("linkCollegamento").value;
   
   // Correggo i caratteri apostrofo
   nome = nome.replace(/\'/gi, "\'");
   link = link.replace(/\'/gi, "\'");
   // Aggiungili all'Array
   var nuovo = new Array(nome, link);
   myLinksNew[myLinksNew.length] = nuovo;
   
   // E alla tabella
       
   // Correggi il font
   nome = ""+nome+"";
   link = ""+link+"";
   
   creaRiga(tblBody.rows.length, nome, link);

}

// Cancella un collegamento function rimuoviCollegamento(riga) {

   msWndSetup=document.getElementById("SetupCollegamenti");
   //Elimina riga "riga" dalla myLinksNew
   myLinksNew.splice(riga,1); // Rimuove 1 elemento dall'indice "riga"
   // E aggiorna graficamente la tabella
   rowToRemove = document.getElementById('Riga'+riga);
   rowToRemove.parentNode.removeChild(rowToRemove);
   
   // Chiudi e riapri per riordinare gli id delle righe
   RemoveNode('SetupCollegamenti');
   msCreateUI();

}

// Disegna la finestra function msCreateUI(){

   var msWndSetup = document.createElement('div');
       msWndSetup.id = 'SetupCollegamenti';
       msWndSetup.style.position = 'absolute';
       msWndSetup.style.zIndex   = 1000;
       msWndSetup.style.left     =  '0px';
       msWndSetup.style.top      =  '0px';
       msWndSetup.style.backgroundColor = '#FFFFFF';
       msWndSetup.style.borderStyle     = 'solid';
       msWndSetup.style.borderWidth     = ' medium';
       msWndSetup.style.borderColor     = '#000000';
       msWndSetup.style.width = '630px';
   var top = document.createElement('div');
       top.id = 'setupCollegeamentiTop';
       top.style.textAlign = 'right';
       top.style.margin = '8px';
       top.style.backgroundColor = '#DDDDDD';
   // crea il tasto "Close"
   var a = document.createElement('a');
       a.appendChild(document.createTextNode('Close'));
       a.href = "javascript:void RemoveNode('SetupCollegamenti')";
       a.style.margin = '8px';
       top.appendChild(a);
       msWndSetup.appendChild(top);


 var body = msWndSetup;
 var intro = document.createElement('p');
 intro.innerHTML = "Benvenuto nella configurazione dei tuoi Collegamenti personali.

" + "Vedi <a href='http://it.wikisource.org/wiki/Wikisource:Monobook.js/Collegamenti personali' target=new>qui</a> per maggiori informazioni


"; body.appendChild(intro);
 body.appendChild(document.createElement("hr"));
 
 var ACapo = document.createElement('p');
 ACapo.innerHTML = '
'; body.appendChild(ACapo);
 // Mostra la tabella dei comandi esistenti
 var titoloTbl = document.createElement('p');
 titoloTbl.innerHTML = 'Elenco dei collegamenti installati:';
 body.appendChild(titoloTbl);
 var ACapo = document.createElement('p');
 ACapo.innerHTML = '
'; body.appendChild(ACapo);

// crea la

ed il <tbody> var tbl = document.createElement("table"); tblBody = document.createElement("tbody"); // Crea la riga di intestazione della tabella var header = document.createElement("tr"); var cell1 = document.createElement("td"); var cellText1 = document.createElement('p'); cellText1.innerHTML = "·"; cell1.appendChild(cellText1); header.appendChild(cell1); var cell2 = document.createElement("td"); var cellText2 = document.createElement('p'); cellText2.innerHTML = "Nome Collegamento"; cell2.appendChild(cellText2); header.appendChild(cell2); var cell3 = document.createElement("td"); var cellText3 = document.createElement('p'); cellText3.innerHTML = "Indirizzo HTTP"; cell3.appendChild(cellText3); header.appendChild(cell3); tblBody.appendChild(header); // mette <tbody> nella
tbl.appendChild(tblBody); //
nel <body> body.appendChild(tbl); // e setta il bordo a 2 e lo sfondo a bianco tbl.setAttribute("border", "2"); tbl.setAttribute("style","background-color:white;"); // crea tutte le celle for (var j = 0; j < myLinksNew.length; j++) { // Creo "Nome" var nome = myLinksNew[j][0]; // Creo "Link" var link = myLinksNew[j][1]; creaRiga(j, ""+nome+"", ""+link+""); } var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); body.appendChild(document.createElement("hr")); var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); // Mostra i comandi per crearne uno nuovo var titoloCrea = document.createElement('p'); titoloCrea.innerHTML = 'Crea un nuovo collegamento:
'; body.appendChild(titoloCrea); var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); // Nome del collegamento var p = document.createElement('p'); p.innerHTML += "Nome del collegamento: "; var nomeCollegamento = document.createElement('input'); nomeCollegamento.name = 'nomeCollegamento'; nomeCollegamento.id = 'nomeCollegamento'; nomeCollegamento.type = 'text'; p.appendChild(nomeCollegamento); body.appendChild(p); // Link del collegamento var p = document.createElement('p'); p.innerHTML += "Indirizzo HTTP: "; var linkCollegamento = document.createElement('input'); linkCollegamento.name = 'linkCollegamento'; linkCollegamento.id = 'linkCollegamento'; linkCollegamento.type = 'text'; p.appendChild(linkCollegamento); body.appendChild(p); var Help = document.createElement('p'); Help.innerHTML = '
Nota Bene:'; Help.innerHTML += '
L\'indirizzo HTTP deve essere completo di "http://"'; Help.innerHTML += '
La stringa %TITOLO% verra\' sostituita dal nome della pagina in cui vi troverete'; Help.innerHTML += '
La stringa %TITOLO2% verra\' sostituita dal nome della pagina in cui vi troverete ma senza l\'eventuale namespace'; Help.innerHTML += '

Ad esempio:'; Help.innerHTML += '

1) http://it.wikipedia.org/wiki/%TITOLO% vi portera\' all\'omonima pagina su wikipedia'; Help.innerHTML += '
2) http://www.google.it/search?num=100&hl=it&q=%TITOLO2%+site%3Ait.wikisource.org&btnG=Cerca&meta= vi permettera\' di cercare su google il titolo della voce'; Help.innerHTML += '
'; body.appendChild(Help); var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); // Bottone "Crea" var btnCreate=document.createElement('input'); btnCreate.type='button'; btnCreate.value = "Crea questo collegamento"; btnCreate.onclick = msCreate; body.appendChild(btnCreate); var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); body.appendChild(document.createElement("hr")); var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); // Bottone "Salva" var btnSave=document.createElement('input'); btnSave.type='button'; btnSave.value = "Salva"; btnSave.onclick = msSave; body.appendChild(btnSave); var pad=document.createElement('label'); pad.innerHTML="   "; body.appendChild(pad); // Bottone "Annulla" var btnClose=document.createElement('input'); btnClose.type='button'; btnClose.value = "Annulla"; btnClose.onclick = function(){ RemoveNode('SetupCollegamenti') }; body.appendChild(btnClose); var ACapo = document.createElement('p'); ACapo.innerHTML = '
'; body.appendChild(ACapo); // e lo mostra a video document.body.appendChild(msWndSetup); msWndSetup.getElementsByTagName('input')[0].focus(); } //START DEL TOOL if (typeof msConfigFile == 'undefined') msConfigFile = 'monobook.js'; if(typeof(msWndSetup) != 'undefined'){ msWndSetup.focus(); }else{ myLinksNew = new Array(); // se non esiste ancora "myLinks" la creo if (typeof (myLinks) == 'undefined') var myLinks = new Array(); else { for (i=0; i < myLinks.length; i++) myLinksNew[i] = new Array(); for (i=0; i < myLinks.length; i++) for (j=0; j < myLinks[0].length; j++) myLinksNew[i][j] = myLinks[i][j].toString(); } // Disegna la finestra msCreateUI(); } // </nowiki>