User:Wtmitchell/refToolBar.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

// based on [[User:Mr.Z-man/refToolbar.js]]
 
var numforms = 0;
var wikEdAutoUpdateUrl;
 
function refbuttons() {
  if (mwCustomEditButtons && document.getElementById('toolbar') && wikEdAutoUpdateUrl == null) {
    button = document.createElement('a');
    button.href = "javascript:easyCiteMain()";
    button.title = "Insert Citation";
    buttonimage = document.createElement('img');
    buttonimage.src = "http://upload.wikimedia.org/wikipedia/commons/e/ea/Button_easy_cite.png";
    buttonimage.alt = "Insert Citation";
    button.appendChild(buttonimage);
    document.getElementById('toolbar').appendChild(button);
    if (navigator.userAgent.indexOf('MSIE') == -1) {
      citemain = document.createElement('div');
      citemain.style.display = 'none';
      citemain.setAttribute('Id', 'citeselect');
      citemain.appendChild( addOption("citeWeb()", "Web") );
      citemain.appendChild( addOption("citeNews()", "News") );
      citemain.appendChild( addOption("citeBook()", "Book") );
      citemain.appendChild( addOption("citeJournal()", "Journal") );
      citemain.appendChild( addOption("citeNamedRef()", "Named references") );
      citemain.appendChild( addOption("hideInitial()", "Cancel") );
      document.getElementById('wpTextbox1').parentNode.insertBefore(citemain, document.getElementById('wpTextbox1'));
    }
    else {
    selection = '<div id="citeselect" style="display:none"><input type="button" value="Web" onclick="citeWeb()" />'+
      '<input type="button" value="News" onclick="citeNews()" />'+
      '<input type="button" value="Book" onclick="citeBook()" />'+
      '<input type="button" value="Journal" onclick="citeJournal()" />'+
      '<input type="button" value="Named references" onclick="citeNamedRef()" />'+
      '<input type="button" value="Cancel" onclick="hideInitial()" /></div>';
    document.getElementById('editform').innerHTML = selection + document.getElementById('editform').innerHTML;
    }
  }
}
 
function addOption(script, text) {
  option = document.createElement('input');
  option.setAttribute('type', 'button');
  option.setAttribute('onclick', script);
  option.setAttribute("value", text);
  return option;
}
 
function hideInitial() {
  document.getElementById('citeselect').style.display = 'none';
  if (document.getElementById('citediv1') ) {
    document.getElementById('citediv'+numforms).style.display = 'none';
  }
}
 
function easyCiteMain() {
  document.getElementById('citeselect').style.display = '';
}
 
function getTime() {
  var time = new Date();
  var nowdate = time.getUTCDate();
  if (nowdate<10) { nowdate = "0"+ nowdate.toString(); }
  var nowmonth = time.getUTCMonth()+1;
  if (nowmonth<10) { nowmonth = "0"+ nowmonth.toString(); }
  var nowyear = time.getUTCFullYear();
  newtime =  nowyear + '-' + nowmonth + '-' + nowdate;
  return (newtime);
}
 
function citeWeb() {
  citeNewsWeb("cite web");
}
function citeNews() {
  citeNewsWeb("cite news");
}
 
function citeNewsWeb(templatename) {
  if (numforms != 0) {
    document.getElementById('citediv'+numforms).style.display = 'none';
  }
  template = templatename;
  var legend;
  if (template="cite web") {
    legend = "Cite web source";
  } else {
    legend = "Cite news source";
  }
  newtime = getTime();
  numforms++;
  form = '<div id="citediv'+numforms+'">'+
    '<fieldset><legend>'+legend+'</legend>'+
    '<table cellspacing="5">'+
    '<input type="hidden" value="'+template+'" id="template">'+
    '<tr><td width="120"><label for="url">&nbsp;URL: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="url"></td>'+
    '<td width="120"><label for="title">&nbsp;Title: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="title"></td></tr>'+
    '<tr><td width="120"><label for="last">&nbsp;Last name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="last"></td>'+
    '<td width="120"><label for="first">&nbsp;First name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="first"></td></tr>'+
    '<tr><td width="120"><label for="coauthors">&nbsp;Coauthors: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="coauthors"></td>'+
    '<td width="120"><label for="date">&nbsp;Publication date: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="date"></td></tr>'+
    '<tr><td width="120"><label for="work">&nbsp;Work: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="work"></td>'+
    '<td width="120"><label for="publisher">&nbsp;Publisher: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="publisher"></td></tr>'+
    '<tr><td width="120"><label for="pages">&nbsp;Pages: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="pages"></td>'+
    '<td width="120"><label for="language">&nbsp;Language: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="language"></td></tr>'+
    '<tr><td width="120"><label for="accessdate">&nbsp;Access date: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="accessdate" value="'+ newtime +'"></td>'+
    '<td width="120"><label for="refname">&nbsp;Reference name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="refname"></td></tr>'+
    '</table>'+
    '<input type="button" value="Add citation" onClick="addcites()">'+
 '</fieldset></div>';
   document.getElementById('citeselect').innerHTML += form;
}
 
function citeBook() {
  if (numforms != 0) {
    document.getElementById('citediv'+numforms).style.display = 'none';
  }
  template = "cite book";
  numforms++;
  form = '<div id="citediv'+numforms+'">'+
    '<fieldset><legend>Cite book source</legend>'+
    '<table cellspacing="5">'+
    '<input type="hidden" value="'+template+'" id="template">'+
    '<tr><td width="120"><label for="last">&nbsp;Last name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="last"></td>'+
    '<td width="120"><label for="first">&nbsp;First name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="first"></td></tr>'+
    '<tr><td width="120"><label for="coauthors">&nbsp;Coauthors: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="coauthors"></td>'+
    '<td width="120"><label for="others">&nbsp;Others: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="others"></td></tr>'+
    '<tr><td width="120"><label for="title">&nbsp;Title: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="title"></td>'+
    '<td width="120"><label for="editor">&nbsp;Editor: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="editor"></td></tr>'+
    '<tr><td width="120"><label for="publisher">&nbsp;Publisher: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="publisher"></td>'+
    '<td width="120"><label for="location">&nbsp;Location: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="location"></td></tr>'+
    '<tr><td width="120"><label for="date">&nbsp;Publication date: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="date"></td>'+
    '<td width="120"><label for="edition">&nbsp;Edition: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="edition"></td></tr>'+
    '<tr><td width="120"><label for="series">&nbsp;Series: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="series"></td>'+
    '<td width="120"><label for="volume">&nbsp;Volume: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="volume"></td></tr>'+
    '<tr><td width="120"><label for="pages">&nbsp;Pages: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="pages"></td>'+
    '<td width="120"><label for="chapter">&nbsp;Chapter: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="chapter"></td></tr>'+
    '<tr><td width="120"><label for="isbn">&nbsp;ISBN: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="isbn"></td>'+
    '<td width="120"><label for="oclc">&nbsp;OCLC: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="oclc"></td></tr>'+
    '<tr><td width="120"><label for="url">&nbsp;URL: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="url"></td>'+
    '<td width="120"><label for="accessdate">&nbsp;Access date: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="accessdate"></td></tr>'+
    '<tr><td width="120"><label for="language">&nbsp;Language: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="language"></td>'+
    '<td width="120"><label for="refname">&nbsp;Reference name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="refname"></td></tr>'+
    '</table>'+
    '<input type="button" value="Add citation" onClick="addcites()">'+
 '</fieldset></div>';
   document.getElementById('citeselect').innerHTML += form;
}
 
function citeJournal() {
  if (numforms != 0) {
    document.getElementById('citediv'+numforms).style.display = 'none';
  }
  template = "cite journal";
  numforms++;
  form = '<div id="citediv'+numforms+'">'+
    '<fieldset><legend>Cite book source</legend>'+
    '<table cellspacing="5">'+
    '<input type="hidden" value="'+template+'" id="template">'+
    '<tr><td width="120"><label for="last">&nbsp;Last name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="last"></td>'+
    '<td width="120"><label for="first">&nbsp;First name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="first"></td></tr>'+
    '<tr><td width="120"><label for="coauthors">&nbsp;Coauthors: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="coauthors"></td>'+
    '<td width="120"><label for="date">&nbsp;Publication date: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="date"></td></tr>'+
    '<tr><td width="120"><label for="title">&nbsp;Title: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="title"></td>'+
    '<td width="120"><label for="journal">&nbsp;Journal: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="journal"></td></tr>'+
    '<tr><td width="120"><label for="publisher">&nbsp;Publisher: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="publisher"></td>'+
    '<td width="120"><label for="location">&nbsp;Location: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="location"></td></tr>'+
    '<tr><td width="120"><label for="volume">&nbsp;Volume: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="volume"></td>'+
    '<td width="120"><label for="issue">&nbsp;Issue: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="issue"></td></tr>'+
    '<tr><td width="120"><label for="pages">&nbsp;Pages: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="pages"></td>'+
    '<td width="120"><label for="issn">&nbsp;ISSN: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="issn"></td></tr>'+
    '<tr><td width="120"><label for="oclc">&nbsp;OCLC: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="oclc"></td>'+
    '<td width="120"><label for="language">&nbsp;Language: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="language"></td></tr>'+
    '<tr><td width="120"><label for="url">&nbsp;URL: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="url"></td>'+
    '<td width="120"><label for="accessdate">&nbsp;Access date: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="accessdate"></td></tr>'+
    '<tr><td width="120"><label for="refname">&nbsp;Reference name: </label></td>'+
              '<td width="400"><input type="text" style="width:100%" id="refname"></td></tr>'+
    '</table>'+
    '<input type="button" value="Add citation" onClick="addcites()">'+
 '</fieldset></div>';
   document.getElementById('citeselect').innerHTML += form;
}
 
function addcites(template) {
  cites = document.getElementById('citediv'+numforms).getElementsByTagName('input');
  var citebegin = '<ref';
  var citename = '';
  var citeinner = '';
  for (var i=0; i<cites.length-1; i++) {
    if (cites[i].value != '' && cites[i].id != "refname" && cites[i].id != "template") {
      citeinner += "|" + cites[i].id + "=" + cites[i].value;
    }
    else if (cites[i].value != '' && cites[i].id == "refname" && cites[i].id != "template") {
      citebegin += ' name="' + cites[i].value + '"';
    }
    else if (cites[i].value != '' && cites[i].id != "refname" && cites[i].id == "template") {
      citename = '>{{' + cites[i].value;
    }
  }
  cite = citebegin + citename + citeinner + "}}</ref>";
  insertTags(cite, '', '');
  document.getElementById('citediv'+numforms).style.display = 'none';
} 
 
function getNamedRefs() {
  text = document.getElementById('wpTextbox1').value;
  regex = /<\s*?ref\s+?name\s*?=\s*?(('([^']*?)')|("([^"]*?)"))\s*?>/gi //'
  var namedrefs = new Array();
  var i=0;
  var nr=true;
  do {
    ref = regex.exec(text);
    if(ref != null){
      namedrefs[i] = ref[5];
      i++;
    } else {
      nr=false;
    }
  } while (nr==true);
  return namedrefs;
}
 
function citeNamedRef() {
  namedrefs = getNamedRefs();
  if (namedrefs == '') {
    if (numforms != 0) {
      document.getElementById('citediv'+numforms).style.display = 'none';
    }
    numforms++;
    out = '<div id="citediv'+numforms+'"><fieldset>'+
      '<legend>References in text</legend>There are no named refs (<tt>&lt;ref name="Name"&gt;</tt>) in the text</fieldset></div>';
    document.getElementById('citeselect').innerHTML += out;
  }
  else {
    if (numforms != 0) {
      document.getElementById('citediv'+numforms).style.display = 'none';
    }
    numforms++;
    form = '<div id="citediv'+numforms+'">'+
      '<fieldset><legend>References in article</legend>'+
      '<table cellspacing="5">'+
      '<tr><td><label for="namedrefs">&nbsp;Named references in text</label></td>'+
            '<td><select name="namedrefs" id="namedrefs">';
    for (var i=0;i<namedrefs.length;i++) {
      form+= '<option value="'+namedrefs[i]+'">'+namedrefs[i]+'</option>';
    }
    form+= '</select>'+
      '</td></tr></table>'+
      '<input type="button" value="Add citation" onClick="addnamedcite()">'+
      '</fieldset></div>';
     document.getElementById('citeselect').innerHTML += form;
  }
}
 
function addnamedcite() {
  name = document.getElementById('citediv'+numforms).getElementsByTagName('select')[0].value;
  ref = '<ref name="'+name+'" />';
  insertTags(ref, '', '');
  document.getElementById('citediv'+numforms).style.display = 'none';  
}
 
hookEvent("load", refbuttons);