MediaWiki:RefToolbar.js: Difference between revisions

Content deleted Content added
support formatting incomplete dates
Move initializeRefTools to a default gadget (step 3)
Line 1:
/*jshint smarttabs:true, loopfunc:true,forin:false*/
// Todo: make autodate an option in the CiteTemplate object, not a preference
/*global mw, $, importScript */
// TodoTODO: make autodate an option in the CiteTemplate object, not a preference
 
// Global object
// TODO:
// * Remove this once the page is moved to a module 'ext.gadget.refToolbarDialogs' depending on 'ext.gadget.refToolbarBase'
if (typeof CiteTB === 'undefined') {
var CiteTB = {
Line 12 ⟶ 16:
}
 
// onlyOnly loadexecute on edit, unless it is a user JS/CSS page
// TODO: Remove tests already done by [[MediaWiki:Gadget-refToolbar.js]]
if (
( mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit' ) &&
Line 19 ⟶ 24:
) {
 
// TODO: Move this to [[MediaWiki:Gadget-refToolbarDialogs.css]] and add it to the definition of module 'ext.gadget.refToolbarDialogs'
mw.util.addCSS(".cite-form-td {"+
"height: 0 !important;"+
Line 36 ⟶ 42:
// 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];
}
Line 61 ⟶ 67:
var $target = $('#wpTextbox1');
var temlist = {};
var d = new Date();
var start = d.getTime();
for (var t in CiteTB.Templates) {
var tem = CiteTB.Templates[t];
Line 236 ⟶ 240:
'cite-form-submit': function() {
var refname = $("#cite-namedref-select").val();
if (refname === '') {
return;
}
Line 277 ⟶ 281:
switch( mw.config.get('wgUserLanguage') ) {
case 'de': // German
var RefToolbarMessages = importScript('MediaWiki:RefToolbarMessages-de.js');
break;
default: // English
var RefToolbarMessages = importScript('MediaWiki:RefToolbarMessages-en.js');
}
});
Line 313 ⟶ 317:
}
var content ='{{'+templatename;
var fieldname, field;
for( i=0; i<template.basic.length; i++ ) {
var fieldname = template.basic[i].field;
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val();
if (field) {
content+='|'+fieldname+'=';
Line 323 ⟶ 328:
if ($('#cite-form-status').val() !== 'closed') {
for( i=0; i<template.extra.length; i++ ) {
var fieldname = template.extra[i].field;
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val();
if (field) {
content+='|'+fieldname+'=';
Line 345 ⟶ 350:
// Make a reference to a named ref
CiteTB.getNamedRef = function(refname, forinsert) {
var inner = 'name=';
if (forinsert) {
CiteTB.mainRefList.push( {'shorttag':true, 'refname':refname} );
Line 367 ⟶ 371:
while (true) {
var ref = refsregex.exec(text);
if (ref === null) {
break;
}
Line 378 ⟶ 382:
refobj.shorttag = true;
}
if (ref[1] !== '') { // First name/group
if (ref[2]) {
refobj['ref'+ref[1]] = ref[2];
Line 387 ⟶ 391:
}
}
if (ref[5] !== '') { // Second name/group
if (ref[6]) {
refobj['ref'+ref[5]] = ref[6];
Line 431 ⟶ 435:
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')) {
Line 478 ⟶ 482:
CiteTB.autoFill = function(data, template, type) {
var cl = 'cite-'+template+'-';
var i,j, coauthors;
$('.'+cl+'title').val(data.title);
if ($('.'+cl+'last1').length != 0) {
for(var 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 {
var coauthors = [];
for(var j=i; j<data.authors.length; j++) {
coauthors.push(data.authors[j].join(', '));
}
Line 494 ⟶ 499:
}
} else if($('.'+cl+'author1').length !== 0) {
for(var 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 coauthors = [];
for(var j=i; j<data.authors.length; j++) {
coauthors.push(data.authors[j].join(', '));
}
Line 508 ⟶ 513:
} else {
var authors = [];
for(var i=0; data.authors && i<data.authors.length; i++) {
authors.push(data.authors[i].join(', '));
}
Line 514 ⟶ 519:
}
if (type === 'pmid' || type === 'doi') {
if (type === 'doi' && (data.fulldate || $('.'+cl+'month').size() == 0length)) {
var DT = new Date(data.date);
var useday = /\d{4}-\d{2}-\d{2}/.test(data.date);
Line 547 ⟶ 552:
var field = res[2];
var DT = new Date();
var datestr = CiteTB.formatDate(DT);
$('#cite-'+id+'-'+field).val(datestr);
return false;
Line 588 ⟶ 593:
datestr = datestr.replace('<monthname>', CiteTB.getOption('months')[DT.getUTCMonth()]);
} else {
datestr = datestr.replace('<month>', '');
datestr = datestr.replace('<zmonth>', '');
datestr = datestr.replace('<monthname>', '');
Line 599 ⟶ 604:
// Until the list is loaded, its just a "Loading" placeholder
CiteTB.setupNamedRefs = function() {
var names = [], i;
for( var i=0; i<CiteTB.mainRefList.length; i++) {
if (!CiteTB.mainRefList[i].shorttag && CiteTB.mainRefList[i].refname) {
names.push(CiteTB.mainRefList[i]);
Line 613 ⟶ 618:
var select = $('<select id="cite-namedref-select">');
select.append($('<option value="" />').text(mw.usability.getMsg('cite-named-refs-dropdown')));
for(var i=0; i<names.length; i++) {
select.append($('<option />').text(names[i].refname));
}
Line 638 ⟶ 643:
var form = $('<div id="cite-errorcheck-heading" />').html(mw.usability.getMsg('cite-errorcheck-heading'));
var ul = $("<ul id='cite-errcheck-list' />");
var test;
for (var t in CiteTB.ErrorChecks) {
test = CiteTB.ErrorChecks[t];
Line 679 ⟶ 685:
$('#cite-parsed-label').hide();
$('#cite-namedref-parsed').text('');
if (choice === '') {
$('#cite-nref-preview-label').hide();
$('#cite-namedref-preview').text('');
Line 780 ⟶ 786:
var dialogs = $(".ui-dialog-content.ui-widget-content:visible");
var templatename = $(dialogs[0]).find(".cite-template").val();
var template = null;
return CiteTB.Templates[templatename];
};
Line 789 ⟶ 794:
var table = $('<table id="cite-err-report" style="width:100%; border:1px solid #A9A9A9; background-color:#FFEFD5; padding:0.25em; margin-top:0.5em" />');
$('#editpage-copywarn').before(table);
var tr;
var tr1 = $('<tr style="width:100%" />');
var th1 = $('<th style="width:60%; font-size:110%" />').html(mw.usability.getMsg('cite-err-report-heading'));
var th2 = $('<th style="text-align:right; width:40%" />');
var im = $('<img />').attr('src', '//upload.wikimedia.org/wikipedia/commons/thumb/5/55/Gtk-stop.svg/20px-Gtk-stop.svg.png');
im.attr('alt', mw.usability.getMsg('cite-err-report-close')).attr('title', mw.usability.getMsg('cite-err-report-close'));
var ad = $('<a id="cite-err-check-close" />').attr('href', '#');
Line 801 ⟶ 807:
$('#cite-err-check-close').bind('click', function() { $('#cite-err-report').remove(); });
if (errors.length === 0) {
var tr = $('<tr style="width:100%;" />');
var td = $('<td style="text-align:center; margin:1.5px;" />').html(mw.usability.getMsg('cite-err-report-empty'));
tr.append(td);
Line 810 ⟶ 816:
for(var e in errors) {
var err = errors[e];
var tr = $('<tr style="width:100%;" />');
var td1 = $('<td style="border: 1px solid black; margin:1.5px; width:60%" />').html(err.err);
var td2 = $('<td style="border: 1px solid black; margin:1.5px; width:40%" />').html(mw.usability.getMsg(err.msg));