User:Gerbrant/edit/citeHelper.js

// switch(wgAction) { case "edit": case "submit": new function {		var settings, self = this, myWin = null, chp = "Gerbrant-edit-citeHelper-"; try{ settings = Gerbrant.edit.blame;  }catch(nosettings){} if(!settings) settings = {};

function g(i){ return document.getElementById(chp + i).value; } function op(n, v){ if(v == undefined) v = g(n); return v && "\n|" + n + "=" + v; } function id(d){ return d && !/^[012]\d{3}-(?:0\d|1[012])-(?:[012]\d|3[01])$/.test(d); }

//To add parameters or change the validation, edit this. this.doit = function {			var u, t, a, l, f, d, y, m, c, x, il = g("doi"), i = encodeURI(il); //Let's hope encodeURI catches everything. if(!(u = g("url"))){ alert("The URL is not optional."); return } if(!(t = g("title"))){ alert("The title is not optional."); return } if((x = g("accessdate")) == ""){ alert("Please provide an access date."); return; } if(id(x)){ alert("Please provide a valid access date."); return; } if((a = g("author")) && ((l = g("last")) || (f = g("first")))){ alert("Please provide either the full author name or a separate last and first name."); return; } if((d = g("date")) && ((y = g("year")) || (m = g("month")))){ alert("Please provide either a full date or a separate year and optionally a month."); return; } if(id(d)){ alert("Please provide a valid date."); return; } if(id(c = g("archivedate"))){ alert("Please provide a valid archive date."); return; } if(m && !y){ alert("Please provide a year."); return; } if(il == i){ i = il; il = ""; } Gerbrant.edit.selection.replaceSelection(function(s){ return s + [ ""].join(""); }); }

//To change the interface, edit the table below. //In the future maybe add a dropdown for other citation templates? function buildWin {			var i = ' ' + i,			ps = '" size="', s = ' ', np = '"> ' + s, pp = ")  " + s, ymd = '40"> (yyyy-mm-dd' + pp, pf = ps + "64" + np, cs = pf + ' <td colspan="2" align="',			d = new Date, pad = function(s){s = s.toString; if(s.length < 2) return "0" + s; else return s;};			return [' '].join("");		}

this.show = function {			if(myWin == null) {				myWin = new Gerbrant.gui.window("Cite helper"); myWin.getContent.innerHTML = buildWin; }		}		this.caption = "Cite helper";

loadModules(["Gerbrant.edit.selection", "Gerbrant.gui.window", "Gerbrant.gui.toolbox"], function		{			module("Gerbrant.edit.citeHelper", self);			if(!settings.notInToolbox) Gerbrant.gui.toolbox.addObject(self);		}); }; } //