MediaWiki:RefToolbar.js: Difference between revisions

Content deleted Content added
rm ws...
 
(34 intermediate revisions by 11 users not shown)
Line 1:
/*jshint smarttabs:true, loopfunc:true,forin:false*/
/*global mw, $, importScript */
// TODO: make autodate an option in the CiteTemplate object, not a preference
 
Line 16:
}
 
// Only execute onwhen edit,editing/previewing unless it is a user JS/CSSwikitext pagepages
// TODO: Remove tests already done by [[MediaWiki:Gadget-refToolbar.js]]
if (
( mw.config.get(['wgActionedit') ===, 'editsubmit' ||].indexOf( mw.config.get('wgAction') =) !== 'submit' )-1 &&
( ( mw.config.get('wgNamespaceNumberwgPageContentModel') !=== 2 && mw.config.get('wgNamespaceNumberwikitext') !== 4 ) ||
( mw.config.get('wgPageName').indexOf('.js') === -1 && mw.config.get('wgPageName').indexOf('.css') === -1 ) )
) {
 
Line 77 ⟶ 76:
dialogobj['cite-dialog-'+sform] = {
resizeme: false,
titleMsgtitle: mw.message( 'cite-dialog-'+sform ).parse(),
id: 'citetoolbar-'+sform,
init: function() {},
html: tem.getInitial(),
dialog: {
width:675680,
open: function() {
$(this).html(CiteTB.getOpenTemplate().getForm());
/** @param {jQuery.Event} e */
$('.cite-prev-parse').bindon( 'click', function ( e ) {
e.preventDefault();
CiteTB.prevParseClick();
Line 126 ⟶ 125:
}
};
try {
$target.wikiEditor('addDialog', dialogobj);
} catch (e) {
// TypeError: range is null
}
//if (!CiteTB.getOption('modal')) {
//$('#citetoolbar-'+sform).dialog('option', 'modal', false);
Line 137 ⟶ 140:
'cites': {
type: 'toolbar',
labelMsglabel: mw.msg( 'cite-section-label' ),
groups: {
'template': {
Line 143 ⟶ 146:
'template': {
type: 'select',
labelMsglabel: mw.msg( 'cite-template-list' ),
list: temlist
}
Line 149 ⟶ 152:
},
'namedrefs': {
labelMsglabel: mw.msg( 'cite-named-refs-label' ),
tools: {
'nrefs': {
Line 160 ⟶ 163:
section: 'cites',
group: 'namedrefs',
labelMsglabel: mw.msg( 'cite-named-refs-button' )
}
}
},
'errorcheck': {
labelMsglabel: mw.msg( 'cite-errorcheck-label' ),
tools: {
'echeck': {
Line 176 ⟶ 179:
section: 'cites',
group: 'errorcheck',
labelMsglabel: mw.msg( 'cite-errorcheck-button' )
}
}
Line 187 ⟶ 190:
var defaultdialogs = {
'cite-toolbar-errorcheck': {
titleMsgtitle: mw.message( 'cite-errorcheck-label' ).parse(),
id: 'citetoolbar-errorcheck',
resizeme: false,
Line 216 ⟶ 219:
},
'cite-toolbar-namedrefs': {
titleMsgtitle: mw.message( 'cite-named-refs-title' ).parse(),
resizeme: false,
id: 'citetoolbar-namedrefs',
Line 250 ⟶ 253:
};
try {
$target.wikiEditor('addDialog', defaultdialogs);
$target.wikiEditor('addDialog', defaultdialogs);
$('#citetoolbar-namedrefs').unbind('dialogopen');
} catch (e) {
// error occurred setting up wikieditor.
}
$('#citetoolbar-namedrefs').off('dialogopen');
if (!CiteTB.getOption('modal')) {
//$('#citetoolbar-namedrefs').dialog('option', 'modal', false);
Line 259 ⟶ 266:
"}");
}
try {
$target.wikiEditor('addToToolbar', refsection);
$target.wikiEditor('addToToolbar', refsection);
} catch (e) {
// error occurred setting up wikieditor.
}
};
 
Line 266 ⟶ 277:
switch( mw.config.get('wgUserLanguage') ) {
case 'de': // German
importScriptmw.loader.load('/w/index.php?title=MediaWiki:RefToolbarMessages-de.js&action=raw&ctype=text/javascript');
break;
default: // English
importScriptmw.loader.load('/w/index.php?title=MediaWiki:RefToolbarMessages-en.js&action=raw&ctype=text/javascript');
}
});
Line 280 ⟶ 291:
// Actually assemble a ref from user input
CiteTB.getRef = function(inneronly, forinsert) {
var i, j, g, group;
var template = CiteTB.getOpenTemplate();
var templatename = template.templatename;
Line 286 ⟶ 297:
var refobj = {'shorttag':false};
if (!inneronly) {
var group = $('#cite-'+CiteTB.escStr(template.shortform)+'-group').val();
var refname = $('#cite-'+CiteTB.escStr(template.shortform)+'-name').val();
res += '<ref';
Line 310 ⟶ 321:
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldid).val();
if (field) {
content+=' |'+fieldid+'=';
content+= $.trim(field);
}
Line 323 ⟶ 334:
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val();
if (field) {
content+=' |'+fieldname+'=';
content+= $.trim(field);
}
Line 335 ⟶ 346:
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val();
if (field) {
content+=' |'+fieldname+'=';
content+= $.trim(field);
}
Line 461 ⟶ 472:
};
 
// Safe version of decodeURIComponent() that doesn't throw exceptions.
// Autofill a template from an ID (ISBN, DOI, PMID)
// If the native decodeURIComponent() threw an exception, the original string will be returned.
CiteTB.safeDecodeURIComponent = function(s) {
try {
s = decodeURIComponent(s);
} catch (e) {}
return s;
};
 
// Autofill a template from an ID (ISBN, DOI, PMID, URL)
CiteTB.initAutofill = function() {
var elemid = $(this).attr('id');
Line 472 ⟶ 492:
return false;
}
var url = '//toolsreftoolbar.wmflabstoolforge.org/reftoolbar/lookup.php?';
// Citoid expects minimally encoded input, so do some speculative decoding here to avoid
// 404 fetches. https://phabricator.wikimedia.org/T146539
id = CiteTB.safeDecodeURIComponent(id);
url+=autotype+'='+encodeURIComponent(id);
url+='&template='+encodeURIComponent(tem);
Line 488 ⟶ 511:
var i,j, coauthors;
$('.'+cl+'title').val(data.title);
// Fill in authors
if ($('.'+cl+'last-incr-1').length != 0) {
if (data.authors && data.authors.length > 0) {
var classes = $('.'+cl+'last-incr-1').eq(0).attr('class').split(/\s+/);
if ($('.'+cl+'last-incr-1').length != 0) {
var group = false;
var pattclasses = /cite-[^-]*?$('.'+cl+'last-incr-1').eq(0).*attr('class').split(/\s+/);
var group = false;
for (var c=0; c<classes.length; c++) {
ifvar (patt.exec(classes = /cite-[c^-]*?-incr-(.*)) {/
groupfor (var c=0; patt.exec(c<classes[.length; c]++)[1]; {
if (patt.exec(classes[c])) {
break;
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])
$('.'+cl+'last-incr-1').val(data.authors[0][0])
elemid = '#cite-incr-'+template+'-'+group;
for $('.'+cl+'first-incr-1').val(var i=2; i<data.authors.length+[0][1; i++]) {
elemid = '#cite-incr-'+template+'-'+group;
$(elemid).click();
$for ('.'+cl+'last-incr-'+var i=2; i.toString()).val(<data.authors[.length+1; i-1][0]++) {
$(elemid).click();
$('.'+cl+'firstlast-incr-'+i.toString()).val(data.authors[i-1][10])
$('.'+cl+'first-incr-'+i.toString()).val(data.authors[i-1][1])
}
}
} else if ($('.'+cl+'author-incr-1').length != 0) {
var classes =} else if ($('.'+cl+'author-incr-1').eq(length != 0).attr('class').split(/\s+/); {
var classes = $('.'+cl+'author-incr-1').eq(0).attr('class').split(/\s+/);
var group = false;
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;
}
}
$('.'+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 {
var coauthorsauthors = [];
for(j=i=0; jdata.authors && i<data.authors.length; ji++) {
coauthorsauthors.push(data.authors[ji].join(', '));
}
$('.'+cl+'coauthorsauthors').val(coauthorsauthors.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') {
// Format partial dates of the format YYYY-MM, YYYY-MM-XX, or YYYY-MM-DD correctly
if (type === 'doi' && (data.fulldate || $('.'+cl+'month').length)) {
if ( data.date ) {
try {
var DT = new Date(data.date);
varif useday =( /^\d{4}-\d{2}(-\d{2}XX)?$/i.test(data.date); ) {
var usemonth data.date = /\d{4}-\d{2}/.test(data.date.replace('-XX','');
$('.'+cl+'date').val(CiteTB.formatDate(DT, usedayfalse, usemonthtrue));
} else if (type === 'doi' && !/^\d{4}-\d{2}-\d{2}?/i.test(data.fulldatedate) ) {
$('.'+cl+'date').val(CiteTB.formatDate(DT, true, true));
if (data.month) { // lookup.php sets month to false if it isn't provided
} else {
$('.'+cl+'month').val( CiteTB.getOption('months')[parseInt(data.month)-1] );
$('.'+cl+'date').val(data.date);
}
} catch (e) {}
$('.'+cl+'year').val(data.year);
} else {
$('.'+cl+'date').val(data.date);
}
 
if (type === 'pmid' || type === 'doi') {
$('.'+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') {
$('.'+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);
}
};
Line 692 ⟶ 740:
$('#cite-namedref-parsed').after(link);
$("#cite-namedref-select").bindon( 'change', CiteTB.namedRefSelectClick);
$('#cite-nref-parse').bindon( 'click', CiteTB.nrefParseClick);
}
};
Line 807 ⟶ 855:
// Returns a string quoted as necessary for name/group attributes
CiteTB.getQuotedString = function(s) {
var sp = /\s/.test(s); // spaces
var sq = /\'/.test(s); // single quotes
var dq = /\"/.test(s); // double quotes
if (!sp && !sq && !dq) { // NoAlways quotes necessaryfor non-latin alphabet
return '"'+s+'"';
} else if (!dq) { // Can use double quotes
return '"'+s+'"';
Line 817 ⟶ 864:
return "'"+s+"'";
} else { // Has double and single quotes
s = s.replace(/\"/g, '\"'');
return '"'+s+'"';
}
Line 858 ⟶ 905:
tr1.append(th1).append(th2);
table.append(tr1);
$('#cite-err-check-close').bindon('click', function() { $('#cite-err-report').remove(); });
if (errors.length === 0) {
tr = $('<tr style="width:100%;" />');