Jump to content

User:D'Ranged 1/script/CustomRefToolbar.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*

Many, many thanks to [[User:Mr.Z-man]] for his creation of the excellent RefToolbar gadget. 
I have only modified his work; I have not created a new gadget, as he did, from scratch. 
If you're using RefToolbar, this modification, or have modified it for your own use, you 
owe a great debt of gratitude to Mr.Z-man for all his hard work, which continues.
Thank you, good sir!

*/

/*

Change log:
2016-02-25 Update new parameter names.
2014-06-30 Add "agency" parameter to web template.
2014-06-10 Add UN doc template.
2014-06-04 Added cite interview template. Updated all labels for dialog boxes to include "(custom)". Tried to override default options, but "expandtemplates" still doesn't work.
           Turned off "modal" as a test; while it doesn't allow me to leave the form and edit the document, it does bypass graying out the entire screen; easier to see edit area.
2014-06-03 Added the code that parses the information; customized it to include a space before the pipe.
2014-06-02 Updated to include new incrementing features of RefToolbar; removed "coauthors" everywhere.
2014-05-25 Added autofill to DOI fields; added the deprecated "coauthors" field which is still being populated by autofill tools, along with a tooltip about not using it.
           Made all fields on patent template show; it's so short there's no reason not to show them all.
2014-05-25 Finalized modifications and wrote documentation.

*/

// Custom RefToolbar
$(function(){ // start wrapping function
  if (typeof CiteTB === 'undefined') return;
$('head').one('reftoolbarbase', function() {

// Set default options
	
   CiteTB.Options = {
    "date format" : "<monthname> <date>, <year>",
    "autodate fields" : ['date', 'access-date', 'archive-date'],
    "months" : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    "modal" : false,
    "autoparse" : true,
    "expandtemplates" : true,
};

// Set user options
/*
   CiteTB.UserOptions['date format'] = "<monthname> <date>, <year>";
   CiteTB.UserOptions['autodate fields'] = ['date', 'access-date', 'archive-date'];
   CiteTB.UserOptions['autoparse'] = true;
*/
// Cite template definitions

   new citeTemplate('cite book 2', 'book2',
   [ // Basic fields
      {"field": "title", "autofillprop":"title"},
      {"field": "date", "autofillprop":"year"},
	  {"field": "first<N>", "autofillprop":"first-incr", 'increment_group':'author'}, 
	  {"field": "last<N>", "autofillprop":"last-incr", 'increment_group':'author', 'increment_button':true},
	  {"field": "author<N>-link", "tooltip":"cite-author-link-tooltip", 'increment_group':'authorlink', 'increment_button':true},
      {"field": "author<N>", 'increment_group':'author_alt', 'increment_button':true},
      {"field": "isbn", "autofillid":"isbn"},
      {"field": "access-date", "autofillprop":"date"},
      {"field": "url"},
      {"field": "archive-url"},
      {"field": "archive-date", "autofillprop":"date"},
      {"field": "dead-url"},
      {"field": "page", "tooltip":"cite-page-tooltip"},
      {"field": "pages", "autofillprop":"pages", "tooltip":"cite-pages-tooltip"},
      {"field": "publisher", "autofillprop":"publisher"},
      {"field": "location", "autofillprop":"location"},
      {"field": "edition", "autofillprop":"edition"},
      {"field": "type"},
      {"field": "quote"},
      {"field": "asin"},
   ],
   [ // Expanded fields
      {"field": "editor<N>-first", "increment_group":"editor"}, 
      {"field": "editor<N>-last", "increment_group":"editor", "increment_button":true},
      {"field": "editor<N>-link", "tooltip":"cite-editor-link-tooltip", 'increment_group':'editorlink', 'increment_button':true},
      {"field": "others", "tooltip":"cite-others-tooltip"},
      {"field": "format"},
      {"field": "year"},
      {"field": "language"},
      {"field": "trans-title"},
      {"field": "chapter"},
      {"field": "ref", "tooltip":"cite-ref-tooltip"},
      ]);

   new citeTemplate('cite interview', 'interview',
   [ // Basic fields
      {"field": "title", "autofillprop":"title"},
      {"field": "date", "autofillprop":"year"},
	  {"field": "first<N>", "autofillprop":"first-incr", 'increment_group':'author'}, 
	  {"field": "last<N>", "autofillprop":"last-incr", 'increment_group':'author', 'increment_button':true},
	  {"field": "subject-link<N>", "tooltip":"cite-subject-link-tooltip", 'increment_group':'authorlink', 'increment_button':true},
      {"field": "subject<N>", 'increment_group':'author_alt', 'increment_button':true},
      {"field": "interviewer"},
      {"field": "program"},
      {"field": "callsign"},
      {"field": "access-date", "autofillprop":"date"},
      {"field": "url"},
      {"field": "archive-url"},
      {"field": "archive-date", "autofillprop":"date"},
      {"field": "dead-url"},
      {"field": "publisher", "autofillprop":"publisher"},
      {"field": "city", "autofillprop":"location"},
      {"field": "type"},
      {"field": "quote"},
   ],
   [ // Expanded fields
      {"field": "others", "tooltip":"cite-others-tooltip"},
      {"field": "page", "tooltip":"cite-page-tooltip"},
      {"field": "pages", "autofillprop":"pages", "tooltip":"cite-pages-tooltip"},
      {"field": "format"},
      {"field": "year"},
      {"field": "language"},
      {"field": "trans-title"},
      {"field": "ref", "tooltip":"cite-ref-tooltip"},
      {"field": "id"},
      ]);

   new citeTemplate('cite journal 2', 'journal2',
   [ // Basic fields
      {"field": "title", "autofillprop":"title"},
      {"field": "date", "autofillprop":"date"},
	  {"field": "first<N>", "autofillprop":"first-incr", 'increment_group':'author'}, 
	  {"field": "last<N>", "autofillprop":"last-incr", 'increment_group':'author', 'increment_button':true},
	  {"field": "author<N>-link", "tooltip":"cite-author-link-tooltip", 'increment_group':'authorlink', 'increment_button':true},
      {"field": "author<N>", 'increment_group':'author_alt', 'increment_button':true},
      {"field": "access-date"},
      {"field": "doi", "autofillid":"doi"},
      {"field": "journal", "autofillprop":"journal"},
      {"field": "volume", "autofillprop":"volume"},
      {"field": "series"},
      {"field": "issue", "autofillprop":"issue"},
      {"field": "page", "tooltip":"cite-page-tooltip"},
      {"field": "pages", "autofillprop":"pages", "tooltip":"cite-pages-tooltip"},
      {"field": "pmid", "autofillid":"pmid"},
      {"field": "pmc"},
      {"field": "url"},
      {"field": "archive-url"},
      {"field": "archive-date", "autofillprop":"date"},
      {"field": "dead-url"},
   ],
   [ // Expanded fields
      {"field": "editor<N>-first", "increment_group":"editor"}, 
      {"field": "editor<N>-last", "increment_group":"editor", "increment_button":true},
      {"field": "editor<N>-link", "tooltip":"cite-editor-link-tooltip", 'increment_group':'editorlink', 'increment_button':true},
      {"field": "others", "tooltip":"cite-others-tooltip"},
      {"field": "language"},
      {"field": "trans-title"},
      {"field": "publisher"},
      {"field": "location"},
      {"field": "format"},
      {"field": "year"},
      {"field": "issn"},
      {"field": "oclc"},
      {"field": "bibcode"},
      {"field": "id"},
      {"field": "quote"},
      {"field": "at", "tooltip":"cite-at-tooltip"},
      {"field": "ref", "tooltip":"cite-ref-tooltip"},
      {"field": "postscript", "tooltip":"cite-postscript-tooltip"},
      ]);

   new citeTemplate('cite news 2', 'news2',
   [ // Basic fields
	  {"field": "title", "autofillprop":"title"},
      {"field": "date", "autofillprop":"date"},
	  {"field": "first<N>", "autofillprop":"first-incr", 'increment_group':'author'}, 
	  {"field": "last<N>", "autofillprop":"last-incr", 'increment_group':'author', 'increment_button':true},
	  {"field": "author<N>-link", "tooltip":"cite-author-link-tooltip", 'increment_group':'authorlink', 'increment_button':true},
      {"field": "author<N>", 'increment_group':'author_alt', 'increment_button':true},
      {"field": "access-date"},
      {"field": "work", "tooltip":"cite-work-tooltip", "autofillprop":"journal"},
      {"field": "url"},
      {"field": "archive-url"},
      {"field": "archive-date", "autofillprop":"date"},
      {"field": "dead-url"},
      {"field": "agency"}
   ],
   [ // Expanded fields
      {"field": "editor<N>-first", "increment_group":"editor"}, 
      {"field": "editor<N>-last", "increment_group":"editor", "increment_button":true},
      {"field": "editor<N>-link", "tooltip":"cite-editor-link-tooltip", 'increment_group':'editorlink', 'increment_button':true},
      {"field": "others", "tooltip":"cite-others-tooltip"},
      {"field": "language"},
      {"field": "trans-title"},
      {"field": "page", "tooltip":"cite-page-tooltip"},
      {"field": "pages", "autofillprop":"pages", "tooltip":"cite-pages-tooltip"},
      {"field": "format"},
      {"field": "location"},
      {"field": "quote"},
      {"field": "doi", "autofillid":"doi"},
      ]);

   new citeTemplate('cite patent', 'patent',
   [ // Basic fields
      {"field": "country"},
      {"field": "number"}, 
	  {"field": "title", "autofillprop":"title"},
      {"field": "status"},
      {"field": "pubdate"},
      {"field": "gdate"},
      {"field": "fdate"},
      {"field": "assign1"},
      {"field": "invent1"},
      {"field": "invent2"},
      {"field": "invent3"},
      {"field": "invent4"},
      {"field": "assign2"},
   ],
   [ // Expanded fields
   ]);

   new citeTemplate('UN document', 'UN doc',
   [ // Basic fields
      {"field": "docid"},
      {"field": "body", "tooltip":"cite-body-tooltip"},
      {"field": "type", "tooltip":"cite-type-tooltip"},
	  {"field": "session"},
      {"field": "resolution_number"},
      {"field": "document_number"},
      {"field": "title"},
      {"field": "page"},
      {"field": "pages"},
      {"field": "date"},
      {"field": "year"},
      {"field": "meeting"},
      {"field": "meetingtime"},
      {"field": "speakername"},
      {"field": "speakernation"},
      {"field": "language"},
      {"field": "access-date", "autofillprop":"date"},
   ],
   [ // Expanded fields
   ]);
   new citeTemplate('cite web 2', 'web2',
   [ // Basic fields
      {"field": "title", "autofillprop":"title"},
      {"field": "date", "autofillprop":"date"},
	  {"field": "first<N>", "autofillprop":"first-incr", 'increment_group':'author'}, 
	  {"field": "last<N>", "autofillprop":"last-incr", 'increment_group':'author', 'increment_button':true},
	  {"field": "author<N>-link", "tooltip":"cite-author-link-tooltip", 'increment_group':'authorlink', 'increment_button':true},
	  {"field": "author<N>", 'increment_group':'author_alt', 'increment_button':true},
      {"field": "access-date"},
      {"field": "website", "tooltip": "cite-work-tooltip", "autofillprop":"journal"},
      {"field": "url"},
      {"field": "archive-url"},
      {"field": "archive-date", "autofillprop":"date"},
      {"field": "dead-url"},
      {"field": "format"},
      {"field": "year"},
      {"field": "publisher"},
      {"field": "location"},
      {"field": "language"},
      {"field": "trans-title"},
      {"field": "quote"},
      {"field": "doi", "autofillid":"doi"},
      {"field": "agency"},
   ],
   [ // Expanded fields
      {"field": "editor<N>-first", "increment_group":"editor"}, 
      {"field": "editor<N>-last", "increment_group":"editor", "increment_button":true},
      {"field": "editor<N>-link", "tooltip":"cite-editor-link-tooltip", 'increment_group':'editorlink', 'increment_button':true},
      {"field": "others", "tooltip":"cite-others-tooltip"},
      {"field": "page", "tooltip":"cite-page-tooltip"},
      {"field": "pages", "autofillprop":"pages", "tooltip":"cite-pages-tooltip"}
   ]);

// Cite template headings, field labels, and tooltips

mw.usability.addMessages( { 
'cite-dialog-book2' : 'Book citation (custom)',
'cite-dialog-interview2' : 'Interview citation (custom)',
'cite-dialog-journal2' : 'Journal citation (custom)',
'cite-dialog-news2' : 'News citation (custom)',
'cite-dialog-patent' : 'Patent citation (custom)',
'cite-dialog-UN doc' : 'UN document',
'cite-dialog-web2' : 'Web citation (custom)',

'cite-increment-alt' : 'Add another',	// Alt text for field increment button, not specific to a particular field

'cite-author-link-tooltip' : 'If the author has a Wikipedia article, the name of the article',
'cite-body-tooltip' : "A=General Assembly; S=Security Council; T=Secretariat; E=Economic and Social Council; H=Human Rights Campaign",
'cite-subject-link-tooltip' : 'If the subject has a Wikipedia article, the name of the article',
'cite-type-tooltip' : "A=Agenda item; D=Document; M=Meeting; R=Resolution; S=Summary Report; V=Verbatim Report",
'cite-others-tooltip' : "Use for translators, photographers, illustrators, etc. List last name followed by a comma and a space, then first name followed by their role in parentheses; if more than one, separate with semicolons",

'cite-access-date-label' : 'Access date',
'cite-archive-date-label' : 'Archive date',
'cite-archive-url-label' : 'Archive URL',
'cite-asin-label' : 'ASIN',
'cite-assign1-label' : 'Assignee 1',
'cite-assign2-label' : 'Assignee 2',
'cite-body-label' : 'Body',
'cite-country-label' : 'Country',
'cite-dead-url-label' : "Dead url?",
'cite-docid-label' : 'Doc ID',
'cite-document_number-label' : 'Document number',
'cite-fdate-label' : 'Filing date',
'cite-gdate-label' : 'Grant date',
'cite-invent1-label' : 'Inventor 1',
'cite-invent2-label' : 'Inventor 2',
'cite-invent3-label' : 'Inventor 3',
'cite-invent4-label' : 'Inventor 4',
'cite-meeting-label' : 'Meeting number',
'cite-meetingtime-label' : 'Meeting time',
'cite-number-label' : 'Number',
'cite-others-label' : 'Others',
'cite-pubdate-label' : 'Publication date',
'cite-resolution_number-label' : 'Resolution number',
'cite-session-label' : 'Session number',
'cite-speakername-label' : 'Speaker name',
'cite-speakernation-label' : 'Speaker nation',
'cite-status-label' : 'Status',
'cite-subject-link-label' : "Subject's article",
'cite-trans-title-label' : 'Translated title',
'cite-type-label' : 'Type',
});
});

// REF FUNCTIONS - hoping to customize this to add a space before the pipe
// Actually assemble a ref from user input
CiteTB.getRef = function(inneronly, forinsert) {
  var i;
  var template = CiteTB.getOpenTemplate();
  var templatename = template.templatename;
  var res = '';
  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';
    if (refname) {
      refname = $.trim(refname);
      res+=' name='+CiteTB.getQuotedString(refname);
      refobj.refname = refname;
    }
    if (group) {
      group = $.trim(group);
      res+=' group='+CiteTB.getQuotedString(group);
      refobj.refgroup = group;
    }
    res+='>';
  }
  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);
      }
    }
  }
  content+= '}}';
  res+=content;
  refobj.content = content;
  if (!inneronly) {
    res+= '</ref>';
  }
  if (forinsert) {
    CiteTB.mainRefList.push(refobj);
  }
  return res;
};

}); // end wrapping function