Gebruiker:Tomgreep/Zeusmode/qtemplate.js

Opmerking: na het publiceren is het wellicht nodig uw browsercache te legen om de veranderingen te zien.

  • Firefox / Safari: houd Shift ingedrukt terwijl u:je op Vernieuwen klikt of druk op Ctrl-F5 of Ctrl-R (⌘-Shift-R op een Mac)
  • Google Chrome: druk op Ctrl-Shift-R (⌘-Shift-R op een Mac)
  • Edge: houd Ctrl ingedrukt terwijl u:je op Vernieuwen klikt of druk op Ctrl-F5.
//<nowiki><pre>

//------------------------------------------------------
//quickTemplate from [[User:Zanaq/Zeusmode]]
//(c) 2006-2008 Zanaq, released under GPL 
//------------------------------------------------------

templateLanguage=wgServer.substring(7, wgServer.indexOf('.'));

var qTemplate = new Array();
var qPos = new Array();
var qCategory = new Array();
var qTemplateText = new Array();
var qPrompt = new Array();
var qDisplayText = new Array();
var qDefault = new Array();
var dontusesquirlyBrackets = new Array();
var qAdvancedParameters = new Array();
var qHidden = new Array;
var qCustomTemplateIdentifier = new Array();
var qSection = new Array;
var qOptions = new Array;
var k = 0;

var regexpBO = new Array();
var regexpBC = new Array();
var bracketsOpen = new Array();
var bracketsClose = new Array();

var lastTemplate;
var templateReason;
vandalizedPage=getPreviousPage().replace(/_/g, ' ');


if (zmUseLocalTemplates)
{

document.write('<scr'+'ipt type="text/javascript"'
  + 'src="http://nl.wikipedia.org/w/index.php?title=Gebruiker:Tomgreep/Zeusmode/' + wgContentLanguage + '-templates.js'
  + '&action=raw&ctype=text/javascript&dontcountme=s"></scr'+'ipt>');

}

if (zmUseCustomTemplates)
   document.write('<scr'+'ipt type="text/javascript"'
  + 'src="http://nl.wikipedia.org/w/index.php?title=' + zmCustomTemplateLocation
  + '&action=raw&ctype=text/javascript&dontcountme=s"></scr'+'ipt>');


function preProcessTemplates()
{

//alert(qTemplate.length);

for (i=1;i < qTemplate.length; i++)
  {
 
    if (dontusesquirlyBrackets[i] == null)
    {
       bracketsOpen[i] = '{{' ; bracketsClose[i] = '}}';
 
    } else 
    {
       q=dontusesquirlyBrackets[i].length / 2;
       if (q > 0) {
       bracketsOpen[i] = dontusesquirlyBrackets[i].substring(0,q);
       bracketsClose[i] = dontusesquirlyBrackets[i].substring(q);
       } else {
         bracketsOpen[i] = '';
         bracketsClose[i] = '';
       }
 
     }
 
     regexpBO[i]=bracketsOpen[i].replace( /\{/g ,'\\{').replace( /\[/g ,'\\['); 
     regexpBC[i]=bracketsClose[i].replace( /\{/g ,'\\}').replace( /\]/g ,'\\]'); 


     if (qTemplate[i] != '-')
     {
      q=qTemplate[i].split(/\|/g); 
      for (j=0; j < q.length; j++)
      {
        qTemplate[i]+='|' + q[j].substring(0,1).toUpperCase() + q[j].substring(1);
       }
     }


 }

}



function toggleHiddenElement(element, controlelement, hidetext, showtext)
{

 hidden=document.getElementById(element);
 hidden.style.display = (hidden.style.display == 'none') ? '' : 'none';
 
 document.getElementById(controlelement).innerHTML = (hidden.style.display == 'none') ? showtext : hidetext;
}

 
// -----------------------------------------------------------------------------
// Trim
// -----------------------------------------------------------------------------
 
function trim(stringToTrim)
{
 
  for (s=0;s < stringToTrim.length && stringToTrim[s++] < 33;); 
  for (e=stringToTrim.length; e >0  && stringToTrim[--e] < 33;);
 
  return(stringToTrim.substring(s-1,e+1));
}
 
 
// -----------------------------------------------------------------------------
// Load the edit form in the background
// -----------------------------------------------------------------------------
 
function loadEditFormBackground()
{
 
  xmlhttp = HTTPClient();
  if (!xmlhttp)
    return;

  var thetitle="/w/index.php?title="+encodeURIComponent(mw.config.get('wgPageName'));

  document.getElementById('bodyContent').innerHTML +='<a name=zmEditform style="margin:-1em;padding:-1em" id="editformcontrolelement" href=#zmEditform onclick=toggleHiddenElement("hiddeneditform","editformcontrolelement","-","+")>+</a>';

  document.getElementById('bodyContent').innerHTML += '<div id="hiddeneditform" style="display:none"></div>';

  xmlhttp.open("GET", thetitle + '&action=edit', true);
  xmlhttp.onreadystatechange = addEditForm;
  xmlhttp.send(null);
 
}
 
 
function addEditForm() 
{
 
  if (xmlhttp.readyState != 4)
    return;

   a=xmlhttp.responseText.indexOf('<form id="editform"');
   b=xmlhttp.responseText.indexOf('</form>',a+1)+7;
  
   document.getElementById('hiddeneditform').innerHTML = xmlhttp.responseText.substring(a,b);

   detectTemplates();
}
 




//----------------------------------------------
//    Section Parser
// identify sections according to wiki markup.
// returns an array of ['header']['content']
// (c) 2008 Zanaq, released under GPL
//----------------------------------------------
function sectioning(text)
{
 regex=/(^=+[^=\n]+=+ *$)/mgi;
 a=text.split(regex);
 sections=new Array;
 currentsection=0;
 sections[currentsection]=new Array;
sections[currentsection]['header']='';
sections[currentsection]['content']='';
 for (i=0;i<a.length;i++)
 {
  if (a[i].charAt(0) == "=")
  {
   sections[++currentsection]=new Array;
   sections[currentsection]['header']= a[i];
   sections[currentsection]['content']='';

  } else {
   sections[currentsection]['content'] = a[i];
  }
 }
 return(sections);
}





function detectTemplates()
{

preProcessTemplates();

//alert(qTemplate.length);
editText=document.getElementById('wpTextbox1').value;

 
//alert(r.test(editText));
for (i=1;i < qTemplate.length; i++)
  {
 
   if (qTemplate[i] != '-') {
 
    r=RegExp( regexpBO[i] +'[\\n ]*?(' +  qTemplate[i] + ')[\\n ]*?(|\\|[^\\}]*?)' + regexpBC[i],'g' );

     aaa=r.exec(editText);
     
     if (!aaa)
     {
     //alert(r.test(editText));
      document.getElementById('chkqt' + i).checked = false;
    } else {
     //alert(r.test(editText));


 if ((qOptions[i])  && (aaa[2]))
 {
  argument=findInArray(qOptions[i], aaa[2].substring(1));
  //alert(argument);

  document.getElementById('selcmbqt' + i).value=qOptions[i][argument];
document.getElementById('cmbqt' + i).value=qOptions[i][argument];
//alert(aaa[2].substring(1));
}

 document.getElementById('chkqt' + i).checked = true;
 
    }
 
     document.getElementById('chkqt' + i).disabled = false;
 
   }
  }
 
//if (wgPageName.substring(0,4) == 'Gebr')
//if (wgPageName.substring(0,24) == 'Wikipedia:Te_verwijderen')
 //detectFirstEmptySection();
//findTemplate()
 
 
}

function addQTemplate() {
 
 
//    var csub=document.getElementById("top");
//    var msg=document.createElement("p");
 
//    msg.innerHTML = ''<img src="' + imgZeus + '" align="left">';
 
//    csub.appendChild(msg);
 
 
    var csub=document.getElementById("contentSub");
    var msg=document.createElement("p");
 
    msg.innerHTML = '';
    wikifybar = '';
    hiddenBar='';
 
if (location.href.indexOf('token=') != -1)
  return 0;
 
  wikifybar = '<table width=100% style="margin-bottom:-.2em;z-index:99999"><tr>';
  wikifybar +='<a id="toggleHiddenBar" href=javascript:toggleHiddenElement("hiddenBar","toggleHiddenBar","-","+") style="font-size:200%;display:none">+</a>';
 
 
  for (i=1;i < qTemplate.length; i++)
  {
 
 
      qT= "'" + qTemplate[i] + "'";
 
      if (qDisplayText[i]== null) 
       {
        if (qTemplate[i].indexOf('|') != -1)
          qDisplayText[i] = qTemplate[i].substring(0, qTemplate[i].indexOf('|'));
       }
 
       
       wkfb = (qTemplate[i] != '-') 
           ? (qTemplateText[i] == null)
                ? '<td style=vertical-align:bottom><center><input type="checkbox" DISABLED style="width:15px;height:15px;margin-top:-1px;margin-bottom:1px" id="chkqt' + i + '" name="chkqt' + i + '" value="greyed" onclick="toggleTemplate(' + i + ');">&nbsp;<a href="/wiki/' + qCategory[i] + '">' + ((qDisplayText[i]==null) ? qTemplate[i] : qDisplayText[i])+ '</a></center></td>'
                :'<td style=vertical-align:bottom><center><input type="button" style="width:15px;height:15px;margin-top:-1px;margin-bottom:1px" id="chkqt' + i + '" name=chkqt"' + i + '" value="" onclick="submitButtonedTemplate(' + i + ');">&nbsp;<a href="/wiki/' + qCategory[i] + '">' + ((qDisplayText[i]==null) ? qTemplate[i] : qDisplayText[i]) + '</a></center></td>'
           : '<td width="' + qPos[i] + '">&nbsp;</td>';
        
	if (qOptions[i] !=null)
       {
          wkfCombobox=createComboBox('cmbqt'+i, 65, qOptions[i][0], qOptions[i], qOptions[i]);

           wkfb+='<td style=vertical-align:top>'+wkfCombobox+'</center></td>';

       
         }
 
 
 
 
if (qHidden[i] == null)
{
wikifybar += wkfb;
} else {
hiddenBar += wkfb;
}
 
 
//<input type="INPUT" 
  
  } 
 
 
  wikifybar += '<td><center><input type="checkbox"  style="margin-top:-1em" CHECKED id="chkMinorEdit" name="chkMinorEdit" value="grayed" onclick="">&nbsp;' + txtMinorEdit + '</center></td>'
  wikifybar += '<td><input id="wpSave" type="submit"  style="margin-top:-1em" value="' + txtSavePage + '" name="wpSave" accesskey="s" title="Sla uw wijzigingen op [alt-s]"/  onclick=\"barClickSubmit();\"></td>';
  wikifybar += '</tr></table>';
  wikifybar += '<table  id="hiddenBar" style="display:none;margin-top:1em" width=100%><tr>' + hiddenBar + '</tr></table>';
 

 
 
msg.innerHTML+= wikifybar;
// alert(msg.innerHTML);
  csub.insertBefore(msg, csub.firstChild);

if (hiddenBar != '')
{
 hidden=document.getElementById('toggleHiddenBar');
 hidden.style.display = '';
}

if ((wgAction == 'submit') || (wgAction == 'edit'))
    detectTemplates();


} 


function barClickSubmit()
{
if ((wgAction != 'submit') && (wgAction != 'edit'))
{
 
 editsummary=prompt(txtComment);
 if (editsummary==null) return(0);
 if (editsummary!='') document.getElementById('wpSummary').value += ' - ' + editsummary;
}
 


 document.getElementById('editform').submit();
 
}

function toggleTemplate(template)
{
    editText=document.getElementById('wpTextbox1').value;
    
smr=document.getElementById('wpSummary');

       if (qTemplate[template].indexOf('|') != -1)
       {
          placeText = qTemplate[template].substring(0, qTemplate[template].indexOf('|'));
       } else {
          placeText = qTemplate[template];
       }

    
    if (document.getElementById('chkqt' + template).checked) //checkmark has just been added
     {

//     if (qTemplateText[k] == null)
//     {
       if (qOptions[template]) placeText+=(document.getElementById('cmbqt' + template).value == '') ? '' : ('|'+ document.getElementById('cmbqt' + template).value);



	rr=RegExp( regexpBO[template] +'(' +  qTemplate[template] + ')(|\\|[^\\}]*?)' + regexpBC[template],'g' );

    	if (rr.test(smr.value))
	{
		rr=RegExp( '(, )*\\-' + regexpBO[template] +'(' +  qTemplate[template] + ')(|\\|[^\\}]*?)' + regexpBC[template],'g' );
		
		smr.value=trim(smr.value.replace(rr,''));
        
		if (smr.value.substr(0,2) == ', ') smr.value=smr.value.substr(2);
       } else {


		if (smr.value!='') smr.value += ', ';
		smr.value+='+' + bracketsOpen[template] + placeText + bracketsClose[template];
       }
       if (!qTemplateText[template])
       {
         if (qPos[template] != 'bottom')
         {
         document.getElementById('wpTextbox1').value = trim(bracketsOpen[template] + placeText + bracketsClose[template] + '\n' + trim(document.editform.wpTextbox1.value));
          } else {
          document.getElementById('wpTextbox1').value =  trim(trim(document.editform.wpTextbox1.value) + '\n'+ bracketsOpen[template] + placeText + bracketsClose[template]);
          }
         }   else {

//alert(document.getElementById('wpTextbox1').value);
document.getElementById('wpTextbox1').value =  trim(qTemplateText[template].replace(/\$1/g, trim(form.getElementsByTagName('textarea')[0].value)).replace(/\$2/g, prompted));
  

         }
       
     } else {





 //alert(escape(bracketsOpen));
//alert(template);
 //alert(regexpBO[template]);
   
    r=RegExp( regexpBO[template] +'[\\n ]*?(' +  qTemplate[template] + ')[\\n ]*?(|\\|[^\\}]*?)' + regexpBC[template],'g' );

     document.getElementById('wpTextbox1').value=trim(editText.replace(r,''));

    r=RegExp( '(, )\\+' + regexpBO[template] + placeText + regexpBC[template],'g' );
//    smr.value=trim(smr.value.replace(r,''));

    r=RegExp( regexpBO[template] +'(' +  qTemplate[template] + ')(|\\|[^\\}]*?)' + regexpBC[template],'g' );

    if (r.test(smr.value))
    {
      r=RegExp( '(, )*\\+' + regexpBO[template] +'(' +  qTemplate[template] + ')(|\\|[^\\}]*?)' + regexpBC[template],'g' );

      smr.value=trim(smr.value.replace(r,''));
      if (smr.value.substr(0,2) == ', ') smr.value=smr.value.substr(2);

    } else {

       if (smr.value!='') smr.value += ', ';
       smr.value+='-' + bracketsOpen[template] + placeText + bracketsClose[template];
    }
       
     

     }
   




}



function submitButtonedTemplate(template)
{
 editText=document.getElementById('wpTextbox1').value;
//alert(qSection[template]);

 if ( (document.location.href.indexOf('&section=') == -1) && ( (qSection[template]!=null) ) )//             (qSection[template]>=0) || (qSection[template] == 'lastfilled') ) )
    {
      sections=sectioning(editText);

      sct=qSection[template];

      if (sct=='lastfilled')
      { 
        for (i=0;i<sections.length;i++)
        {
  //         alert(sections[i]['content'].length);
           if (sections[i]['content'].length < 3) break;
        }
        
        sct= (i<sections.length)  ? i -1 : sections.length - 1;

      }
//alert('dhjsh' + sct);
      editText=sections[sct]['content'];
//     alert(editText);
    }

 

     if (qPrompt[template] == null) 
            { 
                 prompted = '';
            } else {
                prompted = prompt(qPrompt[template], (qDefault[template]==null) ? '' : qDefault[template]);
                if (prompted==null) return;
            }

if ( typeof ( qTemplateText[template] ) == "string" ) 
{
       editText =  trim(qTemplateText[template].replace(/\$1/g, trim(editText)).replace(/\$2/g, prompted));
       selectedValue='';

} else {

//a=qTemplateText[template].length
selectedValue=document.getElementById('cmbqt' + template).value

a=findInArray(qOptions[template], '"' + selectedValue + '"');

selectedOption=(a==-1) 
     ? (qTemplateText[template][qTemplateText[template].length-1])
     :(qTemplateText[template][a]);
//alert(selectedOption);

       editText =  trim(selectedOption.replace(/\$1/g, trim(editText)).replace(/\$2/g, prompted).replace(/\$3/g, selectedValue));


//alert(typeof ( qTemplateText[template] ));

}

       
if ( (document.location.href.indexOf('&section=') == -1) && ( (qSection[template]!=null) ) )
    {
      sections[sct]['content']='\n\n'+editText +'\n\n';
      editText=unSectioning(sections);
    }

//alert(qTemplate[template]);
document.getElementById('wpSummary').value+= '+' + bracketsOpen[template] + qTemplate[template].substring(0,qTemplate[template].indexOf('|')) + bracketsClose[template];

document.getElementById('wpSummary').value+=((prompted != null) && (prompted != '')) ? (' - ' + prompted) : '';

document.getElementById('wpSummary').value+=((selectedValue != null) && (selectedValue != '')) ? (', ' + selectedValue) : '';



document.getElementById('wpTextbox1').value=trim(editText);

if ( (wgAction != 'submit') && (wgAction != 'edit') &&  (document.getElementById('hiddeneditform').style.display == 'none') )
           document.getElementById('editform').submit();

} 




function getPreviousPage()
{


     url=document.location.href;
 
     tp=url.indexOf('?template=');
     if (tp != -1) 
     {
      pp=url.indexOf('&vandalized=');
      rp=url.indexOf('&reason=');
      if (rp == -1) 
      {
        rp = url.length;
       templateReason= '';
      } else {
       templateReason=decodeURIComponent(url.substring(rp+8));

      }
 
      lastTemplate=decodeURIComponent(url.substring(tp+10,pp));

      articleName=decodeURIComponent(url.substring(pp+12,rp));

      return(decodeURIComponent(articleName));
     }



 
 z=   document.location.href.indexOf('vandalized=');
 if (z != -1)
 {
  articleName=document.location.href.substring(z+11);
  
  z=  document.location.href.indexOf('&');
  if (z != -1)
  {
     articleName=articleName.substring(0,z);
  }
  
  
  return(decodeURIComponent(articleName));
 }
 
 if (document.referrer == null)
  return('');
 
 
 ref = document.referrer;

 
 z=   ref.indexOf('vandalized=');
 if (z != -1)
 {
  articleName=ref.substring(z+11);
  z=  articleName.indexOf('&');
  if (z != -1)
  {
     articleName=articleName.substring(0,z);
  }

  return(decodeURIComponent(articleName));
 }
 





 
z=   ref.indexOf('/wiki/');
 
if (z == -1)
{
  z=ref.indexOf('title=');
  zz=ref.indexOf('&');
 
  articleName=ref.substring(z+6,zz);
 
} else {
  zz=ref.indexOf('?');
  
  articleName=(zz!=-1) ? ref.substring(z+6,zz) : ref.substring(z+6);

}
 
 return(decodeURIComponent(articleName));
}


function passArticlename()
{

q=document.getElementById('contentSub').getElementsByTagName('a');
q = (q.length==4) ? q[0] : q[1];

sep = (q.href.indexOf('action=edit') == -1) ? '?' : '&';

q.href += sep + 'vandalized=' + encodeURIComponent(vandalizedPage);
 
//q.href =  'http://nl.wikipedia.org/wiki/Overleg_gebruiker:Zanaq/Test' + '?vandalized=' + encodeURIComponent(vandalizedPage);

}




function unSectioning(sections)
{
  //alert(sections.length);
  unSectionedText='';
  for (i=0;i<sections.length;i++)
  {
 //alert(sections[i]['header']);
     unSectionedText+=sections[i]['header'] +sections[i]['content'];
  }
  return(unSectionedText);
}



/*===============
Find deletion templates in a page
================*/
 
function findTemplate()
{
 
verwijderlink=document.getElementById(deletionLinkId);
 if (verwijderlink==null) return(0);
 //alert('sjhj');
 if (document.getElementById('wpTextbox1') == null) 
 {t=setTimeout('findTemplate();',100);
  return(0);
 } 
var regexFindTemplates = RegExp('\\{\\{(' + deletionTemplates + ')(.*)\\}\\}','im');
 a = regexFindTemplates.exec(document.getElementById('wpTextbox1').value);
 
 a[2] = (a[2][0]=='|') ? a[2].substring(1) : '';
 a[2] = (a[2][1]=='=') ? a[2].substring(2) : a[2];
 
 a[2]=a[2].split(/\|(\d+=\d+|\d+)/)[0];
 
 url='?template=' + encodeURIComponent(a[1]) + '&vandalized=' + encodeURIComponent(wgPageName.replace(/_/g, ' '));
 
 if (a[2] != '') url += '&reason=' + encodeURIComponent(a[2]);
 
 verwijderlink.getElementsByTagName('a')[0].href += url;
 
}


if (zmUseQuickTemplate == true)
{
 if (wgCanonicalSpecialPageName == "Contributions")
 {
    window.addEventListener("DOMContentLoaded", passArticlename, false);
}
 if ((wgCanonicalNamespace != 'Special') && (wgAction != 'submit') && (wgAction != 'edit'))
    window.addEventListener("DOMContentLoaded", loadEditFormBackground, false);

 if (wgCanonicalNamespace != 'Special')
  window.addEventListener ('DOMContentLoaded',addQTemplate,false);

 if (wgCanonicalNamespace != 'Special') 
    window.addEventListener("DOMContentLoaded", findTemplate, false);
}
//</pre></nowiki>