Jump to content

User:Dr pda/editrefs.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//This function adds a link to the toolbox which, when clicked, searches the article for <ref></ref>
//tags and presents them in textboxes for ease of editing
//To use this function add importScript('User:Dr pda/editrefs.js'); //[[User:Dr pda/editrefs.js]] to your monobook.js
//
 function editRefs(){
  //Hide edit window
  var editform = document.getElementById('editform');
  editform.style.cssText += 'display:none';

  text = document.getElementById('wpTextbox1').value;

  //Hack to cope with HTML comments
  text = text.replace(/-->/g,"--@#%");

  // var refs = text.match(/<ref[^/]*\/ref>|<ref[^/]*\/>/g);
  refs = text.match(/<[Rr]ef(\s*name=[^\>]*)?>[^>]*<\/[Rr]ef>/g);
  text = text.replace(/--@#%/g,"-->");

  var output = document.createElement("ol");
  output.id = 'ref-edit-ol';

  if(refs != null){
    for (x=0; x<refs.length;x++){
      //Hack to cope with HTML comments
      refs[x] = refs[x].replace(/--@#%/g,"-->");
      var ref_textbox = document.createElement("textarea");
      ref_textbox.id = 'ref-box-'+x;
      var ref_li = document.createElement("li");
      ref_li.id = 'ref-li-'+x;
      ref_textbox.value = refs[x];
      var newlines = refs[x].match(/\n/g);
      var lines = (newlines == null) ? 1 : newlines.length+1;
      ref_textbox.rows = (lines > refs[x].length/70) ? lines : refs[x].length/70;
      ref_textbox.cols = 70;
      ref_textbox.style.cssText = 'display:block;';
      ref_li.appendChild(ref_textbox);
      output.appendChild(ref_li);
    }
  }
  else{
   var ref_li = document.createElement("li");
   ref_li.id = 'ref-li';
   ref_li.innerHTML = 'This article contains no &lt;ref&gt;&lt;/ref&gt; tags';
   output.appendChild(ref_li);
 }
  var dummy = document.getElementById("editform");
  dummy.parentNode.insertBefore(output, dummy);

  //Add buttons
  var update = document.createElement("input");
  update.id = 'ref-edit-update';
  update.value = 'Apply changes and preview';
  update.title = 'Apply changes and preview';
  update.type = 'button';
  update.onclick = updateRefs;

  var cancel = document.createElement("input");
  cancel.id = 'ref-edit-cancel';
  cancel.value = 'Cancel and return to edit';
  cancel.title = 'Cancel and return to edit';
  cancel.type = 'button';
  cancel.onclick = returnToEdit;

  var reset = document.createElement("input");
  reset.id = 'ref-edit-reset';
  reset.value = 'Reset fields';
  reset.title = 'Reset fields';
  reset.type = 'button';
  reset.onclick = resetRefs;

  if(refs != null){
    output.parentNode.insertBefore(update, output.nextSibling);
    update.parentNode.insertBefore(cancel, update.nextSibling);
    cancel.parentNode.insertBefore(reset,  cancel.nextSibling);
  }
  else{
    output.parentNode.insertBefore(cancel, output.nextSibling);
  }
 }

 function updateRefs(){
  var startIndex = -1;

  for (x=0; x<refs.length;x++){
    var newref = document.getElementById('ref-box-'+x).value;

    startIndex = text.indexOf(refs[x],startIndex);
    //Only update if changed
    if(refs[x] != newref){
     text = text.substring(-1,startIndex) + newref + text.substring(startIndex + refs[x].length);
    }
    startIndex = startIndex + newref.length;
  }

  var textbox = document.getElementById('wpTextbox1');
  textbox.value = text;

  //returnToEdit();
  document.getElementById('wpPreview').click();
 }

 function returnToEdit(){

  //Hide textboxes and buttons
  var output = document.getElementById('ref-edit-ol');
  output.parentNode.removeChild(output);
  var update = document.getElementById('ref-edit-update');
  if(update) update.parentNode.removeChild(update);
  var cancel = document.getElementById('ref-edit-cancel');
  if(cancel) cancel.parentNode.removeChild(cancel);
  var reset = document.getElementById('ref-edit-reset');
  if(reset) reset.parentNode.removeChild(reset);

  //Show edit window
  var editform = document.getElementById('editform');
  editform.style.cssText = '';

 }

 function resetRefs(){
  for (x=0; x<refs.length;x++){
    var ref_textbox = document.getElementById('ref-box-'+x);
    ref_textbox.value = refs[x];
   }
 }

 $(function () {
   if(document.forms.editform){
     mw.util.addPortletLink('p-tb', 'javascript:editRefs()', 'Edit references', 't-edit-refs', 'Edit <ref></ref> tags', '', '');
   }
 });