MediaWiki:Gadget-refToolbarBase.js: Difference between revisions

Content deleted Content added
fix for issue reported on talk page
 
(12 intermediate revisions by 5 users not shown)
Line 1:
/* jshint asi: true */
// Global object
/* global mw, $, CiteTB, citeTemplate, citeErrorCheck */
if (typeof CiteTB == 'undefined') {
 
var CiteTB = {
if (typeof window.CiteTB === 'undefined' ) {
var window.CiteTB = {
"Templates" : {}, // All templates
"Options" : {}, // Global options
Line 17 ⟶ 19:
 
// Object for cite templates
window.citeTemplate = function citeTemplate(templatename, shortform, basicfields, expandedfields) {
// Properties
this.templatename = templatename; // The template name - "cite web", "cite book", etc.
Line 24 ⟶ 26:
// Less common - quote, archiveurl - should be everything the template supports minus the basic ones
this.extra = expandedfields;
 
this.incrementables = {};
 
// Add it to the list
CiteTB.Templates[this.templatename] = this;
// Methods
this.makeFormInner = function(fields, incrsetup) {
var i=0;
var trs = new Array()[];
var autofills = []
for (i=0; i<fields.length; i++) {
var fieldobj = fields[i];
var field = labelfield = fieldobj.field;
var ad = false;
if (incrsetup && fieldobj.increment_group) {
if ($j.inArray(field, CiteTB.getOption('autodate fields')) != -1 ) {
field = fieldobj.field.replace('<N>', '1');
im = $j('<img />').attr('src', 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/7b/Nuvola_apps_date.svg/20px-Nuvola_apps_date.svg.png');
labelfield = fieldobj.field.replace('<N>', '');
if (this.incrementables[fieldobj.increment_group] && !this.incrementables[fieldobj.increment_group].setup) {
// The object has been created, but not fully initialized
this.incrementables[fieldobj.increment_group]['fields'].push(fieldobj)
} else if (!this.incrementables[fieldobj.increment_group]) {
// Object not yet created
this.incrementables[fieldobj.increment_group] = {
'fields': [fieldobj],
'val':1,
'setup': false
};
} else if (this.incrementables[fieldobj.increment_group] && this.incrementables[fieldobj.increment_group].setup) {
// Fully initialized from a previous invocation of this function, just resetting the number
this.incrementables[fieldobj.increment_group].val = 1
}
} else if (fieldobj.increment_group) {
// Adding a new row
incrval = this.incrementables[fieldobj.increment_group].val;
field = fieldobj.field.replace('<N>', incrval.toString());
labelfield = fieldobj.field.replace('<N>', '');
}
if ($j.inArray(field, CiteTB.getOption('autodate fields')) != -1 ) {
im = $j('<img />').attr('src', 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/7b/Nuvola_apps_date.svg/20px-Nuvola_apps_date.svg.png');
im.attr('alt', mw.usability.getMsg('cite-insert-date')).attr('title', mw.usability.getMsg('cite-insert-date'));
var ad = $j('<a />').attr('href', '#');
ad.append(im);
ad.attr('id', 'cite-date-'+CiteTB.escStr(this.shortform)+'-'+field);
$j( document ).on( 'click', '#cite-date-'+CiteTB.escStr(this.shortform)+'-'+field).live('click', CiteTB.fillAccessdate );
}
if (fieldobj.autofillid) {
var autotype = fieldobj.autofillid;
im = $j('<img />').attr('src', 'http://upload.wikimedia.org/wikipedia/commons/thumb/1/17/System-search.svg/20px-System-search.svg.png');
im.attr('alt', mw.usability.getMsg('cite-autofill-alt')).attr('title', mw.usability.getMsg('cite-autofill-alt'));
var ad = $j('<a />').attr('href', '#');
ad.append(im);
ad.attr('id', 'cite-auto-'+CiteTB.escStr(this.shortform)+'-'+field+'-'+autotype);
$jautofills.push('#cite-auto-'+CiteTB.escStr(this.shortform)+'-'+field+'-'+autotype).live('click', CiteTB.initAutofill);
}
if (fieldobj.increment_button) {
var incrtype = fieldobj.increment_group;
im = $('<img />').attr('src', '//upload.wikimedia.org/wikipedia/commons/thumb/b/b9/Nuvola_action_edit_add.svg/20px-Nuvola_action_edit_add.svg.png');
im.attr('alt', mw.usability.getMsg('cite-increment-alt')).attr('title', mw.usability.getMsg('cite-increment-alt'));
var ad = $('<a />').attr('href', '#');
ad.append(im);
ad.attr('id', 'cite-incr-'+CiteTB.escStr(this.shortform)+'-'+incrtype);
}
var display = mw.usability.getMsg('cite-'+fieldlabelfield+'-label');
if (typeof display !== 'string') {
var tooltip = fieldobj.tooltip ? $j('<abbr />').attr('title', mw.usability.getMsg(fieldobj.tooltip)).text('*') : false;
if (fieldobj.label) {
display = CiteTB.fixStr(fieldobj.label);
} else {
display = CiteTB.fixStr(labelfield)
}
}
var tooltip = fieldobj.tooltip ? $j('<abbr />').attr('title', mw.usability.getMsg(fieldobj.tooltip)).texthtml('*<sup>?</sup>') : false;
var input = '';
if (ad) {
input = $j('<input tabindex="1" style="width:85%" type="text" />');
} else {
input = $j('<input tabindex="1" style="width:100%" type="text" />');
}
input.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-'+field);
if (fieldobj.autofillprop) {
classname = input.addClass('cite-'+CiteTB.escStr(this.shortform)+'-'+fieldobj.autofillprop);
if (fieldobj.increment_group) {
input.addClass('cite-'+CiteTB.escStr(this.shortform)+'-incr-'+fieldobj.increment_group);
classname+='-'+this.incrementables[fieldobj.increment_group].val.toString();
}
input.addClass(classname);
}
var label = $j('<label />');
label.attr('for', 'cite-'+CiteTB.escStr(this.shortform)+'-'+field).text(display);
if (tooltip) {
Line 76 ⟶ 125:
style += ' padding-left:1em;';
} else {
var tr = $j('<tr />');
}
var td1 = $j('<td class="cite-form-td" />').attr('style', style);
td1.append(label);
tr.append(td1);
var td2 = $j('<td class="cite-form-td" style="width:30%" />');
td2.append(input);
if (ad) {
Line 90 ⟶ 139:
trs.push(tr);
}
}
var needsetup = false;
for (g in this.incrementables) {
if (!this.incrementables[g].setup) {
needsetup = true;
$( document ).on( 'click', '#cite-incr-'+CiteTB.escStr(this.shortform)+'-'+g, CiteTB.incrementFields );
this.incrementables[g].setup = true;
}
}
if (needsetup || $.isEmptyObject(this.incrementables)) {
for(i=0; i<autofills.length; i++) {
$( document ).on( 'click', autofills[i], CiteTB.initAutofill );
}
}
return trs;
Line 97 ⟶ 159:
// gives a little bit of HTML so the open form can be identified
this.getInitial = function() {
var hidden = $j('<input type="hidden" class="cite-template" value="' + this.templatename + '"/>');
hidden.val(this.templatename);
return hidden;
}
Line 104 ⟶ 165:
// makes the form used in the dialog boxes
this.getForm = function() {
var main = $j("<div class='cite-form-container' />");
var form1 = $j('<table style="width:100%; background-color:transparent;" class="cite-basic-fields" />');
var i=0;
var trs = this.makeFormInner(this.basic, true);
for (var i=0; i<trs.length; i++) {
form1.append(trs[i]);
}
var form2 = $j('<table style="width:100%; background-color:transparent; display:none" class="cite-extra-fields">');
trs = this.makeFormInner(this.extra, true);
for (var i=0; i<trs.length; i++) {
form2.append(trs[i]);
Line 119 ⟶ 180:
main.append(form1).append(form2);
var form3 = $j('<table style="width:100%; background-color:transparent;padding-top:1em" class="cite-other-fields">');
var tr = $j('<tr />');
var td1 = $j('<td class="cite-form-td" style="text-align:right; width:20%" />');
var label1 = $j('<label />');
label1.attr('for', "cite-"+CiteTB.escStr(this.shortform)+'-name').text(mw.usability.getMsg('cite-name-label'));
td1.append(label1);
var td2 = $j('<td class="cite-form-td" style="width:30%" />');
var input1 = $j('<input tabindex="1" style="width:100%" type="text" />');
input1.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-name');
td2.append(input1);
var td3 = $j('<td class="cite-form-td" style="text-align:right; padding-left:1em; width:20%">');
var label2 = $j('<label />');
label2.attr('for', 'cite-'+CiteTB.escStr(this.shortform)+'-group').text(mw.usability.getMsg('cite-group-label'));
td3.append(label2);
var td4 = $j('<td class="cite-form-td" style="width:30%" />');
var input2 = $j('<input tabindex="1" style="width:100%" type="text" />');
input2.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-group');
td4.append(input2);
Line 140 ⟶ 201:
form3.append(tr);
main.append(form3);
var extras = $j('<div />');
extras.append('<input type="hidden" class="cite-form-status" value="closed" />');
var hidden = $j('<input type="hidden" class="cite-template" />');
hidden.val(this.templatename);
extras.append(hidden);
var span1 = $j('<span class="cite-preview-label" style="display:none;" />');
span1.text(mw.usability.getMsg('cite-raw-preview'));
extras.append(span1).append('<div class="cite-ref-preview" style="padding:0.5em; font-size:110%" />');
var span2 = $j('<span class="cite-prev-parsed-label" style="display:none;" />');
span2.text(mw.usability.getMsg('cite-parsed-label'));
extras.append(span2).append('<div class="cite-preview-parsed" style="padding-bottom:0.5em; font-size:110%" />');
var link = $j('<a href="#" class="cite-prev-parse" style="margin:0 1em 0 1em; display:none; color:darkblue" />');
link.text(mw.usability.getMsg('cite-form-parse'));
extras.append(link);
Line 176 ⟶ 237:
*/
 
window.citeErrorCheck = function citeErrorCheck(obj) {
this.obj = obj
CiteTB.ErrorChecks[this.obj.testname] = this;
Line 207 ⟶ 268:
this.getRow = function() {
var row = $j("<li />");
var check = $j("<input type='checkbox' name='cite-err-test' />");
check.attr('value', this.obj.testname);
var label = $j("<label />").html(mw.usability.getMsg(this.obj.desc));
label.attr('for', this.obj.testname);
row.append(check).append(' &ndash; ').append(label);
Line 216 ⟶ 277:
}
}
$j('head').trigger('reftoolbarbase');