User:Apoc2400/refToolbar.js

// var numforms = 0; var wikEdAutoUpdateUrl; var citeUserDateFormat; function refbuttons { if (typeof mwCustomEditButtons !== 'undefined' && document.getElementById('toolbar') ) { 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); var toolbar = document.getElementById('toolbar'); if (navigator.userAgent.indexOf('MSIE') == -1) { if (toolbar.style != null) { toolbar.style.height = 'auto'; }     else { toolbar.setAttribute('style', 'margin-bottom: 6px; height: auto;'); }     toolbar.appendChild(button); citemain = document.createElement('div'); citemain.style.display = 'none'; citemain.style.margin = '0.2em 0 -0.2em 0'; 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("dispErrors", "Error check") ); citemain.appendChild( addOption("showMore", "More") ); citemain.appendChild( addOption("hideInitial", "Cancel") ); citemore = document.createElement('div'); citemore.style.display = 'none'; citemore.setAttribute('Id', 'citemore'); citemore.appendChild( addOption("citeEncyclopedia", "Encyclopedia") ); citemore.appendChild( addOption("citePressRelease", "Press release") ); citemore.appendChild( addOption("citeMap", "Map") ); citemore.appendChild( addOption("showRefSectionOptions", "Ref Section") ); citemain.appendChild(citemore); toolbar.appendChild(citemain); }   else { toolbar.appendChild(button); selection = ' '+ ''+ ''+ ''+ ''+ ''+ ''+ ''+ ' \     \ \ \ \ '+     ' ';      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'; oldFormHide; }

function oldFormHide { if (numforms != 0) { document.getElementById('citediv'+numforms).style.display = 'none'; } if (document.getElementById('errorform') != null) { document.getElementById('citeselect').removeChild(document.getElementById('errorform')); } }

function easyCiteMain { document.getElementById('citeselect').style.display = ''; document.getElementById('citemore').style.display = 'none'; }

function showMore { document.getElementById('citemore').style.display = ''; }

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var citeGlobalDateFormat = "  "; function getTime { var datestr = ''; if (citeUserDateFormat) { datestr = citeUserDateFormat; } else { datestr = citeGlobalDateFormat; } var DT = new Date; var zmonth = ''; var month = DT.getUTCMonth+1; if (month < 10) { zmonth = "0"+month.toString; } else { zmonth = month.toString; } month = month.toString; var zdate = ''; var date = DT.getUTCDate if (date < 10) { zdate = "0"+date.toString; } else { zdate = date.toString; } date = date.toString datestr = datestr.replace(' ', date); datestr = datestr.replace(' ', month); datestr = datestr.replace(' ', zdate); datestr = datestr.replace(' ', zmonth); datestr = datestr.replace(' ', months[DT.getUTCMonth]); datestr = datestr.replace(' ', DT.getUTCFullYear.toString); return (datestr); }

function citeWeb { citeNewsWeb("cite web"); } function citeNews { citeNewsWeb("cite news"); }

function citeNewsWeb(templatename) { oldFormHide; template = templatename; var legend; if (template == "cite web") { legend = "Cite web source"; } else { legend = "Cite news source"; } newtime = getTime; numforms++; form = ''+ ' '+legend+' '+ ' '+   ''+ ' [Template documentation] '+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function citeBook { oldFormHide; template = "cite book"; numforms++; form = '<div id="citediv'+numforms+'">'+ ' Cite book source '+ ' '+   '<input type="button" value="Add citation" onClick="addcites">'+ ' <a href="http://en.wikipedia.org/wiki/Template:'+template.replace(/ /g, '_')+'" target="_blank">[Template documentation]</a> '+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function citeJournal { oldFormHide; template = "cite journal"; numforms++; form = '<div id="citediv'+numforms+'">'+ ' Cite journal article '+ ' '+   '<input type="button" value="Add citation" onClick="addcites">'+ ' <a href="http://en.wikipedia.org/wiki/Template:'+template.replace(/ /g, '_')+'" target="_blank">[Template documentation]</a> '+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function citeEncyclopedia { oldFormHide; template = "cite encyclopedia"; numforms++; form = '<div id="citediv'+numforms+'">'+ ' Cite encyclopedia source '+ ' '+   '<input type="button" value="Add citation" onClick="addcites">'+ ' <a href="http://en.wikipedia.org/wiki/Template:'+template.replace(/ /g, '_')+'" target="_blank">[Template documentation]</a> '+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function citePressRelease { oldFormHide; template = "cite press release"; numforms++; form = '<div id="citediv'+numforms+'">'+ ' Cite press release '+ ' '+   '<input type="button" value="Add citation" onClick="addcites">'+ ' <a href="http://en.wikipedia.org/wiki/Template:'+template.replace(/ /g, '_')+'" target="_blank">[Template documentation]</a> '+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function citeMap { oldFormHide; template = "cite map"; numforms++; form = '<div id="citediv'+numforms+'">'+ ' Cite map '+ ' '+   '<input type="button" value="Add citation" onClick="addcites">'+ ' <a href="http://en.wikipedia.org/wiki/Template:'+template.replace(/ /g, '_')+'" target="_blank">[Template documentation]</a> '+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function showRefSectionOptions { oldFormHide; template = "cite encyclopedia"; numforms++; form = '<div id="citediv'+numforms+'">'+ ' Add references section \ Headline: \	<input id="references" name="headline" type="radio" tabindex=1 checked="checked"> == References == \	<input id="notes" name="headline" type="radio" tabindex=1> == Notes == \	Type: \	<input id="type-references" name="type" type="radio" tabindex=1><label for="type-references">&lt;references/&gt; \	<input id="type-reflist" name="type" type="radio" tabindex=1 checked="checked"><label for="type-reflist"> \	<input id="type-reflist2" name="type" type="radio" tabindex=1><label for="type-reflist2"> \	<input type="checkbox" tabindex=1 id="ldr"> List-defined references (<a href="http://en.wikipedia.org/wiki/Wikipedia:Footnotes#List-defined_references" target="_blank">Info 1</a>, <a href="http://en.wikipedia.org/wiki/Help:Footnotes#List-defined_references" target="_blank">Info 2</a>) \   <input type="button" value="Add references section" onClick="addRefSection">'+ ' ';  document.getElementById('citeselect').innerHTML += form; }

function addcites(template) { cites = document.getElementById('citediv'+numforms).getElementsByTagName('input'); var template = ''; var citebegin = '<ref'; var citename = ''; var citeinner = ''; for (var i=0; i<cites.length-1; i++) { var citeid = cites[i].id; var citevalue = cites[i].value; citevalue = citevalue.replace(/^\s*(.*?)\s*$/, "$1");   //Trim leading and trailing whitespace if (citevalue != '') { if (citeid == "refname") { citebegin += ' name="' + citevalue + '"'; }     else if (citeid == "template") { citename = '>{{' + citevalue; template = citevalue; }     else { if (citeid == "pages") { if (citevalue.match(/^\w+$/) && template != 'cite encyclopedia') { citeid = "page";   //Use page= instead of pages= if only one page. Makes p. 5 instead of pp. 5. }         else { citevalue = citevalue.replace(/-/g, "–");   //Replace hyphens with en dashes WP:ENDASH citevalue = citevalue.replace(/,\s*/g, ", "); //One space after each comma }       }        else if (citeid == "date" && citevalue.match(/^\d\d\d\d$/)) { citeid = "year"; // Use year= instead of date= if only the year is specified }       citeinner += "|" + citeid + "=" + citevalue; }   }  }  cite = citebegin + citename + citeinner + "}} "; insertTags(cite, , ); document.getElementById('citediv'+numforms).style.display = 'none'; }

function addRefSection { var wikicode = "\n"; if (document.getElementById('references').checked) { wikicode += "== References ==\n"; }	else if (document.getElementById('notes').checked) { wikicode += "== Notes ==\n"; }	else {alert('No headline selected!');} if (document.getElementById('type-references').checked) { if (document.getElementById('ldr').checked) { wikicode += " \n"; }		else { wikicode += " '+     '<input type="button" value="Add citation" onClick="addnamedcite">'+      ' ';     document.getElementById('citeselect').innerHTML += form;  } }

function addnamedcite { name = document.getElementById('citediv'+numforms).getElementsByTagName('select')[0].value; ref = ' '; insertTags(ref, , ); document.getElementById('citediv'+numforms).style.display = 'none'; }

function getAllRefs { if (typeof(wikEdUseWikEd) != 'undefined') { if (wikEdUseWikEd == true) { WikEdUpdateTextarea; } }  text = document.getElementById('wpTextbox1').value; regex = /< *?ref( +?name *?= *?(('([^']*?)')|("([^"]*?)")|([^'"\s]*?[^\/]\b)))? *?>((.|\n)*?)< *?\/? *?ref *?>/gim //" var allrefs = new Array;  var i=0;  var nr=true;  do {    ref = regex.exec(text);    if(ref != null){      if (ref[0].search(/[^\s]{150}/) != -1) {        ref[0] = ref[0].replace(/\|([^\s])/g, "| $1");      }      ref[0] = ref[0].replace(/</g, "&lt;");      ref[0] = ref[0].replace(/>/g, "&gt;");      allrefs[i] = ref[0];      i++;    } else {      nr=false;    }  } while (nr==true);  return allrefs; }

function NRcallError(namedrefs, refname) { for (var i=0; i<namedrefs.length; i++) { if (namedrefs[i] == refname) { return true; } }  return false; }

function errorCheck { var allrefs = getAllRefs; var allrefscontent = new Array; var samecontentexclude = new Array; var sx=0; var templateexclude = new Array; var tx=0; var skipcheck = false; var namedrefcalls = getNamedRefs(true); for (var i=0; i<allrefs.length; i++) { allrefscontent[i] = allrefs[i].replace(/&lt; *?ref( +?name *?= *?(('([^']*?)')|("([^"]*?)")|([^'"\s]*?[^\/]\b)))? *?&gt;((.|\n)*?)&lt; *?\/? *?ref *?&gt;/gim, "$8"); //"  }  var namedrefs = getNamedRefs(false);  var errorlist = new Array;  var q=0;  unclosed = document.getElementById('unclosed').checked;  samecontent = document.getElementById('samecontent').checked;  templates = document.getElementById('templates').checked;  repeated = document.getElementById('repeated').checked;  undef = document.getElementById('undef').checked;  for (var i=0; i<allrefs.length; i++) {    if (allrefs[i].search(/&lt; *?\/ *?ref *?&gt;/) == -1 && unclosed) {      errorlist[q] = ' <td width="75%"><tt>'+allrefs[i]+'</tt> ';      errorlist[q] += '<td width="25%">Unclosed <tt>&lt;ref&gt;</tt> tag  ';      q++;    }    if (samecontent) {      for (var d=0; d<samecontentexclude.length; d++) {        if (allrefscontent[i] == samecontentexclude[d]) { skipcheck = true; }     }      var p=0; while (p<allrefs.length && !skipcheck) { if (allrefscontent[i] == allrefscontent[p] && i != p) { errorlist[q] = ' <td width="75%"><tt>'+allrefscontent[i]+'</tt> '; errorlist[q] += '<td width="25%">Multiple refs contain this content, a <a href="http://en.wikipedia.org/wiki/Wikipedia:Footnotes#Naming_a_ref_tag_so_it_can_be_used_more_than_once">named reference</a> should be used instead '; q++; samecontentexclude[sx] = allrefscontent[i] sx++; break; }       p++; }    skipcheck=false; }   if (templates) { if (allrefscontent[i].search(/\{\{cite/i) == -1 && allrefscontent[i].search(/\{\{citation/i) == -1 && allrefscontent[i].search(/\{\{Comic (book|strip) reference/i) == -1 && allrefscontent[i].search(/\{\{Editorial cartoon reference/i) == -1 && allrefscontent[i].search(/\{\{harv/i) == -1) { for (var x=0; x<templateexclude.length; x++) { if (allrefscontent[i] == templateexclude[x]) { skipcheck = true; }       }        if (!skipcheck) { errorlist[q] = ' <td width="75%"><tt>'+allrefs[i]+'</tt> '; errorlist[q] += '<td width="25%">Does not use a <a href="http://en.wikipedia.org/wiki/Wikipedia:Citation_templates">citation template</a> '; q++; templateexclude[tx] = allrefscontent[i]; tx++; }       skipcheck = false; }   }  }  if (repeated) { var repeatnameexclude = new Array; var rx=0; for (var k=0; k<namedrefs.length; k++) { for (var d=0; d<repeatnameexclude.length; d++) { if (namedrefs[k] == repeatnameexclude[d]) { skipcheck = true; }     }      var z=0; while (z<namedrefs.length && !skipcheck) { if (namedrefs[k] == namedrefs[z] && k != z) { errorlist[q] = ' <td width="75%"><tt>'+namedrefs[k]+'</tt> '; errorlist[q] += '<td width="25%">Multiple references are given the same <a href="http://en.wikipedia.org/wiki/Wikipedia:Footnotes#Naming_a_ref_tag_so_it_can_be_used_more_than_once">name</a> '; q++; repeatnameexclude[rx] = namedrefs[z]; rx++; break; }       z++; }    skipcheck = false; } }  if (undef) { var undefexclude = new Array; var ux=0; for (var p=0; p<namedrefcalls.length; p++) { for (var d=0; d<undefexclude.length; d++) { if (allrefscontent[i] == undefexclude[d]) { skipcheck = true; }     }      if (!skipcheck) { if (!NRcallError(namedrefs, namedrefcalls[p])) { errorlist[q] = ' <td width="75%"><tt>'+namedrefcalls[p]+'</tt> '; errorlist[q] += '<td width="25%">A <a href="http://en.wikipedia.org/wiki/Wikipedia:Footnotes#Naming_a_ref_tag_so_it_can_be_used_more_than_once">named reference</a> is used but not defined '; q++; undefexclude[ux] = namedrefs[p]; ux++; }     }      skipcheck = false; } } if (q > 0) { return errorlist; } else { return 0; } }

function dispErrors { oldFormHide; form = ' '+ ' Error checking '+ 'Check for: '+ '<input type="checkbox" id="unclosed" /> Unclosed <tt>&lt;ref&gt;</tt> tags '+ '<input type="checkbox" id="samecontent" /> References with the same content '+ '<input type="checkbox" id="templates" /> References not using a <a href="http://en.wikipedia.org/wiki/Wikipedia:Citation_templates">citation template</a> '+ '<input type="checkbox" id="repeated" /> Multiple references with the same name '+ '<input type="checkbox" id="undef" /> Usage of undefined named references '+ '<input type="button" id="errorchecksubmit" value="Check for selected errors" onclick="doErrorCheck"/>'+ ' '; document.getElementById('citeselect').innerHTML += form; }

function doErrorCheck { var errors = errorCheck; document.getElementById('citeselect').removeChild(document.getElementById('errorform')); if (errors == 0) { if (numforms != 0) { document.getElementById('citediv'+numforms).style.display = 'none'; }   numforms++; out = '<div id="citediv'+numforms+'"> '+ ' Error checking No errors found. ';   document.getElementById('citeselect').innerHTML += out; } else { if (numforms != 0) { document.getElementById('citediv'+numforms).style.display = 'none'; }   numforms++; form = '<div id="citediv'+numforms+'">'+ ' Error checking '+ ' '+     ' ';     document.getElementById('citeselect').innerHTML += form; } }

$(window).on("load", refbuttons); //