MediaWiki:RefToolbar.js: Difference between revisions
Content deleted Content added
Jason Quinn (talk | contribs) reverting after problems are appearing |
rm ws... |
||
(45 intermediate revisions by 14 users not shown) | |||
Line 1:
/*jshint smarttabs:true, loopfunc:true,forin:false*/
/*global mw, $ */
// TODO: 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:
}
// Only execute when editing/previewing wikitext pages
// TODO: Remove tests already done by [[MediaWiki:Gadget-refToolbar.js]]
if (
) {
// 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 33 ⟶ 38:
"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];
}
Line 60 ⟶ 66:
var $target = $('#wpTextbox1');
var temlist = {};
for (var t in CiteTB.Templates) {
var tem = CiteTB.Templates[t];
Line 72 ⟶ 76:
dialogobj['cite-dialog-'+sform] = {
resizeme: false,
id: 'citetoolbar-'+sform,
init: function() {},
html: tem.getInitial(),
dialog: {
width:
open: function() {
$(this).html(CiteTB.getOpenTemplate().getForm());
/** @param {jQuery.Event} e */
$('.cite-prev-parse').
e.preventDefault();
CiteTB.prevParseClick();
});
},
buttons: {
'cite-form-submit': function() {
var ref = CiteTB.getRef(false, true);
$(this).dialog( 'close' );
Line 102 ⟶ 97:
type: 'encapsulate',
options: {
}
}, $(this) );
Line 130 ⟶ 125:
}
};
try {
$target.wikiEditor('addDialog', dialogobj); } catch (e) {
// TypeError: range is null
}
//if (!CiteTB.getOption('modal')) {
//$('#citetoolbar-'+sform).dialog('option', 'modal', false);
Line 141 ⟶ 140:
'cites': {
type: 'toolbar',
groups: {
'template': {
Line 147 ⟶ 146:
'template': {
type: 'select',
list: temlist
}
Line 153 ⟶ 152:
},
'namedrefs': {
tools: {
'nrefs': {
Line 164 ⟶ 163:
section: 'cites',
group: 'namedrefs',
}
}
},
'errorcheck': {
tools: {
'echeck': {
Line 180 ⟶ 179:
section: 'cites',
group: 'errorcheck',
}
}
Line 191 ⟶ 190:
var defaultdialogs = {
'cite-toolbar-errorcheck': {
id: 'citetoolbar-errorcheck',
resizeme: false,
Line 220 ⟶ 219:
},
'cite-toolbar-namedrefs': {
resizeme: false,
id: 'citetoolbar-namedrefs',
Line 235 ⟶ 234:
'cite-form-submit': function() {
var refname = $("#cite-namedref-select").val();
if (refname === '') {
return;
}
$(this).dialog( 'close' );
$.wikiEditor.modules.toolbar.fn.doAction( $(this).data( 'context' ), {
type: 'encapsulate',
options: {
}
}, $(this) );
Line 260 ⟶ 253:
};
try {
$target.wikiEditor('addDialog', defaultdialogs);
} catch (e) {
// error occurred setting up wikieditor.
}
$('#citetoolbar-namedrefs').off('dialogopen');
if (!CiteTB.getOption('modal')) {
//$('#citetoolbar-namedrefs').dialog('option', 'modal', false);
Line 269 ⟶ 266:
"}");
}
try {
$target.wikiEditor('addToToolbar', refsection);
} catch (e) {
// error occurred setting up wikieditor.
}
};
Line 276 ⟶ 277:
switch( mw.config.get('wgUserLanguage') ) {
case 'de': // German
break;
default: // English
}
});
Line 290 ⟶ 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 296 ⟶ 297:
var refobj = {'shorttag':false};
if (!inneronly) {
var refname = $('#cite-'+CiteTB.escStr(template.shortform)+'-name').val();
res += '<ref';
Line 312 ⟶ 313:
}
var content ='{{'+templatename;
for( g in template.incrementables ) {
group = template.incrementables[g];
for (i=1; i<=group.val; i++) {
for (j=0; j<group.fields.length; j++) {
var fieldname = group.fields[j].field;
var fieldid = fieldname.replace('<N>', i.toString());
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldid).val();
if (field) {
content+=' |'+fieldid+'=';
content+= $.trim(field);
}
}
}
}
for( i=0; i<template.basic.length; i++ ) {
if (template.basic[i].increment_group) {
continue;
}
var fieldname = template.basic[i].field;
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val();
if (field) {
content+=' |'+fieldname+'=';
content+= $.trim(field
}
}
if ($('#cite-form-status').val() !== 'closed') {
for( i=0; i<template.extra.length; i++ ) {
if (template.extra[i].increment_group) {
continue;
}
var fieldname = template.extra[i].field;
var field = $('#cite-'+CiteTB.escStr(template.shortform)+'-'+fieldname).val();
if (field) {
content+=' |'+fieldname+'=';
content+= $.trim(field
}
}
Line 344 ⟶ 365:
// Make a reference to a named ref
CiteTB.getNamedRef = function(refname, forinsert) {
if (forinsert) {
CiteTB.mainRefList.push( {'shorttag':true, 'refname':refname} );
Line 366 ⟶ 386:
while (true) {
var ref = refsregex.exec(text);
if (ref === null) {
break;
}
Line 377 ⟶ 397:
refobj.shorttag = true;
}
if (ref[1] !== '') { // First name/group
if (ref[2]) {
refobj['ref'+ref[1]] = ref[2];
Line 386 ⟶ 406:
}
}
if (ref[5] !== '') { // Second name/group
if (ref[6]) {
refobj['ref'+ref[5]] = ref[6];
Line 430 ⟶ 450:
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 452 ⟶ 472:
};
// Safe version of decodeURIComponent() that doesn't throw exceptions.
// 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 463 ⟶ 492:
return false;
}
var url = '
// 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 477 ⟶ 509:
CiteTB.autoFill = function(data, template, type) {
var cl = 'cite-'+template+'-';
var i,j, coauthors;
$('.'+cl+'title').val(data.title);
// Fill in authors
if
if ($('.'+cl+'last-incr-1'
var classes
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 {
}
$('.'+cl+'
}
// Format partial dates of the format YYYY-MM, YYYY-MM-XX, or YYYY-MM-DD correctly
if ( data.date ) {
try {
var DT = new Date(data.date);
if ( /^\d{4}-\d{2}(-XX)?$/i.test(data.date) ) {
data.date = data.date.replace('-XX','');
$('.'+cl+'date').val(CiteTB.formatDate(DT, false, true));
} else if ( /^\d{4}-\d{2}-\d{2}?/i.test(data.date) ) {
$('.'+cl+'date').val(CiteTB.formatDate(DT, true, true));
} else {
$('.'+cl+'date').val(data.date);
}
} catch (e) {}
} 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+'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);
}
};
// FORM DIALOG FUNCTIONS
// Add new incrementable fields
CiteTB.incrementFields = function() {
template = CiteTB.getOpenTemplate();
var currentrow = $(this).parents('tr')[0];
$(this).prev().css('width', '100%')
$(this).detach();
var elemid = $(this).attr('id');
var res = /^cite\-incr\-(.*?)\-(.*)$/.exec(elemid);
group = res[2];
increments = template.incrementables[group];
fields = increments.fields;
incrval = increments.val+1;
template.incrementables[group].val += 1;
trs = template.makeFormInner(fields, false);
trs.reverse();
for (var i=0; i<trs.length; i++) {
$(currentrow).after(trs[i]);
}
};
// fill the accessdate param with the current date
CiteTB.fillAccessdate = function() {
Line 539 ⟶ 658:
var field = res[2];
var DT = new Date();
var datestr = CiteTB.formatDate(DT);
$('#cite-'+id+'-'+field).val(datestr);
return false;
};
CiteTB.formatDate = function(DT, useday, usemonth) {
if (typeof useday == "undefined") {
useday = true;
}
if (typeof usemonth == "undefined") {
usemonth = true;
}
var datestr = CiteTB.getOption('date format');
var zmonth = '';
Line 562 ⟶ 687:
}
date = date.toString();
if (useday) {
datestr = datestr.replace('<date>', date); datestr = datestr.replace('<
} else {
datestr = datestr.replace('<
datestr = datestr.replace('<
}
if (usemonth) {
datestr = datestr.replace('<month>', month);
datestr = datestr.replace('<zmonth>', zmonth);
datestr = datestr.replace('<monthname>', CiteTB.getOption('months')[DT.getUTCMonth()]);
} else {
datestr = datestr.replace('<month>', '');
datestr = datestr.replace('<zmonth>', '');
datestr = datestr.replace('<monthname>', '');
}
datestr = datestr.replace('<year>', DT.getUTCFullYear().toString());
return datestr.replace(/^[ \/\-\,\.]*(.*?)[ \/\-\,\.]*$/g, "$1"); // Cleanup any dangling spaces or connectors that might result from omitting date/month
};
Line 574 ⟶ 710:
// Until the list is loaded, its just a "Loading" placeholder
CiteTB.setupNamedRefs = function() {
var names = [], i;
for(
if (!CiteTB.mainRefList[i].shorttag && CiteTB.mainRefList[i].refname) {
names.push(CiteTB.mainRefList[i]);
Line 588 ⟶ 724:
var select = $('<select id="cite-namedref-select">');
select.append($('<option value="" />').text(mw.usability.getMsg('cite-named-refs-dropdown')));
for(
select.append($('<option />').text(names[i].refname));
}
Line 604 ⟶ 740:
$('#cite-namedref-parsed').after(link);
$("#cite-namedref-select").
$('#cite-nref-parse').
}
};
Line 613 ⟶ 749:
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 654 ⟶ 791:
$('#cite-parsed-label').hide();
$('#cite-namedref-parsed').text('');
if (choice === '') {
$('#cite-nref-preview-label').hide();
$('#cite-namedref-preview').text('');
Line 707 ⟶ 844:
};
// Resets form fields and previews
// Resets form fields and previews
CiteTB.resetForm = function() {
var template = CiteTB.getOpenTemplate();
var div = $("#citetoolbar-"+CiteTB.escStr(template.shortform));
div.
};
Line 723 ⟶ 855:
// Returns a string quoted as necessary for name/group attributes
CiteTB.getQuotedString = function(s) {
var sq = /\'/.test(s); // single quotes
var dq = /\"/.test(s); // double quotes
if (
return '"'+s+'"';
} else if (!dq) { // Can use double quotes
return '"'+s+'"';
Line 733 ⟶ 864:
return "'"+s+"'";
} else { // Has double and single quotes
s = s.replace(/\"/g, '\
return '"'+s+'"';
}
Line 744 ⟶ 875:
return s;
};
// Escape spaces and quotes for use in HTML classes/ids
CiteTB.escStr = function(s) {
Line 754 ⟶ 886:
var dialogs = $(".ui-dialog-content.ui-widget-content:visible");
var templatename = $(dialogs[0]).find(".cite-template").val();
return CiteTB.Templates[templatename];
};
Line 763 ⟶ 894:
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 773 ⟶ 905:
tr1.append(th1).append(th2);
table.append(tr1);
$('#cite-err-check-close').
if (errors.length === 0) {
var td = $('<td style="text-align:center; margin:1.5px;" />').html(mw.usability.getMsg('cite-err-report-empty'));
tr.append(td);
Line 784 ⟶ 916:
for(var e in errors) {
var err = errors[e];
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));
|