MediaWiki:Gadget-refToolbar

De Bioeticawiki

/*jshint smarttabs:true, loopfunc:true,forin:false*/ /*global mw, $, importScript */ // 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 = {
   "Templates" : {}, // All templates
   "Options" : {}, // Global options
   "UserOptions" : {}, // User options
   "DefaultOptions" : {}, // Script defaults
   "ErrorChecks" : {} // Error check functions
 };

}

// Only execute 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' ) && ( ( mw.config.get('wgNamespaceNumber') !== 2 && mw.config.get('wgNamespaceNumber') !== 4 ) || ( mw.config.get('wgPageName').indexOf('.js') === -1 && mw.config.get('wgPageName').indexOf('.css') === -1 ) ) ) {

// 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;"+ "padding: 0.1em !important;"+ "}");

// Default options, these mainly exist so the script won't break if a new option is added CiteTB.DefaultOptions = {

 "date format" : "<year>-<zmonth>-<zdate>",
 "autodate fields" : [],
 "months" : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
 "modal" : true,
 "autoparse" : false,
 "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];
 }
 return CiteTB.DefaultOptions[opt];

};

CiteTB.init = function() {

 /* Main stuff, build the actual toolbar structure
  * 1. get the template list, make the dropdown list and set up the template dialog boxes
  * 2. actually build the toolbar:
  *    * A section for cites
  *    ** dropdown for the templates (previously defined)
  *    ** button for named refs with a dialog box
  *    ** button for errorcheck
  * 3. add the whole thing to the main toolbar
 */
 if (typeof $('div[rel=cites]')[0] !== 'undefined') { // Mystery IE bug workaround
   return;
 }
 $('head').trigger('reftoolbarbase');
 var $target = $('#wpTextbox1');
 var temlist = {};
 for (var t in CiteTB.Templates) {
   var tem = CiteTB.Templates[t];
   var sform = CiteTB.escStr(tem.shortform);
   var actionobj = { 
     type: 'dialog',
     module: 'cite-dialog-'+sform
   };
   var dialogobj = {};
   dialogobj['cite-dialog-'+sform] = {
     resizeme: false,
     titleMsg: 'cite-dialog-'+sform, 
     id: 'citetoolbar-'+sform,
     init: function() {}, 
     html: tem.getInitial(), 
     dialog: {
       width:675,
       open: function() { 
         $(this).html(CiteTB.getOpenTemplate().getForm());
         /** @param {jQuery.Event} e */
         $('.cite-prev-parse').bind( 'click', function ( e ) {
           e.preventDefault();
           CiteTB.prevParseClick();
         });
       },
       buttons: {
         'cite-form-submit': function() {
           var ref = CiteTB.getRef(false, true);
           $(this).dialog( 'close' );
           $.wikiEditor.modules.toolbar.fn.doAction( $(this).data( 'context' ), {
             type: 'encapsulate',
             options: {
               post: ref
             }
           }, $(this) );
         },
         'cite-form-showhide': CiteTB.showHideExtra,
         'cite-refpreview': function() {   
           var ref = CiteTB.getRef(false, false);
           var template = CiteTB.getOpenTemplate();
           var div = $("#citetoolbar-"+CiteTB.escStr(template.shortform));
           div.find('.cite-preview-label').show();
           div.find('.cite-ref-preview').text(ref).show();
           if (CiteTB.getOption('autoparse')) {
             CiteTB.prevParseClick();
           } else {
             div.find('.cite-prev-parse').show();
             div.find('.cite-prev-parsed-label').hide();
             div.find('.cite-preview-parsed').html();
           }         
         },
         'wikieditor-toolbar-tool-link-cancel': function() {
           $(this).dialog( 'close' );
         },
         'cite-form-reset': function() {
           CiteTB.resetForm();
         }
       }
     } 
   };