MediaWiki talk:RefToolbar.js/sandbox
// Todo: make autodate an option in the CiteTemplate object, not a preference
// Global object if (typeof CiteTB == 'undefined') {
var CiteTB = { "Templates" : {}, // All templates "Options" : {}, // Global options "UserOptions" : {}, // User options "DefaultOptions" : {}, // Script defaults "ErrorChecks" : {} // Error check functions };
}
// only load on edit, unless its a user JS/CSS page if ((wgAction == 'edit' || wgAction == 'submit') && !((wgNamespaceNumber == 2 || wgNamespaceNumber == 4) &&
(wgPageName.indexOf('.js') != -1 || wgPageName.indexOf('.css') != -1 ))) {
appendCSS(".cite-form-td {"+ "height: 0 !important;"+ "padding: 0.1em !important;"+ "}");
// Default options, these mainly exist so the script won't break if a new option is added CiteTB.DefaultOptions = {
"date format" : "<year>-<zmonth>-<zdate>", "autodate fields" : [], "months" : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], "modal" : true, "autoparse" : false, "expandtemplates": false
}; // Get an option - user settings override global which override defaults CiteTB.getOption = function(opt) {
if (CiteTB.UserOptions[opt] != undefined) { return CiteTB.UserOptions[opt]; } else if (CiteTB.Options[opt] != undefined) { return CiteTB.Options[opt]; } return CiteTB.DefaultOptions[opt];
}
CiteTB.init = function() {
/* Main stuff, build the actual toolbar structure * 1. get the template list, make the dropdown list and set up the template dialog boxes * 2. actually build the toolbar: * * A section for cites * ** dropdown for the templates (previously defined) * ** button for named refs with a dialog box * ** button for errorcheck * 3. add the whole thing to the main toolbar */
if (typeof $j('div[rel=cites]')[0] != 'undefined') { // Mystery IE bug workaround return; } $j('head').trigger('reftoolbarbase'); var $target = $j('#wpTextbox1'); var temlist = {}; var d = new Date(); var start = d.getTime(); for (var t in CiteTB.Templates) { var tem = CiteTB.Templates[t]; sform = CiteTB.escStr(tem.shortform); var actionobj = { type: 'dialog', module: 'cite-dialog-'+sform }; var dialogobj = {}; dialogobj['cite-dialog-'+sform] = { resizeme: false, titleMsg: 'cite-dialog-'+sform, id: 'citetoolbar-'+sform, init: function() {}, html: tem.getInitial(), dialog: { width:675, open: function() { $j(this).html(CiteTB.getOpenTemplate().getForm()); $j('.cite-prev-parse').bind( 'click', CiteTB.prevParseClick); }, beforeclose: function() { CiteTB.resetForm(); }, buttons: { 'cite-form-submit': function() { $j.wikiEditor.modules.toolbar.fn.doAction( $j(this).data( 'context' ), { type: 'encapsulate', options: { peri: ' ' } }, $j(this) ); var ref = CiteTB.getRef(false, true); $j(this).dialog( 'close' ); $j.wikiEditor.modules.toolbar.fn.doAction( $j(this).data( 'context' ), { type: 'encapsulate', options: { pre: ref } }, $j(this) ); }, 'cite-form-showhide': CiteTB.showHideExtra, 'cite-refpreview': function() { var ref = CiteTB.getRef(false, false); var template = CiteTB.getOpenTemplate(); var div = $j("#citetoolbar-"+CiteTB.escStr(template.shortform)); div.find('.cite-preview-label').show(); div.find('.cite-ref-preview').text(ref).show(); if (CiteTB.getOption('autoparse')) { CiteTB.prevParseClick(); } else { div.find('.cite-prev-parse').show(); div.find('.cite-prev-parsed-label').hide(); div.find('.cite-preview-parsed').html(); } }, 'wikieditor-toolbar-tool-link-cancel': function() { $j(this).dialog( 'close' ); }, 'cite-form-reset': function() { CiteTB.resetForm(); } } } }; $target.wikiEditor('addDialog', dialogobj); if (!CiteTB.getOption('modal')) { //$j('#citetoolbar-'+sform).dialog('option', 'modal', false); } temlist[sform] = {label: tem.templatename, action: actionobj }; }
var refsection = { 'sections': { 'cites': { type: 'toolbar', labelMsg: 'cite-section-label', groups: { 'template': { tools: { 'template': { type: 'select', labelMsg: 'cite-template-list', list: temlist } } }, 'namedrefs': { labelMsg: 'cite-named-refs-label', tools: { 'nrefs': { type: 'button', action: { type: 'dialog', module: 'cite-toolbar-namedrefs' }, icon: '/media/wikipedia/commons/thumb/b/be/Nuvola_clipboard_lined.svg/22px-Nuvola_clipboard_lined.svg.png', section: 'cites', group: 'namedrefs', labelMsg: 'cite-named-refs-button' } } }, 'errorcheck': { labelMsg: 'cite-errorcheck-label', tools: { 'echeck': { type: 'button', action: { type: 'dialog', module: 'cite-toolbar-errorcheck' }, icon: '/media/wikipedia/commons/thumb/a/a3/Nuvola_apps_korganizer-NO.png/22px-Nuvola_apps_korganizer-NO.png', section: 'cites', group: 'errorcheck', labelMsg: 'cite-errorcheck-button' } } } } } } }; var defaultdialogs = { 'cite-toolbar-errorcheck': { titleMsg: 'cite-errorcheck-label', id: 'citetoolbar-errorcheck', resizeme: false, init: function() {},
html: '
'<img src="/media/wikipedia/commons/4/42/Loading.gif" />'+' '+mw.usability.getMsg('cite-loading')+'
',
dialog: { width:550, open: function() { CiteTB.loadRefs(); }, buttons: { 'cite-errorcheck-submit': function() { var errorchecks = $j("input[name='cite-err-test']:checked"); var errors = []; for (var i=0; i<errorchecks.length; i++) { errors = errors.concat(CiteTB.ErrorChecks[$j(errorchecks[i]).val()].run()); } CiteTB.displayErrors(errors); $j(this).dialog( 'close' ); }, 'wikieditor-toolbar-tool-link-cancel': function() { $j(this).dialog( 'close' ); } } } }, 'cite-toolbar-namedrefs': { titleMsg: 'cite-named-refs-title', resizeme: false, id: 'citetoolbar-namedrefs',
html: '
'<img src="/media/wikipedia/commons/4/42/Loading.gif" />'+' '+mw.usability.getMsg('cite-loading')+'
',
init: function() {}, dialog: { width: 550, open: function() { CiteTB.loadRefs(); }, buttons: { 'cite-form-submit': function() { var refname = $j("#cite-namedref-select").val(); if (refname == ) { return; } $j.wikiEditor.modules.toolbar.fn.doAction( $j(this).data( 'context' ), { type: 'encapsulate', options: { peri: ' ' } }, $j(this) ); $j(this).dialog( 'close' ); $j.wikiEditor.modules.toolbar.fn.doAction( $j(this).data( 'context' ), { type: 'encapsulate', options: { pre: CiteTB.getNamedRef(refname, true) } }, $j(this) ); }, 'wikieditor-toolbar-tool-link-cancel': function() { $j(this).dialog( 'close' ); } } } } }; $target.wikiEditor('addDialog', defaultdialogs); $j('#citetoolbar-namedrefs').unbind('dialogopen'); if (!CiteTB.getOption('modal')) { //$j('#citetoolbar-namedrefs').dialog('option', 'modal', false); //$j('#citetoolbar-errorcheck').dialog('option', 'modal', false); appendCSS(".ui-widget-overlay {"+ "display:none !important;"+ "}"); } $target.wikiEditor('addToToolbar', refsection);
}
// Load local data - messages, cite templates, etc. $j(document).ready( function() {
switch( wgUserLanguage ) { case 'de': // German var RefToolbarMessages = importScript('MediaWiki:RefToolbarMessages-de.js'); break; default: // English var RefToolbarMessages = importScript('MediaWiki:RefToolbarMessages-en.js'); }
});
// Setup the main object CiteTB.mainRefList = []; CiteTB.refsLoaded = false;
// REF FUNCTIONS // Actually assemble a ref from user input CiteTB.getRef = function(inneronly, forinsert) {
var template = CiteTB.getOpenTemplate(); var templatename = template.templatename; var res = ; var refobj = {'shorttag':false}; if (!inneronly) { var group = $j('#cite-'+CiteTB.escStr(template.shortform)+'-group').val(); var refname = $j('#cite-'+CiteTB.escStr(template.shortform)+'-name').val(); res += '<ref'; if (refname) { refname = $j.trim(refname); res+=' name='+CiteTB.getQuotedString(refname); refobj.refname = refname; } if (group) { group = $j.trim(group); res+=' group='+CiteTB.getQuotedString(group); refobj.refgroup = group; } res+='>'; } var content ='{{'+templatename; for( var i=0; i<template.basic.length; i++ ) { var fieldname = template.basic[i].field; var field = $j('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val(); if (field) { content+='|'+fieldname+'='; content+= $j.trim(field.replace("|", "|")); } } if ($j('#cite-form-status').val() != 'closed') { for( var i=0; i<template.extra.length; i++ ) { var fieldname = template.extra[i].field; var field = $j('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val(); if (field) { content+='|'+fieldname+'='; content+= $j.trim(field.replace("|", "|")); } } } content+= '}}'; res+=content; refobj.content = content; if (!inneronly) { res+= '</ref>'; } if (forinsert) { CiteTB.mainRefList.push(refobj); } return res;
}
// Make a reference to a named ref CiteTB.getNamedRef = function(refname, forinsert) {
var inner = 'name='; if (forinsert) { CiteTB.mainRefList.push( {'shorttag':true, 'refname':refname} ); } return '[1]';
}
// 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) {
$j.post( wgServer+wgScriptPath+'/api.php', {action:'parse', title: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) {
$j.post( wgServer+wgScriptPath+'/api.php', {action:'expandtemplates', title: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 = $j("input[name='wpSection']").val(); if ( section != ) { var postdata = {action:'query', prop:'revisions', rvprop:'content', pageids:wgArticleId, format:'json'}; if (CiteTB.getOption('expandtemplates')) { postdata['rvexpandtemplates'] = '1'; } $j.get( wgServer+wgScriptPath+'/api.php', postdata, function(data) { var pagetext = data['query']['pages'][wgArticleId.toString()]['revisions'][0]['*']; callback(pagetext); }, 'json' ); } else { if (CiteTB.getOption('expandtemplates')) { CiteTB.expandtemplates($j('#wpTextbox1').wikiEditor('getContents').text(), callback); } else { callback($j('#wpTextbox1').wikiEditor('getContents').text()); } }
}
// Autofill a template from an ID (ISBN, DOI, PMID) CiteTB.initAutofill = function() {
var elemid = $j(this).attr('id'); var res = /^cite\-auto\-(.*?)\-(.*)\-(.*)$/.exec(elemid); var tem = res[1]; var field = res[2]; var autotype = res[3]; var id = $j('#cite-'+tem+'-'+field).val(); if (!id) { return false; } var url = 'http://toolserver.org/~alexz/ref/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+'-'; $j('.'+cl+'title').val(data.title); if ($j('.'+cl+'last1')) { for(var i=0; i<data.authors.length; i++) {
if ($j('.'+cl+'last'+(i+1)).length) { $j('.'+cl+'last'+(i+1)).val(data.authors[i][0]); $j('.'+cl+'first'+(i+1)).val(data.authors[i][1]); } else { var coauthors = []; for(var j=i; j<data.authors.length; j++) { coauthors.push(data.authors[j].join(', ')); } $j('.'+cl+'coauthors').val(coauthors.join(', ')); break; } }
} else if($j('.'+cl+'author1')) { for(var i=0; i<data.authors.length; i++) {
if ($j('.'+cl+'author'+(i+1)).length) { $j('.'+cl+'author'+(i+1)).val(data.authors[i].join(', ')); } else { var coauthors = []; for(var j=i; j<data.authors.length; j++) { coauthors.push(data.authors[j].join(', ')); } $j('.'+cl+'coauthors').val(coauthors.join(', ')); break; } }
} else { var authors = [];
for(var i=0; j<data.authors.length; j++) { authors.push(data.authors[j].join(', ')); } $j('.'+cl+'authors').val(authors.join(', '));
} if (type == 'pmid' || type == 'doi') { if (type == 'doi') { var DT = new Date(data.date); $j('.'+cl+'date').val(CiteTB.formatDate(DT)); } else { $j('.'+cl+'date').val(data.date); } $j('.'+cl+'journal').val(data.journal); $j('.'+cl+'volume').val(data.volume); $j('.'+cl+'issue').val(data.issue); $j('.'+cl+'pages').val(data.pages); } else if (type == 'isbn') { $j('.'+cl+'publisher').val(data.publisher); $j('.'+cl+'location').val(data.location); $j('.'+cl+'year').val(data.year); $j('.'+cl+'edition').val(data.edition); }
}
// FORM DIALOG FUNCTIONS // fill the accessdate param with the current date CiteTB.fillAccessdate = function() {
var elemid = $j(this).attr('id'); var res = /^cite\-date\-(.*?)\-(.*)$/.exec(elemid); var id = res[1]; var field = res[2]; var DT = new Date(); datestr = CiteTB.formatDate(DT); $j('#cite-'+id+'-'+field).val(datestr); return false;
}
CiteTB.formatDate = function(DT) {
var datestr = CiteTB.getOption('date format'); 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>', date); datestr = datestr.replace('<month>', month); datestr = datestr.replace('<zdate>', zdate); datestr = datestr.replace('<zmonth>', zmonth); datestr = datestr.replace('<monthname>', CiteTB.getOption('months')[DT.getUTCMonth()]); datestr = datestr.replace('<year>', DT.getUTCFullYear().toString()); return datestr;
}
// Function called after the ref list is loaded, to actually set the contents of the named ref dialog // Until the list is loaded, its just a "Loading" placeholder CiteTB.setupNamedRefs = function() {
var names = [] for( var i=0; i<CiteTB.mainRefList.length; i++) { if (!CiteTB.mainRefList[i].shorttag && CiteTB.mainRefList[i].refname) { names.push(CiteTB.mainRefList[i]); } }
var stuff = $j('
$j('#citetoolbar-namedrefs').html( stuff ); if (names.length == 0) { stuff.html(mw.usability.getMsg('cite-no-namedrefs')); } else { stuff.html(mw.usability.getMsg('cite-namedrefs-intro')); var select = $j('<select id="cite-namedref-select">'); select.append($j('<option value="" />').text(mw.usability.getMsg('cite-named-refs-dropdown'))); for(var i=0; i<names.length; i++) { select.append($j('<option />').text(names[i].refname)); } stuff.after(select); select.before('var prevlabel = $j('
');