மீடியாவிக்கி:Gadget-refToolbarBase.js
/* jshint asi: true */
 /* global mw, $, CiteTB, citeTemplate, citeErrorCheck */
 
 if ( window.CiteTB === undefined ) {
   window.CiteTB = {
     &quot;Templates&quot; : {}, // All templates
     &quot;Options&quot; : {}, // Global options
     &quot;UserOptions&quot; : {}, // User options
     &quot;DefaultOptions&quot; : {}, // Script defaults
     &quot;ErrorChecks&quot; : {} // Error check functions
   };
 }
 
 if (typeof mw.usability == 'undefined') {
   mw.usability = {};
   mw.usability.getMsg = function(m) { return mw.messages.get(m); }
   mw.usability.addMessages = function(msgs) { mw.messages.set(msgs); };
 }
 
 // Object for cite templates
 window.citeTemplate = function (templatename, shortform, basicfields, expandedfields) {
   // Properties
   this.templatename = templatename; // The template name - &quot;cite web&quot;, &quot;cite book&quot;, etc.
   this.shortform = shortform; // A short form, used for the dropdown box
   this.basic = basicfields; // Basic fields - author, title, publisher...
   // 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 = [];
     var autofills = []
     for (i=0; i&lt;fields.length; i++) {
       var fieldobj = fields[i];
       var field = labelfield = fieldobj.field;
       var ad = false;
       if (incrsetup &amp;&amp; fieldobj.increment_group) {
       		field = fieldobj.field.replace('&lt;N&gt;', '1');
       		labelfield = fieldobj.field.replace('&lt;N&gt;', '');
       		if (this.incrementables[fieldobj.increment_group] &amp;&amp; !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] &amp;&amp; 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('&lt;N&gt;', incrval.toString());
       	labelfield = fieldobj.field.replace('&lt;N&gt;', '');
       }
       if ($.inArray(field, CiteTB.getOption('autodate fields')) != -1 ) {
         im = $('&lt;img /&gt;').attr('src', '//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 = $('&lt;a /&gt;').attr('href', '#');
         ad.append(im);
         ad.attr('id', 'cite-date-'+CiteTB.escStr(this.shortform)+'-'+field);
         $( document ).on( 'click', '#cite-date-'+CiteTB.escStr(this.shortform)+'-'+field, CiteTB.fillAccessdate );
       }
 	  
       if (fieldobj.autofillid) {
         var autotype = fieldobj.autofillid;
         im = $('&lt;img /&gt;').attr('src', '//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 = $('&lt;a /&gt;').attr('href', '#');
         ad.append(im);
         ad.attr('id', 'cite-auto-'+CiteTB.escStr(this.shortform)+'-'+field+'-'+autotype);
         autofills.push('#cite-auto-'+CiteTB.escStr(this.shortform)+'-'+field+'-'+autotype);
       }
       
       if (fieldobj.increment_button) {
         var incrtype = fieldobj.increment_group;
         im = $('&lt;img /&gt;').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 = $('&lt;a /&gt;').attr('href', '#');
         ad.append(im);
         ad.attr('id', 'cite-incr-'+CiteTB.escStr(this.shortform)+'-'+incrtype);
       }
 	  
       var display = mw.usability.getMsg('cite-'+labelfield+'-label');
       if (typeof display !== 'string') {
       	if (fieldobj.label) {
       		display = CiteTB.fixStr(fieldobj.label);
       	} else {
       		display = CiteTB.fixStr(labelfield)
       	}
       }
       var tooltip = fieldobj.tooltip ? $('&lt;abbr /&gt;').attr('title', mw.usability.getMsg(fieldobj.tooltip)).html('&lt;sup&gt;?&lt;/sup&gt;') : false;
       
       var input = '';
       if (ad) {
         input = $('&lt;input tabindex=&quot;1&quot; style=&quot;width:85%&quot; type=&quot;text&quot; /&gt;');
       } else {
         input = $('&lt;input tabindex=&quot;1&quot; style=&quot;width:100%&quot; type=&quot;text&quot; /&gt;');
       }
       input.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-'+field);
 	  if (fieldobj.autofillprop) {
 	  	classname = '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 = $('&lt;label /&gt;');
       label.attr('for', 'cite-'+CiteTB.escStr(this.shortform)+'-'+field).text(display);
       if (tooltip) {
         label.append(tooltip);
       }
       var style = 'text-align:right; width:20%;';
       if (i%2 == 1) {
         style += ' padding-left:1em;';
       } else {
         var tr = $('&lt;tr /&gt;');
       }
       var td1 = $('&lt;td class=&quot;cite-form-td&quot; /&gt;').attr('style', style);
       td1.append(label);
       tr.append(td1);
       var td2 = $('&lt;td class=&quot;cite-form-td&quot; style=&quot;width:30%&quot; /&gt;');
       td2.append(input);
       if (ad) {
         td2.append(ad);
       }
       tr.append(td2);
       if (i%2 == 0) {
         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&lt;autofills.length; i++) {
     		$( document ).on( 'click', autofills[i], CiteTB.initAutofill );
     	}	
     }
     return trs;
   
   }
   
   // gives a little bit of HTML so the open form can be identified
   this.getInitial = function() {
     var hidden = '&lt;input type=&quot;hidden&quot; class=&quot;cite-template&quot; value=&quot;' + this.templatename + '&quot;/&gt;';
     return hidden;
   }
   
   // makes the form used in the dialog boxes
   this.getForm = function() {
     var main = $(&quot;&lt;div class='cite-form-container' /&gt;&quot;);
     var form1 = $('&lt;table style=&quot;width:100%; background-color:transparent;&quot; class=&quot;cite-basic-fields&quot; /&gt;');
     var i=0;
     var trs = this.makeFormInner(this.basic, true);
     for (var i=0; i&lt;trs.length; i++) {
       form1.append(trs[i]);
     }
     
     var form2 = $('&lt;table style=&quot;width:100%; background-color:transparent; display:none&quot; class=&quot;cite-extra-fields&quot;&gt;');
     trs = this.makeFormInner(this.extra, true);
     for (var i=0; i&lt;trs.length; i++) {
       form2.append(trs[i]);
     }    
     main.append(form1).append(form2);
     
     var form3 = $('&lt;table style=&quot;width:100%; background-color:transparent;padding-top:1em&quot; class=&quot;cite-other-fields&quot;&gt;');
     var tr = $('&lt;tr /&gt;');
     var td1 = $('&lt;td class=&quot;cite-form-td&quot; style=&quot;text-align:right; width:20%&quot; /&gt;');
     var label1 = $('&lt;label /&gt;');
     label1.attr('for', &quot;cite-&quot;+CiteTB.escStr(this.shortform)+'-name').text(mw.usability.getMsg('cite-name-label'));
     td1.append(label1);
     var td2 = $('&lt;td class=&quot;cite-form-td&quot; style=&quot;width:30%&quot; /&gt;');
     var input1 = $('&lt;input tabindex=&quot;1&quot; style=&quot;width:100%&quot; type=&quot;text&quot; /&gt;');
     input1.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-name');
     td2.append(input1);
     var td3 = $('&lt;td class=&quot;cite-form-td&quot; style=&quot;text-align:right; padding-left:1em; width:20%&quot;&gt;');
     var label2 = $('&lt;label /&gt;');
     label2.attr('for', 'cite-'+CiteTB.escStr(this.shortform)+'-group').text(mw.usability.getMsg('cite-group-label'));
     td3.append(label2);
     var td4 = $('&lt;td class=&quot;cite-form-td&quot; style=&quot;width:30%&quot; /&gt;');
     var input2 = $('&lt;input tabindex=&quot;1&quot; style=&quot;width:100%&quot; type=&quot;text&quot; /&gt;');
     input2.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-group');
     td4.append(input2);
     tr.append(td1).append(td2).append(td3).append(td4);
     form3.append(tr);
     main.append(form3);
     var extras = $('&lt;div /&gt;');
     extras.append('&lt;input type=&quot;hidden&quot; class=&quot;cite-form-status&quot; value=&quot;closed&quot; /&gt;');
     var hidden = $('&lt;input type=&quot;hidden&quot; class=&quot;cite-template&quot; /&gt;');
     hidden.val(this.templatename);
     extras.append(hidden);
     var span1 = $('&lt;span class=&quot;cite-preview-label&quot; style=&quot;display:none;&quot; /&gt;');
     span1.text(mw.usability.getMsg('cite-raw-preview'));
     extras.append(span1).append('&lt;div class=&quot;cite-ref-preview&quot; style=&quot;padding:0.5em; font-size:110%&quot; /&gt;');
     var span2 = $('&lt;span class=&quot;cite-prev-parsed-label&quot; style=&quot;display:none;&quot; /&gt;');
     span2.text(mw.usability.getMsg('cite-parsed-label'));
     extras.append(span2).append('&lt;div class=&quot;cite-preview-parsed&quot; style=&quot;padding-bottom:0.5em; font-size:110%&quot; /&gt;');
     var link = $('&lt;a href=&quot;#&quot; class=&quot;cite-prev-parse&quot; style=&quot;margin:0 1em 0 1em; display:none; color:darkblue&quot; /&gt;');
     link.text(mw.usability.getMsg('cite-form-parse'));
     extras.append(link);    
     main.append(extras);
     
     return main;
   }
 }
 
 /* Class for error checks
     FIXME: DOCS OUT OF DATE
    type - type of error check - current options:
     * 'refcheck' - apply a function on each ref individually
       * function should accept a ref object, return a string
     * 'reflist' - apply a function on the entire ref list
       * function should accept an array of ref objects, return an array of strings
     * 'search' - apply a function ro the page text
       * function should accept the page text as a string, return an array of strings
    The strings returned by the function should be valid HTML
    
    func - The function described above
    testname - Name of the error check, must not contain spaces
    desc - A short description of the test
 */
 
 window.citeErrorCheck = function (obj) {
   this.obj = obj
   CiteTB.ErrorChecks[this.obj.testname] = this;
   
   this.run = function() {
     var errors = [];
     switch(this.obj['type']) {
       case &quot;refcheck&quot;:
         CiteTB.loadRefs();
         for(var i=0; i&lt;CiteTB.mainRefList.length; i++) {
           var e = this.obj.func(CiteTB.mainRefList[i]);
           if (e) {
             errors.push(e);
           }
         }
         break;
       case &quot;reflist&quot;:
         CiteTB.loadRefs();
         errors = this.obj.func(CiteTB.mainRefList);
         break;
       case &quot;search&quot;:
         var func = this.obj.func
         CiteTB.getPageText(function(text) {
           errors = func(text);
         });
         break;
     }
     return errors;
   }
   
   this.getRow = function() {
     var row = $(&quot;&lt;li /&gt;&quot;);
     var check = $(&quot;&lt;input type='checkbox' name='cite-err-test' /&gt;&quot;);
     check.attr('value', this.obj.testname);
     var label = $(&quot;&lt;label /&gt;&quot;).html(mw.usability.getMsg(this.obj.desc));
     label.attr('for', this.obj.testname); 
     row.append(check).append(' &amp;ndash; ').append(label);
     return row;
   }
 }