CiteTB.getNamedRef = function(refname, forinsert) {
if (forinsert) {
CiteTB.mainRefList.push( {'shorttag':true, 'refname':refname} );
}
return '';
};  
// Function to load the ref list
CiteTB.loadRefs = function {
if (CiteTB.refsLoaded) {
return;
}
CiteTB.getPageText(CiteTB.loadRefsInternal);
};  
// Function that actually loads the list from the page text
CiteTB.loadRefsInternal = function(text) {
// What this does: extract first name/group extract second name/group shorttag inner content
var refsregex = /< *ref(?: +(name|group) *= *(?:"([^"]*?)"|'([^']*?)'|([^ '"\/\>]*?)) *)? *(?: +(name|group) *= *(?:"([^"]*?)"|'([^']*?)'|([^ '"\/\>]*?)) *)? *(?:\/ *>|>((?:.|\n)*?)< *\/ *ref *>)/gim;
// This should work regardless of the quoting used for names/groups and for linebreaks in the inner content
while (true) {
var ref = refsregex.exec(text);
if (ref === null) {
break;
}
var refobj = {};
if (ref[9]) { // Content + short tag check
//alert('"'+ref[9]+'"');
refobj.content = ref[9];
refobj.shorttag = false;
} else {
refobj.shorttag = true;
}
if (ref[1] !== '') { // First name/group
if (ref[2]) {
refobj['ref'+ref[1]] = ref[2];
} else if (ref[3]) {
refobj['ref'+ref[1]] = ref[3];
} else {
refobj['ref'+ref[1]] = ref[4];
}
}
if (ref[5] !== '') { // Second name/group
if (ref[6]) {
refobj['ref'+ref[5]] = ref[6];
} else if (ref[7]) {
refobj['ref'+ref[5]] = ref[7];
} else {
refobj['ref'+ref[5]] = ref[8];
}
}
CiteTB.mainRefList.push(refobj);
}
CiteTB.refsLoaded = true;
CiteTB.setupErrorCheck;
CiteTB.setupNamedRefs;
};  
// AJAX FUNCTIONS
// Parse some wikitext and hand it off to a callback function
CiteTB.parse = function(text, callback) {
$.post( mw.util.wikiScript( 'api' ),
{action:'parse', title:mw.config.get('wgPageName'), text:text, prop:'text', format:'json'},
function(data) {
var html = data.parse.text['*'];
callback(html);
},
'json'
);
};  
// Use the API to expand templates on some text
CiteTB.expandtemplates = function(text, callback) {
$.post( mw.util.wikiScript( 'api' ),
{action:'expandtemplates', title:mw.config.get('wgPageName'), text:text, format:'json'},
function(data) {
var restext = data.expandtemplates['*'];
callback(restext);
},
'json'
);
};  
// Function to get the page text
CiteTB.getPageText = function(callback) {
var section = $("input[name='wpSection']").val;
if ( section !== '' ) {
var postdata = {action:'query', prop:'revisions', rvprop:'content', pageids:mw.config.get('wgArticleId'), format:'json'};
if (CiteTB.getOption('expandtemplates')) {
postdata.rvexpandtemplates = '1';
}
$.get( mw.util.wikiScript( 'api' ),
postdata,
function(data) {
var pagetext = data.query.pages[mw.config.get('wgArticleId').toString].revisions[0]['*'];
callback(pagetext);
},
'json'
);
} else {
if (CiteTB.getOption('expandtemplates')) {
CiteTB.expandtemplates($('#wpTextbox1').wikiEditor('getContents').text, callback);
} else {
callback($('#wpTextbox1').wikiEditor('getContents').text);
}
}
};  
// Autofill a template from an ID (ISBN, DOI, PMID, URL)
CiteTB.initAutofill = function {
var elemid = $(this).attr('id');
var res = /^cite\-auto\-(.*?)\-(.*)\-(.*)$/.exec(elemid);
var tem = res[1];
var field = res[2];
var autotype = res[3];
var id = $('#cite-'+tem+'-'+field).val;
if (!id) {
return false;
}
var url = '//tools.wmflabs.org/reftoolbar/lookup.php?';
url+=autotype+'='+encodeURIComponent(id);
url+='&template='+encodeURIComponent(tem);
var s = document.createElement('script');
s.setAttribute('src', url);
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);
return false;
};  
// Callback for autofill
//TODO: Autofill the URL, at least for DOI
CiteTB.autoFill = function(data, template, type) {
var cl = 'cite-'+template+'-';
var i,j, coauthors;
$('.'+cl+'title').val(data.title);
// Fill in authors
if (data.authors && data.authors.length > 0) {
if ($('.'+cl+'last-incr-1').length != 0) {
var classes = $('.'+cl+'last-incr-1').eq(0).attr('class').split(/\s+/);
var group = false;
var patt = /cite-[^-]*?-incr-(.*)/
for (var c=0; c<classes.length; c++) {
if (patt.exec(classes[c])) {
group = patt.exec(classes[c])[1];
break;
}
}
$('.'+cl+'last-incr-1').val(data.authors[0][0])
$('.'+cl+'first-incr-1').val(data.authors[0][1])
elemid = '#cite-incr-'+template+'-'+group;
for (var i=2; i<data.authors.length+1; i++) {
$(elemid).click;
$('.'+cl+'last-incr-'+i.toString).val(data.authors[i-1][0])
$('.'+cl+'first-incr-'+i.toString).val(data.authors[i-1][1])
}
} else if ($('.'+cl+'author-incr-1').length != 0) {
var classes = $('.'+cl+'author-incr-1').eq(0).attr('class').split(/\s+/);
var group = false;
var patt = /cite-[^-]*?-incr-(.*)/
for (var c=0; c<classes.length; c++) {
if (patt.exec(classes[c])) {
group = patt.exec(classes[c])[1];
break;
}
}
$('.'+cl+'author-incr-1').val(data.authors[0].join(', '))
elemid = '#cite-incr-'+template+'-'+group;
for (var i=2; i<data.authors.length+1; i++) {
$(elemid).click;
$('.'+cl+'author-incr-'+i.toString).val(data.authors[i-1].join(', '))
}
} else if ($('.'+cl+'last1').length != 0) {
for(i=0; data.authors && i<data.authors.length; i++) {
if ($('.'+cl+'last'+(i+1)).length) {
$('.'+cl+'last'+(i+1)).val(data.authors[i][0]);
$('.'+cl+'first'+(i+1)).val(data.authors[i][1]);
} else {
coauthors = [];
for(j=i; j<data.authors.length; j++) {
coauthors.push(data.authors[j].join(', '));
}
$('.'+cl+'coauthors').val(coauthors.join('; '));
break;
}
}
} else if($('.'+cl+'author1').length !== 0) {
for(i=0; data.authors && i<data.authors.length; i++) {
if ($('.'+cl+'author'+(i+1)).length) {
$('.'+cl+'author'+(i+1)).val(data.authors[i].join(', '));
} else {
coauthors = [];
for(j=i; j<data.authors.length; j++) {
coauthors.push(data.authors[j].join(', '));
}
$('.'+cl+'coauthors').val(coauthors.join('; '));
break;
}
}
} else {
var authors = [];
for(i=0; data.authors && i<data.authors.length; i++) {
authors.push(data.authors[i].join(', '));
}
$('.'+cl+'authors').val(authors.join('; '));
}
}  
if (type === 'pmid' || type === 'doi') {
if (data.date && (data.fulldate || !$('.'+cl+'month').length)) {
var DT = new Date(data.date);
var useday = /\d{4}-\d{2}-\d{2}/.test(data.date);
var usemonth = /\d{4}-\d{2}/.test(data.date);
$('.'+cl+'date').val(CiteTB.formatDate(DT, useday, usemonth));
} else if (!data.fulldate && $('.'+cl+'month').length && $('.'+cl+'year').length) {
if (data.month) { // lookup.php sets month to false if it isn't provided
$('.'+cl+'month').val( CiteTB.getOption('months')[parseInt(data.month)-1] );
}
$('.'+cl+'year').val(data.year);
} else {
$('.'+cl+'date').val(data.date);
}
$('.'+cl+'journal').val(data.journal);
$('.'+cl+'volume').val(data.volume);
$('.'+cl+'issue').val(data.issue);
$('.'+cl+'pages').val(data.pages);
if (type === 'pmid' && data.doi) {
$('.'+cl+'doi').val(data.doi);
}
} else if (type === 'isbn') {
$('.'+cl+'publisher').val(data.publisher);
$('.'+cl+'location').val(data.location);
$('.'+cl+'year').val(data.year);
$('.'+cl+'edition').val(data.edition);
} else if (type === 'url') {
if (data.date) {
var DT = new Date(data.date);
var useday = /\d{4}-\d{2}-\d{2}/.test(data.date) && template !== 'book';
var usemonth = /\d{4}-\d{2}/.test(data.date) && template !== 'book';
var yearOrDate = (template === 'book') ? 'year' : 'date';
$('.'+cl+yearOrDate).val(CiteTB.formatDate(DT, useday, usemonth));
}
$('.'+cl+'journal').val(data.journal);
$('.'+cl+'volume').val(data.volume);
$('.'+cl+'issue').val(data.issue);
$('.'+cl+'pages').val(data.pages);
$('.'+cl+'publisher').val(data.publisher);
$('.'+cl+'edition').val(data.edition);
$('.'+cl+'isbn').val(data.isbn);
$('.'+cl+'issn').val(data.issn);
$('.'+cl+'doi').val(data.doi);
// "en-US" isn't a valid value for the language parameter
if (data.language && data.language !== 'en-US' && data.language !== 'en-GB') {
$('.'+cl+'language').val(data.language);
}
$('.'+cl+'chapter').val(data.chapter);
}
};  
// FORM DIALOG FUNCTIONS
// Add new incrementable fields
CiteTB.incrementFields = function {
template = CiteTB.getOpenTemplate;
var currentrow = $(this).parents('tr')[0];